Skip to content

Commit 09a8b38

Browse files
feat: add date and day label to home
Within this commit, we add labels to show date and the day name in the home screen for better user-experience. This implementation uses exact coordinates.
1 parent c1bf87a commit 09a8b38

File tree

3 files changed

+102
-13
lines changed

3 files changed

+102
-13
lines changed

src/display/screens/home/home.c

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,40 @@
44
// Holds the home screen object.
55
lv_obj_t *home_screen;
66
lv_obj_t *label_clock;
7+
lv_obj_t *label_date;
8+
lv_obj_t *label_day;
79

810
void home_screen_init() {
911
// Create the screen object which is the LV object with no parent.
1012
home_screen = lv_obj_create(NULL);
1113
// Remove the ability to scroll the screen.
1214
lv_obj_remove_flag(home_screen, LV_OBJ_FLAG_SCROLLABLE);
1315

16+
// Render items in the screen.
17+
render_clock_label();
18+
render_date_label();
19+
render_day_label();
20+
21+
// Add an event handler for all possible events.
22+
lv_obj_add_event_cb(home_screen, home_screen_event, LV_EVENT_ALL, NULL);
23+
}
24+
25+
void home_screen_event(lv_event_t * e) {
26+
// lv_event_code_t event_code = lv_event_get_code(e);lv_obj_t * target = lv_event_get_target(e);
27+
}
28+
29+
void render_clock_label() {
1430
// Create a new label and expand its size to content size and align it to center.
1531
label_clock = lv_label_create(home_screen);
1632
lv_obj_set_width(label_clock, LV_SIZE_CONTENT);
1733
lv_obj_set_height(label_clock, LV_SIZE_CONTENT);
1834
lv_obj_set_align(label_clock, LV_ALIGN_CENTER);
35+
36+
// Align to center but with upward Y offset.
37+
lv_obj_align(label_clock, LV_ALIGN_CENTER, 0, -20);
1938

2039
// Set the default text.
21-
lv_label_set_text(label_clock, "HH\nMM");
40+
lv_label_set_text(label_clock, "HH:mm");
2241

2342
// Format the clock text string.
2443
/* 1. Set the letter spacing to 5. (Between letters)
@@ -32,13 +51,30 @@ void home_screen_init() {
3251
lv_obj_set_style_text_align(label_clock, LV_TEXT_ALIGN_AUTO, LV_PART_MAIN | LV_STATE_DEFAULT);
3352
lv_obj_set_style_text_decor(label_clock, LV_TEXT_DECOR_NONE, LV_PART_MAIN | LV_STATE_DEFAULT);
3453
lv_obj_set_style_text_font(label_clock, &lv_font_montserrat_46, LV_PART_MAIN | LV_STATE_DEFAULT);
54+
}
3555

36-
// Add an event handler for all possible events.
37-
lv_obj_add_event_cb(home_screen, home_screen_event, LV_EVENT_ALL, NULL);
56+
void render_date_label() {
57+
// Create a new label and expand its size to content size and align it to center.
58+
label_date = lv_label_create(home_screen);
59+
60+
// Align it to the bottom of the clock label with a margin of 2x5y pixels.
61+
lv_obj_align_to(label_date, label_clock, LV_ALIGN_OUT_BOTTOM_MID, -65, 0);
62+
63+
// Set the default text.
64+
lv_label_set_text(label_date, "YYYY-MM-DD");
65+
lv_obj_set_style_text_font(label_date, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);
3866
}
3967

40-
void home_screen_event(lv_event_t * e) {
41-
// lv_event_code_t event_code = lv_event_get_code(e);lv_obj_t * target = lv_event_get_target(e);
68+
void render_day_label() {
69+
// Create a new label and expand its size to content size and align it to center.
70+
label_day = lv_label_create(home_screen);
71+
72+
// Align it to the bottom of the clock label with a margin of 2x5y pixels.
73+
lv_obj_align_to(label_day, label_clock, LV_ALIGN_OUT_BOTTOM_MID, +45, 0);
74+
75+
// Set the default text.
76+
lv_label_set_text(label_day, "DAY");
77+
lv_obj_set_style_text_font(label_day, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);
4278
}
4379

4480
uint8_t home_screen_set_clock(uint8_t hour, uint8_t minute) {
@@ -50,3 +86,23 @@ uint8_t home_screen_set_clock(uint8_t hour, uint8_t minute) {
5086
lv_disp_flush_ready(lv_disp_get_default());
5187
return 0;
5288
}
89+
90+
uint8_t home_screen_set_date(uint16_t year, uint8_t month, uint8_t day) {
91+
// Check if the label_date is NULL.
92+
if (label_date == NULL) return 1;
93+
// Set the text of the label_date to the current date in "YYYY-MM-DD" format.
94+
lv_label_set_text_fmt(label_date, "%04u-%02d-%02d", year, month + 1, day);
95+
// Update the display.
96+
lv_disp_flush_ready(lv_disp_get_default());
97+
return 0;
98+
}
99+
100+
uint8_t home_screen_set_day(const char* day) {
101+
// Check if the label_clock is NULL.
102+
if (label_day == NULL) return 1;
103+
// Set the text of the label_day to the 3 character day name.
104+
lv_label_set_text(label_day, day);
105+
// Update the display.
106+
lv_disp_flush_ready(lv_disp_get_default());
107+
return 0;
108+
}

src/display/screens/home/home.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ extern lv_obj_t *home_screen;
1414
void home_screen_init();
1515
void home_screen_event(lv_event_t * e);
1616

17-
// Set the time on the clock. Returns 0 if successful, 1 otherwise.
18-
uint8_t home_screen_set_clock(uint8_t hour, uint8_t minute);
17+
// Render labels.
18+
void render_clock_label();
19+
void render_date_label();
20+
void render_day_label();
1921

22+
// Set the values on the home-screen. Returns 0 if successful, 1 otherwise.
23+
uint8_t home_screen_set_clock(uint8_t hour, uint8_t minute);
24+
uint8_t home_screen_set_date(uint16_t year, uint8_t month, uint8_t day);
25+
uint8_t home_screen_set_day(const char* day);
2026

2127
#ifdef __cplusplus
2228
} // extern "C"

src/main.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ int8_t utc_zone = +1;
3434
// Define timers to track real time.
3535
void update_unix_time_callback(struct k_timer *timer);
3636
void update_clock_view_callback(struct k_timer *timer);
37+
void update_date_day_view_callback(struct k_timer *timer);
3738
K_TIMER_DEFINE(unix_time_timer, update_unix_time_callback, NULL);
3839
K_TIMER_DEFINE(clock_view_timer, update_clock_view_callback, NULL);
40+
K_TIMER_DEFINE(date_day_view_timer, update_date_day_view_callback, NULL);
3941

4042
// Define the weekday names.
41-
const char* weekdays[] = {
42-
"Sunday", "Monday", "Tuesday", "Wednesday",
43-
"Thursday", "Friday", "Saturday"
44-
};
43+
const char* weekdays[] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
4544

4645
int main(void) {
4746
int ret;
@@ -54,8 +53,9 @@ int main(void) {
5453
}
5554

5655
// Start the timer to track the real time.
57-
k_timer_start(&unix_time_timer, K_MSEC(0), K_MSEC(1000));
58-
k_timer_start(&clock_view_timer, K_SECONDS(60), K_SECONDS(60));
56+
k_timer_start(&unix_time_timer, K_MSEC(60), K_MSEC(1000));
57+
k_timer_start(&clock_view_timer, K_MSEC(60), K_SECONDS(30));
58+
k_timer_start(&date_day_view_timer, K_MSEC(60), K_MINUTES(1));
5959

6060
// Check if the display device is ready.
6161
const struct device *display_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
@@ -132,4 +132,31 @@ void update_clock_view_callback(struct k_timer *timer) {
132132
if (ret != 0) {
133133
LOG_ERR("Failed to update the clock view.");
134134
}
135+
}
136+
137+
/* UPDATE_DATE_DAY_VIEW_CALLBACK
138+
*/
139+
void update_date_day_view_callback(struct k_timer *timer) {
140+
// Get the device twin to find UTC zone.
141+
device_twin_t* device_twin = get_device_twin_instance();
142+
143+
// Construct the local time from UNIX time and save it.
144+
utc_time_t local_time = unix_to_localtime(unix_time, device_twin->utc_zone);
145+
device_twin->current_time = local_time;
146+
147+
// Update the date view.
148+
uint8_t ret = home_screen_set_date(
149+
device_twin->current_time.year,
150+
device_twin->current_time.month,
151+
device_twin->current_time.day
152+
);
153+
if (ret != 0) {
154+
LOG_ERR("Failed to update the date view.");
155+
}
156+
157+
// Update the day view.
158+
ret = home_screen_set_day(weekdays[device_twin->current_time.weekday]);
159+
if (ret != 0) {
160+
LOG_ERR("Failed to update the day view.");
161+
}
135162
}

0 commit comments

Comments
 (0)