mirror of
https://github.com/zmkfirmware/zmk.git
synced 2026-04-10 15:25:18 -05:00
refactor(display): Output/layer/battery thread safety.
* Submit widget updates to display queue. * Use mutex to control access to shared state for widgets.
This commit is contained in:
committed by
Pete Johanson
parent
063d98e3df
commit
2128b2b55f
@@ -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) */
|
||||
|
||||
Reference in New Issue
Block a user