diff --git a/README.md b/README.md index f95bdcad..f6bcb9aa 100644 --- a/README.md +++ b/README.md @@ -16,39 +16,41 @@ original `1` symbol. ## Using the plugin -To use the plugin, one needs to include the header, create a list, and configure -the provided `TopsyTurvy` object to use the dictionary: +To use the plugin, one needs to include the header, mark keys to apply plugin +effects to, and use the plugin: ```c++ #include #include -static const Key topsy_turvy_list[] PROGMEM = { - Key_1, Key_2, Key_3, Key_4, Key_5, - Key_6, Key_7, Key_8, Key_9, Key_0, - Key_NoKey -}; +// In the keymap: +TOPSY(1), TOPSY(2), TOPSY(3) void setup () { USE_PLUGINS (&TopsyTurvy); Kaleidoscope.setup (); - - TopsyTurvy.key_list = topsy_turvy_list; } ``` -The list of keys **must** be terminated with a `Key_NoKey`, and **must** reside -in `PROGMEM`. +## Keymap markup -## Plugin methods +There is only one macro that the plugin provides, which one can use in keymap definitions: + +### `TOPSY(key)` -The plugin provides the `TopsyTurvy` object, with the following property: +> Mark the specified `key` (without the `Key_` prefix!) for TopsyTurvy, and swap +> the effect of `Shift` when the key is used. One can have any number of +> topsy-turvy keys on a keymap. +> +> The keys must be plain old keys, modifiers or anything other augmentation +> cannot be applied. -### `.key_list` +The plugin provides a number of macros one can use in keymap definitions: + +## Plugin methods -> Set this property to the list of `Key`s `TopsyTurvy` should invert the `Shift` -> behaviour for. +The plugin provides the `TopsyTurvy` object, without any public methods or properties. ## Further reading diff --git a/examples/TopsyTurvy/TopsyTurvy.ino b/examples/TopsyTurvy/TopsyTurvy.ino index 4828d57e..6e2c5d09 100644 --- a/examples/TopsyTurvy/TopsyTurvy.ino +++ b/examples/TopsyTurvy/TopsyTurvy.ino @@ -22,35 +22,27 @@ 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, + Key_NoKey, TOPSY(1), TOPSY(2), TOPSY(3), TOPSY(4), TOPSY(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, Key_skip, - 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, TOPSY(6), TOPSY(7), TOPSY(8), TOPSY(9), TOPSY(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, Key_skip), }; -static const Key topsy_turvy_list[] PROGMEM = { - Key_1, Key_2, Key_3, Key_4, Key_5, - Key_6, Key_7, Key_8, Key_9, Key_0, - Key_NoKey -}; - void setup() { USE_PLUGINS(&TopsyTurvy); Kaleidoscope.setup(); - - TopsyTurvy.key_list = topsy_turvy_list; } void loop() { diff --git a/src/Kaleidoscope/TopsyTurvy.cpp b/src/Kaleidoscope/TopsyTurvy.cpp index 54ddfdab..73295939 100644 --- a/src/Kaleidoscope/TopsyTurvy.cpp +++ b/src/Kaleidoscope/TopsyTurvy.cpp @@ -22,7 +22,6 @@ namespace kaleidoscope { -const Key *TopsyTurvy::key_list = NULL; uint8_t TopsyTurvy::mod_state_; TopsyTurvy::TopsyTurvy(void) { @@ -36,9 +35,6 @@ Key TopsyTurvy::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key if (key_state & TOPSYTURVY) return mapped_key; - if (!key_list) - return mapped_key; - if (mapped_key.raw == Key_LeftShift.raw) bitWrite(mod_state_, 0, key_is_pressed(key_state)); if (mapped_key.raw == Key_RightShift.raw) @@ -47,15 +43,11 @@ Key TopsyTurvy::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key if (!key_is_pressed(key_state) && !key_was_pressed(key_state)) return mapped_key; - uint8_t idx = 0; - Key newKey; - - do { - newKey.raw = pgm_read_word(&(key_list[idx].raw)); - idx++; - } while (newKey.raw != mapped_key.raw && newKey.raw != Key_NoKey.raw); + if (mapped_key < ranges::TT_FIRST || mapped_key > ranges::TT_LAST) + return mapped_key; - if (newKey.raw == Key_NoKey.raw) + Key new_key = {.raw = mapped_key.raw - ranges::TT_FIRST}; + if (new_key.raw == Key_NoKey.raw) return mapped_key; // invert the shift state @@ -63,7 +55,7 @@ Key TopsyTurvy::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key if (!mod_state_) { if (key_is_pressed(key_state)) Keyboard.press(Key_LeftShift.keyCode); - handle_keyswitch_event(mapped_key, row, col, key_state | TOPSYTURVY); + handle_keyswitch_event(new_key, row, col, key_state | TOPSYTURVY | INJECTED); Keyboard.sendReport(); if (key_toggled_off(key_state)) Keyboard.release(Key_LeftShift.keyCode); @@ -71,7 +63,7 @@ Key TopsyTurvy::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key Keyboard.release(Key_LeftShift.keyCode); Keyboard.release(Key_RightShift.keyCode); Keyboard.sendReport(); - handle_keyswitch_event(mapped_key, row, col, key_state | TOPSYTURVY); + handle_keyswitch_event(new_key, row, col, key_state | TOPSYTURVY | INJECTED); Keyboard.sendReport(); if (bitRead(mod_state_, 0)) diff --git a/src/Kaleidoscope/TopsyTurvy.h b/src/Kaleidoscope/TopsyTurvy.h index 92db2288..be085c44 100644 --- a/src/Kaleidoscope/TopsyTurvy.h +++ b/src/Kaleidoscope/TopsyTurvy.h @@ -19,6 +19,9 @@ #pragma once #include +#include + +#define TOPSY(k) (Key) { .raw = kaleidoscope::ranges::TT_FIRST + (Key_ ## k).keyCode } namespace kaleidoscope { @@ -27,8 +30,6 @@ class TopsyTurvy: public KaleidoscopePlugin { TopsyTurvy(void); void begin(void) final; - - static const Key *key_list; private: static uint8_t mod_state_;