Skip to content

Commit

Permalink
watchface quick menu: Sync brightness value between settings app and …
Browse files Browse the repository at this point in the history
…quick menu brightness control.
  • Loading branch information
jakkra committed May 17, 2024
1 parent ec83158 commit b3c4874
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/src/applications/settings/settings_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ static void ble_pairing_work_handler(struct k_work *work)

static void settings_app_start(lv_obj_t *root, lv_group_t *group)
{
settings_load_subtree(ZSW_SETTINGS_PATH); // Update any values that may have changed outside of the settings app.
lv_settings_create(root, settings_menu, ARRAY_SIZE(settings_menu), "N/A", group, on_close_settings);
}

Expand Down
38 changes: 32 additions & 6 deletions app/src/applications/watchface/watchface_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ static void zbus_ble_comm_data_callback(const struct zbus_channel *chan);
static void zbus_accel_data_callback(const struct zbus_channel *chan);
static void zbus_battery_sample_data_callback(const struct zbus_channel *chan);
static void zbus_activity_event_callback(const struct zbus_channel *chan);
static int settings_load_handler(const char *key, size_t len, settings_read_cb read_cb, void *cb_arg, void *param);
static int settings_load_handler_watchface(const char *key, size_t len, settings_read_cb read_cb, void *cb_arg,
void *param);
static int settings_load_handler_brightness(const char *key, size_t len, settings_read_cb read_cb, void *cb_arg,
void *param);

ZBUS_CHAN_DECLARE(ble_comm_data_chan);
ZBUS_LISTENER_DEFINE(watchface_ble_comm_lis, zbus_ble_comm_data_callback);
Expand Down Expand Up @@ -118,6 +121,7 @@ static lv_obj_t *watchface_root_screen;
static watchface_ui_api_t *watchfaces[MAX_WATCHFACES];
static uint8_t num_watchfaces;
static zsw_settings_watchface_t watchface_settings;
static zsw_settings_brightness_t brightness_setting;

static watchface_app_evt_listener watchface_evt_cb;

Expand All @@ -143,11 +147,16 @@ void watchface_app_register_ui(watchface_ui_api_t *ui_api)
void watchface_app_start(lv_obj_t *root_screen, lv_group_t *group, watchface_app_evt_listener evt_cb)
{
__ASSERT(num_watchfaces > 0, "Must enable at least one watchface.");
int err = settings_load_subtree_direct(ZSW_SETTINGS_WATCHFACE, settings_load_handler, &watchface_settings);
int err = settings_load_subtree_direct(ZSW_SETTINGS_WATCHFACE, settings_load_handler_watchface, &watchface_settings);
if (err != 0) {
LOG_ERR("Failed loading watchface settings");
}

err = settings_load_subtree_direct(ZSW_SETTINGS_BRIGHTNESS, settings_load_handler_brightness, &brightness_setting);
if (err != 0) {
LOG_ERR("Failed loading brightness settings");
}

if (watchface_settings.watchface_index >= num_watchfaces) {
watchface_settings.watchface_index = 0;
}
Expand Down Expand Up @@ -182,7 +191,7 @@ void watchface_change(int index)
watchfaces[watchface_settings.watchface_index]->remove();

// Make sure we have the latest settings
int err = settings_load_subtree_direct(ZSW_SETTINGS_WATCHFACE, settings_load_handler, &watchface_settings);
int err = settings_load_subtree_direct(ZSW_SETTINGS_WATCHFACE, settings_load_handler_watchface, &watchface_settings);
if (err != 0) {
LOG_ERR("Failed loading watchface settings");
}
Expand Down Expand Up @@ -255,7 +264,7 @@ static void general_work(struct k_work *item)
is_suspended = false;
// Dropdown
watchfaces[watchface_settings.watchface_index]->show(watchface_root_screen, watchface_evt_cb, &watchface_settings);
zsw_watchface_dropdown_ui_add(watchface_root_screen, watchface_evt_cb);
zsw_watchface_dropdown_ui_add(watchface_root_screen, watchface_evt_cb, brightness_setting);
refresh_ui();

__ASSERT(0 <= k_work_schedule(&clock_work.work, K_NO_WAIT), "FAIL clock_work");
Expand Down Expand Up @@ -420,8 +429,8 @@ static void zbus_activity_event_callback(const struct zbus_channel *chan)
}
}

static int settings_load_handler(const char *key, size_t len,
settings_read_cb read_cb, void *cb_arg, void *param)
static int settings_load_handler_watchface(const char *key, size_t len,
settings_read_cb read_cb, void *cb_arg, void *param)
{
int rc;
zsw_settings_watchface_t *settings = (zsw_settings_watchface_t *)param;
Expand All @@ -437,4 +446,21 @@ static int settings_load_handler(const char *key, size_t len,
return -ENODATA;
}

static int settings_load_handler_brightness(const char *key, size_t len,
settings_read_cb read_cb, void *cb_arg, void *param)
{
int rc;
zsw_settings_brightness_t *settings = (zsw_settings_brightness_t *)param;
if (len != sizeof(zsw_settings_brightness_t)) {
return -EINVAL;
}

rc = read_cb(cb_arg, settings, sizeof(zsw_settings_brightness_t));
if (rc >= 0) {
return 0;
}

return -ENODATA;
}

SYS_INIT(watchface_app_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
5 changes: 4 additions & 1 deletion app/src/applications/watchface/watchface_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ typedef struct watchface_app_evt_t {
watchface_app_evt_type_t type;
union watchface_app_evt_data_t {
watchface_app_evt_open_app_t app;
uint16_t brightness;
struct {
uint16_t brightness;
bool store_brightness;
};
} data;
} watchface_app_evt_t;

Expand Down
4 changes: 4 additions & 0 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,10 @@ static void on_watchface_app_event_callback(watchface_app_evt_t evt)
break;
case WATCHFACE_APP_EVENT_SET_BRIGHTNESS:
zsw_display_control_set_brightness(evt.data.brightness);
zsw_settings_brightness_t brightness = evt.data.brightness;
if (evt.data.store_brightness) {
settings_save_one(ZSW_SETTINGS_BRIGHTNESS, &brightness, sizeof(brightness));
}
break;
case WATCHFACE_APP_EVENT_RESTART:
sys_reboot(SYS_REBOOT_COLD);
Expand Down
9 changes: 5 additions & 4 deletions app/src/ui/watchfaces/zsw_watchface_dropdown_ui.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "zsw_ui.h"
#include "lvgl.h"
#include "drivers/zsw_display_control.h"
#include "applications/watchface/watchface_app.h"

static lv_obj_t *ui_down_bg_panel;
Expand Down Expand Up @@ -54,10 +53,11 @@ static const void *face_goog_battery[] = {
void ui_event_light_slider(lv_event_t *e)
{
lv_event_code_t event_code = lv_event_get_code(e);
if (event_code == LV_EVENT_VALUE_CHANGED) {
if ((event_code == LV_EVENT_VALUE_CHANGED) || (event_code == LV_EVENT_RELEASED)) {
ui_light_slider_value = lv_slider_get_value(ui_bri_slider);
evt_cb((watchface_app_evt_t) {
WATCHFACE_APP_EVENT_SET_BRIGHTNESS, .data.brightness = ui_light_slider_value
WATCHFACE_APP_EVENT_SET_BRIGHTNESS, .data.brightness = ui_light_slider_value,
.data.store_brightness = event_code == LV_EVENT_RELEASED
});
}
}
Expand Down Expand Up @@ -123,12 +123,13 @@ static void on_lvgl_screen_gesture_event_callback_drop(lv_event_t *e)
}

void zsw_watchface_dropdown_ui_add(lv_obj_t *root_page,
watchface_app_evt_listener callback /*, TODO input starting state of buttons and sliders */)
watchface_app_evt_listener callback, int brightness)
{
__ASSERT(dropdown_root == NULL, "dropdown_root is not NULL");

evt_cb = callback;
dropdown_root = root_page;
ui_light_slider_value = brightness;

ui_down_bg_panel = lv_obj_create(root_page);
lv_obj_set_width(ui_down_bg_panel, 240);
Expand Down
2 changes: 1 addition & 1 deletion app/src/ui/watchfaces/zsw_watchface_dropdown_ui.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "applications/watchface/watchface_app.h"

void zsw_watchface_dropdown_ui_add(lv_obj_t *root_page, watchface_app_evt_listener evt_cb);
void zsw_watchface_dropdown_ui_add(lv_obj_t *root_page, watchface_app_evt_listener evt_cb, int brightness);
void zsw_watchface_dropdown_ui_set_music_info(char *track_name, char *artist);
void zsw_watchface_dropdown_ui_set_battery_info(uint8_t battery_percent, bool is_charging, int tte, int ttf);
void zsw_watchface_dropdown_ui_remove(void);

0 comments on commit b3c4874

Please sign in to comment.