Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkra committed Oct 22, 2023
1 parent b283d75 commit ad53934
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
43 changes: 28 additions & 15 deletions app/src/applications/settings/settings_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -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,
}
Expand All @@ -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,
}
}
},
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -207,32 +215,36 @@ 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)
{
const char *next;
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;
}

Expand All @@ -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);
6 changes: 3 additions & 3 deletions app/src/applications/settings/settings_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions app/src/zsw_settings.h
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit ad53934

Please sign in to comment.