diff --git a/examples/Leader/Leader.ino b/examples/Leader/Leader.ino index c83c21d9..8ba224de 100644 --- a/examples/Leader/Leader.ino +++ b/examples/Leader/Leader.ino @@ -20,49 +20,49 @@ #include const Key keymaps[][ROWS][COLS] PROGMEM = { - [0] = KEYMAP_STACKED - ( - Key_NoKey, Key_1, Key_2, Key_3, Key_4, Key_5, Key_NoKey, - Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab, - Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G, - Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape, + [0] = KEYMAP_STACKED + ( + Key_NoKey, Key_1, Key_2, Key_3, Key_4, Key_5, Key_NoKey, + Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab, + Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G, + Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape, - Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, - LEAD(0), + Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, + LEAD(0), - Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip, - Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals, - Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, - Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, + Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip, + Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals, + Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, + Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, - Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, - LEAD(0) - ), + Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, + LEAD(0) + ), }; -static void leaderTestA (uint8_t seqIndex) { - Serial.println (F("leaderTestA")); +static void leaderTestA(uint8_t seqIndex) { + Serial.println(F("leaderTestA")); } -static void leaderTestAA (uint8_t seqIndex) { - Serial.println (F("leaderTestAA")); +static void leaderTestAA(uint8_t seqIndex) { + Serial.println(F("leaderTestAA")); } static const KaleidoscopePlugins::Leader::dictionary_t leaderDictionary[] PROGMEM = LEADER_DICT - ( + ( {LEADER_SEQ(LEAD(0), Key_A), leaderTestA}, {LEADER_SEQ(LEAD(0), Key_A, Key_A), leaderTestAA} - ); + ); -void setup () { - Serial.begin (9600); +void setup() { + Serial.begin(9600); - Leader.configure (leaderDictionary); + Leader.configure(leaderDictionary); - Kaleidoscope.setup (KEYMAP_SIZE); - Kaleidoscope.use (&Leader, NULL); + Kaleidoscope.setup(KEYMAP_SIZE); + Kaleidoscope.use(&Leader, NULL); } -void loop () { - Kaleidoscope.loop (); +void loop() { + Kaleidoscope.loop(); } diff --git a/src/Kaleidoscope/Leader.cpp b/src/Kaleidoscope/Leader.cpp index 7ff9f629..9e213859 100644 --- a/src/Kaleidoscope/Leader.cpp +++ b/src/Kaleidoscope/Leader.cpp @@ -38,139 +38,139 @@ const Leader::dictionary_t *Leader::dictionary; // --- actions --- int8_t -Leader::lookup (void) { - bool match; - - for (uint8_t seqIndex = 0; ; seqIndex++) { - match = true; - - if (pgm_read_word (&(dictionary[seqIndex].sequence[0].raw)) == Key_NoKey.raw) - break; - - Key seqKey; - for (uint8_t i = 0; i <= sequencePos; i++) { - seqKey.raw = pgm_read_word (&(dictionary[seqIndex].sequence[i].raw)); - - if (sequence[i].raw != seqKey.raw) { - match = false; - break; - } - } - - if (!match) - continue; - - seqKey.raw = pgm_read_word (&(dictionary[seqIndex].sequence[sequencePos + 1].raw)); - if (seqKey.raw == Key_NoKey.raw) { - return seqIndex; - } else { - return PARTIAL_MATCH; - } +Leader::lookup(void) { + bool match; + + for (uint8_t seqIndex = 0; ; seqIndex++) { + match = true; + + if (pgm_read_word(&(dictionary[seqIndex].sequence[0].raw)) == Key_NoKey.raw) + break; + + Key seqKey; + for (uint8_t i = 0; i <= sequencePos; i++) { + seqKey.raw = pgm_read_word(&(dictionary[seqIndex].sequence[i].raw)); + + if (sequence[i].raw != seqKey.raw) { + match = false; + break; + } + } + + if (!match) + continue; + + seqKey.raw = pgm_read_word(&(dictionary[seqIndex].sequence[sequencePos + 1].raw)); + if (seqKey.raw == Key_NoKey.raw) { + return seqIndex; + } else { + return PARTIAL_MATCH; } + } - return NO_MATCH; + return NO_MATCH; } // --- api --- -Leader::Leader (void) { +Leader::Leader(void) { } void -Leader::begin (void) { - event_handler_hook_use (this->eventHandlerHook); - loop_hook_use (this->loopHook); +Leader::begin(void) { + event_handler_hook_use(this->eventHandlerHook); + loop_hook_use(this->loopHook); } void -Leader::configure (const Leader::dictionary_t dictionary_[]) { - dictionary = dictionary_; +Leader::configure(const Leader::dictionary_t dictionary_[]) { + dictionary = dictionary_; } void -Leader::reset (void) { - sequencePos = 0; - sequence[0].raw = Key_NoKey.raw; +Leader::reset(void) { + sequencePos = 0; + sequence[0].raw = Key_NoKey.raw; } void -Leader::inject (Key key, uint8_t keyState) { - eventHandlerHook (key, 255, 255, keyState); +Leader::inject(Key key, uint8_t keyState) { + eventHandlerHook(key, 255, 255, keyState); } // --- hooks --- Key -Leader::eventHandlerHook (Key mappedKey, byte row, byte col, uint8_t keyState) { - if (keyState & INJECTED) - return mappedKey; - - if (!key_is_pressed (keyState) && !key_was_pressed (keyState)) { - if (isLeader (mappedKey)) - return Key_NoKey; - return mappedKey; +Leader::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState) { + if (keyState & INJECTED) + return mappedKey; + + if (!key_is_pressed(keyState) && !key_was_pressed(keyState)) { + if (isLeader(mappedKey)) + return Key_NoKey; + return mappedKey; + } + + if (!isActive() && !isLeader(mappedKey)) + return mappedKey; + + if (!isActive()) { + // Must be a leader key! + + if (key_toggled_off(keyState)) { + // not active, but a leader key = start the sequence on key release! + endTime = millis() + timeOut; + sequencePos = 0; + sequence[sequencePos].raw = mappedKey.raw; } - if (!isActive () && !isLeader (mappedKey)) - return mappedKey; - - if (!isActive ()) { - // Must be a leader key! + // If the sequence was not active yet, ignore the key. + return Key_NoKey; + } - if (key_toggled_off (keyState)) { - // not active, but a leader key = start the sequence on key release! - endTime = millis () + timeOut; - sequencePos = 0; - sequence[sequencePos].raw = mappedKey.raw; - } + // active + int8_t actionIndex = lookup(); - // If the sequence was not active yet, ignore the key. - return Key_NoKey; + if (key_toggled_on(keyState)) { + sequencePos++; + if (sequencePos > LEADER_MAX_SEQUENCE_LENGTH) { + reset(); + return mappedKey; } - // active - int8_t actionIndex = lookup (); - - if (key_toggled_on (keyState)) { - sequencePos++; - if (sequencePos > LEADER_MAX_SEQUENCE_LENGTH) { - reset (); - return mappedKey; - } - - endTime = millis () + timeOut; - sequence[sequencePos].raw = mappedKey.raw; - actionIndex = lookup (); - - if (actionIndex >= 0) - return Key_NoKey; - } else if (key_is_pressed (keyState)) { - // held, no need for anything here. - return Key_NoKey; - } + endTime = millis() + timeOut; + sequence[sequencePos].raw = mappedKey.raw; + actionIndex = lookup(); - if (actionIndex == NO_MATCH) { - reset (); - return mappedKey; - } - if (actionIndex == PARTIAL_MATCH) { - return Key_NoKey; - } + if (actionIndex >= 0) + return Key_NoKey; + } else if (key_is_pressed(keyState)) { + // held, no need for anything here. + return Key_NoKey; + } - action_t leaderAction = (action_t) pgm_read_ptr (&(dictionary[actionIndex].action)); - (*leaderAction) (actionIndex); + if (actionIndex == NO_MATCH) { + reset(); + return mappedKey; + } + if (actionIndex == PARTIAL_MATCH) { return Key_NoKey; + } + + action_t leaderAction = (action_t) pgm_read_ptr(&(dictionary[actionIndex].action)); + (*leaderAction)(actionIndex); + return Key_NoKey; } void -Leader::loopHook (bool postClear) { - if (!postClear) - return; +Leader::loopHook(bool postClear) { + if (!postClear) + return; - if (!isActive ()) - return; + if (!isActive()) + return; - if (millis () >= endTime) - reset (); + if (millis() >= endTime) + reset(); } }; diff --git a/src/Kaleidoscope/Leader.h b/src/Kaleidoscope/Leader.h index d402c9b0..d8b0a8fa 100644 --- a/src/Kaleidoscope/Leader.h +++ b/src/Kaleidoscope/Leader.h @@ -30,32 +30,32 @@ namespace KaleidoscopePlugins { class Leader : public KaleidoscopePlugin { - public: - typedef void (*action_t) (uint8_t seqIndex); - typedef struct { - Key sequence[LEADER_MAX_SEQUENCE_LENGTH + 1]; - action_t action; - } dictionary_t; + public: + typedef void (*action_t)(uint8_t seqIndex); + typedef struct { + Key sequence[LEADER_MAX_SEQUENCE_LENGTH + 1]; + action_t action; + } dictionary_t; - Leader (void); + Leader(void); - virtual void begin (void) final; + virtual void begin(void) final; - static void configure (const dictionary_t dictionary[]); - static void reset (void); - static uint16_t timeOut; + static void configure(const dictionary_t dictionary[]); + static void reset(void); + static uint16_t timeOut; - void inject (Key key, uint8_t keyState); + void inject(Key key, uint8_t keyState); - private: - static Key sequence[LEADER_MAX_SEQUENCE_LENGTH + 1]; - static uint8_t sequencePos; - static uint32_t endTime; - static const Leader::dictionary_t *dictionary; + private: + static Key sequence[LEADER_MAX_SEQUENCE_LENGTH + 1]; + static uint8_t sequencePos; + static uint32_t endTime; + static const Leader::dictionary_t *dictionary; - static Key eventHandlerHook (Key mappedKey, byte row, byte col, uint8_t keyState); - static void loopHook (bool postClear); - static int8_t lookup (void); + static Key eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState); + static void loopHook(bool postClear); + static int8_t lookup(void); }; };