Adapt Turbo plugin to KeyEvent handlers

Signed-off-by: Michael Richters <gedankenexperimenter@gmail.com>
pull/1024/head
Michael Richters 3 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 {
uint16_t Turbo::interval_ = 10;
uint16_t Turbo::flashInterval_ = 69;
uint16_t Turbo::flash_interval_ = 69;
bool Turbo::sticky_ = false;
bool Turbo::flash_ = true;
cRGB Turbo::activeColor_ = CRGB(160, 0, 0);
cRGB Turbo::active_color_ = CRGB(160, 0, 0);
bool Turbo::enable = false;
uint32_t Turbo::startTime = 0;
uint32_t Turbo::flashStartTime = 0;
KeyAddr Turbo::keyPositions[4];
uint16_t Turbo::numKeys = 0;
bool Turbo::active_ = false;
uint32_t Turbo::start_time_ = 0;
uint32_t Turbo::flash_start_time_ = 0;
uint16_t Turbo::interval() {
return interval_;
@ -44,10 +42,10 @@ void Turbo::interval(uint16_t newVal) {
}
uint16_t Turbo::flashInterval() {
return flashInterval_;
return flash_interval_;
}
void Turbo::flashInterval(uint16_t newVal) {
flashInterval_ = newVal;
flash_interval_ = newVal;
}
bool Turbo::sticky() {
@ -65,75 +63,91 @@ void Turbo::flash(bool newVal) {
}
cRGB Turbo::activeColor() {
return activeColor_;
return active_color_;
}
void Turbo::activeColor(cRGB newVal) {
activeColor_ = newVal;
active_color_ = newVal;
}
void Turbo::findKeyPositions() {
numKeys = 0;
for (auto key_addr : KeyAddr::all()) {
if (Layer.lookupOnActiveLayer(key_addr) == Key_Turbo) {
keyPositions[numKeys++] = key_addr;
}
EventHandlerResult Turbo::onKeyEvent(KeyEvent &event) {
if (active_ && flash_ && keyToggledOff(event.state)) {
if (event.key.isKeyboardKey())
LEDControl::refreshAt(event.addr);
}
}
EventHandlerResult Turbo::onSetup() {
Turbo::findKeyPositions();
return EventHandlerResult::OK;
}
EventHandlerResult Turbo::onNameQuery() {
return ::Focus.sendName(F("Turbo"));
}
EventHandlerResult Turbo::onLayerChange() {
Turbo::findKeyPositions();
return EventHandlerResult::OK;
}
if (event.key != Key_Turbo)
return EventHandlerResult::OK;
EventHandlerResult Turbo::onKeyswitchEvent(Key &key, KeyAddr key_addr, uint8_t key_state) {
if (key != Key_Turbo) return EventHandlerResult::OK;
enable = sticky_ ? (keyIsPressed(key_state) ? enable : !enable) : keyIsPressed(key_state);
if (!enable) {
for (uint16_t i = 0; i < numKeys; i++) {
LEDControl::refreshAt(KeyAddr(keyPositions[i]));
}
if (keyToggledOn(event.state)) {
active_ = true;
start_time_ = Runtime.millisAtCycleStart() - interval_;
} else {
active_ = false;
if (flash_)
LEDControl::refreshAll();
}
return EventHandlerResult::EVENT_CONSUMED;
}
EventHandlerResult Turbo::afterEachCycle() {
if (enable) {
if (Runtime.millisAtCycleStart() - startTime > interval_) {
kaleidoscope::Runtime.hid().keyboard().sendReport();
startTime = Runtime.millisAtCycleStart();
}
if (flash_) {
if (Runtime.millisAtCycleStart() - flashStartTime > flashInterval_ * 2) {
for (uint16_t i = 0; i < numKeys; i++) {
LEDControl::setCrgbAt(KeyAddr(keyPositions[i]), activeColor_);
if (active_) {
if (Runtime.hasTimeExpired(start_time_, interval_)) {
// Reset the timer.
start_time_ = Runtime.millisAtCycleStart();
// Clear the existing Keyboard HID report. It might be nice to keep the
// modifiers active, but I'll save that for another time.
Runtime.hid().keyboard().releaseAllKeys();
// Send the empty report to register the release of all the held keys.
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();
} else if (Runtime.millisAtCycleStart() - flashStartTime > flashInterval_) {
for (uint16_t i = 0; i < numKeys; i++) {
LEDControl::setCrgbAt(KeyAddr(keyPositions[i]), {0, 0, 0});
if (key.isKeyboardKey()) {
Runtime.addToReport(key);
}
}
LEDControl::syncLeds();
} else {
for (uint16_t i = 0; i < numKeys; i++) {
LEDControl::setCrgbAt(KeyAddr(keyPositions[i]), activeColor_);
// Send the re-populated keyboard report.
Runtime.hid().keyboard().sendReport();
}
}
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;
}
EventHandlerResult Turbo::onNameQuery() {
return ::Focus.sendName(F("Turbo"));
}
}
}

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

Loading…
Cancel
Save