summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/CMakeLists.txt1
-rw-r--r--app/Kconfig28
-rw-r--r--app/src/rgb_underglow.c124
-rw-r--r--app/src/settings.c13
-rw-r--r--docs/docs/feature/underglow.md16
5 files changed, 144 insertions, 38 deletions
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index 3e0560b..8c21687 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -59,6 +59,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 6180565..a9d4f34 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
@@ -180,6 +184,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 13912e3..d453755 100644
--- a/app/src/rgb_underglow.c
+++ b/app/src/rgb_underglow.c
@@ -7,6 +7,7 @@
#include <device.h>
#include <init.h>
#include <kernel.h>
+#include <settings/settings.h>
#include <math.h>
#include <stdlib.h>
@@ -14,7 +15,6 @@
#include <logging/log.h>
#include <drivers/led_strip.h>
-#include <device.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
@@ -45,13 +45,43 @@ struct rgb_underglow_state {
bool on;
};
-struct rgb_underglow_state state;
-
struct device *led_strip;
struct led_rgb pixels[STRIP_NUM_PIXELS];
-static struct led_rgb hsb_to_rgb(struct led_hsb hsb) {
+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;
u8_t i = hsb.h / 60;
@@ -100,8 +130,20 @@ static struct led_rgb hsb_to_rgb(struct led_hsb hsb) {
return rgb;
}
-static void zmk_rgb_underglow_effect_solid() {
- for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
+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;
int sat = state.saturation;
int brt = state.brightness;
@@ -181,7 +223,17 @@ 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) {
+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,23 +249,36 @@ 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,
- animation_step : 0,
- on : true
+ 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: 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_cycle_effect(int direction) {
- if (!led_strip)
- return -ENODEV;
+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;
if (state.current_effect == 0 && direction < 0) {
state.current_effect = UNDERGLOW_EFFECT_NUMBER - 1;
@@ -228,7 +293,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 +306,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 +319,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 +344,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 +361,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 +378,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 0000000..390ede7
--- /dev/null
+++ b/app/src/settings.c
@@ -0,0 +1,13 @@
+#include <device.h>
+#include <init.h>
+#include <kernel.h>
+#include <settings/settings.h>
+
+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 c6517ce..ab44ebd 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