Browse Source

feat(rgb): underglow state Kconfig and settings

xmkb
Nick 4 years ago
parent
commit
3ee2d1196b
  1. 24
      app/Kconfig
  2. 77
      app/src/rgb_underglow.c

24
app/Kconfig

@ -180,6 +180,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

77
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>
@ -45,12 +46,39 @@ 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;
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) static struct led_rgb hsb_to_rgb(struct led_hsb hsb)
{ {
double r, g, b; double r, g, b;
@ -187,20 +215,35 @@ 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,
#ifdef CONFIG_ZMK_RGB_UNDERGLOW_ON_START
on: true 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; 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) int zmk_rgb_underglow_cycle_effect(int direction)
{ {
if (!led_strip) return -ENODEV; if (!led_strip) return -ENODEV;
@ -218,7 +261,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()
@ -242,7 +285,7 @@ int zmk_rgb_underglow_toggle()
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)
@ -250,17 +293,15 @@ int zmk_rgb_underglow_change_hue(int direction)
if (!led_strip) return -ENODEV; if (!led_strip) 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)
@ -277,7 +318,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)
@ -294,7 +335,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)
@ -311,7 +352,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, SYS_INIT(zmk_rgb_underglow_init,

Loading…
Cancel
Save