From e81f13fac7c528239f1e953eccc5167677f2386d Mon Sep 17 00:00:00 2001 From: tiltowait Date: Tue, 16 Jul 2019 23:20:06 -0700 Subject: [PATCH] Calculate idle time when set instead of every cycle Signed-off-by: tiltowait --- doc/plugin/IdleLEDs.md | 15 ++++++++++----- examples/LEDs/IdleLEDs/IdleLEDs.ino | 2 +- src/kaleidoscope/plugin/IdleLEDs.cpp | 12 ++++++++++-- src/kaleidoscope/plugin/IdleLEDs.h | 4 +++- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/doc/plugin/IdleLEDs.md b/doc/plugin/IdleLEDs.md index 48ebd797..4e35b14b 100644 --- a/doc/plugin/IdleLEDs.md +++ b/doc/plugin/IdleLEDs.md @@ -34,18 +34,23 @@ Because the plugin needs to know about key events, it is best to make it one of the first plugins, so it can catch all of them, before any other plugin would have a chance to consume key events. -## Plugin properties +## Plugin Methods The plugin provides a single object, `IdleLEDs`, with the following -properties. All times are in seconds. +methods. All times are in seconds. -### `idle_time_limit` +### `.idle_time_limit()` -> The amount of time that can pass without a single key being pressed, before -> the plugin considers the keyboard idle, and turns the LEDs off. +> The amount of time that can pass without a single key being pressed before +> the plugin considers the keyboard idle and turns off the LEDs. > > Defaults to 600 seconds (10 minutes). +### `.set_idle_time_limit(uint16_t new_limit)` + +> Sets the amount of time that can pass without a single key being pressed +> before the plugin considers the keyboard idle and turns off the LEDs. + ## Dependencies * [Kaleidoscope-LEDControl](LEDControl.md) diff --git a/examples/LEDs/IdleLEDs/IdleLEDs.ino b/examples/LEDs/IdleLEDs/IdleLEDs.ino index 704f499e..bd3cd2a6 100644 --- a/examples/LEDs/IdleLEDs/IdleLEDs.ino +++ b/examples/LEDs/IdleLEDs/IdleLEDs.ino @@ -50,7 +50,7 @@ KALEIDOSCOPE_INIT_PLUGINS(LEDControl, void setup() { Kaleidoscope.setup(); - IdleLEDs.idle_time_limit = 300; // 5 minutes + IdleLEDs.set_idle_time_limit(300); // 5 minutes LEDRainbowWaveEffect.activate(); } diff --git a/src/kaleidoscope/plugin/IdleLEDs.cpp b/src/kaleidoscope/plugin/IdleLEDs.cpp index 34f75dff..de6a5a18 100644 --- a/src/kaleidoscope/plugin/IdleLEDs.cpp +++ b/src/kaleidoscope/plugin/IdleLEDs.cpp @@ -21,12 +21,20 @@ namespace kaleidoscope { namespace plugin { -uint16_t IdleLEDs::idle_time_limit = 600; // 10 minutes +uint32_t IdleLEDs::idle_time_limit_ = 600000; // 10 minutes uint32_t IdleLEDs::start_time_ = 0; +uint16_t IdleLEDs::idle_time_limit() { + return uint16_t(idle_time_limit_ / 1000); +} + +void IdleLEDs::set_idle_time_limit(uint16_t new_limit) { + idle_time_limit_ = (uint32_t)new_limit * 1000; +} + EventHandlerResult IdleLEDs::beforeEachCycle() { if (!::LEDControl.paused && - Kaleidoscope.hasTimeExpired(start_time_, uint32_t(idle_time_limit * 1000))) { + Kaleidoscope.hasTimeExpired(start_time_, idle_time_limit_)) { ::LEDControl.set_all_leds_to(CRGB(0, 0, 0)); ::LEDControl.syncLeds(); diff --git a/src/kaleidoscope/plugin/IdleLEDs.h b/src/kaleidoscope/plugin/IdleLEDs.h index 801eafa9..8074a9ec 100644 --- a/src/kaleidoscope/plugin/IdleLEDs.h +++ b/src/kaleidoscope/plugin/IdleLEDs.h @@ -26,12 +26,14 @@ class IdleLEDs: public kaleidoscope::Plugin { public: IdleLEDs(void) {} - static uint16_t idle_time_limit; + static uint16_t idle_time_limit(); + static void set_idle_time_limit(uint16_t new_limit); EventHandlerResult beforeEachCycle(); EventHandlerResult onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state); private: + static uint32_t idle_time_limit_; static uint32_t start_time_; }; }