Updated to use the new plugin APIs

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/389/head
Gergely Nagy 7 years ago
parent ed70ccb529
commit 8074c18d2d

@ -52,11 +52,11 @@ static const kaleidoscope::Leader::dictionary_t leader_dictionary[] PROGMEM =
LEADER_DICT({LEADER_SEQ(LEAD(0), Key_A), leaderA}, LEADER_DICT({LEADER_SEQ(LEAD(0), Key_A), leaderA},
{LEADER_SEQ(LEAD(0), Key_T, Key_X), leaderTX}); {LEADER_SEQ(LEAD(0), Key_T, Key_X), leaderTX});
KALEIDOSCOPE_INIT_PLUGINS(Leader);
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
Kaleidoscope.use(&Leader);
Kaleidoscope.setup(); Kaleidoscope.setup();
Leader.dictionary = leader_dictionary; Leader.dictionary = leader_dictionary;

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-Leader -- VIM-style leader keys * Kaleidoscope-Leader -- VIM-style leader keys
* 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-Leader.h> #include <Kaleidoscope-Leader.h>
// *INDENT-OFF*
const Key keymaps[][ROWS][COLS] PROGMEM = { const Key keymaps[][ROWS][COLS] PROGMEM = {
[0] = KEYMAP_STACKED [0] = KEYMAP_STACKED
(Key_NoKey, Key_1, Key_2, Key_3, Key_4, Key_5, Key_NoKey, (Key_NoKey, Key_1, Key_2, Key_3, Key_4, Key_5, Key_NoKey,
@ -31,12 +32,13 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, 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_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,
LEAD(0)), LEAD(0)),
}; };
// *INDENT-ON*
static void leaderTestA(uint8_t seq_index) { static void leaderTestA(uint8_t seq_index) {
Serial.println(F("leaderTestA")); Serial.println(F("leaderTestA"));
@ -50,9 +52,9 @@ static const kaleidoscope::Leader::dictionary_t leader_dictionary[] PROGMEM =
LEADER_DICT({LEADER_SEQ(LEAD(0), Key_A), leaderTestA}, LEADER_DICT({LEADER_SEQ(LEAD(0), Key_A), leaderTestA},
{LEADER_SEQ(LEAD(0), Key_A, Key_A), leaderTestAA}); {LEADER_SEQ(LEAD(0), Key_A, Key_A), leaderTestAA});
void setup() { KALEIDOSCOPE_INIT_PLUGINS(Leader);
Kaleidoscope.use(&Leader);
void setup() {
Kaleidoscope.setup(); Kaleidoscope.setup();
Leader.dictionary = leader_dictionary; Leader.dictionary = leader_dictionary;

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-Leader -- VIM-style leader keys * Kaleidoscope-Leader -- VIM-style leader keys
* 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
@ -71,41 +71,34 @@ int8_t Leader::lookup(void) {
// --- api --- // --- api ---
Leader::Leader(void) {
}
void Leader::begin(void) {
Kaleidoscope.useEventHandlerHook(eventHandlerHook);
Kaleidoscope.useLoopHook(loopHook);
}
void Leader::reset(void) { void Leader::reset(void) {
sequence_pos_ = 0; sequence_pos_ = 0;
sequence_[0].raw = Key_NoKey.raw; sequence_[0].raw = Key_NoKey.raw;
} }
void Leader::inject(Key key, uint8_t key_state) { void Leader::inject(Key key, uint8_t key_state) {
eventHandlerHook(key, UNKNOWN_KEYSWITCH_LOCATION, key_state); onKeyswitchEvent(key, UNKNOWN_KEYSWITCH_LOCATION, key_state);
} }
// --- hooks --- // --- hooks ---
Key Leader::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { EventHandlerResult Leader::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t keyState) {
if (key_state & INJECTED) if (keyState & INJECTED)
return mapped_key; return EventHandlerResult::OK;
if (!keyIsPressed(key_state) && !keyWasPressed(key_state)) { if (!keyIsPressed(keyState) && !keyWasPressed(keyState)) {
if (isLeader(mapped_key)) if (isLeader(mapped_key)) {
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
return mapped_key; }
return EventHandlerResult::OK;
} }
if (!isActive() && !isLeader(mapped_key)) if (!isActive() && !isLeader(mapped_key))
return mapped_key; return EventHandlerResult::OK;
if (!isActive()) { if (!isActive()) {
// Must be a leader key! // Must be a leader key!
if (keyToggledOff(key_state)) { if (keyToggledOff(keyState)) {
// not active, but a leader key = start the sequence on key release! // not active, but a leader key = start the sequence on key release!
end_time_ = millis() + time_out; end_time_ = millis() + time_out;
sequence_pos_ = 0; sequence_pos_ = 0;
@ -113,53 +106,76 @@ Key Leader::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_sta
} }
// If the sequence was not active yet, ignore the key. // If the sequence was not active yet, ignore the key.
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
// active // active
int8_t action_index = lookup(); int8_t action_index = lookup();
if (keyToggledOn(key_state)) { if (keyToggledOn(keyState)) {
sequence_pos_++; sequence_pos_++;
if (sequence_pos_ > LEADER_MAX_SEQUENCE_LENGTH) { if (sequence_pos_ > LEADER_MAX_SEQUENCE_LENGTH) {
reset(); reset();
return mapped_key; return EventHandlerResult::OK;
} }
end_time_ = millis() + time_out; end_time_ = millis() + time_out;
sequence_[sequence_pos_].raw = mapped_key.raw; sequence_[sequence_pos_].raw = mapped_key.raw;
action_index = lookup(); action_index = lookup();
if (action_index >= 0) if (action_index >= 0) {
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} else if (keyIsPressed(key_state)) { }
} else if (keyIsPressed(keyState)) {
// held, no need for anything here. // held, no need for anything here.
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
if (action_index == NO_MATCH) { if (action_index == NO_MATCH) {
reset(); reset();
return mapped_key; return EventHandlerResult::OK;
} }
if (action_index == PARTIAL_MATCH) { if (action_index == PARTIAL_MATCH) {
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
action_t leaderAction = (action_t) pgm_read_ptr(&(dictionary[action_index].action)); action_t leaderAction = (action_t) pgm_read_ptr(&(dictionary[action_index].action));
(*leaderAction)(action_index); (*leaderAction)(action_index);
return Key_NoKey;
}
void Leader::loopHook(bool is_post_clear) { return EventHandlerResult::EVENT_CONSUMED;
if (!is_post_clear) }
return;
EventHandlerResult Leader::afterEachCycle() {
if (!isActive()) if (!isActive())
return; return EventHandlerResult::OK;
if (millis() >= end_time_) if (millis() >= end_time_)
reset(); reset();
return EventHandlerResult::OK;
}
// Legacy V1 API
#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
void Leader::begin() {
Kaleidoscope.useEventHandlerHook(legacyEventHandler);
Kaleidoscope.useLoopHook(legacyLoopHook);
}
Key Leader::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) {
EventHandlerResult r = ::Leader.onKeyswitchEvent(mapped_key, row, col, key_state);
if (r == EventHandlerResult::OK)
return mapped_key;
return Key_NoKey;
}
void Leader::legacyLoopHook(bool is_post_clear) {
if (!is_post_clear)
return;
::Leader.afterEachCycle();
} }
#endif
} }

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-Leader -- VIM-style leader keys * Kaleidoscope-Leader -- VIM-style leader keys
* 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
@ -30,7 +30,7 @@
namespace kaleidoscope { namespace kaleidoscope {
class Leader : public KaleidoscopePlugin { class Leader : public kaleidoscope::Plugin {
public: public:
typedef void (*action_t)(uint8_t seq_index); typedef void (*action_t)(uint8_t seq_index);
typedef struct { typedef struct {
@ -38,23 +38,29 @@ class Leader : public KaleidoscopePlugin {
action_t action; action_t action;
} dictionary_t; } dictionary_t;
Leader(void); Leader(void) {}
static const dictionary_t *dictionary; static const dictionary_t *dictionary;
void begin(void) final;
static void reset(void); static void reset(void);
static uint16_t time_out; static uint16_t time_out;
void inject(Key key, uint8_t key_state); void inject(Key key, uint8_t key_state);
EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t keyState);
EventHandlerResult afterEachCycle();
#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
protected:
void begin();
static Key legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state);
static void legacyLoopHook(bool is_post_clear);
#endif
private: private:
static Key sequence_[LEADER_MAX_SEQUENCE_LENGTH + 1]; static Key sequence_[LEADER_MAX_SEQUENCE_LENGTH + 1];
static uint8_t sequence_pos_; static uint8_t sequence_pos_;
static uint32_t end_time_; static uint32_t end_time_;
static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state);
static void loopHook(bool is_post_clear);
static int8_t lookup(void); static int8_t lookup(void);
}; };

Loading…
Cancel
Save