feat(display): Add config to show battery level in percentage

This commit is contained in:
Cem Aksoylar 2022-11-29 22:05:04 -08:00 committed by Pete Johanson
parent 6ccb5280c9
commit 58eb7a3305
2 changed files with 14 additions and 1 deletions

View file

@ -15,6 +15,13 @@ config ZMK_WIDGET_BATTERY_STATUS
default y if BT default y if BT
select LVGL_USE_LABEL select LVGL_USE_LABEL
if ZMK_WIDGET_BATTERY_STATUS
config ZMK_WIDGET_BATTERY_STATUS_SHOW_PERCENTAGE
bool "Show battery level percentage in text"
endif
config ZMK_WIDGET_OUTPUT_STATUS config ZMK_WIDGET_OUTPUT_STATUS
bool "Widget for keyboard output status icons" bool "Widget for keyboard output status icons"
depends on BT && (!ZMK_SPLIT_BLE || ZMK_SPLIT_ROLE_CENTRAL) depends on BT && (!ZMK_SPLIT_BLE || ZMK_SPLIT_ROLE_CENTRAL)

View file

@ -27,7 +27,7 @@ struct battery_status_state {
}; };
static void set_battery_symbol(lv_obj_t *label, struct battery_status_state state) { static void set_battery_symbol(lv_obj_t *label, struct battery_status_state state) {
char text[8] = {}; char text[9] = {};
uint8_t level = state.level; uint8_t level = state.level;
@ -37,6 +37,11 @@ static void set_battery_symbol(lv_obj_t *label, struct battery_status_state stat
} }
#endif /* IS_ENABLED(CONFIG_USB_DEVICE_STACK) */ #endif /* IS_ENABLED(CONFIG_USB_DEVICE_STACK) */
#if IS_ENABLED(CONFIG_ZMK_WIDGET_BATTERY_STATUS_SHOW_PERCENTAGE)
char perc[5] = {};
snprintf(perc, sizeof(perc), "%3u%%", level);
strcat(text, perc);
#else
if (level > 95) { if (level > 95) {
strcat(text, LV_SYMBOL_BATTERY_FULL); strcat(text, LV_SYMBOL_BATTERY_FULL);
} else if (level > 65) { } else if (level > 65) {
@ -48,6 +53,7 @@ static void set_battery_symbol(lv_obj_t *label, struct battery_status_state stat
} else { } else {
strcat(text, LV_SYMBOL_BATTERY_EMPTY); strcat(text, LV_SYMBOL_BATTERY_EMPTY);
} }
#endif
lv_label_set_text(label, text); lv_label_set_text(label, text);
lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0); lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
} }