Merge pull request #8 from keyboardio/f/plugin-v2

Updated to use the new plugin APIs
pull/389/head
Gergely Nagy 7 years ago committed by GitHub
commit 48063dbd5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,6 @@
# Kaleidoscope-TopsyTurvy # 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:image]: https://travis-ci.org/keyboardio/Kaleidoscope-TopsyTurvy.svg?branch=master
[travis:status]: https://travis-ci.org/keyboardio/Kaleidoscope-TopsyTurvy [travis:status]: https://travis-ci.org/keyboardio/Kaleidoscope-TopsyTurvy
@ -26,9 +26,9 @@ effects to, and use the plugin:
// In the keymap: // In the keymap:
TOPSY(1), TOPSY(2), TOPSY(3) TOPSY(1), TOPSY(2), TOPSY(3)
void setup () { KALEIDOSCOPE_INIT_PLUGINS(TopsyTurvy);
Kaleidoscope.use (&TopsyTurvy);
void setup () {
Kaleidoscope.setup (); Kaleidoscope.setup ();
} }
``` ```

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-TopsyTurvy -- Turn the effect of Shift upside down for certain keys * 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 * 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 * it under the terms of the GNU General Public License as published by
@ -19,6 +19,7 @@
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-TopsyTurvy.h> #include <Kaleidoscope-TopsyTurvy.h>
// *INDENT-OFF*
const Key keymaps[][ROWS][COLS] PROGMEM = { const Key keymaps[][ROWS][COLS] PROGMEM = {
[0] = KEYMAP_STACKED [0] = KEYMAP_STACKED
( (
@ -38,10 +39,11 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl,
Key_skip), Key_skip),
}; };
// *INDENT-ON*
void setup() { KALEIDOSCOPE_INIT_PLUGINS(TopsyTurvy);
Kaleidoscope.use(&TopsyTurvy);
void setup() {
Kaleidoscope.setup(); Kaleidoscope.setup();
} }

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-TopsyTurvy -- Turn the effect of Shift upside down for certain keys * 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 * 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 * 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::mod_state_;
uint8_t TopsyTurvy::last_pressed_position_; uint8_t TopsyTurvy::last_pressed_position_;
TopsyTurvy::TopsyTurvy(void) { EventHandlerResult TopsyTurvy::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) {
}
void TopsyTurvy::begin(void) {
Kaleidoscope.useEventHandlerHook(eventHandlerHook);
}
Key TopsyTurvy::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
if (key_state & TOPSYTURVY) if (key_state & TOPSYTURVY)
return mapped_key; return EventHandlerResult::OK;
if (mapped_key.raw == Key_LeftShift.raw) if (mapped_key.raw == Key_LeftShift.raw)
bitWrite(mod_state_, 0, keyIsPressed(key_state)); 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)); bitWrite(mod_state_, 1, keyIsPressed(key_state));
if (!keyIsPressed(key_state) && !keyWasPressed(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) if (mapped_key < ranges::TT_FIRST || mapped_key > ranges::TT_LAST)
return mapped_key; return EventHandlerResult::OK;
if (keyToggledOn(key_state)) { if (keyToggledOn(key_state)) {
last_pressed_position_ = row * COLS + col; last_pressed_position_ = row * COLS + col;
} else { } else {
if (last_pressed_position_ != row * COLS + col) if (last_pressed_position_ != row * COLS + col) {
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
}
} }
Key new_key = {.raw = mapped_key.raw - ranges::TT_FIRST}; Key new_key = {.raw = mapped_key.raw - ranges::TT_FIRST};
if (new_key.raw == Key_NoKey.raw) if (new_key.raw == Key_NoKey.raw)
return mapped_key; return EventHandlerResult::OK;
// invert the shift state // 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); 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; return Key_NoKey;
} }
#endif
} }

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-TopsyTurvy -- Turn the effect of Shift upside down for certain keys * 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 * 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 * it under the terms of the GNU General Public License as published by
@ -27,14 +27,19 @@ namespace kaleidoscope {
class TopsyTurvy: public KaleidoscopePlugin { class TopsyTurvy: public KaleidoscopePlugin {
public: 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: private:
static uint8_t mod_state_; static uint8_t mod_state_;
static uint8_t last_pressed_position_; static uint8_t last_pressed_position_;
static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state);
}; };
} }

Loading…
Cancel
Save