From 4850787b6594938caba46b515b25ea267def00fd Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 6 Oct 2017 11:27:15 +0200 Subject: [PATCH] Decrease the timer precision to seconds While we use milliseconds internally, the end-user will not need higher precision than a second, therefore, store the settings with second-precision. This is much friendlier towards the user, and also uses less space in EEPROM, by about six bytes. Addresses a part of #8, thanks @gedankenexperimenter! Signed-off-by: Gergely Nagy --- README.md | 8 ++++---- src/Kaleidoscope/TypingBreaks.cpp | 20 ++++++++++++-------- src/Kaleidoscope/TypingBreaks.h | 6 +++--- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 23b5f52c..0226534f 100644 --- a/README.md +++ b/README.md @@ -40,27 +40,27 @@ void setup (void) { ## Plugin methods The plugin provides a single object, `TypingBreaks`, with the following -properties. All times are in milliseconds. +properties. All times are in seconds. ### `.settings.idle_time_limit` > The amount of time that can pass between two pressed keys, before the plugin > considers it a new session, and starts all timers and counters over. > -> Defaults to 10000 (10 seconds). +> Defaults to 10 seconds. ### `.settings.lock_time_out` > The length of the session, after which the keyboard will be locked. > -> Defaults to 2700000 (45 minutes). +> Defaults to 2700 seconds (45 minutes). ### `.settings.lock_length` > The length until the keyboard lock is held. Any key pressed while the lock is > active, will be discarded. > -> Defaults to 300000 (5 minutes). +> Defaults to 300 seconds (5 minutes). ### `.settings.left_hand_max_keys` diff --git a/src/Kaleidoscope/TypingBreaks.cpp b/src/Kaleidoscope/TypingBreaks.cpp index f178c2b0..79fe88c6 100644 --- a/src/Kaleidoscope/TypingBreaks.cpp +++ b/src/Kaleidoscope/TypingBreaks.cpp @@ -23,9 +23,9 @@ namespace kaleidoscope { TypingBreaks::settings_t TypingBreaks::settings = { - .idle_time_limit = 10000, // 10s - .lock_time_out = 2700000, // 45m - .lock_length = 300000, // 5m + .idle_time_limit = 10, // 10s + .lock_time_out = 2700, // 45m + .lock_length = 300, // 5m .left_hand_max_keys = 0, .right_hand_max_keys = 0 }; @@ -45,14 +45,18 @@ void TypingBreaks::begin(void) { } Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { + uint32_t lock_length = settings.lock_length * 1000; + uint32_t idle_time_limit = settings.idle_time_limit * 1000; + uint32_t lock_time_out = settings.lock_time_out * 1000; + // If we are locked, and didn't time out yet, no key has to be pressed. - if (lock_start_time_ && (millis() - lock_start_time_ <= settings.lock_length)) + if (lock_start_time_ && (millis() - lock_start_time_ <= lock_length)) return Key_NoKey; // If we are locked... if (lock_start_time_) { // ...and the lock has not expired yet - if (millis() - lock_start_time_ <= settings.lock_length) + if (millis() - lock_start_time_ <= lock_length) return Key_NoKey; // remain locked // ...otherwise clear the lock @@ -66,7 +70,7 @@ Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t k // Any other case, we are not locked yet! (or we just unlocked) // Are we still in the same session? - if (last_key_time_ && (millis() - last_key_time_) >= settings.idle_time_limit) { + if (last_key_time_ && (millis() - last_key_time_) >= idle_time_limit) { // No, we are not. Clear timers and start over. lock_start_time_ = 0; left_hand_keys_ = right_hand_keys_ = 0; @@ -87,9 +91,9 @@ Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t k return Key_NoKey; } - if (settings.lock_time_out) { + if (lock_time_out) { // Is the session longer than lock_time_out? - if (millis() - session_start_time_ >= settings.lock_time_out) { + if (millis() - session_start_time_ >= lock_time_out) { // Yeah, it is. lock_start_time_ = millis(); TypingBreak(true); diff --git a/src/Kaleidoscope/TypingBreaks.h b/src/Kaleidoscope/TypingBreaks.h index e5ca838b..0f5ea278 100644 --- a/src/Kaleidoscope/TypingBreaks.h +++ b/src/Kaleidoscope/TypingBreaks.h @@ -32,9 +32,9 @@ class TypingBreaks : public KaleidoscopePlugin { static bool focusHook(const char *command); typedef struct settings_t { - uint32_t idle_time_limit; - uint32_t lock_time_out; - uint32_t lock_length; + uint16_t idle_time_limit; + uint16_t lock_time_out; + uint16_t lock_length; uint16_t left_hand_max_keys; uint16_t right_hand_max_keys; } settings_t;