From ad53934464b3af5742ca9773c1ea4fb3cfc449e7 Mon Sep 17 00:00:00 2001 From: Jakob Krantz Date: Sun, 22 Oct 2023 15:44:11 +0200 Subject: [PATCH] WIP --- app/src/applications/settings/settings_app.c | 43 +++++++++++++------- app/src/applications/settings/settings_ui.h | 6 +-- app/src/zsw_settings.h | 14 +++++++ 3 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 app/src/zsw_settings.h diff --git a/app/src/applications/settings/settings_app.c b/app/src/applications/settings/settings_app.c index 161d3573..d7633485 100644 --- a/app/src/applications/settings/settings_app.c +++ b/app/src/applications/settings/settings_app.c @@ -10,6 +10,7 @@ #include "sensors/zsw_imu.h" #include "drivers/zsw_display_control.h" #include "managers/zsw_app_manager.h" +#include "zsw_settings.h" static void settings_app_start(lv_obj_t *root, lv_group_t *group); static void settings_app_stop(void); @@ -24,6 +25,13 @@ static void on_clear_bonded_changed(lv_setting_value_t value, bool final); LV_IMG_DECLARE(settings); +typedef setting_app_t { + zsw_settings_brightness_t brightness; + zsw_settings_vib_on_press_t vibration_on_click; +} setting_app_t; + +static setting_app_t settings_app; + static application_t app = { .name = "Settings", .icon = &settings, @@ -39,7 +47,7 @@ static lv_settings_item_t general_page_items[] = { .item = { .slider = { .name = "Brightness", - .inital_val = 3, + .inital_val = &settings_app.brightness, .min_val = 1, .max_val = 10, } @@ -51,7 +59,7 @@ static lv_settings_item_t general_page_items[] = { .item = { .sw = { .name = "Vibrate on click", - .inital_val = true + .inital_val = &settings_app.vibration_on_click, } } }, @@ -161,9 +169,9 @@ static void on_close_settings(void) static void on_brightness_changed(lv_setting_value_t value, bool final) { // Slider have values 0-10 hence multiply with 10 to get brightness in percent - zsw_display_control_set_brightness(value.item.slider * 10); - value.item.slider *= 10; - settings_save_one("settings/brightness", &value.item.slider, sizeof(value.item.slider)); + settings_app.brightness = value.item.slider * 10; + zsw_display_control_set_brightness(settings_app.brightness); + settings_save_one(ZSW_SETTINGS_BRIGHTNESS, &settings_app.brightness, sizeof(settings_app.brightness)); } static void on_display_on_changed(lv_setting_value_t value, bool final) @@ -207,12 +215,6 @@ static void on_reset_steps_changed(lv_setting_value_t value, bool final) } } -static int setting_on_commit(void) -{ - printk("Settings loaded\n"); - return 0; -} - static int settings_load_cb(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg) { @@ -220,19 +222,29 @@ static int settings_load_cb(const char *name, size_t len, int rc; int32_t bri; - if (settings_name_steq(name, "brightness", &next) && !next) { + if (settings_name_steq(name, ZSW_SETTINGS_KEY_BRIGHTNESS, &next) && !next) { if (len != sizeof(bri)) { return -EINVAL; } rc = read_cb(cb_arg, &bri, sizeof(bri)); - printk("Read br: %d\n", bri); general_page_items[0].item.slider.inital_val = bri / 10; zsw_display_control_set_brightness(bri); if (rc >= 0) { return 0; } + return rc; + } if (settings_name_steq(name, ZSW_SETTINGS_KEY_VIBRATION, &next) && !next) { + if (len != sizeof(bri)) { + return -EINVAL; + } + rc = read_cb(cb_arg, &bri, sizeof(bri)); + general_page_items[0].item.slider.inital_val = bri / 10; + zsw_display_control_set_brightness(bri); + if (rc >= 0) { + return 0; + } return rc; } @@ -243,11 +255,12 @@ static int settings_load_cb(const char *name, size_t len, static int settings_app_add(void) { zsw_app_manager_add_application(&app); + memset(settings_app, 0, sizeof(settings_app)); return 0; } -SETTINGS_STATIC_HANDLER_DEFINE(my_name, "settings", NULL, - settings_load_cb, setting_on_commit, NULL); +SETTINGS_STATIC_HANDLER_DEFINE(settings_app_handler, ZSW_SETTINGS_PATH, NULL, + settings_load_cb, NULL, NULL); SYS_INIT(settings_app_add, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); diff --git a/app/src/applications/settings/settings_ui.h b/app/src/applications/settings/settings_ui.h index 31ce3b8b..1b5dae52 100644 --- a/app/src/applications/settings/settings_ui.h +++ b/app/src/applications/settings/settings_ui.h @@ -46,13 +46,13 @@ typedef struct { } lv_settings_button_t; typedef struct { - const char *name; - bool inital_val; + const char *name; + bool *inital_val; } lv_settings_switch_t; typedef struct { const char *name; - int32_t inital_val; + int32_t *inital_val; int32_t min_val; int32_t max_val; } lv_settings_slider_t; diff --git a/app/src/zsw_settings.h b/app/src/zsw_settings.h new file mode 100644 index 00000000..5d46c302 --- /dev/null +++ b/app/src/zsw_settings.h @@ -0,0 +1,14 @@ +#pragma once + +#define ZSW_SETTINGS_NAME(name, key) (name "/" key) + +#define ZSW_SETTINGS_PATH "settings" + +typedef zsw_settings_brightness_t uint32_t +#define ZSW_SETTINGS_KEY_BRIGHTNESS "bri" +#define ZSW_SETTINGS_BRIGHTNESS (ZSW_SETTINGS_PATH "/" ZSW_SETTINGS_KEY_BRIGHTNESS) + +typedef zsw_settings_vib_on_press_t bool +#define ZSW_SETTINGS_KEY_VIBRATION_ON_PRESS "vib" +#define ZSW_SETTINGS_VIBRATE_ON_PRESS (ZSW_SETTINGS_PATH "/" ZSW_SETTINGS_KEY_VIBRATION) +