Browse Source

Merge pull request #64 from Nicell/feature/rgb_underglow

Initial RGB Underglow implementation
xmkb
Pete Johanson 4 years ago committed by GitHub
parent
commit
4e5a819f3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/CMakeLists.txt
  2. 24
      app/Kconfig
  3. 10
      app/boards/arm/nice_nano/nice_nano.dts
  4. 3
      app/dts/behaviors.dtsi
  5. 9
      app/dts/behaviors/rgb_underglow.dtsi
  6. 8
      app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml
  7. 12
      app/include/dt-bindings/zmk/rgb.h
  8. 14
      app/include/zmk/rgb_underglow.h
  9. 62
      app/src/behaviors/behavior_rgb_underglow.c
  10. 319
      app/src/rgb_underglow.c

2
app/CMakeLists.txt

@ -45,6 +45,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_momentary_layer.c)
target_sources(app PRIVATE src/behaviors/behavior_transparent.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_sensor_rotate_key_press.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_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_listener.c)
target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split/bluetooth/service.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split/bluetooth/service.c)
@ -53,6 +54,7 @@ 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_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_USB app PRIVATE src/usb_hid.c)
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/hog.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(app PRIVATE src/endpoints.c) target_sources(app PRIVATE src/endpoints.c)
target_sources(app PRIVATE src/main.c) target_sources(app PRIVATE src/main.c)

24
app/Kconfig

@ -146,6 +146,30 @@ config ZMK_ACTION_MOD_TAP
endmenu 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 config HEAP_MEM_POOL_SIZE
default 8192 default 8192

10
app/boards/arm/nice_nano/nice_nano.dts

@ -52,16 +52,6 @@
scl-pin = <20>; 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 { &uart0 {
compatible = "nordic,nrf-uarte"; compatible = "nordic,nrf-uarte";
status = "okay"; status = "okay";

3
app/dts/behaviors.dtsi

@ -3,4 +3,5 @@
#include <behaviors/mod_tap.dtsi> #include <behaviors/mod_tap.dtsi>
#include <behaviors/momentary_layer.dtsi> #include <behaviors/momentary_layer.dtsi>
#include <behaviors/reset.dtsi> #include <behaviors/reset.dtsi>
#include <behaviors/sensor_rotate_key_press.dtsi> #include <behaviors/sensor_rotate_key_press.dtsi>
#include <behaviors/rgb_underglow.dtsi>

9
app/dts/behaviors/rgb_underglow.dtsi

@ -0,0 +1,9 @@
/ {
behaviors {
rgb_ug: behavior_rgb_underglow {
compatible = "zmk,behavior-rgb-underglow";
label = "RGB_UNDERGLOW";
#binding-cells = <1>;
};
};
};

8
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

12
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

14
app/include/zmk/rgb_underglow.h

@ -0,0 +1,14 @@
/*
* Copyright (c) 2020 Nick Winans <nick@winans.codes>
*
* SPDX-License-Identifier: MIT
*/
#pragma once
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);

62
app/src/behaviors/behavior_rgb_underglow.c

@ -0,0 +1,62 @@
/*
* Copyright (c) 2020 Nick Winans <nick@winans.codes>
*
* SPDX-License-Identifier: MIT
*/
#define DT_DRV_COMPAT zmk_behavior_rgb_underglow
#include <device.h>
#include <drivers/behavior.h>
#include <logging/log.h>
#include <dt-bindings/zmk/rgb.h>
#include <zmk/rgb_underglow.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
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:
return zmk_rgb_underglow_toggle();
case RGB_HUI:
return zmk_rgb_underglow_change_hue(1);
case RGB_HUD:
return zmk_rgb_underglow_change_hue(-1);
case RGB_SAI:
return zmk_rgb_underglow_change_sat(1);
case RGB_SAD:
return zmk_rgb_underglow_change_sat(-1);
case RGB_BRI:
return zmk_rgb_underglow_change_brt(1);
case RGB_BRD:
return zmk_rgb_underglow_change_brt(-1);
case RGB_SPI:
return zmk_rgb_underglow_change_spd(1);
case RGB_SPD:
return zmk_rgb_underglow_change_spd(-1);
case RGB_EFF:
return zmk_rgb_underglow_cycle_effect(1);
case RGB_EFR:
return zmk_rgb_underglow_cycle_effect(-1);
}
return -ENOTSUP;
}
static const struct behavior_driver_api behavior_rgb_underglow_driver_api = {
.binding_pressed = on_keymap_binding_pressed,
};
DEVICE_AND_API_INIT(behavior_rgb_underglow, DT_INST_LABEL(0), behavior_rgb_underglow_init,
NULL,
NULL,
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&behavior_rgb_underglow_driver_api);

319
app/src/rgb_underglow.c

@ -0,0 +1,319 @@
/*
* Copyright (c) 2020 Nick Winans <nick@winans.codes>
*
* SPDX-License-Identifier: MIT
*/
#include <device.h>
#include <init.h>
#include <kernel.h>
#include <math.h>
#include <stdlib.h>
#include <logging/log.h>
#include <drivers/led_strip.h>
#include <device.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#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,
UNDERGLOW_EFFECT_BREATHE,
UNDERGLOW_EFFECT_SPECTRUM,
UNDERGLOW_EFFECT_SWIRL,
UNDERGLOW_EFFECT_NUMBER // Used to track number of underglow effects
};
struct led_hsb {
u16_t h;
u8_t s;
u8_t b;
};
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 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<STRIP_NUM_PIXELS; i++)
{
int hue = state.hue;
int sat = state.saturation;
int brt = state.brightness;
struct led_hsb hsb = { hue, sat, brt };
pixels[i] = hsb_to_rgb(hsb);
}
}
static void zmk_rgb_underglow_effect_breathe()
{
for (int i=0; i<STRIP_NUM_PIXELS; i++)
{
int hue = state.hue;
int sat = state.saturation;
int brt = abs(state.animation_step - 1200) / 12;
struct led_hsb hsb = { hue, sat, brt };
pixels[i] = hsb_to_rgb(hsb);
}
state.animation_step += state.animation_speed * 10;
if (state.animation_step > 2400) {
state.animation_step = 0;
}
}
static void zmk_rgb_underglow_effect_spectrum()
{
for (int i=0; i<STRIP_NUM_PIXELS; i++)
{
int hue = state.animation_step;
int sat = state.saturation;
int brt = state.brightness;
struct led_hsb hsb = { hue, sat, brt };
pixels[i] = hsb_to_rgb(hsb);
}
state.animation_step += state.animation_speed;
state.animation_step = state.animation_step % 360;
}
static void zmk_rgb_underglow_effect_swirl()
{
for (int i=0; i<STRIP_NUM_PIXELS; i++)
{
int hue = (360 / STRIP_NUM_PIXELS * i + state.animation_step) % 360;
int sat = state.saturation;
int brt = state.brightness;
struct led_hsb hsb = { hue, sat, brt };
pixels[i] = hsb_to_rgb(hsb);
}
state.animation_step += state.animation_speed * 2;
state.animation_step = state.animation_step % 360;
}
static void zmk_rgb_underglow_tick(struct k_work *work)
{
switch (state.current_effect)
{
case UNDERGLOW_EFFECT_SOLID:
zmk_rgb_underglow_effect_solid();
break;
case UNDERGLOW_EFFECT_BREATHE:
zmk_rgb_underglow_effect_breathe();
break;
case UNDERGLOW_EFFECT_SPECTRUM:
zmk_rgb_underglow_effect_spectrum();
break;
case UNDERGLOW_EFFECT_SWIRL:
zmk_rgb_underglow_effect_swirl();
break;
}
led_strip_update_rgb(led_strip, pixels, STRIP_NUM_PIXELS);
}
K_WORK_DEFINE(underglow_work, zmk_rgb_underglow_tick);
static void zmk_rgb_underglow_tick_handler(struct k_timer *timer)
{
k_work_submit(&underglow_work);
}
K_TIMER_DEFINE(underglow_tick, zmk_rgb_underglow_tick_handler, NULL);
static int zmk_rgb_underglow_init(struct device *_arg)
{
led_strip = device_get_binding(STRIP_LABEL);
if (led_strip) {
LOG_INF("Found LED strip device %s", STRIP_LABEL);
} else {
LOG_ERR("LED strip device %s not found", STRIP_LABEL);
return -EINVAL;
}
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;
}
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 0;
}
int zmk_rgb_underglow_toggle()
{
if (!led_strip) return -ENODEV;
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<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);
}
return 0;
}
int zmk_rgb_underglow_change_hue(int direction)
{
if (!led_strip) return -ENODEV;
if (state.hue == 0 && direction < 0) {
state.hue = 350;
return 0;
}
state.hue += direction * CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP;
if (state.hue > 350) {
state.hue = 0;
}
return 0;
}
int zmk_rgb_underglow_change_sat(int direction)
{
if (!led_strip) return -ENODEV;
if (state.saturation == 0 && direction < 0) {
return 0;
}
state.saturation += direction * CONFIG_ZMK_RGB_UNDERGLOW_SAT_STEP;
if (state.saturation > 100) {
state.saturation = 100;
}
return 0;
}
int zmk_rgb_underglow_change_brt(int direction)
{
if (!led_strip) return -ENODEV;
if (state.brightness == 0 && direction < 0) {
return 0;
}
state.brightness += direction * CONFIG_ZMK_RGB_UNDERGLOW_BRT_STEP;
if (state.brightness > 100) {
state.brightness = 100;
}
return 0;
}
int zmk_rgb_underglow_change_spd(int direction)
{
if (!led_strip) return -ENODEV;
if (state.animation_speed == 1 && direction < 0) {
return 0;
}
state.animation_speed += direction;
if (state.animation_speed > 5) {
state.animation_speed = 5;
}
return 0;
}
SYS_INIT(zmk_rgb_underglow_init,
APPLICATION,
CONFIG_APPLICATION_INIT_PRIORITY);
Loading…
Cancel
Save