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

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

@ -45,9 +45,9 @@ enabling the plugin:
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-SpaceCadet.h> #include <Kaleidoscope-SpaceCadet.h>
void setup() { KALEIDOSCOPE_INIT_PLUGINS(SpaceCadet);
Kaleidoscope.use(&SpaceCadet);
void setup() {
Kaleidoscope.setup(); Kaleidoscope.setup();
} }
``` ```
@ -65,8 +65,10 @@ passing a new keymap into the SpaceCadet object, as shown below:
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-SpaceCadet.h> #include <Kaleidoscope-SpaceCadet.h>
KALEIDOSCOPE_INIT_PLUGINS(SpaceCadet);
void setup() { void setup() {
Kaleidoscope.use(&SpaceCadet); Kaleidoscope.setup();
//Set the keymap with a 250ms timeout per-key //Set the keymap with a 250ms timeout per-key
//Setting is {KeyThatWasPressed, AlternativeKeyToSend, TimeoutInMS} //Setting is {KeyThatWasPressed, AlternativeKeyToSend, TimeoutInMS}
@ -83,8 +85,6 @@ void setup() {
}; };
//Set the map. //Set the map.
SpaceCadet.map = spacecadetmap; SpaceCadet.map = spacecadetmap;
Kaleidoscope.setup();
} }
``` ```

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-SpaceCadet -- Space Cadet Shift * Kaleidoscope-SpaceCadet -- Space Cadet Shift
* Copyright (C) 2016, 2017 Gergely Nagy * Copyright (C) 2016, 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-SpaceCadet.h> #include <Kaleidoscope-SpaceCadet.h>
// *INDENT-OFF*
const Key keymaps[][ROWS][COLS] PROGMEM = { const Key keymaps[][ROWS][COLS] PROGMEM = {
[0] = KEYMAP_STACKED [0] = KEYMAP_STACKED
( (
@ -30,18 +31,20 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift,
Key_skip, Key_skip,
Key_SpaceCadetDisable, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip, Key_SpaceCadetDisable, 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_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_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_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus,
Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl,
Key_skip), Key_skip),
}; };
// *INDENT-ON*
KALEIDOSCOPE_INIT_PLUGINS(SpaceCadet);
void setup() { void setup() {
//Tell Kaleidoscope to use SpaceCadet Kaleidoscope.setup();
Kaleidoscope.use(&SpaceCadet);
//Set the SpaceCadet map //Set the SpaceCadet map
//Setting is {KeyThatWasPressed, AlternativeKeyToSend, TimeoutInMS} //Setting is {KeyThatWasPressed, AlternativeKeyToSend, TimeoutInMS}
@ -58,8 +61,6 @@ void setup() {
}; };
//Set the map. //Set the map.
SpaceCadet.map = spacecadetmap; SpaceCadet.map = spacecadetmap;
Kaleidoscope.setup();
} }
void loop() { void loop() {

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-SpaceCadet -- Space Cadet Shift Extended * Kaleidoscope-SpaceCadet -- Space Cadet Shift Extended
* Copyright (C) 2016, 2017 Gergely Nagy, Ben Gemperline * Copyright (C) 2016, 2017, 2018 Gergely Nagy, Ben Gemperline
* *
* 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
@ -39,7 +39,6 @@ SpaceCadet::KeyBinding * SpaceCadet::map;
uint16_t SpaceCadet::time_out = 1000; uint16_t SpaceCadet::time_out = 1000;
bool SpaceCadet::disabled = false; bool SpaceCadet::disabled = false;
//Empty Constructor
SpaceCadet::SpaceCadet() { SpaceCadet::SpaceCadet() {
static SpaceCadet::KeyBinding initialmap[] = { static SpaceCadet::KeyBinding initialmap[] = {
//By default, respect the default timeout //By default, respect the default timeout
@ -71,11 +70,7 @@ bool SpaceCadet::active() {
return !disabled; return !disabled;
} }
void SpaceCadet::begin() { EventHandlerResult SpaceCadet::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) {
Kaleidoscope.useEventHandlerHook(eventHandlerHook);
}
Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
//Handle our synthetic keys for enabling and disabling functionality //Handle our synthetic keys for enabling and disabling functionality
if (mapped_key.raw >= kaleidoscope::ranges::SC_FIRST && if (mapped_key.raw >= kaleidoscope::ranges::SC_FIRST &&
mapped_key.raw <= kaleidoscope::ranges::SC_LAST) { mapped_key.raw <= kaleidoscope::ranges::SC_LAST) {
@ -88,8 +83,7 @@ Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key
} }
} }
//in any case, return NoKey (these don't do anything else) return EventHandlerResult::EVENT_CONSUMED;
return Key_NoKey;
} }
//if SpaceCadet is disabled, this was an injected key, it was NoKey, //if SpaceCadet is disabled, this was an injected key, it was NoKey,
@ -101,7 +95,7 @@ Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key
|| mapped_key == Key_NoKey || mapped_key == Key_NoKey
|| (!keyIsPressed(key_state) && !keyWasPressed(key_state)) || (!keyIsPressed(key_state) && !keyWasPressed(key_state))
) { ) {
return mapped_key; return EventHandlerResult::OK;
} }
// If a key has been just toggled on... // If a key has been just toggled on...
@ -152,13 +146,13 @@ Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key
//anything until we know that we're either sending the alternate key or we //anything until we know that we're either sending the alternate key or we
//know for sure that we want to send the originally pressed key. //know for sure that we want to send the originally pressed key.
if (valid_key) { if (valid_key) {
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
//this is all we need to do on keypress, let the next handler do its thing too. //this is all we need to do on keypress, let the next handler do its thing too.
//This case assumes we weren't a valid key that we were watching, so we don't //This case assumes we weren't a valid key that we were watching, so we don't
//need to do anything else. //need to do anything else.
return mapped_key; return EventHandlerResult::OK;
} }
// if the state is empty, that means that either an activating key wasn't pressed, // if the state is empty, that means that either an activating key wasn't pressed,
@ -194,7 +188,7 @@ Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key
//If no valid mapped keys were pressed, simply return the key that //If no valid mapped keys were pressed, simply return the key that
//was originally passed in. //was originally passed in.
if (!valid_key) { if (!valid_key) {
return mapped_key; return EventHandlerResult::OK;
} }
//use the map index to find the local timeout for this key //use the map index to find the local timeout for this key
@ -212,14 +206,14 @@ Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key
map[index].start_time = 0; map[index].start_time = 0;
//Just return this key itself (we won't run alternative keys check) //Just return this key itself (we won't run alternative keys check)
return mapped_key; return EventHandlerResult::OK;
} }
// If the key that was pressed isn't one of our mapped keys, just // If the key that was pressed isn't one of our mapped keys, just
// return. This can happen when another key is released, and that should not // return. This can happen when another key is released, and that should not
// interrupt us. // interrupt us.
if (!pressed_key_was_valid) { if (!pressed_key_was_valid) {
return mapped_key; return EventHandlerResult::OK;
} }
// if a key toggled off (and that must be one of the mapped keys at this point), // if a key toggled off (and that must be one of the mapped keys at this point),
@ -243,12 +237,26 @@ Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key
//This prevents us from accidentally triggering a keypress that we didn't //This prevents us from accidentally triggering a keypress that we didn't
//mean to handle. //mean to handle.
if (valid_key) { if (valid_key) {
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
//Finally, as a final sanity check, simply return the passed-in key as-is. //Finally, as a final sanity check, simply return the passed-in key as-is.
return mapped_key; return EventHandlerResult::OK;
}
// Legacy V1 API
#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
void SpaceCadet::begin() {
Kaleidoscope.useEventHandlerHook(legacyEventHandler);
}
Key SpaceCadet::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) {
EventHandlerResult r = ::SpaceCadet.onKeyswitchEvent(mapped_key, row, col, key_state);
if (r == EventHandlerResult::OK)
return mapped_key;
return Key_NoKey;
} }
#endif
} }

@ -29,8 +29,8 @@
#define Key_SpaceCadetDisable (Key) { .raw = kaleidoscope::ranges::SC_LAST } #define Key_SpaceCadetDisable (Key) { .raw = kaleidoscope::ranges::SC_LAST }
namespace kaleidoscope { namespace kaleidoscope {
//Declaration for the method (implementing KaleidoscopePlugin)
class SpaceCadet : public KaleidoscopePlugin { class SpaceCadet : public kaleidoscope::Plugin {
public: public:
//Internal Class //Internal Class
//Declarations for the modifier key mapping //Declarations for the modifier key mapping
@ -54,11 +54,9 @@ class SpaceCadet : public KaleidoscopePlugin {
uint32_t start_time = 0; uint32_t start_time = 0;
}; };
//Empty constructor
SpaceCadet(void); SpaceCadet(void);
//Methods //Methods
void begin(void) final;
static void enable(void); static void enable(void);
static void disable(void); static void disable(void);
static bool active(void); static bool active(void);
@ -67,8 +65,15 @@ class SpaceCadet : public KaleidoscopePlugin {
static uint16_t time_out; // The global timeout in milliseconds static uint16_t time_out; // The global timeout in milliseconds
static SpaceCadet::KeyBinding * map; // The map of key bindings static SpaceCadet::KeyBinding * map; // The map of key bindings
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
private: private:
static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state);
static bool disabled; static bool disabled;
}; };
}; };

Loading…
Cancel
Save