Adapt Turbo plugin to KeyEvent handlers

Signed-off-by: Michael Richters <gedankenexperimenter@gmail.com>
pull/1024/head
Michael Richters 4 years ago
parent f91d2a30a3
commit b535d203a6
No known key found for this signature in database
GPG Key ID: 1288FD13E4EEF0C0

@ -25,16 +25,14 @@ namespace kaleidoscope {
namespace plugin { namespace plugin {
uint16_t Turbo::interval_ = 10; uint16_t Turbo::interval_ = 10;
uint16_t Turbo::flashInterval_ = 69; uint16_t Turbo::flash_interval_ = 69;
bool Turbo::sticky_ = false; bool Turbo::sticky_ = false;
bool Turbo::flash_ = true; bool Turbo::flash_ = true;
cRGB Turbo::activeColor_ = CRGB(160, 0, 0); cRGB Turbo::active_color_ = CRGB(160, 0, 0);
bool Turbo::enable = false; bool Turbo::active_ = false;
uint32_t Turbo::startTime = 0; uint32_t Turbo::start_time_ = 0;
uint32_t Turbo::flashStartTime = 0; uint32_t Turbo::flash_start_time_ = 0;
KeyAddr Turbo::keyPositions[4];
uint16_t Turbo::numKeys = 0;
uint16_t Turbo::interval() { uint16_t Turbo::interval() {
return interval_; return interval_;
@ -44,10 +42,10 @@ void Turbo::interval(uint16_t newVal) {
} }
uint16_t Turbo::flashInterval() { uint16_t Turbo::flashInterval() {
return flashInterval_; return flash_interval_;
} }
void Turbo::flashInterval(uint16_t newVal) { void Turbo::flashInterval(uint16_t newVal) {
flashInterval_ = newVal; flash_interval_ = newVal;
} }
bool Turbo::sticky() { bool Turbo::sticky() {
@ -65,75 +63,91 @@ void Turbo::flash(bool newVal) {
} }
cRGB Turbo::activeColor() { cRGB Turbo::activeColor() {
return activeColor_; return active_color_;
} }
void Turbo::activeColor(cRGB newVal) { void Turbo::activeColor(cRGB newVal) {
activeColor_ = newVal; active_color_ = newVal;
} }
void Turbo::findKeyPositions() { EventHandlerResult Turbo::onKeyEvent(KeyEvent &event) {
numKeys = 0; if (active_ && flash_ && keyToggledOff(event.state)) {
if (event.key.isKeyboardKey())
for (auto key_addr : KeyAddr::all()) { LEDControl::refreshAt(event.addr);
if (Layer.lookupOnActiveLayer(key_addr) == Key_Turbo) {
keyPositions[numKeys++] = key_addr;
}
} }
}
EventHandlerResult Turbo::onSetup() {
Turbo::findKeyPositions();
return EventHandlerResult::OK;
}
EventHandlerResult Turbo::onNameQuery() {
return ::Focus.sendName(F("Turbo"));
}
EventHandlerResult Turbo::onLayerChange() { if (event.key != Key_Turbo)
Turbo::findKeyPositions(); return EventHandlerResult::OK;
return EventHandlerResult::OK;
}
EventHandlerResult Turbo::onKeyswitchEvent(Key &key, KeyAddr key_addr, uint8_t key_state) { if (keyToggledOn(event.state)) {
if (key != Key_Turbo) return EventHandlerResult::OK; active_ = true;
enable = sticky_ ? (keyIsPressed(key_state) ? enable : !enable) : keyIsPressed(key_state); start_time_ = Runtime.millisAtCycleStart() - interval_;
if (!enable) { } else {
for (uint16_t i = 0; i < numKeys; i++) { active_ = false;
LEDControl::refreshAt(KeyAddr(keyPositions[i])); if (flash_)
} LEDControl::refreshAll();
} }
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
EventHandlerResult Turbo::afterEachCycle() { EventHandlerResult Turbo::afterEachCycle() {
if (enable) { if (active_) {
if (Runtime.millisAtCycleStart() - startTime > interval_) { if (Runtime.hasTimeExpired(start_time_, interval_)) {
kaleidoscope::Runtime.hid().keyboard().sendReport(); // Reset the timer.
startTime = Runtime.millisAtCycleStart(); start_time_ = Runtime.millisAtCycleStart();
}
// Clear the existing Keyboard HID report. It might be nice to keep the
if (flash_) { // modifiers active, but I'll save that for another time.
if (Runtime.millisAtCycleStart() - flashStartTime > flashInterval_ * 2) { Runtime.hid().keyboard().releaseAllKeys();
for (uint16_t i = 0; i < numKeys; i++) { // Send the empty report to register the release of all the held keys.
LEDControl::setCrgbAt(KeyAddr(keyPositions[i]), activeColor_); Runtime.hid().keyboard().sendReport();
// Just in case the Turbo key has been wiped from `live_keys[]` without
// `onKeyEvent()` being called with a toggle-off:
active_ = false;
// Go through the `live_keys[]` array and add any Keyboard HID keys to the
// new report.
for (Key key : live_keys.all()) {
if (key == Key_Turbo) {
active_ = true;
} }
flashStartTime = Runtime.millisAtCycleStart(); if (key.isKeyboardKey()) {
} else if (Runtime.millisAtCycleStart() - flashStartTime > flashInterval_) { Runtime.addToReport(key);
for (uint16_t i = 0; i < numKeys; i++) {
LEDControl::setCrgbAt(KeyAddr(keyPositions[i]), {0, 0, 0});
} }
} }
LEDControl::syncLeds();
} else { // Send the re-populated keyboard report.
for (uint16_t i = 0; i < numKeys; i++) { Runtime.hid().keyboard().sendReport();
LEDControl::setCrgbAt(KeyAddr(keyPositions[i]), activeColor_); }
}
return EventHandlerResult::OK;
}
EventHandlerResult Turbo::beforeSyncingLeds() {
if (flash_ && active_) {
static bool leds_on = false;
cRGB color = CRGB(0, 0, 0);
if (leds_on) {
color = active_color_;
}
if (Runtime.hasTimeExpired(flash_start_time_, flash_interval_)) {
flash_start_time_ = Runtime.millisAtCycleStart();
leds_on = !leds_on;
}
for (KeyAddr key_addr : KeyAddr::all()) {
Key key = live_keys[key_addr];
if (key.isKeyboardKey()) {
LEDControl::setCrgbAt(key_addr, color);
} }
} }
} }
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
EventHandlerResult Turbo::onNameQuery() {
return ::Focus.sendName(F("Turbo"));
}
} }
} }

@ -21,7 +21,7 @@
#pragma once #pragma once
#define Key_Turbo Key{kaleidoscope::ranges::TURBO } #define Key_Turbo Key{kaleidoscope::ranges::TURBO}
namespace kaleidoscope { namespace kaleidoscope {
namespace plugin { namespace plugin {
@ -44,25 +44,21 @@ class Turbo : public kaleidoscope::Plugin {
cRGB activeColor(); cRGB activeColor();
void activeColor(cRGB newVal); void activeColor(cRGB newVal);
EventHandlerResult onSetup();
EventHandlerResult onNameQuery(); EventHandlerResult onNameQuery();
EventHandlerResult onLayerChange(); EventHandlerResult onKeyEvent(KeyEvent &event);
EventHandlerResult onKeyswitchEvent(Key &key, KeyAddr key_addr, uint8_t key_state);
EventHandlerResult afterEachCycle(); EventHandlerResult afterEachCycle();
private: EventHandlerResult beforeSyncingLeds();
void findKeyPositions();
private:
static uint16_t interval_; static uint16_t interval_;
static uint16_t flashInterval_; static uint16_t flash_interval_;
static bool sticky_; static bool sticky_;
static bool flash_; static bool flash_;
static cRGB activeColor_; static cRGB active_color_;
static bool enable; static bool active_;
static uint32_t startTime; static uint32_t start_time_;
static uint32_t flashStartTime; static uint32_t flash_start_time_;
static KeyAddr keyPositions[4];
static uint16_t numKeys;
}; };
} }
} }

Loading…
Cancel
Save