Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(underglow): Add RGB auto off timeout on idle and on usb disconnect #1010

Merged
merged 15 commits into from
Jun 25, 2022
8 changes: 8 additions & 0 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ config ZMK_RGB_UNDERGLOW_ON_START
bool "RGB underglow starts on by default"
default y

config ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE
bool "Turn off RGB underglow when keyboard goes into idle state"
default y
ReFil marked this conversation as resolved.
Show resolved Hide resolved

config ZMK_RGB_UNDERGLOW_AUTO_OFF_USB
bool "Turn off RGB underglow when USB is disconnected"
depends on USB
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
depends on USB
depends on USB_DEVICE_STACK

Needed w/ the Zephyr 3.0 update.

Copy link
Contributor Author

@ReFil ReFil Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One code comment, and then a general item:

When testing this, the initial auto state isn't set for USB. If I've enabled the auto off USB setting, and I start the firmware from reset w/o USB connected, the RGB starts on, instead of off.

Need to update the auto stuff at the end of the init callback, I imagine.

Given itll always be active when the leds are initialised i think this is only relevant to usb, i'll add in that functionality

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked and the backlighting auto off does the same thing, is it better to mirror the slightly worse behaviour so both sets of lights are in sync or fix the rgb now and then i can submit another pr to do the same for the backlighting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rectified both issues


#ZMK_RGB_UNDERGLOW
endif

Expand Down
55 changes: 55 additions & 0 deletions app/src/rgb_underglow.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@

#include <zmk/rgb_underglow.h>

#include <zmk/activity.h>
#include <zmk/usb.h>
#include <zmk/event_manager.h>
#include <zmk/events/activity_state_changed.h>
#include <zmk/events/usb_conn_state_changed.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#define STRIP_LABEL DT_LABEL(DT_CHOSEN(zmk_underglow))
Expand Down Expand Up @@ -434,4 +440,53 @@ int zmk_rgb_underglow_change_spd(int direction) {
return zmk_rgb_underglow_save_state();
}

#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) {
return 0;
}
if (new_state) {
state.on = *prev_state;
*prev_state = false;
return zmk_rgb_underglow_on();
} else {
state.on = false;
*prev_state = true;
return zmk_rgb_underglow_off();
}
}

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

return -ENOTSUP;
}

ZMK_LISTENER(rgb_underglow, rgb_underglow_event_listener);
#endif // IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) ||
// IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB)

#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE)
ZMK_SUBSCRIPTION(rgb_underglow, zmk_activity_state_changed);
#endif

#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB)
ZMK_SUBSCRIPTION(rgb_underglow, zmk_usb_conn_state_changed);
#endif

SYS_INIT(zmk_rgb_underglow_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);