From 1feb474a6296de745c9e4f0091b250cd99882464 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 31 Mar 2017 12:03:51 +0200 Subject: [PATCH] EEPROM & Focus support This makes it possible to persist the settings in EEPROM too, and allow setting them via Focus. Fixes #2. Signed-off-by: Gergely Nagy --- src/Kaleidoscope/TypingBreaks.cpp | 96 ++++++++++++++++++++++++++++++- src/Kaleidoscope/TypingBreaks.h | 28 ++++++--- 2 files changed, 116 insertions(+), 8 deletions(-) diff --git a/src/Kaleidoscope/TypingBreaks.cpp b/src/Kaleidoscope/TypingBreaks.cpp index cc1baccd..83543cef 100644 --- a/src/Kaleidoscope/TypingBreaks.cpp +++ b/src/Kaleidoscope/TypingBreaks.cpp @@ -17,20 +17,32 @@ */ #include +#include +#include namespace KaleidoscopePlugins { - +#if 0 uint32_t TypingBreaks::settings::idleTimeLimit = 10000; // 10s uint32_t TypingBreaks::settings::lockTimeOut = 2700000; // 45m uint32_t TypingBreaks::settings::lockLength = 300000; // 5m uint16_t TypingBreaks::settings::leftHandMaxKeys; uint16_t TypingBreaks::settings::rightHandMaxKeys; +#else + TypingBreaks::settings_t TypingBreaks::settings = { + .idleTimeLimit = 10000, + .lockTimeOut = 2700000, + .lockLength = 300000, + .leftHandMaxKeys = 0, + .rightHandMaxKeys = 0 + }; +#endif uint32_t TypingBreaks::sessionStartTime; uint32_t TypingBreaks::lastKeyTime; uint32_t TypingBreaks::lockStartTime; uint16_t TypingBreaks::leftHandKeys; uint16_t TypingBreaks::rightHandKeys; + uint16_t TypingBreaks::settingsBase; TypingBreaks::TypingBreaks (void) { } @@ -102,6 +114,88 @@ namespace KaleidoscopePlugins { return mappedKey; } + + void + TypingBreaks::enableEEPROM (void) { + settingsBase = ::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); + } + + EEPROM.get (settingsBase, settings); + } + + bool + TypingBreaks::focusHook (const char *command) { + enum { + IDLE_TIME_LIMIT, + LOCK_TIMEOUT, + LOCK_LENGTH, + LEFT_MAX, + RIGHT_MAX, + } subCommand; + + if (strncmp_P (command, PSTR ("typingbreaks."), 13) != 0) + return false; + if (strcmp_P (command + 13, PSTR ("idleTimeLimit")) == 0) + subCommand = IDLE_TIME_LIMIT; + else if (strcmp_P (command + 13, PSTR ("lockTimeOut")) == 0) + subCommand = LOCK_TIMEOUT; + else if (strcmp_P (command + 13, PSTR ("lockLength")) == 0) + subCommand = LOCK_LENGTH; + else if (strcmp_P (command + 13, PSTR ("leftMaxKeys")) == 0) + subCommand = LEFT_MAX; + else if (strcmp_P (command + 13, PSTR ("rightMaxKeys")) == 0) + subCommand = RIGHT_MAX; + else + return false; + + switch (subCommand) { + case IDLE_TIME_LIMIT: + if (Serial.peek () == '\n') { + Serial.println (settings.idleTimeLimit); + } else { + settings.idleTimeLimit = Serial.parseInt (); + } + break; + case LOCK_TIMEOUT: + if (Serial.peek () == '\n') { + Serial.println (settings.lockTimeOut); + } else { + settings.lockTimeOut = Serial.parseInt (); + } + break; + case LOCK_LENGTH: + if (Serial.peek () == '\n') { + Serial.println (settings.lockLength); + } else { + settings.lockLength = Serial.parseInt (); + } + break; + case LEFT_MAX: + if (Serial.peek () == '\n') { + Serial.println (settings.leftHandMaxKeys); + } else { + settings.leftHandMaxKeys = Serial.parseInt (); + } + break; + case RIGHT_MAX: + if (Serial.peek () == '\n') { + Serial.println (settings.rightHandMaxKeys); + } else { + settings.rightHandMaxKeys = Serial.parseInt (); + } + break; + } + + EEPROM.put (settingsBase, settings); + return true; + } }; KaleidoscopePlugins::TypingBreaks TypingBreaks; diff --git a/src/Kaleidoscope/TypingBreaks.h b/src/Kaleidoscope/TypingBreaks.h index 0d6670f2..9ef8a858 100644 --- a/src/Kaleidoscope/TypingBreaks.h +++ b/src/Kaleidoscope/TypingBreaks.h @@ -27,13 +27,18 @@ namespace KaleidoscopePlugins { virtual void begin (void) final; - static struct settings { - static uint32_t idleTimeLimit; - static uint32_t lockTimeOut; - static uint32_t lockLength; - static uint16_t leftHandMaxKeys; - static uint16_t rightHandMaxKeys; - } settings; + static void enableEEPROM (void); + 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; + } settings_t; + + static settings_t settings; private: static uint32_t sessionStartTime; @@ -42,8 +47,17 @@ namespace KaleidoscopePlugins { static uint16_t leftHandKeys; static uint16_t rightHandKeys; + static uint16_t settingsBase; + static Key eventHandlerHook (Key mappedKey, byte row, byte col, uint8_t keyState); }; }; extern KaleidoscopePlugins::TypingBreaks TypingBreaks; + +#define FOCUS_HOOK_TYPINGBREAKS FOCUS_HOOK(TypingBreaks.focusHook, \ + "typingbreaks.idleTimeLimit\n" \ + "typingbreaks.lockTimeOut\n" \ + "typingbreaks.lockLength\n" \ + "typingbreaks.leftMaxKeys\n" \ + "typingbreaks.rightMaxKeys")