From 3ee2d1196b9335a081d841ff3918d2fdf722d5b9 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 20 Aug 2020 00:07:04 -0500 Subject: [PATCH 1/7] feat(rgb): underglow state Kconfig and settings --- app/Kconfig | 24 +++++++++++++ app/src/rgb_underglow.c | 77 +++++++++++++++++++++++++++++++---------- 2 files changed, 83 insertions(+), 18 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index 416c9852..cefae21a 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -180,6 +180,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 95adcec2..5f557980 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -45,12 +46,39 @@ 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; + +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", + .h_set = rgb_settings_set +}; + static struct led_rgb hsb_to_rgb(struct led_hsb hsb) { double r, g, b; @@ -187,20 +215,35 @@ 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, +#ifdef CONFIG_ZMK_RGB_UNDERGLOW_ON_START on: true +#else + on: false +#endif }; - k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); + settings_subsys_init(); + settings_register(&rgb_conf); + settings_load(); + + if (state.on) { + k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); + } return 0; } +int zmk_rgb_underglow_save_state() +{ + return settings_save_one("rgb/state", &state, sizeof(state)); +} + int zmk_rgb_underglow_cycle_effect(int direction) { if (!led_strip) return -ENODEV; @@ -218,7 +261,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() @@ -242,7 +285,7 @@ int zmk_rgb_underglow_toggle() k_timer_stop(&underglow_tick); } - return 0; + return zmk_rgb_underglow_save_state(); } int zmk_rgb_underglow_change_hue(int direction) @@ -250,17 +293,15 @@ int zmk_rgb_underglow_change_hue(int direction) if (!led_strip) 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) @@ -277,7 +318,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) @@ -294,7 +335,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) @@ -311,7 +352,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, From bee5f7c350be17f0260004850ac715336a27fc94 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 20 Aug 2020 21:13:05 -0500 Subject: [PATCH 2/7] fix(rgb): add missing Kconfig docs --- docs/docs/feature/underglow.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) 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 From 74fd4fc997c0cf78cc9acabdd61466fa4857e7ba Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 20 Aug 2020 21:13:17 -0500 Subject: [PATCH 3/7] fix(rgb): fix underglow settings location --- app/src/rgb_underglow.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 5f557980..86223b17 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -75,7 +75,7 @@ static int rgb_settings_set(const char *name, size_t len, } struct settings_handler rgb_conf = { - .name = "rgb", + .name = "rgb/underglow", .h_set = rgb_settings_set }; @@ -241,7 +241,7 @@ static int zmk_rgb_underglow_init(struct device *_arg) int zmk_rgb_underglow_save_state() { - return settings_save_one("rgb/state", &state, sizeof(state)); + return settings_save_one("rgb/underglow/state", &state, sizeof(state)); } int zmk_rgb_underglow_cycle_effect(int direction) From c5c21022a23cae3fecaf17e13d18d7d1ebd5f150 Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 23 Aug 2020 10:33:19 -0500 Subject: [PATCH 4/7] Move settings load --- app/CMakeLists.txt | 1 + app/src/rgb_underglow.c | 40 ++++++++++++++++++++++------------------ app/src/settings.c | 13 +++++++++++++ 3 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 app/src/settings.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 054f84a7..bb4af555 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -55,4 +55,5 @@ 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(app PRIVATE src/settings.c) target_sources(app PRIVATE src/main.c) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 86223b17..3851421b 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -15,7 +15,6 @@ #include #include -#include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -106,6 +105,16 @@ static struct led_rgb hsb_to_rgb(struct led_hsb hsb) return rgb; } +static void zmk_rgb_underglow_off() +{ + for (int i=0; i +#include +#include +#include + +static int zmk_settings_init(struct device *_arg) +{ + return settings_load(); +} + +SYS_INIT(zmk_settings_init, + APPLICATION, + CONFIG_APPLICATION_INIT_PRIORITY); From 979a5bffffeac0fed12db00409dedb8ed6c4ad87 Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 23 Aug 2020 15:56:18 -0500 Subject: [PATCH 5/7] fix(rgb): check if settings enabled --- app/CMakeLists.txt | 2 +- app/src/rgb_underglow.c | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index bb4af555..bbb54b95 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -55,5 +55,5 @@ 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(app PRIVATE src/settings.c) +target_sources_ifdef(CONFIG_SETTINGS app PRIVATE src/settings.c) target_sources(app PRIVATE src/main.c) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 3851421b..cb98e8d9 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -51,6 +51,7 @@ 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) { @@ -77,6 +78,7 @@ 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) { @@ -242,7 +244,9 @@ static int zmk_rgb_underglow_init(struct device *_arg) 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)); @@ -251,7 +255,11 @@ static int zmk_rgb_underglow_init(struct device *_arg) 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) From 81bc157f539235ad032fde78b6f6cec7a16d2c39 Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 15 Sep 2020 14:31:59 -0500 Subject: [PATCH 6/7] Fix underglow not working by default --- app/Kconfig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Kconfig b/app/Kconfig index cefae21a..cda5c582 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -168,6 +168,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 From 608ae0df6dfe4d75bc98c36f6756dc4d69399109 Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 15 Sep 2020 14:47:19 -0500 Subject: [PATCH 7/7] fix lint with clang-format --- app/src/rgb_underglow.c | 57 ++++++++++++++++------------------------- app/src/settings.c | 9 ++----- 2 files changed, 24 insertions(+), 42 deletions(-) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index d453755b..b371c943 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -52,9 +52,7 @@ 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) -{ +static int rgb_settings_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg) { const char *next; int rc; @@ -74,14 +72,10 @@ static int rgb_settings_set(const char *name, size_t len, return -ENOENT; } -struct settings_handler rgb_conf = { - .name = "rgb/underglow", - .h_set = rgb_settings_set -}; +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) -{ +static struct led_rgb hsb_to_rgb(struct led_hsb hsb) { double r, g, b; u8_t i = hsb.h / 60; @@ -130,20 +124,16 @@ static struct led_rgb hsb_to_rgb(struct led_hsb hsb) return rgb; } -static void zmk_rgb_underglow_off() -{ - for (int i=0; i #include -static int zmk_settings_init(struct device *_arg) -{ - return settings_load(); -} +static int zmk_settings_init(struct device *_arg) { return settings_load(); } -SYS_INIT(zmk_settings_init, - APPLICATION, - CONFIG_APPLICATION_INIT_PRIORITY); +SYS_INIT(zmk_settings_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);