Skip to content

fix(underglow): turn on with USB power from boot when AUTO_OFF_USB is set - #3555

Open
akira-toriyama wants to merge 1 commit into
zmkfirmware:mainfrom
akira-toriyama:fix/underglow-auto-off-usb-boot
Open

akira-toriyama wants to merge 1 commit into
zmkfirmware:mainfrom
akira-toriyama:fix/underglow-auto-off-usb-boot

Conversation

@akira-toriyama

Copy link
Copy Markdown

With CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB=y the underglow never comes on by itself (zmk 4dd95d7, an nRF52840 split peripheral with a WS2812 strip, CONFIG_ZMK_RGB_UNDERGLOW_EXT_POWER at its default):

  • booted on USB: off
  • booted on battery, then plugged in: off
  • rebooted on USB: off

Two causes in app/src/rgb_underglow.c:

  • zmk_rgb_underglow_init() runs at CONFIG_APPLICATION_INIT_PRIORITY, before zmk_usb_init() (ZMK_USB_INIT_PRIORITY, 96), so zmk_usb_is_powered() reads "not powered" and the underglow starts off.
  • rgb_underglow_auto_state() starts with is_awake = true, so the first connection event, the only one a keyboard booted on USB gets, is taken for a wake while awake and ignored. Only a disconnect acts, and the off it saves is restored at the next boot.

The fix evaluates the USB rule once the state is known: from the settings commit handler, after zmk_usb_init() and the saved state (at the end of init without CONFIG_SETTINGS), with the sleep state set as after an automatic off, so that the first evaluation and every later USB event take the same path. The saved on/off is not restored with this option, since an automatic off writes it too.

With the change, on the same hardware: on when booted on USB, on when plugged in after a battery boot, on when rebooted on USB, off when the cable is pulled. Nothing of this is reachable from the posix tests (no USB), so no test is added.

PR check-list

  • Branch has a clean commit history
  • Additional tests are included, if changing behaviors/core code that is testable. (USB state is not testable under native_sim)
  • Proper Copyright + License headers added to applicable files (no new files)
  • Pre-commit used to check formatting of files, commit messages, etc. (clang-format 18.1.8, gitlint)
  • Includes any necessary documentation changes. (none needed)

🤖 Generated with Claude Code

… set

With CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB the underglow never came on by
itself. zmk_rgb_underglow_init() read zmk_usb_is_powered() before
zmk_usb_init() had run (CONFIG_APPLICATION_INIT_PRIORITY against
ZMK_USB_INIT_PRIORITY, 96), so it read "not powered" and stayed off, and
rgb_underglow_auto_state() started with is_awake = true, so the first
connection event, the only one a keyboard booted on USB gets, counted as
a wake while awake and did nothing. Only a disconnect acted, and the off
it saved was restored at the next boot.

The underglow now follows USB power from boot. The first evaluation runs
from the settings commit handler, after zmk_usb_init() and the saved
state, with the sleep state set as after an automatic off, so that it
and every later USB event take the same path; without CONFIG_SETTINGS it
runs at the end of init and the connection event turns the underglow
on. The saved on/off is not restored with this option, since an
automatic off writes it too.

On an nRF52840 split peripheral with a WS2812 strip (zmk 4dd95d7), the
underglow stayed off when booted on USB, when booted on battery and then
plugged in, and when rebooted on USB; with this change it is on in all
three cases and goes off when the cable is pulled.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@akira-toriyama
akira-toriyama requested a review from a team as a code owner November 5, 2026 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant