Browse Source

Merge pull request #109 from Nicell/underglow/state-persistence

Add RGB underglow state Kconfig and settings
xmkb
Pete Johanson 4 years ago committed by GitHub
parent
commit
ef37f17796
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/CMakeLists.txt
  2. 28
      app/Kconfig
  3. 95
      app/src/rgb_underglow.c
  4. 8
      app/src/settings.c
  5. 14
      docs/docs/feature/underglow.md

1
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_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/rgb_underglow.c)
target_sources(app PRIVATE src/endpoints.c) target_sources(app PRIVATE src/endpoints.c)
target_sources(app PRIVATE src/hid_listener.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) target_sources(app PRIVATE src/main.c)
zephyr_cc_option(-Wfatal-errors) zephyr_cc_option(-Wfatal-errors)

28
app/Kconfig

@ -217,6 +217,10 @@ menuconfig ZMK_RGB_UNDERGLOW
if 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 config ZMK_RGB_UNDERGLOW_HUE_STEP
int "RGB underglow hue step in degrees of 360" int "RGB underglow hue step in degrees of 360"
default 10 default 10
@ -229,6 +233,30 @@ config ZMK_RGB_UNDERGLOW_BRT_STEP
int "RGB underglow brightness step in percent" int "RGB underglow brightness step in percent"
default 10 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 endif
endmenu endmenu

95
app/src/rgb_underglow.c

@ -7,6 +7,7 @@
#include <device.h> #include <device.h>
#include <init.h> #include <init.h>
#include <kernel.h> #include <kernel.h>
#include <settings/settings.h>
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
@ -14,7 +15,6 @@
#include <logging/log.h> #include <logging/log.h>
#include <drivers/led_strip.h> #include <drivers/led_strip.h>
#include <device.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
@ -45,12 +45,36 @@ struct rgb_underglow_state {
bool on; bool on;
}; };
struct rgb_underglow_state state;
struct device *led_strip; struct device *led_strip;
struct led_rgb pixels[STRIP_NUM_PIXELS]; 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) { static struct led_rgb hsb_to_rgb(struct led_hsb hsb) {
double r, g, b; double r, g, b;
@ -100,6 +124,14 @@ static struct led_rgb hsb_to_rgb(struct led_hsb hsb) {
return rgb; 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() { static void zmk_rgb_underglow_effect_solid() {
for (int i = 0; i < STRIP_NUM_PIXELS; i++) { for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
int hue = state.hue; 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); 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); k_work_submit(&underglow_work);
} }
@ -197,20 +237,32 @@ static int zmk_rgb_underglow_init(struct device *_arg) {
} }
state = (struct rgb_underglow_state){ state = (struct rgb_underglow_state){
hue : 0, hue : CONFIG_ZMK_RGB_UNDERGLOW_HUE_START,
saturation : 100, saturation : CONFIG_ZMK_RGB_UNDERGLOW_SAT_START,
brightness : 100, brightness : CONFIG_ZMK_RGB_UNDERGLOW_BRT_START,
animation_speed : 3, animation_speed : CONFIG_ZMK_RGB_UNDERGLOW_SPD_START,
current_effect : 0, current_effect : CONFIG_ZMK_RGB_UNDERGLOW_EFF_START,
animation_step : 0, 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)); k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50));
return 0; 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) { int zmk_rgb_underglow_cycle_effect(int direction) {
if (!led_strip) if (!led_strip)
return -ENODEV; return -ENODEV;
@ -228,7 +280,7 @@ int zmk_rgb_underglow_cycle_effect(int direction) {
state.animation_step = 0; state.animation_step = 0;
return 0; return zmk_rgb_underglow_save_state();
} }
int zmk_rgb_underglow_toggle() { int zmk_rgb_underglow_toggle() {
@ -241,17 +293,12 @@ int zmk_rgb_underglow_toggle() {
state.animation_step = 0; state.animation_step = 0;
k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50));
} else { } else {
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);
k_timer_stop(&underglow_tick); k_timer_stop(&underglow_tick);
} }
return 0; return zmk_rgb_underglow_save_state();
} }
int zmk_rgb_underglow_change_hue(int direction) { int zmk_rgb_underglow_change_hue(int direction) {
@ -259,17 +306,15 @@ int zmk_rgb_underglow_change_hue(int direction) {
return -ENODEV; return -ENODEV;
if (state.hue == 0 && direction < 0) { if (state.hue == 0 && direction < 0) {
state.hue = 350; state.hue = 360 - CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP;
return 0; return 0;
} }
state.hue += direction * CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP; state.hue += direction * CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP;
if (state.hue > 350) { state.hue = state.hue % 360;
state.hue = 0;
}
return 0; return zmk_rgb_underglow_save_state();
} }
int zmk_rgb_underglow_change_sat(int direction) { int zmk_rgb_underglow_change_sat(int direction) {
@ -286,7 +331,7 @@ int zmk_rgb_underglow_change_sat(int direction) {
state.saturation = 100; state.saturation = 100;
} }
return 0; return zmk_rgb_underglow_save_state();
} }
int zmk_rgb_underglow_change_brt(int direction) { int zmk_rgb_underglow_change_brt(int direction) {
@ -303,7 +348,7 @@ int zmk_rgb_underglow_change_brt(int direction) {
state.brightness = 100; state.brightness = 100;
} }
return 0; return zmk_rgb_underglow_save_state();
} }
int zmk_rgb_underglow_change_spd(int direction) { int zmk_rgb_underglow_change_spd(int direction) {
@ -320,7 +365,7 @@ int zmk_rgb_underglow_change_spd(int direction) {
state.animation_speed = 5; state.animation_speed = 5;
} }
return 0; return zmk_rgb_underglow_save_state();
} }
SYS_INIT(zmk_rgb_underglow_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); SYS_INIT(zmk_rgb_underglow_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);

8
app/src/settings.c

@ -0,0 +1,8 @@
#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);

14
docs/docs/feature/underglow.md

@ -36,10 +36,16 @@ 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. There are various Kconfig options used to configure the RGB underglow feature. These can all be set in the `.conf` file.
| Option | Description | Default | | Option | Description | Default |
| ---------------------------- | ---------------------------------------------- | ------- | |-------------------------------|------------------------------------------------|---------|
| `ZMK_RGB_UNDERGLOW_HUE_STEP` | Hue step in degrees of 360 used by RGB actions | `10` | | `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_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_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 ## Adding RGB Underglow to a Board

Loading…
Cancel
Save