Updated to use the new plugin APIs

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/365/head
Gergely Nagy 7 years ago
parent 198e332e05
commit 9f8e87c2d1

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

@ -22,7 +22,7 @@ namespace kaleidoscope {
* A LED mode **must** implement at least one of @ref onActivate or @ref * A LED mode **must** implement at least one of @ref onActivate or @ref
* update, and possibly @ref refreshAt too. * update, and possibly @ref refreshAt too.
*/ */
class LEDMode : public KaleidoscopePlugin { class LEDMode : public kaleidoscope::Plugin {
friend class LEDControl; friend class LEDControl;
protected: protected:
// These methods should only be called by LEDControl. // 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 * Called via `Kaleidoscope.use()`, registers the LED mode, and does the
* necessary initialization steps. Calls @ref setup at the end. * 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: public:
LEDControl(void); LEDControl(void);
void begin(void) final;
static void next_mode(void); static void next_mode(void);
static void prev_mode(void); static void prev_mode(void);
static void setup(void); static void setup(void);
@ -130,14 +132,23 @@ class LEDControl : public KaleidoscopePlugin {
static bool focusHook(const char *command); 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: private:
static uint16_t syncTimer; static uint16_t syncTimer;
static LEDMode *modes[LED_MAX_MODES]; static LEDMode *modes[LED_MAX_MODES];
static uint8_t mode; 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; extern kaleidoscope::LEDControl LEDControl;

Loading…
Cancel
Save