display_screen_ui_status_widget.c
1 #include "display_screen_ui_common.h" 2 3 #define STATUS_WIDGET_STRING_SIZE 16 4 5 static lv_obj_t* status_widget = NULL; 6 static char statusBuffer[STATUS_WIDGET_STRING_SIZE] = { 0 }; 7 8 lv_obj_t* ui_status_widget_get(lv_obj_t* parent) 9 { 10 if (status_widget != NULL) 11 { 12 lv_obj_set_parent(status_widget, parent); 13 } 14 else 15 { 16 status_widget = lv_label_create(parent, NULL); 17 } 18 19 //ui_common_set_label_font_theme_small(status_widget); 20 lv_label_set_long_mode(status_widget, LV_LABEL_LONG_CROP); 21 lv_label_set_align(status_widget, LV_LABEL_ALIGN_RIGHT); 22 lv_obj_set_width_fit(status_widget, lv_obj_get_width(parent)); 23 lv_obj_align(status_widget, parent, LV_ALIGN_IN_TOP_LEFT, 0, 0); 24 lv_label_set_text(status_widget, statusBuffer); 25 26 return status_widget; 27 } 28 29 void ui_status_widget_set_percentage(uint8_t battery_percent, bool is_charging) 30 { 31 char* battery_char = NULL; 32 33 if (battery_percent < 20) 34 { 35 battery_char = LV_SYMBOL_BATTERY_EMPTY; 36 } 37 else if (battery_percent < 50) 38 { 39 battery_char = LV_SYMBOL_BATTERY_1; 40 } 41 else if (battery_percent < 75) 42 { 43 battery_char = LV_SYMBOL_BATTERY_2; 44 } 45 else if (battery_percent < 90) 46 { 47 battery_char = LV_SYMBOL_BATTERY_3; 48 } 49 else 50 { 51 battery_char = LV_SYMBOL_BATTERY_FULL; 52 } 53 54 // Status widget format: 55 // %s - The charge symbol (if is charging, otherwise, as space) 56 // ' ' - A single space between the charge symbol and the battery symbol 57 // %s - The battery SoC symbol, as defined by the percentages above. 58 // ' ' -Another space, to move the content away from the right edge of the screen. 59 static const char* const STATUS_WIDGET_FORMAT = "%s %s "; 60 61 lv_snprintf(statusBuffer, STATUS_WIDGET_STRING_SIZE, STATUS_WIDGET_FORMAT, 62 is_charging ? LV_SYMBOL_CHARGE : " ", // show a symbol if we're charging 63 battery_char // show the current battery state 64 ); 65 66 lv_label_set_text(status_widget, statusBuffer); 67 }