diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 39509edf..8a3971e8 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -62,6 +62,7 @@ target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/hog.c) target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/rgb_underglow.c) target_sources(app PRIVATE src/endpoints.c) target_sources(app PRIVATE src/hid_listener.c) +target_sources_ifdef(CONFIG_SETTINGS app PRIVATE src/settings.c) target_sources(app PRIVATE src/main.c) zephyr_cc_option(-Wfatal-errors) diff --git a/app/Kconfig b/app/Kconfig index 4cd01eba..285ed912 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -217,6 +217,10 @@ menuconfig ZMK_RGB_UNDERGLOW if ZMK_RGB_UNDERGLOW +# This default value cuts down on tons of excess .conf files, if you're using GPIO, manually disable this +config SPI + default y + config ZMK_RGB_UNDERGLOW_HUE_STEP int "RGB underglow hue step in degrees of 360" default 10 @@ -229,6 +233,30 @@ config ZMK_RGB_UNDERGLOW_BRT_STEP int "RGB underglow brightness step in percent" default 10 +config ZMK_RGB_UNDERGLOW_HUE_START + int "RGB underglow start hue value from 0-359" + default 0 + +config ZMK_RGB_UNDERGLOW_SAT_START + int "RGB underglow start saturations value from 0-100" + default 100 + +config ZMK_RGB_UNDERGLOW_BRT_START + int "RGB underglow start brightness value from 0-100" + default 100 + +config ZMK_RGB_UNDERGLOW_SPD_START + int "RGB underglow start animation speed value from 1-5" + default 3 + +config ZMK_RGB_UNDERGLOW_EFF_START + int "RGB underglow start effect int value related to the effect enum list" + default 0 + +config ZMK_RGB_UNDERGLOW_ON_START + bool "Whether RGB underglow starts on by default" + default y + endif endmenu diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 13912e30..b371c943 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -14,7 +15,6 @@ #include #include -#include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -45,12 +45,36 @@ struct rgb_underglow_state { bool on; }; -struct rgb_underglow_state state; - struct device *led_strip; struct led_rgb pixels[STRIP_NUM_PIXELS]; +struct rgb_underglow_state state; + +#if IS_ENABLED(CONFIG_SETTINGS) +static int rgb_settings_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg) { + const char *next; + int rc; + + if (settings_name_steq(name, "state", &next) && !next) { + if (len != sizeof(state)) { + return -EINVAL; + } + + rc = read_cb(cb_arg, &state, sizeof(state)); + if (rc >= 0) { + return 0; + } + + return rc; + } + + return -ENOENT; +} + +struct settings_handler rgb_conf = {.name = "rgb/underglow", .h_set = rgb_settings_set}; +#endif + static struct led_rgb hsb_to_rgb(struct led_hsb hsb) { double r, g, b; @@ -100,6 +124,14 @@ static struct led_rgb hsb_to_rgb(struct led_hsb hsb) { return rgb; } +static void zmk_rgb_underglow_off() { + for (int i = 0; i < STRIP_NUM_PIXELS; i++) { + pixels[i] = (struct led_rgb){r : 0, g : 0, b : 0}; + } + + led_strip_update_rgb(led_strip, pixels, STRIP_NUM_PIXELS); +} + static void zmk_rgb_underglow_effect_solid() { for (int i = 0; i < STRIP_NUM_PIXELS; i++) { int hue = state.hue; @@ -182,6 +214,14 @@ static void zmk_rgb_underglow_tick(struct k_work *work) { K_WORK_DEFINE(underglow_work, zmk_rgb_underglow_tick); static void zmk_rgb_underglow_tick_handler(struct k_timer *timer) { + if (!state.on) { + zmk_rgb_underglow_off(); + + k_timer_stop(timer); + + return; + } + k_work_submit(&underglow_work); } @@ -197,20 +237,32 @@ static int zmk_rgb_underglow_init(struct device *_arg) { } state = (struct rgb_underglow_state){ - hue : 0, - saturation : 100, - brightness : 100, - animation_speed : 3, - current_effect : 0, + hue : CONFIG_ZMK_RGB_UNDERGLOW_HUE_START, + saturation : CONFIG_ZMK_RGB_UNDERGLOW_SAT_START, + brightness : CONFIG_ZMK_RGB_UNDERGLOW_BRT_START, + animation_speed : CONFIG_ZMK_RGB_UNDERGLOW_SPD_START, + current_effect : CONFIG_ZMK_RGB_UNDERGLOW_EFF_START, animation_step : 0, - on : true + on : IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_ON_START) }; +#if IS_ENABLED(CONFIG_SETTINGS) + settings_register(&rgb_conf); +#endif + k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); return 0; } +int zmk_rgb_underglow_save_state() { +#if IS_ENABLED(CONFIG_SETTINGS) + return settings_save_one("rgb/underglow/state", &state, sizeof(state)); +#else + return 0; +#endif +} + int zmk_rgb_underglow_cycle_effect(int direction) { if (!led_strip) return -ENODEV; @@ -228,7 +280,7 @@ int zmk_rgb_underglow_cycle_effect(int direction) { state.animation_step = 0; - return 0; + return zmk_rgb_underglow_save_state(); } int zmk_rgb_underglow_toggle() { @@ -241,17 +293,12 @@ int zmk_rgb_underglow_toggle() { state.animation_step = 0; k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); } else { - - for (int i = 0; i < STRIP_NUM_PIXELS; i++) { - pixels[i] = (struct led_rgb){r : 0, g : 0, b : 0}; - } - - led_strip_update_rgb(led_strip, pixels, STRIP_NUM_PIXELS); + zmk_rgb_underglow_off(); k_timer_stop(&underglow_tick); } - return 0; + return zmk_rgb_underglow_save_state(); } int zmk_rgb_underglow_change_hue(int direction) { @@ -259,17 +306,15 @@ int zmk_rgb_underglow_change_hue(int direction) { return -ENODEV; if (state.hue == 0 && direction < 0) { - state.hue = 350; + state.hue = 360 - CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP; return 0; } state.hue += direction * CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP; - if (state.hue > 350) { - state.hue = 0; - } + state.hue = state.hue % 360; - return 0; + return zmk_rgb_underglow_save_state(); } int zmk_rgb_underglow_change_sat(int direction) { @@ -286,7 +331,7 @@ int zmk_rgb_underglow_change_sat(int direction) { state.saturation = 100; } - return 0; + return zmk_rgb_underglow_save_state(); } int zmk_rgb_underglow_change_brt(int direction) { @@ -303,7 +348,7 @@ int zmk_rgb_underglow_change_brt(int direction) { state.brightness = 100; } - return 0; + return zmk_rgb_underglow_save_state(); } int zmk_rgb_underglow_change_spd(int direction) { @@ -320,7 +365,7 @@ int zmk_rgb_underglow_change_spd(int direction) { state.animation_speed = 5; } - return 0; + return zmk_rgb_underglow_save_state(); } SYS_INIT(zmk_rgb_underglow_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); diff --git a/app/src/settings.c b/app/src/settings.c new file mode 100644 index 00000000..8914ccc3 --- /dev/null +++ b/app/src/settings.c @@ -0,0 +1,8 @@ +#include +#include +#include +#include + +static int zmk_settings_init(struct device *_arg) { return settings_load(); } + +SYS_INIT(zmk_settings_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); diff --git a/docs/docs/feature/underglow.md b/docs/docs/feature/underglow.md index c6517ced..ab44ebd1 100644 --- a/docs/docs/feature/underglow.md +++ b/docs/docs/feature/underglow.md @@ -35,11 +35,17 @@ If your board or shield does not have RGB underglow configured, refer to [Adding There are various Kconfig options used to configure the RGB underglow feature. These can all be set in the `.conf` file. -| Option | Description | Default | -| ---------------------------- | ---------------------------------------------- | ------- | -| `ZMK_RGB_UNDERGLOW_HUE_STEP` | Hue step in degrees of 360 used by RGB actions | `10` | -| `ZMK_RGB_UNDERGLOW_SAT_STEP` | Saturation step in percent used by RGB actions | `10` | -| `ZMK_RGB_UNDERGLOW_BRT_STEP` | Brightness step in percent used by RGB actions | `10` | +| Option | Description | Default | +|-------------------------------|------------------------------------------------|---------| +| `ZMK_RGB_UNDERGLOW_HUE_STEP` | Hue step in degrees of 360 used by RGB actions | 10 | +| `ZMK_RGB_UNDERGLOW_SAT_STEP` | Saturation step in percent used by RGB actions | 10 | +| `ZMK_RGB_UNDERGLOW_BRT_STEP` | Brightness step in percent used by RGB actions | 10 | +| `ZMK_RGB_UNDERGLOW_HUE_START` | Default hue 0-359 in degrees | 0 | +| `ZMK_RGB_UNDERGLOW_SAT_START` | Default saturation 0-100 in percent | 100 | +| `ZMK_RGB_UNDERGLOW_BRT_START` | Default brightness 0-100 in percent | 100 | +| `ZMK_RGB_UNDERGLOW_SPD_START` | Default effect speed 1-5 | 3 | +| `ZMK_RGB_UNDERGLOW_EFF_START` | Default effect integer from the effect enum | 0 | +| `ZMK_RGB_UNDERGLOW_ON_START` | Default on state | y | ## Adding RGB Underglow to a Board