diff --git a/README.md b/README.md index ce486743..c8e50aec 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,9 @@ the box, without any further configuration: #include void setup (void) { - Kaleidoscope.setup (); USE_PLUGINS (&TypingBreaks); + + Kaleidoscope.setup (); } ``` @@ -41,27 +42,27 @@ void setup (void) { The plugin provides a single object, `TypingBreaks`, with the following properties. All times are in milliseconds. -### `.settings.idleTimeLimit` +### `.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). -### `.settings.lockTimeOut` +### `.settings.lock_time_out` > The length of the session, after which the keyboard will be locked. > > Defaults to 2700000 (45 minutes). -### `.settings.lockLength` +### `.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 (15 minutes). -### `.settings.leftHandMaxKeys` +### `.settings.left_hand_max_keys` > It is possible to lock the keyboard after a number of keys pressed, too. If > this happens sooner than the timeout, the keyboard will still be locked. @@ -70,7 +71,7 @@ properties. All times are in milliseconds. > > Defaults to 0 (off). -### `.settings.rightHandMaxKeys` +### `.settings.right_hand_max_keys` > It is possible to lock the keyboard after a number of keys pressed, too. If > this happens sooner than the timeout, the keyboard will still be locked. diff --git a/examples/TypingBreaks/TypingBreaks.ino b/examples/TypingBreaks/TypingBreaks.ino index 5e5a8c80..5ac15515 100644 --- a/examples/TypingBreaks/TypingBreaks.ino +++ b/examples/TypingBreaks/TypingBreaks.ino @@ -40,8 +40,9 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { }; void setup() { - Kaleidoscope.setup(); USE_PLUGINS(&TypingBreaks); + + Kaleidoscope.setup(); } void loop() { diff --git a/src/Kaleidoscope/TypingBreaks.cpp b/src/Kaleidoscope/TypingBreaks.cpp index cc4c1a07..41a7e74b 100644 --- a/src/Kaleidoscope/TypingBreaks.cpp +++ b/src/Kaleidoscope/TypingBreaks.cpp @@ -20,46 +20,45 @@ #include #include -namespace KaleidoscopePlugins { +namespace kaleidoscope { + TypingBreaks::settings_t TypingBreaks::settings = { - .idleTimeLimit = 10000, // 10s - .lockTimeOut = 2700000, // 45m - .lockLength = 300000, // 5m - .leftHandMaxKeys = 0, - .rightHandMaxKeys = 0 + .idle_time_limit = 10000, // 10s + .lock_time_out = 2700000, // 45m + .lock_length = 300000, // 5m + .left_hand_max_keys = 0, + .right_hand_max_keys = 0 }; -uint32_t TypingBreaks::sessionStartTime; -uint32_t TypingBreaks::lastKeyTime; -uint32_t TypingBreaks::lockStartTime; -uint16_t TypingBreaks::leftHandKeys; -uint16_t TypingBreaks::rightHandKeys; -uint16_t TypingBreaks::settingsBase; +uint32_t TypingBreaks::session_start_time_; +uint32_t TypingBreaks::last_key_time_; +uint32_t TypingBreaks::lock_start_time_; +uint16_t TypingBreaks::left_hand_keys_; +uint16_t TypingBreaks::right_hand_keys_; +uint16_t TypingBreaks::settings_base_; TypingBreaks::TypingBreaks(void) { } -void -TypingBreaks::begin(void) { +void TypingBreaks::begin(void) { event_handler_hook_use(this->eventHandlerHook); } -Key -TypingBreaks::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState) { +Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { // If we are locked, and didn't time out yet, no key has to be pressed. - if (lockStartTime && (millis() - lockStartTime <= settings.lockLength)) + if (lock_start_time_ && (millis() - lock_start_time_ <= settings.lock_length)) return Key_NoKey; // If we are locked... - if (lockStartTime) { + if (lock_start_time_) { // ...and the lock has not expired yet - if (millis() - lockStartTime <= settings.lockLength) + if (millis() - lock_start_time_ <= settings.lock_length) return Key_NoKey; // remain locked // ...otherwise clear the lock - lockStartTime = 0; - leftHandKeys = rightHandKeys = 0; - sessionStartTime = millis(); + lock_start_time_ = 0; + left_hand_keys_ = right_hand_keys_ = 0; + session_start_time_ = millis(); TypingBreak(false); } @@ -67,32 +66,32 @@ TypingBreaks::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keySta // Any other case, we are not locked yet! (or we just unlocked) // Are we still in the same session? - if (lastKeyTime && (millis() - lastKeyTime) >= settings.idleTimeLimit) { + if (last_key_time_ && (millis() - last_key_time_) >= settings.idle_time_limit) { // No, we are not. Clear timers and start over. - lockStartTime = 0; - leftHandKeys = rightHandKeys = 0; - sessionStartTime = millis(); + lock_start_time_ = 0; + left_hand_keys_ = right_hand_keys_ = 0; + session_start_time_ = millis(); } // If we have a limit on the left hand, and we reached it, lock up! - if (settings.leftHandMaxKeys && leftHandKeys >= settings.leftHandMaxKeys) { - lockStartTime = millis(); + if (settings.left_hand_max_keys && left_hand_keys_ >= settings.left_hand_max_keys) { + lock_start_time_ = millis(); TypingBreak(true); return Key_NoKey; } // If we have a limit on the right hand, and we reached it, lock up! - if (settings.rightHandMaxKeys && rightHandKeys >= settings.rightHandMaxKeys) { - lockStartTime = millis(); + if (settings.right_hand_max_keys && right_hand_keys_ >= settings.right_hand_max_keys) { + lock_start_time_ = millis(); TypingBreak(true); return Key_NoKey; } - if (settings.lockTimeOut) { - // Is the session longer than lockTimeOut? - if (millis() - sessionStartTime >= settings.lockTimeOut) { + if (settings.lock_time_out) { + // Is the session longer than lock_time_out? + if (millis() - session_start_time_ >= settings.lock_time_out) { // Yeah, it is. - lockStartTime = millis(); + lock_start_time_ = millis(); TypingBreak(true); return Key_NoKey; } @@ -101,34 +100,32 @@ TypingBreaks::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keySta // So it seems we did not need to lock up. In this case, lets increase key // counters if need be. - if (key_toggled_on(keyState)) { + if (key_toggled_on(key_state)) { if (col <= COLS / 2) - leftHandKeys++; + left_hand_keys_++; else - rightHandKeys++; - lastKeyTime = millis(); + right_hand_keys_++; + last_key_time_ = millis(); } - return mappedKey; + return mapped_key; } -void -TypingBreaks::enableEEPROM(void) { - settingsBase = ::EEPROMSettings.requestSlice(sizeof(settings)); +void TypingBreaks::enableEEPROM(void) { + settings_base_ = ::EEPROMSettings.requestSlice(sizeof(settings)); // If idleTime is max, assume that EEPROM is uninitialized, and store the // defaults. - uint32_t idleTime; - EEPROM.get(settingsBase, idleTime); - if (idleTime == 0xffffffff) { - EEPROM.put(settingsBase, settings); + uint32_t idle_time; + EEPROM.get(settings_base_, idle_time); + if (idle_time == 0xffffffff) { + EEPROM.put(settings_base_, settings); } - EEPROM.get(settingsBase, settings); + EEPROM.get(settings_base_, settings); } -bool -TypingBreaks::focusHook(const char *command) { +bool TypingBreaks::focusHook(const char *command) { enum { IDLE_TIME_LIMIT, LOCK_TIMEOUT, @@ -155,49 +152,47 @@ TypingBreaks::focusHook(const char *command) { switch (subCommand) { case IDLE_TIME_LIMIT: if (Serial.peek() == '\n') { - Serial.println(settings.idleTimeLimit); + Serial.println(settings.idle_time_limit); } else { - settings.idleTimeLimit = Serial.parseInt(); + settings.idle_time_limit = Serial.parseInt(); } break; case LOCK_TIMEOUT: if (Serial.peek() == '\n') { - Serial.println(settings.lockTimeOut); + Serial.println(settings.lock_time_out); } else { - settings.lockTimeOut = Serial.parseInt(); + settings.lock_time_out = Serial.parseInt(); } break; case LOCK_LENGTH: if (Serial.peek() == '\n') { - Serial.println(settings.lockLength); + Serial.println(settings.lock_length); } else { - settings.lockLength = Serial.parseInt(); + settings.lock_length = Serial.parseInt(); } break; case LEFT_MAX: if (Serial.peek() == '\n') { - Serial.println(settings.leftHandMaxKeys); + Serial.println(settings.left_hand_max_keys); } else { - settings.leftHandMaxKeys = Serial.parseInt(); + settings.left_hand_max_keys = Serial.parseInt(); } break; case RIGHT_MAX: if (Serial.peek() == '\n') { - Serial.println(settings.rightHandMaxKeys); + Serial.println(settings.right_hand_max_keys); } else { - settings.rightHandMaxKeys = Serial.parseInt(); + settings.right_hand_max_keys = Serial.parseInt(); } break; } - EEPROM.put(settingsBase, settings); + EEPROM.put(settings_base_, settings); return true; } } -KaleidoscopePlugins::TypingBreaks TypingBreaks; +kaleidoscope::TypingBreaks TypingBreaks; -__attribute__((weak)) -void -TypingBreak(bool isLocked) { +__attribute__((weak)) void TypingBreak(bool isLocked) { } diff --git a/src/Kaleidoscope/TypingBreaks.h b/src/Kaleidoscope/TypingBreaks.h index f4a0782d..e5ca838b 100644 --- a/src/Kaleidoscope/TypingBreaks.h +++ b/src/Kaleidoscope/TypingBreaks.h @@ -20,7 +20,8 @@ #include -namespace KaleidoscopePlugins { +namespace kaleidoscope { + class TypingBreaks : public KaleidoscopePlugin { public: TypingBreaks(void); @@ -31,31 +32,32 @@ class TypingBreaks : public KaleidoscopePlugin { static bool focusHook(const char *command); typedef struct settings_t { - uint32_t idleTimeLimit; - uint32_t lockTimeOut; - uint32_t lockLength; - uint16_t leftHandMaxKeys; - uint16_t rightHandMaxKeys; + uint32_t idle_time_limit; + uint32_t lock_time_out; + uint32_t lock_length; + uint16_t left_hand_max_keys; + uint16_t right_hand_max_keys; } settings_t; static settings_t settings; private: - static uint32_t sessionStartTime; - static uint32_t lockStartTime; - static uint32_t lastKeyTime; - static uint16_t leftHandKeys; - static uint16_t rightHandKeys; + static uint32_t session_start_time_; + static uint32_t lock_start_time_; + static uint32_t last_key_time_; + static uint16_t left_hand_keys_; + static uint16_t right_hand_keys_; - static uint16_t settingsBase; + static uint16_t settings_base_; - static Key eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState); + static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state); }; + } -extern KaleidoscopePlugins::TypingBreaks TypingBreaks; +extern kaleidoscope::TypingBreaks TypingBreaks; -void TypingBreak(bool isLocked); +void TypingBreak(bool is_locked); #define FOCUS_HOOK_TYPINGBREAKS FOCUS_HOOK(TypingBreaks.focusHook, \ "typingbreaks.idleTimeLimit\n" \