From 592c0945655cf02e63411fe0e349c0cc5128811f Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 13 May 2018 22:11:54 +0200 Subject: [PATCH] Updated to use the new plugin APIs Signed-off-by: Gergely Nagy --- README.md | 6 ++--- examples/TopsyTurvy/TopsyTurvy.ino | 8 +++--- src/Kaleidoscope/TopsyTurvy.cpp | 39 ++++++++++++++++++------------ src/Kaleidoscope/TopsyTurvy.h | 15 ++++++++---- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index dedaa01a..6df17635 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Kaleidoscope-TopsyTurvy -![status][st:stable] [![Build Status][travis:image]][travis:status] +![status][st:experimental] [![Build Status][travis:image]][travis:status] [travis:image]: https://travis-ci.org/keyboardio/Kaleidoscope-TopsyTurvy.svg?branch=master [travis:status]: https://travis-ci.org/keyboardio/Kaleidoscope-TopsyTurvy @@ -26,9 +26,9 @@ effects to, and use the plugin: // In the keymap: TOPSY(1), TOPSY(2), TOPSY(3) -void setup () { - Kaleidoscope.use (&TopsyTurvy); +KALEIDOSCOPE_INIT_PLUGINS(TopsyTurvy); +void setup () { Kaleidoscope.setup (); } ``` diff --git a/examples/TopsyTurvy/TopsyTurvy.ino b/examples/TopsyTurvy/TopsyTurvy.ino index ca2dfddf..c62886d8 100644 --- a/examples/TopsyTurvy/TopsyTurvy.ino +++ b/examples/TopsyTurvy/TopsyTurvy.ino @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-TopsyTurvy -- Turn the effect of Shift upside down for certain keys - * Copyright (C) 2017 Gergely Nagy + * Copyright (C) 2017, 2018 Gergely Nagy * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,6 +19,7 @@ #include #include +// *INDENT-OFF* const Key keymaps[][ROWS][COLS] PROGMEM = { [0] = KEYMAP_STACKED ( @@ -38,10 +39,11 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, Key_skip), }; +// *INDENT-ON* -void setup() { - Kaleidoscope.use(&TopsyTurvy); +KALEIDOSCOPE_INIT_PLUGINS(TopsyTurvy); +void setup() { Kaleidoscope.setup(); } diff --git a/src/Kaleidoscope/TopsyTurvy.cpp b/src/Kaleidoscope/TopsyTurvy.cpp index fb1db387..dad4976e 100644 --- a/src/Kaleidoscope/TopsyTurvy.cpp +++ b/src/Kaleidoscope/TopsyTurvy.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-TopsyTurvy -- Turn the effect of Shift upside down for certain keys - * Copyright (C) 2017 Gergely Nagy + * Copyright (C) 2017, 2018 Gergely Nagy * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,16 +26,9 @@ namespace kaleidoscope { uint8_t TopsyTurvy::mod_state_; uint8_t TopsyTurvy::last_pressed_position_; -TopsyTurvy::TopsyTurvy(void) { -} - -void TopsyTurvy::begin(void) { - Kaleidoscope.useEventHandlerHook(eventHandlerHook); -} - -Key TopsyTurvy::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { +EventHandlerResult TopsyTurvy::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) { if (key_state & TOPSYTURVY) - return mapped_key; + return EventHandlerResult::OK; if (mapped_key.raw == Key_LeftShift.raw) bitWrite(mod_state_, 0, keyIsPressed(key_state)); @@ -43,21 +36,22 @@ Key TopsyTurvy::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key bitWrite(mod_state_, 1, keyIsPressed(key_state)); if (!keyIsPressed(key_state) && !keyWasPressed(key_state)) - return mapped_key; + return EventHandlerResult::OK; if (mapped_key < ranges::TT_FIRST || mapped_key > ranges::TT_LAST) - return mapped_key; + return EventHandlerResult::OK; if (keyToggledOn(key_state)) { last_pressed_position_ = row * COLS + col; } else { - if (last_pressed_position_ != row * COLS + col) - return Key_NoKey; + if (last_pressed_position_ != row * COLS + col) { + return EventHandlerResult::EVENT_CONSUMED; + } } Key new_key = {.raw = mapped_key.raw - ranges::TT_FIRST}; if (new_key.raw == Key_NoKey.raw) - return mapped_key; + return EventHandlerResult::OK; // invert the shift state @@ -81,8 +75,23 @@ Key TopsyTurvy::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key hid::pressRawKey(Key_RightShift); } + return EventHandlerResult::EVENT_CONSUMED; +} + +// Legacy V1 API + +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API +void TopsyTurvy::begin() { + Kaleidoscope.useEventHandlerHook(legacyEventHandler); +} + +Key TopsyTurvy::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) { + EventHandlerResult r = ::TopsyTurvy.onKeyswitchEvent(mapped_key, row, col, key_state); + if (r == EventHandlerResult::OK) + return mapped_key; return Key_NoKey; } +#endif } diff --git a/src/Kaleidoscope/TopsyTurvy.h b/src/Kaleidoscope/TopsyTurvy.h index f97f665c..9402b6cc 100644 --- a/src/Kaleidoscope/TopsyTurvy.h +++ b/src/Kaleidoscope/TopsyTurvy.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-TopsyTurvy -- Turn the effect of Shift upside down for certain keys - * Copyright (C) 2017 Gergely Nagy + * Copyright (C) 2017, 2018 Gergely Nagy * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,14 +27,19 @@ namespace kaleidoscope { class TopsyTurvy: public KaleidoscopePlugin { public: - TopsyTurvy(void); + TopsyTurvy(void) {} + + EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state); + +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API + protected: + void begin(); + static Key legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state); +#endif - void begin(void) final; private: static uint8_t mod_state_; static uint8_t last_pressed_position_; - - static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state); }; }