Browse Source

refactor(rgb): Expose explicit on/off command/API.

xmkb
Pete Johanson 4 years ago
parent
commit
bb2c478af9
  1. 26
      app/include/dt-bindings/zmk/rgb.h
  2. 3
      app/include/zmk/rgb_underglow.h
  3. 4
      app/src/behaviors/behavior_rgb_underglow.c
  4. 81
      app/src/rgb_underglow.c

26
app/include/dt-bindings/zmk/rgb.h

@ -5,19 +5,23 @@
*/ */
#define RGB_TOG_CMD 0 #define RGB_TOG_CMD 0
#define RGB_HUI_CMD 1 #define RGB_ON_CMD 1
#define RGB_HUD_CMD 2 #define RGB_OFF_CMD 2
#define RGB_SAI_CMD 3 #define RGB_HUI_CMD 3
#define RGB_SAD_CMD 4 #define RGB_HUD_CMD 4
#define RGB_BRI_CMD 5 #define RGB_SAI_CMD 5
#define RGB_BRD_CMD 6 #define RGB_SAD_CMD 6
#define RGB_SPI_CMD 7 #define RGB_BRI_CMD 7
#define RGB_SPD_CMD 8 #define RGB_BRD_CMD 8
#define RGB_EFF_CMD 9 #define RGB_SPI_CMD 9
#define RGB_EFR_CMD 10 #define RGB_SPD_CMD 10
#define RGB_COLOR_HSB_CMD 11 #define RGB_EFF_CMD 11
#define RGB_EFR_CMD 12
#define RGB_COLOR_HSB_CMD 13
#define RGB_TOG RGB_TOG_CMD 0 #define RGB_TOG RGB_TOG_CMD 0
#define RGB_ON RGB_ON_CMD 0
#define RGB_OFF RGB_OFF_CMD 0
#define RGB_HUI RGB_HUI_CMD 0 #define RGB_HUI RGB_HUI_CMD 0
#define RGB_HUD RGB_HUD_CMD 0 #define RGB_HUD RGB_HUD_CMD 0
#define RGB_SAI RGB_SAI_CMD 0 #define RGB_SAI RGB_SAI_CMD 0

3
app/include/zmk/rgb_underglow.h

@ -7,6 +7,9 @@
#pragma once #pragma once
int zmk_rgb_underglow_toggle(); int zmk_rgb_underglow_toggle();
int zmk_rgb_underglow_get_state(bool *state);
int zmk_rgb_underglow_on();
int zmk_rgb_underglow_off();
int zmk_rgb_underglow_cycle_effect(int direction); int zmk_rgb_underglow_cycle_effect(int direction);
int zmk_rgb_underglow_change_hue(int direction); int zmk_rgb_underglow_change_hue(int direction);
int zmk_rgb_underglow_change_sat(int direction); int zmk_rgb_underglow_change_sat(int direction);

4
app/src/behaviors/behavior_rgb_underglow.c

@ -25,6 +25,10 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
switch (binding->param1) { switch (binding->param1) {
case RGB_TOG_CMD: case RGB_TOG_CMD:
return zmk_rgb_underglow_toggle(); return zmk_rgb_underglow_toggle();
case RGB_ON_CMD:
return zmk_rgb_underglow_on();
case RGB_OFF_CMD:
return zmk_rgb_underglow_off();
case RGB_HUI_CMD: case RGB_HUI_CMD:
return zmk_rgb_underglow_change_hue(1); return zmk_rgb_underglow_change_hue(1);
case RGB_HUD_CMD: case RGB_HUD_CMD:

81
app/src/rgb_underglow.c

@ -105,14 +105,6 @@ 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;
@ -196,10 +188,6 @@ 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) { if (!state.on) {
zmk_rgb_underglow_off();
k_timer_stop(timer);
return; return;
} }
@ -292,60 +280,83 @@ int zmk_rgb_underglow_save_state() {
#endif #endif
} }
int zmk_rgb_underglow_cycle_effect(int direction) { int zmk_rgb_underglow_get_state(bool *on_off) {
if (!led_strip) if (!led_strip)
return -ENODEV; return -ENODEV;
if (state.current_effect == 0 && direction < 0) { *on_off = state.on;
state.current_effect = UNDERGLOW_EFFECT_NUMBER - 1;
return 0; return 0;
} }
state.current_effect += direction; int zmk_rgb_underglow_on() {
if (!led_strip)
return -ENODEV;
if (state.current_effect >= UNDERGLOW_EFFECT_NUMBER) { #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_EXT_POWER)
state.current_effect = 0; if (ext_power != NULL) {
int rc = ext_power_enable(ext_power);
if (rc != 0) {
LOG_ERR("Unable to enable EXT_POWER: %d", rc);
}
} }
#endif
state.on = true;
state.animation_step = 0; state.animation_step = 0;
k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50));
return zmk_rgb_underglow_save_state(); return zmk_rgb_underglow_save_state();
} }
int zmk_rgb_underglow_toggle() { int zmk_rgb_underglow_off() {
if (!led_strip) if (!led_strip)
return -ENODEV; return -ENODEV;
state.on = !state.on;
#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_EXT_POWER) #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_EXT_POWER)
if (ext_power != NULL) { if (ext_power != NULL) {
int rc; int rc = ext_power_disable(ext_power);
if (state.on) {
rc = ext_power_enable(ext_power);
} else {
rc = ext_power_disable(ext_power);
}
if (rc != 0) { if (rc != 0) {
LOG_ERR("Unable to toggle EXT_POWER: %d", rc); LOG_ERR("Unable to disable EXT_POWER: %d", rc);
} }
} }
#endif #endif
if (state.on) { for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
state.animation_step = 0; pixels[i] = (struct led_rgb){r : 0, g : 0, b : 0};
k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); }
} else {
zmk_rgb_underglow_off(); led_strip_update_rgb(led_strip, pixels, STRIP_NUM_PIXELS);
k_timer_stop(&underglow_tick); k_timer_stop(&underglow_tick);
state.on = false;
return zmk_rgb_underglow_save_state();
}
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;
return 0;
} }
state.current_effect += direction;
if (state.current_effect >= UNDERGLOW_EFFECT_NUMBER) {
state.current_effect = 0;
}
state.animation_step = 0;
return zmk_rgb_underglow_save_state(); return zmk_rgb_underglow_save_state();
} }
int zmk_rgb_underglow_toggle() {
return state.on ? zmk_rgb_underglow_off() : zmk_rgb_underglow_on();
}
int zmk_rgb_underglow_set_hsb(uint16_t hue, uint8_t saturation, uint8_t brightness) { int zmk_rgb_underglow_set_hsb(uint16_t hue, uint8_t saturation, uint8_t brightness) {
if (hue > 360 || saturation > 100 || brightness > 100) { if (hue > 360 || saturation > 100 || brightness > 100) {
return -ENOTSUP; return -ENOTSUP;

Loading…
Cancel
Save