Skip to content

Commit

Permalink
(feat/enhance) Tie RGB Underglow into sleep statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
KemoNine committed Dec 15, 2020
1 parent b5e17e3 commit 609476f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
2 changes: 2 additions & 0 deletions app/include/zmk/rgb_underglow.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#pragma once

int zmk_rgb_underglow_toggle();
int zmk_rgb_underglow_on();
int zmk_rgb_underglow_off();
int zmk_rgb_underglow_cycle_effect(int direction);
int zmk_rgb_underglow_change_hue(int direction);
int zmk_rgb_underglow_change_sat(int direction);
Expand Down
89 changes: 67 additions & 22 deletions app/src/rgb_underglow.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
#include <drivers/led_strip.h>
#include <drivers/ext_power.h>

#include <zmk/event-manager.h>
#include <zmk/events/activity-state-changed.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)

int zmk_rgb_underglow_off();
int zmk_rgb_underglow_on();

enum rgb_underglow_effect {
UNDERGLOW_EFFECT_SOLID,
UNDERGLOW_EFFECT_BREATHE,
Expand Down Expand Up @@ -105,14 +111,6 @@ static struct led_rgb hsb_to_rgb(struct led_hsb hsb) {
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() {
for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
int hue = state.hue;
Expand Down Expand Up @@ -276,6 +274,26 @@ static int zmk_rgb_underglow_init(const struct device *_arg) {
return 0;
}

int underglow_event_handler(const struct zmk_event_header *eh) {
struct activity_state_changed *ev = cast_activity_state_changed(eh);
switch (ev->state) {
case ZMK_ACTIVITY_ACTIVE:
zmk_rgb_underglow_on();
break;
case ZMK_ACTIVITY_IDLE:
case ZMK_ACTIVITY_SLEEP:
zmk_rgb_underglow_off();
break;
default:
LOG_WRN("Unhandled activity state: %d", ev->state);
return -EINVAL;
}
return 0;
}

ZMK_LISTENER(led_strip, underglow_event_handler);
ZMK_SUBSCRIPTION(led_strip, activity_state_changed);

int zmk_rgb_underglow_save_state() {
#if IS_ENABLED(CONFIG_SETTINGS)
k_delayed_work_cancel(&underglow_save_work);
Expand Down Expand Up @@ -309,32 +327,59 @@ int zmk_rgb_underglow_toggle() {
if (!led_strip)
return -ENODEV;

state.on = !state.on;
if (state.on) {
return zmk_rgb_underglow_off();
} else {
return zmk_rgb_underglow_on();
}

return -ENOENT;
}

int zmk_rgb_underglow_off() {
if (!led_strip)
return -ENODEV;

state.on = false;

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);

#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_EXT_POWER)
if (ext_power != NULL) {
int rc;

if (state.on) {
rc = ext_power_enable(ext_power);
} else {
rc = ext_power_disable(ext_power);
}
int rc = ext_power_disable(ext_power);

if (rc != 0) {
LOG_ERR("Unable to toggle EXT_POWER: %d", rc);
}
}
#endif

if (state.on) {
state.animation_step = 0;
k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50));
} else {
zmk_rgb_underglow_off();
k_timer_stop(&underglow_tick);

return zmk_rgb_underglow_save_state();
}

int zmk_rgb_underglow_on() {
if (!led_strip)
return -ENODEV;

k_timer_stop(&underglow_tick);
state.on = true;

#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_EXT_POWER)
if (ext_power != NULL) {
int rc = ext_power_enable(ext_power);
if (rc != 0) {
LOG_ERR("Unable to toggle EXT_POWER: %d", rc);
}
}
#endif

state.animation_step = 0;
k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50));

return zmk_rgb_underglow_save_state();
}
Expand Down

0 comments on commit 609476f

Please sign in to comment.