Skip to content

Commit

Permalink
fix(rgb): auto-off logic
Browse files Browse the repository at this point in the history
  • Loading branch information
elpekenin authored Apr 25, 2024
1 parent f4a070a commit 4d56685
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions app/src/rgb_underglow.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,31 @@ int zmk_rgb_underglow_change_spd(int direction) {

#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) || \
IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB)
static int rgb_underglow_auto_state(bool *prev_state, bool new_state) {
if (state.on == new_state) {
struct rgb_underglow_sleep_state {
bool is_awake;
bool rgb_state_before_sleeping;
};

static int rgb_underglow_auto_state(bool target_wake_state) {
static struct rgb_underglow_sleep_state sleep_state = {
is_awake : true,
rgb_state_before_sleeping : false
};

// wake up event while awake, or sleep event while sleeping -> no-op
if (target_wake_state == sleep_state.is_awake) {
return 0;
}
if (new_state) {
state.on = *prev_state;
*prev_state = false;
return zmk_rgb_underglow_on();
sleep_state.is_awake = target_wake_state;

if (sleep_state.is_awake) {
if (sleep_state.rgb_state_before_sleeping) {
return zmk_rgb_underglow_on();
} else {
return zmk_rgb_underglow_off();
}
} else {
state.on = false;
*prev_state = true;
sleep_state.rgb_state_before_sleeping = sleep_state.on;
return zmk_rgb_underglow_off();
}
}
Expand All @@ -487,16 +501,13 @@ static int rgb_underglow_event_listener(const zmk_event_t *eh) {

#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE)
if (as_zmk_activity_state_changed(eh)) {
static bool prev_state = false;
return rgb_underglow_auto_state(&prev_state,
zmk_activity_get_state() == ZMK_ACTIVITY_ACTIVE);
return rgb_underglow_auto_state(zmk_activity_get_state() == ZMK_ACTIVITY_ACTIVE);
}
#endif

#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB)
if (as_zmk_usb_conn_state_changed(eh)) {
static bool prev_state = false;
return rgb_underglow_auto_state(&prev_state, zmk_usb_is_powered());
return rgb_underglow_auto_state(zmk_usb_is_powered());
}
#endif

Expand Down

0 comments on commit 4d56685

Please sign in to comment.