From e81f13fac7c528239f1e953eccc5167677f2386d Mon Sep 17 00:00:00 2001 From: tiltowait Date: Tue, 16 Jul 2019 23:20:06 -0700 Subject: [PATCH 1/2] 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_; }; } From bb85ecaf43137e12c8f1e9bc4be44dd0b4bd1059 Mon Sep 17 00:00:00 2001 From: tiltowait Date: Mon, 22 Jul 2019 09:38:55 -0700 Subject: [PATCH 2/2] Keep property public and rename methods to conform to style guide. Signed-off-by: tiltowait --- doc/plugin/IdleLEDs.md | 32 ++++++++++++++++++---------- examples/LEDs/IdleLEDs/IdleLEDs.ino | 2 +- src/kaleidoscope/plugin/IdleLEDs.cpp | 14 ++++++------ src/kaleidoscope/plugin/IdleLEDs.h | 7 +++--- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/doc/plugin/IdleLEDs.md b/doc/plugin/IdleLEDs.md index 4e35b14b..3d71d9da 100644 --- a/doc/plugin/IdleLEDs.md +++ b/doc/plugin/IdleLEDs.md @@ -34,22 +34,32 @@ 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 Methods +## Plugin Properties -The plugin provides a single object, `IdleLEDs`, with the following -methods. All times are in seconds. +The plugin provides a single object, `IdleLEDs`, with the following properties +and methods. -### `.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 off the LEDs. -> -> Defaults to 600 seconds (10 minutes). +> Property storing 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. +> Value is expressed in milliseconds. -### `.set_idle_time_limit(uint16_t new_limit)` +> Defaults to 600000 milliseconds (10 minutes). -> 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. +> Provided for compatibility reasons. It is recommended to use one of the +> methods below instead of setting this property directly. + +### `.idleTimeoutSeconds()` + +> Returns the amount of time (in seconds) that can pass without a single key +> being pressed before the plugin considers the keyboard idle and turns off the +> LEDs. + +### `.setIdleTimeoutSeconds(uint32_t new_limit)` + +> Sets the amount of time (in seconds) that can pass without a single key being +> pressed before the plugin considers the keyboard idle and turns off the LEDs. ## Dependencies diff --git a/examples/LEDs/IdleLEDs/IdleLEDs.ino b/examples/LEDs/IdleLEDs/IdleLEDs.ino index bd3cd2a6..25b9791e 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.set_idle_time_limit(300); // 5 minutes + IdleLEDs.setIdleTimeoutSeconds(300); // 5 minutes LEDRainbowWaveEffect.activate(); } diff --git a/src/kaleidoscope/plugin/IdleLEDs.cpp b/src/kaleidoscope/plugin/IdleLEDs.cpp index de6a5a18..0489bd30 100644 --- a/src/kaleidoscope/plugin/IdleLEDs.cpp +++ b/src/kaleidoscope/plugin/IdleLEDs.cpp @@ -21,20 +21,20 @@ namespace kaleidoscope { namespace plugin { -uint32_t IdleLEDs::idle_time_limit_ = 600000; // 10 minutes -uint32_t IdleLEDs::start_time_ = 0; +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); +uint32_t IdleLEDs::idleTimeoutSeconds() { + return idle_time_limit / 1000; } -void IdleLEDs::set_idle_time_limit(uint16_t new_limit) { - idle_time_limit_ = (uint32_t)new_limit * 1000; +void IdleLEDs::setIdleTimeoutSeconds(uint32_t new_limit) { + idle_time_limit = new_limit * 1000; } EventHandlerResult IdleLEDs::beforeEachCycle() { if (!::LEDControl.paused && - Kaleidoscope.hasTimeExpired(start_time_, idle_time_limit_)) { + 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 8074a9ec..8722cf83 100644 --- a/src/kaleidoscope/plugin/IdleLEDs.h +++ b/src/kaleidoscope/plugin/IdleLEDs.h @@ -26,14 +26,15 @@ class IdleLEDs: public kaleidoscope::Plugin { public: IdleLEDs(void) {} - static uint16_t idle_time_limit(); - static void set_idle_time_limit(uint16_t new_limit); + static uint32_t idle_time_limit; + + static uint32_t idleTimeoutSeconds(); + static void setIdleTimeoutSeconds(uint32_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_; }; }