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

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

@ -13,11 +13,18 @@ void LEDMode::activate(void) {
::LEDControl.activate(this);
}
void LEDMode::begin(void) {
Kaleidoscope.use(&::LEDControl);
kaleidoscope::EventHandlerResult LEDMode::onSetup() {
::LEDControl.mode_add(this);
setup();
return EventHandlerResult::OK;
}
#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
void LEDMode::begin() {
onSetup();
}
#endif
LEDControl::LEDControl(void) {
mode = 0;
@ -112,7 +119,7 @@ void LEDControl::syncLeds(void) {
KeyboardHardware.syncLeds();
}
void LEDControl::begin(void) {
kaleidoscope::EventHandlerResult LEDControl::onSetup() {
set_all_leds_to({0, 0, 0});
for (uint8_t i = 0; i < LED_MAX_MODES; i++) {
@ -120,15 +127,14 @@ void LEDControl::begin(void) {
(modes[i]->setup)();
}
Kaleidoscope.useEventHandlerHook(eventHandler);
Kaleidoscope.useLoopHook(loopHook);
syncTimer = millis() + syncDelay;
return EventHandlerResult::OK;
}
Key LEDControl::eventHandler(Key mappedKey, byte row, byte col, uint8_t keyState) {
kaleidoscope::EventHandlerResult LEDControl::onKeyswitchEvent(Key &mappedKey, byte row, byte col, uint8_t keyState) {
if (mappedKey.flags != (SYNTHETIC | IS_INTERNAL | LED_TOGGLE))
return mappedKey;
return kaleidoscope::EventHandlerResult::OK;
if (keyToggledOn(keyState)) {
if (mappedKey == Key_LEDEffectNext) {
@ -138,12 +144,12 @@ Key LEDControl::eventHandler(Key mappedKey, byte row, byte col, uint8_t keyState
}
}
return Key_NoKey;
return kaleidoscope::EventHandlerResult::EVENT_CONSUMED;
}
void LEDControl::loopHook(bool postClear) {
if (postClear || paused)
return;
kaleidoscope::EventHandlerResult LEDControl::beforeReportingState(void) {
if (paused)
return kaleidoscope::EventHandlerResult::OK;
uint16_t current_time = millis();
if ((current_time - syncTimer) > syncDelay) {
@ -151,6 +157,8 @@ void LEDControl::loopHook(bool postClear) {
syncTimer += syncDelay;
}
update();
return kaleidoscope::EventHandlerResult::OK;
}
bool LEDControl::focusHook(const char *command) {
@ -252,6 +260,29 @@ bool LEDControl::focusHook(const char *command) {
return true;
}
// Legacy V1 API
#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
void LEDControl::begin() {
::LEDControl.onSetup();
Kaleidoscope.useEventHandlerHook(legacyEventHandler);
Kaleidoscope.useLoopHook(legacyLoopHook);
}
Key LEDControl::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) {
EventHandlerResult r = ::LEDControl.onKeyswitchEvent(mapped_key, row, col, key_state);
if (r == EventHandlerResult::OK)
return mapped_key;
return Key_NoKey;
}
void LEDControl::legacyLoopHook(bool is_post_clear) {
if (is_post_clear)
return;
::LEDControl.beforeReportingState();
}
#endif
}
kaleidoscope::LEDControl LEDControl;

@ -22,7 +22,7 @@ namespace kaleidoscope {
* A LED mode **must** implement at least one of @ref onActivate or @ref
* update, and possibly @ref refreshAt too.
*/
class LEDMode : public KaleidoscopePlugin {
class LEDMode : public kaleidoscope::Plugin {
friend class LEDControl;
protected:
// These methods should only be called by LEDControl.
@ -81,15 +81,17 @@ class LEDMode : public KaleidoscopePlugin {
* Called via `Kaleidoscope.use()`, registers the LED mode, and does the
* necessary initialization steps. Calls @ref setup at the end.
*/
void begin(void) final;
kaleidoscope::EventHandlerResult onSetup();
#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
void begin();
#endif
};
class LEDControl : public KaleidoscopePlugin {
class LEDControl : public kaleidoscope::Plugin {
public:
LEDControl(void);
void begin(void) final;
static void next_mode(void);
static void prev_mode(void);
static void setup(void);
@ -130,14 +132,23 @@ class LEDControl : public KaleidoscopePlugin {
static bool focusHook(const char *command);
kaleidoscope::EventHandlerResult onSetup();
kaleidoscope::EventHandlerResult onKeyswitchEvent(Key &mappedKey, byte row, byte col, uint8_t keyState);
kaleidoscope::EventHandlerResult beforeReportingState();
#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:
static uint16_t syncTimer;
static LEDMode *modes[LED_MAX_MODES];
static uint8_t mode;
static Key eventHandler(Key mappedKey, byte row, byte col, uint8_t keyState);
static void loopHook(bool postClear);
};
}
extern kaleidoscope::LEDControl LEDControl;

Loading…
Cancel
Save