Skip to content

Commit

Permalink
Refactor rgb_underglow_toggle to be dedicted on/off methods ; tie und…
Browse files Browse the repository at this point in the history
…erglow into sleep activity and turn on/off based on sleep events
  • Loading branch information
KemoNine committed Dec 15, 2020
1 parent b5e17e3 commit 382ffdd
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 30 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
105 changes: 75 additions & 30 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 All @@ -285,57 +303,84 @@ int zmk_rgb_underglow_save_state() {
#endif
}

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

if (state.current_effect == 0 && direction < 0) {
state.current_effect = UNDERGLOW_EFFECT_NUMBER - 1;
return 0;
state.on = false;

for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
pixels[i] = (struct led_rgb){r : 0, g : 0, b : 0};
}

state.current_effect += direction;
led_strip_update_rgb(led_strip, pixels, STRIP_NUM_PIXELS);

if (state.current_effect >= UNDERGLOW_EFFECT_NUMBER) {
state.current_effect = 0;
#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_EXT_POWER)
if (ext_power != NULL) {
int rc = ext_power_disable(ext_power);

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

state.animation_step = 0;
k_timer_stop(&underglow_tick);

return zmk_rgb_underglow_save_state();
}

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

state.on = !state.on;
state.on = true;

#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_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();
}

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

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

k_timer_stop(&underglow_tick);
return -ENOENT;
}

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

Expand Down

0 comments on commit 382ffdd

Please sign in to comment.