From bb09707bd2627b29c150d85244f6f6295b6f0175 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 24 Jul 2020 01:06:56 -0500 Subject: [PATCH 1/6] Add base files and configuration for RGB underglow --- app/CMakeLists.txt | 2 + app/Kconfig | 24 +++++++ app/boards/arm/nice_nano/nice_nano.dts | 10 --- app/src/behaviors/behavior_rgb_underglow.c | 21 ++++++ app/src/rgb_underglow.c | 79 ++++++++++++++++++++++ 5 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 app/src/behaviors/behavior_rgb_underglow.c create mode 100644 app/src/rgb_underglow.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 45c68856..07c78080 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -52,6 +52,8 @@ target_sources_ifdef(CONFIG_ZMK_KSCAN_MOCK_DRIVER app PRIVATE src/kscan_mock.c) target_sources_ifdef(CONFIG_ZMK_KSCAN_COMPOSITE_DRIVER app PRIVATE src/kscan_composite.c) target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c) 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/behaviors/behavior_rgb_underglow.c) target_sources(app PRIVATE src/endpoints.c) target_sources(app PRIVATE src/main.c) diff --git a/app/Kconfig b/app/Kconfig index 997409ef..df768b31 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -138,6 +138,30 @@ config ZMK_ACTION_MOD_TAP endmenu +menu "ZMK Lighting" + +menuconfig ZMK_RGB_UNDERGLOW + bool "RGB Adressable LED Underglow" + select LED_STRIP + +if ZMK_RGB_UNDERGLOW + +config ZMK_RGB_UNDERGLOW_HUE_STEP + int "RGB underglow hue step in degrees of 360" + default 10 + +config ZMK_RGB_UNDERGLOW_SAT_STEP + int "RGB underglow sturation step in percent" + default 10 + +config ZMK_RGB_UNDERGLOW_BRT_STEP + int "RGB underglow brightness step in percent" + default 10 + +endif + +endmenu + config HEAP_MEM_POOL_SIZE default 1024 diff --git a/app/boards/arm/nice_nano/nice_nano.dts b/app/boards/arm/nice_nano/nice_nano.dts index 8c010692..7c676e3e 100644 --- a/app/boards/arm/nice_nano/nice_nano.dts +++ b/app/boards/arm/nice_nano/nice_nano.dts @@ -52,16 +52,6 @@ scl-pin = <20>; }; -/* TODO: Needs testing */ -&spi0 { - compatible = "nordic,nrf-spi"; - /* Cannot be used together with i2c0. */ - /* status = "okay"; */ - sck-pin = <45>; - mosi-pin = <10>; - miso-pin = <43>; -}; - &uart0 { compatible = "nordic,nrf-uarte"; status = "okay"; diff --git a/app/src/behaviors/behavior_rgb_underglow.c b/app/src/behaviors/behavior_rgb_underglow.c new file mode 100644 index 00000000..9239f370 --- /dev/null +++ b/app/src/behaviors/behavior_rgb_underglow.c @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2020 Nick Winans + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_rgb_underglow + +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct behavior_rgb_underglow_config { }; +struct behavior_rgb_underglow_data { }; + +static int behavior_rgb_underglow_init(struct device *dev) +{ + return 0; +} diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c new file mode 100644 index 00000000..5b71aae5 --- /dev/null +++ b/app/src/rgb_underglow.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2020 Nick Winans + * + * SPDX-License-Identifier: MIT + */ + +#include +#include + +#include + +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#define STRIP_LABEL DT_LABEL(DT_ALIAS(led_strip)) +#define STRIP_NUM_PIXELS DT_PROP(DT_ALIAS(led_strip), chain_length) + +#define DELAY_TIME K_MSEC(50) + +#define RGB(_r, _g, _b) { .r = (_r), .g = (_g), .b = (_b) } + +static const struct led_rgb colors[] = { + RGB(0x0f, 0x00, 0x00), /* red */ + RGB(0x00, 0x0f, 0x00), /* green */ + RGB(0x00, 0x00, 0x0f), /* blue */ +}; + +struct led_rgb pixels[STRIP_NUM_PIXELS]; + +static void zmk_rgb_underglow_start() +{ + struct device *strip; + size_t cursor = 0, color = 0; + int rc; + + strip = device_get_binding(STRIP_LABEL); + if (strip) { + LOG_INF("Found LED strip device %s", STRIP_LABEL); + } else { + LOG_ERR("LED strip device %s not found", STRIP_LABEL); + return; + } + + LOG_INF("Displaying pattern on strip"); + while (1) { + memset(&pixels, 0x00, sizeof(pixels)); + memcpy(&pixels[cursor], &colors[color], sizeof(struct led_rgb)); + rc = led_strip_update_rgb(strip, pixels, STRIP_NUM_PIXELS); + + if (rc) { + LOG_ERR("couldn't update strip: %d", rc); + } + + cursor++; + if (cursor >= STRIP_NUM_PIXELS) { + cursor = 0; + color++; + if (color == ARRAY_SIZE(colors)) { + color = 0; + } + } + + k_sleep(DELAY_TIME); + } +} + +static int zmk_rgb_underglow_init(struct device *_arg) +{ + zmk_rgb_underglow_start(); + + return 0; +} + +SYS_INIT(zmk_rgb_underglow_init, + APPLICATION, + CONFIG_APPLICATION_INIT_PRIORITY); From ca569c814393b4ab90aedc5227523f9ce64dd581 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 24 Jul 2020 22:37:00 -0500 Subject: [PATCH 2/6] Initial RGB Underglow implementation --- app/CMakeLists.txt | 1 + app/dts/behaviors.dtsi | 3 +- app/dts/behaviors/rgb_underglow.dtsi | 9 + .../behaviors/zmk,behavior-rgb-underglow.yaml | 8 + app/include/dt-bindings/zmk/rgb.h | 12 + app/include/zmk/rgb_underglow.h | 8 + app/src/behaviors/behavior_rgb_underglow.c | 59 ++++ app/src/rgb_underglow.c | 292 +++++++++++++++--- 8 files changed, 353 insertions(+), 39 deletions(-) create mode 100644 app/dts/behaviors/rgb_underglow.dtsi create mode 100644 app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml create mode 100644 app/include/dt-bindings/zmk/rgb.h create mode 100644 app/include/zmk/rgb_underglow.h diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 07c78080..71ef2b20 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -44,6 +44,7 @@ target_sources(app PRIVATE src/behaviors/behavior_mod_tap.c) target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c) target_sources(app PRIVATE src/behaviors/behavior_transparent.c) target_sources(app PRIVATE src/behaviors/behavior_sensor_rotate_key_press.c) +target_sources(app PRIVATE src/behaviors/behavior_rgb_underglow.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split_listener.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split/bluetooth/service.c) diff --git a/app/dts/behaviors.dtsi b/app/dts/behaviors.dtsi index 39784799..04e42b63 100644 --- a/app/dts/behaviors.dtsi +++ b/app/dts/behaviors.dtsi @@ -3,4 +3,5 @@ #include #include #include -#include \ No newline at end of file +#include +#include \ No newline at end of file diff --git a/app/dts/behaviors/rgb_underglow.dtsi b/app/dts/behaviors/rgb_underglow.dtsi new file mode 100644 index 00000000..9a1ddcf2 --- /dev/null +++ b/app/dts/behaviors/rgb_underglow.dtsi @@ -0,0 +1,9 @@ +/ { + behaviors { + rgb_ug: behavior_rgb_underglow { + compatible = "zmk,behavior-rgb-underglow"; + label = "RGB_UNDERGLOW_ACTION"; + #binding-cells = <1>; + }; + }; +}; diff --git a/app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml b/app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml new file mode 100644 index 00000000..6b6d5b00 --- /dev/null +++ b/app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml @@ -0,0 +1,8 @@ +# Copyright (c) 2020, Nick Winans +# SPDX-License-Identifier: MIT + +description: RGB Underglow Action + +compatible: "zmk,behavior-rgb-underglow" + +include: one_param.yaml \ No newline at end of file diff --git a/app/include/dt-bindings/zmk/rgb.h b/app/include/dt-bindings/zmk/rgb.h new file mode 100644 index 00000000..c2efda88 --- /dev/null +++ b/app/include/dt-bindings/zmk/rgb.h @@ -0,0 +1,12 @@ + +#define RGB_TOG 0 +#define RGB_HUI 1 +#define RGB_HUD 2 +#define RGB_SAI 3 +#define RGB_SAD 4 +#define RGB_BRI 5 +#define RGB_BRD 6 +#define RGB_SPI 7 +#define RGB_SPD 8 +#define RGB_EFF 9 +#define RGB_EFR 10 diff --git a/app/include/zmk/rgb_underglow.h b/app/include/zmk/rgb_underglow.h new file mode 100644 index 00000000..60754d53 --- /dev/null +++ b/app/include/zmk/rgb_underglow.h @@ -0,0 +1,8 @@ +#pragma once + +void zmk_rgb_underglow_toggle(); +void zmk_rgb_underglow_cycle_effect(int direction); +void zmk_rgb_underglow_change_hue(int direction); +void zmk_rgb_underglow_change_sat(int direction); +void zmk_rgb_underglow_change_brt(int direction); +void zmk_rgb_underglow_change_spd(int direction); diff --git a/app/src/behaviors/behavior_rgb_underglow.c b/app/src/behaviors/behavior_rgb_underglow.c index 9239f370..7c839536 100644 --- a/app/src/behaviors/behavior_rgb_underglow.c +++ b/app/src/behaviors/behavior_rgb_underglow.c @@ -10,6 +10,9 @@ #include #include +#include +#include + LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); struct behavior_rgb_underglow_config { }; @@ -19,3 +22,59 @@ static int behavior_rgb_underglow_init(struct device *dev) { return 0; } + +static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t action, u32_t _) +{ + switch (action) + { + case RGB_TOG: + zmk_rgb_underglow_toggle(); + break; + case RGB_HUI: + zmk_rgb_underglow_change_hue(1); + break; + case RGB_HUD: + zmk_rgb_underglow_change_hue(-1); + break; + case RGB_SAI: + zmk_rgb_underglow_change_sat(1); + break; + case RGB_SAD: + zmk_rgb_underglow_change_sat(-1); + break; + case RGB_BRI: + zmk_rgb_underglow_change_brt(1); + break; + case RGB_BRD: + zmk_rgb_underglow_change_brt(-1); + break; + case RGB_SPI: + zmk_rgb_underglow_change_spd(1); + break; + case RGB_SPD: + zmk_rgb_underglow_change_spd(-1); + break; + case RGB_EFF: + zmk_rgb_underglow_cycle_effect(1); + break; + case RGB_EFR: + zmk_rgb_underglow_cycle_effect(-1); + break; + } + + return 0; +} + +static const struct behavior_driver_api behavior_rgb_underglow_driver_api = { + .binding_pressed = on_keymap_binding_pressed, +}; + +static const struct behavior_rgb_underglow_config behavior_rgb_underglow_config = {}; + +static struct behavior_rgb_underglow_data behavior_rgb_underglow_data; + +DEVICE_AND_API_INIT(behavior_rgb_underglow, DT_INST_LABEL(0), behavior_rgb_underglow_init, + &behavior_rgb_underglow_data, + &behavior_rgb_underglow_config, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &behavior_rgb_underglow_driver_api); \ No newline at end of file diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 5b71aae5..e4287c01 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -6,6 +6,10 @@ #include #include +#include + +#include +#include #include @@ -15,63 +19,275 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -#define STRIP_LABEL DT_LABEL(DT_ALIAS(led_strip)) +#define STRIP_LABEL DT_LABEL(DT_ALIAS(led_strip)) #define STRIP_NUM_PIXELS DT_PROP(DT_ALIAS(led_strip), chain_length) -#define DELAY_TIME K_MSEC(50) +enum rgb_underglow_effect { + UNDERGLOW_EFFECT_SOLID, + UNDERGLOW_EFFECT_BREATHE, + UNDERGLOW_EFFECT_SPECTRUM, + UNDERGLOW_EFFECT_SWIRL, + UNDERGLOW_EFFECT_NUMBER // Used to track number of underglow effects +}; -#define RGB(_r, _g, _b) { .r = (_r), .g = (_g), .b = (_b) } +struct led_hsb { + u16_t h; + u8_t s; + u8_t b; +}; -static const struct led_rgb colors[] = { - RGB(0x0f, 0x00, 0x00), /* red */ - RGB(0x00, 0x0f, 0x00), /* green */ - RGB(0x00, 0x00, 0x0f), /* blue */ +struct rgb_underglow_state { + u16_t hue; + u8_t saturation; + u8_t brightness; + u8_t animation_speed; + u8_t current_effect; + u16_t animation_step; + bool on; }; +struct rgb_underglow_state state; + +struct device *led_strip; + struct led_rgb pixels[STRIP_NUM_PIXELS]; -static void zmk_rgb_underglow_start() +static struct led_rgb hsb_to_rgb(struct led_hsb hsb) +{ + double r, g, b; + + u8_t i = hsb.h / 60; + double v = hsb.b / 100.0; + double s = hsb.s / 100.0; + double f = hsb.h / 360.0 * 6 - i; + double p = v * (1 - s); + double q = v * (1 - f * s); + double t = v * (1 - (1 - f) * s); + + switch (i % 6) + { + case 0: r = v; g = t; b = p; break; + case 1: r = q; g = v; b = p; break; + case 2: r = p; g = v; b = t; break; + case 3: r = p; g = q; b = v; break; + case 4: r = t; g = p; b = v; break; + case 5: r = v; g = p; b = q; break; + } + + struct led_rgb rgb = { r: r*255, g: g*255, b: b*255 }; + + return rgb; +} + +static void zmk_rgb_underglow_effect_solid() +{ + for (int i=0; i 2400) { + state.animation_step = 0; + } +} + +static void zmk_rgb_underglow_effect_spectrum() +{ + for (int i=0; i= STRIP_NUM_PIXELS) { - cursor = 0; - color++; - if (color == ARRAY_SIZE(colors)) { - color = 0; - } - } - - k_sleep(DELAY_TIME); - } + state = (struct rgb_underglow_state){ + hue: 0, + saturation: 100, + brightness: 100, + animation_speed: 3, + current_effect: 0, + animation_step: 0, + on: true + }; + + k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); + + return 0; } -static int zmk_rgb_underglow_init(struct device *_arg) +void zmk_rgb_underglow_cycle_effect(int direction) { - zmk_rgb_underglow_start(); + if (state.current_effect == 0 && direction < 0) { + state.current_effect = UNDERGLOW_EFFECT_NUMBER - 1; + } else { + state.current_effect += direction; - return 0; + if (state.current_effect >= UNDERGLOW_EFFECT_NUMBER) { + state.current_effect = 0; + } + } + + state.animation_step = 0; +} + +void zmk_rgb_underglow_toggle() +{ + state.on = !state.on; + + if (state.on) { + state.animation_step = 0; + k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); + } else { + + for (int i=0; i 350) { + state.hue = 0; + } +} + +void zmk_rgb_underglow_change_sat(int direction) +{ + if (state.saturation == 0 && direction < 0) { + return; + } + + state.saturation += direction * CONFIG_ZMK_RGB_UNDERGLOW_SAT_STEP; + + if (state.saturation > 100) { + state.saturation = 100; + } +} + +void zmk_rgb_underglow_change_brt(int direction) +{ + if (state.brightness == 0 && direction < 0) { + return; + } + + state.brightness += direction * CONFIG_ZMK_RGB_UNDERGLOW_BRT_STEP; + + if (state.brightness > 100) { + state.brightness = 100; + } +} + +void zmk_rgb_underglow_change_spd(int direction) +{ + if (state.animation_speed == 1 && direction < 0) { + return; + } + + state.animation_speed += direction; + + if (state.animation_speed > 5) { + state.animation_speed = 5; + } } SYS_INIT(zmk_rgb_underglow_init, From 295af93289e37d9fb9ff89d69e8008ac81c85041 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 24 Jul 2020 22:48:48 -0500 Subject: [PATCH 3/6] Only include behavior if feature enabled --- app/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 71ef2b20..85f29b3c 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -44,7 +44,7 @@ target_sources(app PRIVATE src/behaviors/behavior_mod_tap.c) target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c) target_sources(app PRIVATE src/behaviors/behavior_transparent.c) target_sources(app PRIVATE src/behaviors/behavior_sensor_rotate_key_press.c) -target_sources(app PRIVATE src/behaviors/behavior_rgb_underglow.c) +target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/behaviors/behavior_rgb_underglow.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split_listener.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split/bluetooth/service.c) From 564f7872805e9fa823d767e6dd1b2b55fd5ebdf3 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 25 Jul 2020 14:53:42 -0500 Subject: [PATCH 4/6] Add suggested changes --- app/CMakeLists.txt | 1 - app/dts/behaviors/rgb_underglow.dtsi | 2 +- app/include/zmk/rgb_underglow.h | 18 +++++--- app/src/behaviors/behavior_rgb_underglow.c | 44 ++++++------------- app/src/rgb_underglow.c | 50 ++++++++++++++-------- 5 files changed, 57 insertions(+), 58 deletions(-) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 85f29b3c..243228f0 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -54,7 +54,6 @@ target_sources_ifdef(CONFIG_ZMK_KSCAN_COMPOSITE_DRIVER app PRIVATE src/kscan_com target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c) 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/behaviors/behavior_rgb_underglow.c) target_sources(app PRIVATE src/endpoints.c) target_sources(app PRIVATE src/main.c) diff --git a/app/dts/behaviors/rgb_underglow.dtsi b/app/dts/behaviors/rgb_underglow.dtsi index 9a1ddcf2..e8ad9da8 100644 --- a/app/dts/behaviors/rgb_underglow.dtsi +++ b/app/dts/behaviors/rgb_underglow.dtsi @@ -2,7 +2,7 @@ behaviors { rgb_ug: behavior_rgb_underglow { compatible = "zmk,behavior-rgb-underglow"; - label = "RGB_UNDERGLOW_ACTION"; + label = "RGB_UNDERGLOW"; #binding-cells = <1>; }; }; diff --git a/app/include/zmk/rgb_underglow.h b/app/include/zmk/rgb_underglow.h index 60754d53..69e9a9bb 100644 --- a/app/include/zmk/rgb_underglow.h +++ b/app/include/zmk/rgb_underglow.h @@ -1,8 +1,14 @@ +/* + * Copyright (c) 2020 Nick Winans + * + * SPDX-License-Identifier: MIT + */ + #pragma once -void zmk_rgb_underglow_toggle(); -void zmk_rgb_underglow_cycle_effect(int direction); -void zmk_rgb_underglow_change_hue(int direction); -void zmk_rgb_underglow_change_sat(int direction); -void zmk_rgb_underglow_change_brt(int direction); -void zmk_rgb_underglow_change_spd(int direction); +int zmk_rgb_underglow_toggle(); +int zmk_rgb_underglow_cycle_effect(int direction); +int zmk_rgb_underglow_change_hue(int direction); +int zmk_rgb_underglow_change_sat(int direction); +int zmk_rgb_underglow_change_brt(int direction); +int zmk_rgb_underglow_change_spd(int direction); diff --git a/app/src/behaviors/behavior_rgb_underglow.c b/app/src/behaviors/behavior_rgb_underglow.c index 7c839536..1207ed21 100644 --- a/app/src/behaviors/behavior_rgb_underglow.c +++ b/app/src/behaviors/behavior_rgb_underglow.c @@ -15,9 +15,6 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -struct behavior_rgb_underglow_config { }; -struct behavior_rgb_underglow_data { }; - static int behavior_rgb_underglow_init(struct device *dev) { return 0; @@ -28,53 +25,38 @@ static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t a switch (action) { case RGB_TOG: - zmk_rgb_underglow_toggle(); - break; + return zmk_rgb_underglow_toggle(); case RGB_HUI: - zmk_rgb_underglow_change_hue(1); - break; + return zmk_rgb_underglow_change_hue(1); case RGB_HUD: - zmk_rgb_underglow_change_hue(-1); - break; + return zmk_rgb_underglow_change_hue(-1); case RGB_SAI: - zmk_rgb_underglow_change_sat(1); - break; + return zmk_rgb_underglow_change_sat(1); case RGB_SAD: - zmk_rgb_underglow_change_sat(-1); - break; + return zmk_rgb_underglow_change_sat(-1); case RGB_BRI: zmk_rgb_underglow_change_brt(1); - break; case RGB_BRD: - zmk_rgb_underglow_change_brt(-1); - break; + return zmk_rgb_underglow_change_brt(-1); case RGB_SPI: - zmk_rgb_underglow_change_spd(1); - break; + return zmk_rgb_underglow_change_spd(1); case RGB_SPD: - zmk_rgb_underglow_change_spd(-1); - break; + return zmk_rgb_underglow_change_spd(-1); case RGB_EFF: - zmk_rgb_underglow_cycle_effect(1); - break; + return zmk_rgb_underglow_cycle_effect(1); case RGB_EFR: - zmk_rgb_underglow_cycle_effect(-1); - break; + return zmk_rgb_underglow_cycle_effect(-1); } - return 0; + return -ENOTSUP; } static const struct behavior_driver_api behavior_rgb_underglow_driver_api = { .binding_pressed = on_keymap_binding_pressed, }; -static const struct behavior_rgb_underglow_config behavior_rgb_underglow_config = {}; - -static struct behavior_rgb_underglow_data behavior_rgb_underglow_data; - DEVICE_AND_API_INIT(behavior_rgb_underglow, DT_INST_LABEL(0), behavior_rgb_underglow_init, - &behavior_rgb_underglow_data, - &behavior_rgb_underglow_config, + NULL, + NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_rgb_underglow_driver_api); \ No newline at end of file diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index e4287c01..16a49894 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -15,12 +15,11 @@ #include #include -#include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -#define STRIP_LABEL DT_LABEL(DT_ALIAS(led_strip)) -#define STRIP_NUM_PIXELS DT_PROP(DT_ALIAS(led_strip), chain_length) +#define STRIP_LABEL DT_LABEL(DT_CHOSEN(zmk_underglow)) +#define STRIP_NUM_PIXELS DT_PROP(DT_CHOSEN(zmk_underglow), chain_length) enum rgb_underglow_effect { UNDERGLOW_EFFECT_SOLID, @@ -184,7 +183,7 @@ static int zmk_rgb_underglow_init(struct device *_arg) LOG_INF("Found LED strip device %s", STRIP_LABEL); } else { LOG_ERR("LED strip device %s not found", STRIP_LABEL); - return 1; + return -EINVAL; } state = (struct rgb_underglow_state){ @@ -202,22 +201,25 @@ static int zmk_rgb_underglow_init(struct device *_arg) return 0; } -void zmk_rgb_underglow_cycle_effect(int direction) +int zmk_rgb_underglow_cycle_effect(int direction) { if (state.current_effect == 0 && direction < 0) { state.current_effect = UNDERGLOW_EFFECT_NUMBER - 1; - } else { - state.current_effect += direction; + return 0; + } - if (state.current_effect >= UNDERGLOW_EFFECT_NUMBER) { - state.current_effect = 0; - } + state.current_effect += direction; + + if (state.current_effect >= UNDERGLOW_EFFECT_NUMBER) { + state.current_effect = 0; } state.animation_step = 0; + + return 0; } -void zmk_rgb_underglow_toggle() +int zmk_rgb_underglow_toggle() { state.on = !state.on; @@ -235,13 +237,15 @@ void zmk_rgb_underglow_toggle() k_timer_stop(&underglow_tick); } + + return 0; } -void zmk_rgb_underglow_change_hue(int direction) +int zmk_rgb_underglow_change_hue(int direction) { if (state.hue == 0 && direction < 0) { state.hue = 350; - return; + return 0; } state.hue += direction * CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP; @@ -249,12 +253,14 @@ void zmk_rgb_underglow_change_hue(int direction) if (state.hue > 350) { state.hue = 0; } + + return 0; } -void zmk_rgb_underglow_change_sat(int direction) +int zmk_rgb_underglow_change_sat(int direction) { if (state.saturation == 0 && direction < 0) { - return; + return 0; } state.saturation += direction * CONFIG_ZMK_RGB_UNDERGLOW_SAT_STEP; @@ -262,12 +268,14 @@ void zmk_rgb_underglow_change_sat(int direction) if (state.saturation > 100) { state.saturation = 100; } + + return 0; } -void zmk_rgb_underglow_change_brt(int direction) +int zmk_rgb_underglow_change_brt(int direction) { if (state.brightness == 0 && direction < 0) { - return; + return 0; } state.brightness += direction * CONFIG_ZMK_RGB_UNDERGLOW_BRT_STEP; @@ -275,12 +283,14 @@ void zmk_rgb_underglow_change_brt(int direction) if (state.brightness > 100) { state.brightness = 100; } + + return 0; } -void zmk_rgb_underglow_change_spd(int direction) +int zmk_rgb_underglow_change_spd(int direction) { if (state.animation_speed == 1 && direction < 0) { - return; + return 0; } state.animation_speed += direction; @@ -288,6 +298,8 @@ void zmk_rgb_underglow_change_spd(int direction) if (state.animation_speed > 5) { state.animation_speed = 5; } + + return 0; } SYS_INIT(zmk_rgb_underglow_init, From 6956094ac023c3307a1207d358637bbe9073433b Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 25 Jul 2020 15:39:01 -0500 Subject: [PATCH 5/6] Add missing return To brightness up --- app/src/behaviors/behavior_rgb_underglow.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/behaviors/behavior_rgb_underglow.c b/app/src/behaviors/behavior_rgb_underglow.c index 1207ed21..7a48e070 100644 --- a/app/src/behaviors/behavior_rgb_underglow.c +++ b/app/src/behaviors/behavior_rgb_underglow.c @@ -35,7 +35,7 @@ static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t a case RGB_SAD: return zmk_rgb_underglow_change_sat(-1); case RGB_BRI: - zmk_rgb_underglow_change_brt(1); + return zmk_rgb_underglow_change_brt(1); case RGB_BRD: return zmk_rgb_underglow_change_brt(-1); case RGB_SPI: From dc8c7011b15b1c0703fe788b42f18adce90907ee Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 26 Jul 2020 12:49:43 -0500 Subject: [PATCH 6/6] If LED strip can't be found, behaviors return err --- app/src/rgb_underglow.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 16a49894..95adcec2 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -203,6 +203,8 @@ static int zmk_rgb_underglow_init(struct device *_arg) 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; @@ -221,6 +223,8 @@ int zmk_rgb_underglow_cycle_effect(int direction) int zmk_rgb_underglow_toggle() { + if (!led_strip) return -ENODEV; + state.on = !state.on; if (state.on) { @@ -243,6 +247,8 @@ int zmk_rgb_underglow_toggle() int zmk_rgb_underglow_change_hue(int direction) { + if (!led_strip) return -ENODEV; + if (state.hue == 0 && direction < 0) { state.hue = 350; return 0; @@ -259,6 +265,8 @@ int zmk_rgb_underglow_change_hue(int direction) int zmk_rgb_underglow_change_sat(int direction) { + if (!led_strip) return -ENODEV; + if (state.saturation == 0 && direction < 0) { return 0; } @@ -274,6 +282,8 @@ int zmk_rgb_underglow_change_sat(int direction) int zmk_rgb_underglow_change_brt(int direction) { + if (!led_strip) return -ENODEV; + if (state.brightness == 0 && direction < 0) { return 0; } @@ -289,6 +299,8 @@ int zmk_rgb_underglow_change_brt(int direction) int zmk_rgb_underglow_change_spd(int direction) { + if (!led_strip) return -ENODEV; + if (state.animation_speed == 1 && direction < 0) { return 0; }