diff --git a/plugins/Kaleidoscope-IdleLEDs/README.md b/plugins/Kaleidoscope-IdleLEDs/README.md index 853c6b3805..2c54e04ac1 100644 --- a/plugins/Kaleidoscope-IdleLEDs/README.md +++ b/plugins/Kaleidoscope-IdleLEDs/README.md @@ -91,6 +91,16 @@ the following properties and methods. > Setting the timeout to 0 will disable the plugin until it is set to a higher > value. +### `.suspend()`, `.resume()` + +> Suspends or resumes `IdleLEDs`. When suspended, `IdleLEDs` will immediately +> enter idle state, and turn LEDs off. When resuming, it will turn the LEDs back +> on, and start checking idleness again. +> +> The intended use of these functions is when LEDs are turned on or off by other +> means, such as part of host power management, and syncing that state with +> `IdleLEDs` is desired. + ## Focus commands The plugin provides a single [Focus][FocusSerial] command, but only when using diff --git a/plugins/Kaleidoscope-IdleLEDs/src/kaleidoscope/plugin/IdleLEDs.cpp b/plugins/Kaleidoscope-IdleLEDs/src/kaleidoscope/plugin/IdleLEDs.cpp index 59d3964232..d921c5091c 100644 --- a/plugins/Kaleidoscope-IdleLEDs/src/kaleidoscope/plugin/IdleLEDs.cpp +++ b/plugins/Kaleidoscope-IdleLEDs/src/kaleidoscope/plugin/IdleLEDs.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Idle-LEDs -- Turn off the LEDs when the keyboard's idle - * Copyright (C) 2018, 2019, 2020, 2021 Keyboard.io, Inc + * Copyright (C) 2018, 2019, 2020, 2021, 2022 Keyboard.io, Inc * Copyright (C) 2019 Dygma, Inc * * This program is free software: you can redistribute it and/or modify it under @@ -44,26 +44,38 @@ void IdleLEDs::setIdleTimeoutSeconds(uint32_t new_limit) { idle_time_limit = new_limit * 1000; } +void IdleLEDs::resume() { + // Enabling LEDs is fairly expensive, so we only do it if we have to. + if (!::LEDControl.isEnabled()) { + ::LEDControl.enable(); + } + idle_ = false; + start_time_ = Runtime.millisAtCycleStart(); +} + +void IdleLEDs::suspend() { + // Disabling LEDs is fairly expensive, so we only do it if we have to. + if (::LEDControl.isEnabled()) { + ::LEDControl.disable(); + } + + idle_ = true; +} + EventHandlerResult IdleLEDs::beforeEachCycle() { if (idle_time_limit == 0) return EventHandlerResult::OK; - if (::LEDControl.isEnabled() && + if (!idle_ && Runtime.hasTimeExpired(start_time_, idle_time_limit)) { - ::LEDControl.disable(); - idle_ = true; + suspend(); } return EventHandlerResult::OK; } EventHandlerResult IdleLEDs::onKeyEvent(KeyEvent &event) { - if (idle_) { - ::LEDControl.enable(); - idle_ = false; - } - - start_time_ = Runtime.millisAtCycleStart(); + resume(); return EventHandlerResult::OK; } diff --git a/plugins/Kaleidoscope-IdleLEDs/src/kaleidoscope/plugin/IdleLEDs.h b/plugins/Kaleidoscope-IdleLEDs/src/kaleidoscope/plugin/IdleLEDs.h index e2a85e60a8..711be9526c 100644 --- a/plugins/Kaleidoscope-IdleLEDs/src/kaleidoscope/plugin/IdleLEDs.h +++ b/plugins/Kaleidoscope-IdleLEDs/src/kaleidoscope/plugin/IdleLEDs.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Idle-LEDs -- Turn off the LEDs when the keyboard's idle - * Copyright (C) 2018, 2019, 2021 Keyboard.io, Inc + * Copyright (C) 2018, 2019, 2021, 2022 Keyboard.io, Inc * Copyright (C) 2019 Dygma, Inc * * This program is free software: you can redistribute it and/or modify it under @@ -33,6 +33,8 @@ class IdleLEDs : public kaleidoscope::Plugin { static uint32_t idleTimeoutSeconds(); static void setIdleTimeoutSeconds(uint32_t new_limit); + static void suspend(); + static void resume(); EventHandlerResult beforeEachCycle(); EventHandlerResult onKeyEvent(KeyEvent &event);