Instead of using a list, augment the keymap directly.

This is a little bit user-friendlier, more efficient (both space- and
performance-wise).

Fixes #2.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
pull/389/head
Gergely Nagy 8 years ago
parent e5a8a77a4a
commit a3f7ab082d

@ -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 <Kaleidoscope.h>
#include <Kaleidoscope-TopsyTurvy.h>
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

@ -22,7 +22,7 @@
const Key keymaps[][ROWS][COLS] PROGMEM = {
[0] = KEYMAP_STACKED
(
Key_NoKey, Key_1, Key_2, Key_3, Key_4, Key_5, Key_NoKey,
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,
@ -30,7 +30,7 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
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_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,
@ -39,18 +39,10 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
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() {

@ -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))

@ -19,6 +19,9 @@
#pragma once
#include <Kaleidoscope.h>
#include <Kaleidoscope-Ranges.h>
#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_;

Loading…
Cancel
Save