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

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

@ -63,9 +63,9 @@ const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) {
return MACRO_NONE; return MACRO_NONE;
} }
KALEIDOSCOPE_INIT_PLUGINS(Macros);
void setup() { void setup() {
Kaleidoscope.use(&Macros);
Kaleidoscope.setup (); Kaleidoscope.setup ();
} }
``` ```

@ -183,21 +183,23 @@ const macro_t *Macros_::type(const char *string) {
return MACRO_NONE; return MACRO_NONE;
} }
Key Macros_::handleMacroEvent(Key mappedKey, byte row, byte col, uint8_t keyState) { kaleidoscope::EventHandlerResult Macros_::onKeyswitchEvent(Key &mappedKey, byte row, byte col, uint8_t keyState) {
if (mappedKey.flags != (SYNTHETIC | IS_MACRO)) if (mappedKey.flags != (SYNTHETIC | IS_MACRO))
return mappedKey; return kaleidoscope::EventHandlerResult::OK;
byte key_id = (row * COLS) + col; byte key_id = (row * COLS) + col;
addActiveMacroKey(mappedKey.keyCode, key_id, keyState); addActiveMacroKey(mappedKey.keyCode, key_id, keyState);
return Key_NoKey;
return kaleidoscope::EventHandlerResult::EVENT_CONSUMED;
} }
void Macros_::loopHook(bool post_clear) { kaleidoscope::EventHandlerResult Macros_::afterEachCycle() {
if (post_clear) { active_macro_count = 0;
active_macro_count = 0;
return; return kaleidoscope::EventHandlerResult::OK;
} }
kaleidoscope::EventHandlerResult Macros_::beforeReportingState() {
for (byte i = 0; i < active_macro_count; ++i) { for (byte i = 0; i < active_macro_count; ++i) {
if (active_macros[i].key_id == 0xFF) { if (active_macros[i].key_id == 0xFF) {
// i.e. UNKNOWN_KEYSWITCH_LOCATION // i.e. UNKNOWN_KEYSWITCH_LOCATION
@ -211,16 +213,30 @@ void Macros_::loopHook(bool post_clear) {
active_macros[i].key_state); active_macros[i].key_state);
Macros.play(m); Macros.play(m);
} }
return kaleidoscope::EventHandlerResult::OK;
} }
Macros_::Macros_(void) { // Legacy V1 API
#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
void Macros_::begin() {
Kaleidoscope.useEventHandlerHook(legacyEventHandler);
Kaleidoscope.useLoopHook(legacyLoopHook);
} }
void Key Macros_::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) {
Macros_::begin(void) { kaleidoscope::EventHandlerResult r = Macros.onKeyswitchEvent(mapped_key, row, col, key_state);
active_macro_count = 0; if (r == kaleidoscope::EventHandlerResult::OK)
Kaleidoscope.useEventHandlerHook(handleMacroEvent); return mapped_key;
Kaleidoscope.useLoopHook(loopHook); return Key_NoKey;
}
void Macros_::legacyLoopHook(bool is_post_clear) {
if (is_post_clear) {
Macros.afterEachCycle();
} else {
Macros.beforeReportingState();
}
} }
#endif
Macros_ Macros; Macros_ Macros;

@ -17,11 +17,9 @@ struct MacroKeyEvent {
byte key_state; byte key_state;
}; };
class Macros_ : public KaleidoscopePlugin { class Macros_ : public kaleidoscope::Plugin {
public: public:
Macros_(void); Macros_(void) {}
void begin(void) final;
static MacroKeyEvent active_macros[MAX_CONCURRENT_MACROS]; static MacroKeyEvent active_macros[MAX_CONCURRENT_MACROS];
static byte active_macro_count; static byte active_macro_count;
@ -35,8 +33,10 @@ class Macros_ : public KaleidoscopePlugin {
active_macros[active_macro_count].key_state = key_state; active_macros[active_macro_count].key_state = key_state;
++active_macro_count; ++active_macro_count;
} }
static Key handleMacroEvent(Key mappedKey, byte row, byte col, uint8_t keyState);
static void loopHook(bool post_clear); kaleidoscope::EventHandlerResult onKeyswitchEvent(Key &mappedKey, byte row, byte col, uint8_t keyState);
kaleidoscope::EventHandlerResult beforeReportingState();
kaleidoscope::EventHandlerResult afterEachCycle();
void play(const macro_t *macro_p); void play(const macro_t *macro_p);
@ -57,6 +57,13 @@ class Macros_ : public KaleidoscopePlugin {
static byte row, col; static byte row, col;
#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:
Key lookupAsciiCode(uint8_t ascii_code); Key lookupAsciiCode(uint8_t ascii_code);
}; };

Loading…
Cancel
Save