summaryrefslogtreecommitdiff
path: root/app/src/display/widgets/battery_status.c
diff options
context:
space:
mode:
authorPeter Johanson <peter@peterjohanson.com>2021-08-12 03:44:38 +0000
committerPete Johanson <peter@peterjohanson.com>2021-09-25 17:17:04 -0400
commit2128b2b55f85a6190194d83696f7419eb53c6642 (patch)
tree9ed0a99a5646d6e1b911be6dc3dca1ac58832fdb /app/src/display/widgets/battery_status.c
parent063d98e3dfa8e0089aa0039b24489d29b062cf5e (diff)
refactor(display): Output/layer/battery thread safety.
* Submit widget updates to display queue. * Use mutex to control access to shared state for widgets.
Diffstat (limited to 'app/src/display/widgets/battery_status.c')
-rw-r--r--app/src/display/widgets/battery_status.c53
1 files changed, 36 insertions, 17 deletions
diff --git a/app/src/display/widgets/battery_status.c b/app/src/display/widgets/battery_status.c
index 309c3e9..7503c61 100644
--- a/app/src/display/widgets/battery_status.c
+++ b/app/src/display/widgets/battery_status.c
@@ -4,11 +4,13 @@
* SPDX-License-Identifier: MIT
*/
+#include <kernel.h>
#include <bluetooth/services/bas.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
+#include <zmk/display.h>
#include <zmk/display/widgets/battery_status.h>
#include <zmk/usb.h>
#include <zmk/events/usb_conn_state_changed.h>
@@ -33,12 +35,20 @@ void battery_status_init() {
lv_style_set_text_line_space(&label_style, LV_STATE_DEFAULT, 1);
}
-void set_battery_symbol(lv_obj_t *label) {
+struct battery_status_state {
+ uint8_t level;
+#if IS_ENABLED(CONFIG_USB)
+ bool usb_present;
+#endif
+};
+
+static void set_battery_symbol(lv_obj_t *label, struct battery_status_state state) {
char text[2] = " ";
- uint8_t level = bt_bas_get_battery_level();
+
+ uint8_t level = state.level;
#if IS_ENABLED(CONFIG_USB)
- if (zmk_usb_is_powered()) {
+ if (state.usb_present) {
strcpy(text, LV_SYMBOL_CHARGE);
}
#endif /* IS_ENABLED(CONFIG_USB) */
@@ -57,32 +67,41 @@ void set_battery_symbol(lv_obj_t *label) {
lv_label_set_text(label, text);
}
+void battery_status_update_cb(struct battery_status_state state) {
+ struct zmk_widget_battery_status *widget;
+ SYS_SLIST_FOR_EACH_CONTAINER(&widgets, widget, node) { set_battery_symbol(widget->obj, state); }
+}
+
+static struct battery_status_state battery_status_get_state(const zmk_event_t *eh) {
+ return (struct battery_status_state) {
+ .level = bt_bas_get_battery_level(),
+#if IS_ENABLED(CONFIG_USB)
+ .usb_present = zmk_usb_is_powered(),
+#endif /* IS_ENABLED(CONFIG_USB) */
+ };
+}
+
+ZMK_DISPLAY_WIDGET_LISTENER(widget_battery_status, struct battery_status_state,
+ battery_status_update_cb, battery_status_get_state)
+
+ZMK_SUBSCRIPTION(widget_battery_status, zmk_battery_state_changed);
+#if IS_ENABLED(CONFIG_USB)
+ZMK_SUBSCRIPTION(widget_battery_status, zmk_usb_conn_state_changed);
+#endif /* IS_ENABLED(CONFIG_USB) */
+
int zmk_widget_battery_status_init(struct zmk_widget_battery_status *widget, lv_obj_t *parent) {
battery_status_init();
widget->obj = lv_label_create(parent, NULL);
lv_obj_add_style(widget->obj, LV_LABEL_PART_MAIN, &label_style);
lv_obj_set_size(widget->obj, 40, 15);
- set_battery_symbol(widget->obj);
sys_slist_append(&widgets, &widget->node);
+ widget_battery_status_init();
return 0;
}
lv_obj_t *zmk_widget_battery_status_obj(struct zmk_widget_battery_status *widget) {
- LOG_DBG("Label: %p", widget->obj);
return widget->obj;
}
-
-int battery_status_listener(const zmk_event_t *eh) {
- struct zmk_widget_battery_status *widget;
- SYS_SLIST_FOR_EACH_CONTAINER(&widgets, widget, node) { set_battery_symbol(widget->obj); }
- return ZMK_EV_EVENT_BUBBLE;
-}
-
-ZMK_LISTENER(widget_battery_status, battery_status_listener)
-ZMK_SUBSCRIPTION(widget_battery_status, zmk_battery_state_changed);
-#if IS_ENABLED(CONFIG_USB)
-ZMK_SUBSCRIPTION(widget_battery_status, zmk_usb_conn_state_changed);
-#endif /* IS_ENABLED(CONFIG_USB) */