2020-07-24 16:06:56 +10:00
|
|
|
/*
|
2020-09-10 13:57:44 +10:00
|
|
|
* Copyright (c) 2020 The ZMK Contributors
|
2020-07-24 16:06:56 +10:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <device.h>
|
|
|
|
#include <init.h>
|
2020-07-25 13:37:00 +10:00
|
|
|
#include <kernel.h>
|
2020-08-20 15:07:04 +10:00
|
|
|
#include <settings/settings.h>
|
2020-07-25 13:37:00 +10:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
2020-07-24 16:06:56 +10:00
|
|
|
|
|
|
|
#include <logging/log.h>
|
|
|
|
|
|
|
|
#include <drivers/led_strip.h>
|
|
|
|
|
|
|
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#define STRIP_LABEL DT_LABEL(DT_CHOSEN(zmk_underglow))
|
|
|
|
#define STRIP_NUM_PIXELS DT_PROP(DT_CHOSEN(zmk_underglow), chain_length)
|
2020-07-24 16:06:56 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
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
|
|
|
|
};
|
2020-07-24 16:06:56 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
struct led_hsb {
|
2020-09-14 12:53:24 +10:00
|
|
|
u16_t h;
|
|
|
|
u8_t s;
|
|
|
|
u8_t b;
|
2020-07-25 13:37:00 +10:00
|
|
|
};
|
2020-07-24 16:06:56 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
struct rgb_underglow_state {
|
|
|
|
u16_t hue;
|
2020-09-14 12:53:24 +10:00
|
|
|
u8_t saturation;
|
|
|
|
u8_t brightness;
|
|
|
|
u8_t animation_speed;
|
|
|
|
u8_t current_effect;
|
2020-07-25 13:37:00 +10:00
|
|
|
u16_t animation_step;
|
2020-09-14 12:53:24 +10:00
|
|
|
bool on;
|
2020-07-24 16:06:56 +10:00
|
|
|
};
|
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
struct device *led_strip;
|
|
|
|
|
2020-07-24 16:06:56 +10:00
|
|
|
struct led_rgb pixels[STRIP_NUM_PIXELS];
|
|
|
|
|
2020-08-20 15:07:04 +10:00
|
|
|
struct rgb_underglow_state state;
|
|
|
|
|
2020-08-24 06:56:18 +10:00
|
|
|
#if IS_ENABLED(CONFIG_SETTINGS)
|
2020-09-16 05:47:19 +10:00
|
|
|
static int rgb_settings_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg) {
|
2020-08-20 15:07:04 +10:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-16 05:47:19 +10:00
|
|
|
struct settings_handler rgb_conf = {.name = "rgb/underglow", .h_set = rgb_settings_set};
|
2020-08-24 06:56:18 +10:00
|
|
|
#endif
|
2020-08-20 15:07:04 +10:00
|
|
|
|
2020-09-16 05:47:19 +10:00
|
|
|
static struct led_rgb hsb_to_rgb(struct led_hsb hsb) {
|
2020-07-25 13:37:00 +10:00
|
|
|
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);
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
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;
|
2020-07-25 13:37:00 +10:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct led_rgb rgb = {r : r * 255, g : g * 255, b : b * 255};
|
2020-07-25 13:37:00 +10:00
|
|
|
|
|
|
|
return rgb;
|
|
|
|
}
|
|
|
|
|
2020-09-16 05:47:19 +10:00
|
|
|
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};
|
2020-08-24 01:33:19 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
led_strip_update_rgb(led_strip, pixels, STRIP_NUM_PIXELS);
|
|
|
|
}
|
|
|
|
|
2020-09-16 05:47:19 +10:00
|
|
|
static void zmk_rgb_underglow_effect_solid() {
|
|
|
|
for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
|
2020-07-25 13:37:00 +10:00
|
|
|
int hue = state.hue;
|
|
|
|
int sat = state.saturation;
|
|
|
|
int brt = state.brightness;
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct led_hsb hsb = {hue, sat, brt};
|
2020-07-25 13:37:00 +10:00
|
|
|
|
|
|
|
pixels[i] = hsb_to_rgb(hsb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
static void zmk_rgb_underglow_effect_breathe() {
|
|
|
|
for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
|
2020-07-25 13:37:00 +10:00
|
|
|
int hue = state.hue;
|
|
|
|
int sat = state.saturation;
|
|
|
|
int brt = abs(state.animation_step - 1200) / 12;
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct led_hsb hsb = {hue, sat, brt};
|
2020-07-25 13:37:00 +10:00
|
|
|
|
|
|
|
pixels[i] = hsb_to_rgb(hsb);
|
|
|
|
}
|
|
|
|
|
|
|
|
state.animation_step += state.animation_speed * 10;
|
2020-09-14 12:53:24 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
if (state.animation_step > 2400) {
|
|
|
|
state.animation_step = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
static void zmk_rgb_underglow_effect_spectrum() {
|
|
|
|
for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
|
2020-07-25 13:37:00 +10:00
|
|
|
int hue = state.animation_step;
|
|
|
|
int sat = state.saturation;
|
|
|
|
int brt = state.brightness;
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct led_hsb hsb = {hue, sat, brt};
|
2020-07-25 13:37:00 +10:00
|
|
|
|
|
|
|
pixels[i] = hsb_to_rgb(hsb);
|
|
|
|
}
|
|
|
|
|
|
|
|
state.animation_step += state.animation_speed;
|
|
|
|
state.animation_step = state.animation_step % 360;
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
static void zmk_rgb_underglow_effect_swirl() {
|
|
|
|
for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
|
2020-07-25 13:37:00 +10:00
|
|
|
int hue = (360 / STRIP_NUM_PIXELS * i + state.animation_step) % 360;
|
|
|
|
int sat = state.saturation;
|
|
|
|
int brt = state.brightness;
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct led_hsb hsb = {hue, sat, brt};
|
2020-07-25 13:37:00 +10:00
|
|
|
|
|
|
|
pixels[i] = hsb_to_rgb(hsb);
|
|
|
|
}
|
|
|
|
|
|
|
|
state.animation_step += state.animation_speed * 2;
|
|
|
|
state.animation_step = state.animation_step % 360;
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
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;
|
2020-07-25 13:37:00 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
led_strip_update_rgb(led_strip, pixels, STRIP_NUM_PIXELS);
|
|
|
|
}
|
|
|
|
|
|
|
|
K_WORK_DEFINE(underglow_work, zmk_rgb_underglow_tick);
|
|
|
|
|
2020-09-16 05:47:19 +10:00
|
|
|
static void zmk_rgb_underglow_tick_handler(struct k_timer *timer) {
|
|
|
|
if (!state.on) {
|
2020-08-24 01:33:19 +10:00
|
|
|
zmk_rgb_underglow_off();
|
|
|
|
|
|
|
|
k_timer_stop(timer);
|
2020-09-16 05:47:19 +10:00
|
|
|
|
2020-08-24 01:33:19 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
k_work_submit(&underglow_work);
|
|
|
|
}
|
2020-07-24 16:06:56 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
K_TIMER_DEFINE(underglow_tick, zmk_rgb_underglow_tick_handler, NULL);
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
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;
|
|
|
|
}
|
2020-07-24 16:06:56 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
state = (struct rgb_underglow_state){
|
2020-09-16 05:47:19 +10:00
|
|
|
hue : CONFIG_ZMK_RGB_UNDERGLOW_HUE_START,
|
|
|
|
saturation : CONFIG_ZMK_RGB_UNDERGLOW_SAT_START,
|
|
|
|
brightness : CONFIG_ZMK_RGB_UNDERGLOW_BRT_START,
|
|
|
|
animation_speed : CONFIG_ZMK_RGB_UNDERGLOW_SPD_START,
|
|
|
|
current_effect : CONFIG_ZMK_RGB_UNDERGLOW_EFF_START,
|
|
|
|
animation_step : 0,
|
|
|
|
on : IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_ON_START)
|
2020-07-25 13:37:00 +10:00
|
|
|
};
|
|
|
|
|
2020-08-24 06:56:18 +10:00
|
|
|
#if IS_ENABLED(CONFIG_SETTINGS)
|
2020-08-20 15:07:04 +10:00
|
|
|
settings_register(&rgb_conf);
|
2020-08-24 06:56:18 +10:00
|
|
|
#endif
|
2020-08-20 15:07:04 +10:00
|
|
|
|
2020-08-24 01:33:19 +10:00
|
|
|
k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50));
|
2020-07-25 13:37:00 +10:00
|
|
|
|
|
|
|
return 0;
|
2020-07-24 16:06:56 +10:00
|
|
|
}
|
|
|
|
|
2020-09-16 05:47:19 +10:00
|
|
|
int zmk_rgb_underglow_save_state() {
|
2020-08-24 06:56:18 +10:00
|
|
|
#if IS_ENABLED(CONFIG_SETTINGS)
|
2020-08-21 12:13:17 +10:00
|
|
|
return settings_save_one("rgb/underglow/state", &state, sizeof(state));
|
2020-08-24 06:56:18 +10:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2020-08-20 15:07:04 +10:00
|
|
|
}
|
|
|
|
|
2020-09-16 05:47:19 +10:00
|
|
|
int zmk_rgb_underglow_cycle_effect(int direction) {
|
|
|
|
if (!led_strip)
|
|
|
|
return -ENODEV;
|
2020-07-27 03:49:43 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
if (state.current_effect == 0 && direction < 0) {
|
|
|
|
state.current_effect = UNDERGLOW_EFFECT_NUMBER - 1;
|
2020-07-26 05:53:42 +10:00
|
|
|
return 0;
|
|
|
|
}
|
2020-07-24 16:06:56 +10:00
|
|
|
|
2020-07-26 05:53:42 +10:00
|
|
|
state.current_effect += direction;
|
|
|
|
|
|
|
|
if (state.current_effect >= UNDERGLOW_EFFECT_NUMBER) {
|
|
|
|
state.current_effect = 0;
|
2020-07-25 13:37:00 +10:00
|
|
|
}
|
2020-09-14 12:53:24 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
state.animation_step = 0;
|
2020-07-26 05:53:42 +10:00
|
|
|
|
2020-08-20 15:07:04 +10:00
|
|
|
return zmk_rgb_underglow_save_state();
|
2020-07-25 13:37:00 +10:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_rgb_underglow_toggle() {
|
|
|
|
if (!led_strip)
|
|
|
|
return -ENODEV;
|
2020-07-27 03:49:43 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
state.on = !state.on;
|
|
|
|
|
|
|
|
if (state.on) {
|
|
|
|
state.animation_step = 0;
|
|
|
|
k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50));
|
|
|
|
} else {
|
2020-08-24 01:33:19 +10:00
|
|
|
zmk_rgb_underglow_off();
|
2020-07-25 13:37:00 +10:00
|
|
|
|
|
|
|
k_timer_stop(&underglow_tick);
|
|
|
|
}
|
2020-07-26 05:53:42 +10:00
|
|
|
|
2020-08-20 15:07:04 +10:00
|
|
|
return zmk_rgb_underglow_save_state();
|
2020-07-25 13:37:00 +10:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_rgb_underglow_change_hue(int direction) {
|
|
|
|
if (!led_strip)
|
|
|
|
return -ENODEV;
|
2020-07-27 03:49:43 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
if (state.hue == 0 && direction < 0) {
|
2020-08-20 15:07:04 +10:00
|
|
|
state.hue = 360 - CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP;
|
2020-07-26 05:53:42 +10:00
|
|
|
return 0;
|
2020-07-25 13:37:00 +10:00
|
|
|
}
|
2020-09-14 12:53:24 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
state.hue += direction * CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP;
|
|
|
|
|
2020-08-20 15:07:04 +10:00
|
|
|
state.hue = state.hue % 360;
|
2020-07-26 05:53:42 +10:00
|
|
|
|
2020-08-20 15:07:04 +10:00
|
|
|
return zmk_rgb_underglow_save_state();
|
2020-07-25 13:37:00 +10:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_rgb_underglow_change_sat(int direction) {
|
|
|
|
if (!led_strip)
|
|
|
|
return -ENODEV;
|
2020-07-27 03:49:43 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
if (state.saturation == 0 && direction < 0) {
|
2020-07-26 05:53:42 +10:00
|
|
|
return 0;
|
2020-07-25 13:37:00 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
state.saturation += direction * CONFIG_ZMK_RGB_UNDERGLOW_SAT_STEP;
|
|
|
|
|
|
|
|
if (state.saturation > 100) {
|
|
|
|
state.saturation = 100;
|
|
|
|
}
|
2020-07-26 05:53:42 +10:00
|
|
|
|
2020-08-20 15:07:04 +10:00
|
|
|
return zmk_rgb_underglow_save_state();
|
2020-07-25 13:37:00 +10:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_rgb_underglow_change_brt(int direction) {
|
|
|
|
if (!led_strip)
|
|
|
|
return -ENODEV;
|
2020-07-27 03:49:43 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
if (state.brightness == 0 && direction < 0) {
|
2020-07-26 05:53:42 +10:00
|
|
|
return 0;
|
2020-07-25 13:37:00 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
state.brightness += direction * CONFIG_ZMK_RGB_UNDERGLOW_BRT_STEP;
|
|
|
|
|
|
|
|
if (state.brightness > 100) {
|
|
|
|
state.brightness = 100;
|
|
|
|
}
|
2020-07-26 05:53:42 +10:00
|
|
|
|
2020-08-20 15:07:04 +10:00
|
|
|
return zmk_rgb_underglow_save_state();
|
2020-07-25 13:37:00 +10:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_rgb_underglow_change_spd(int direction) {
|
|
|
|
if (!led_strip)
|
|
|
|
return -ENODEV;
|
2020-07-27 03:49:43 +10:00
|
|
|
|
2020-07-25 13:37:00 +10:00
|
|
|
if (state.animation_speed == 1 && direction < 0) {
|
2020-07-26 05:53:42 +10:00
|
|
|
return 0;
|
2020-07-25 13:37:00 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
state.animation_speed += direction;
|
|
|
|
|
|
|
|
if (state.animation_speed > 5) {
|
|
|
|
state.animation_speed = 5;
|
|
|
|
}
|
2020-07-26 05:53:42 +10:00
|
|
|
|
2020-08-20 15:07:04 +10:00
|
|
|
return zmk_rgb_underglow_save_state();
|
2020-07-24 16:06:56 +10:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
SYS_INIT(zmk_rgb_underglow_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
|