Merge pull request #576 from keyboardio/TapDance/optimization

TapDance: Switch from 16-bit bitfields to using a struct
pull/578/head
Jesse Vincent 6 years ago committed by GitHub
commit 8d57e4ffe4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -24,28 +24,18 @@ namespace plugin {
// --- state --- // --- state ---
uint32_t TapDance::end_time_; uint32_t TapDance::end_time_;
uint16_t TapDance::time_out = 200; uint16_t TapDance::time_out = 200;
uint8_t TapDance::tap_count_[16]; TapDance::TapDanceState TapDance::state_[TapDance::TAPDANCE_KEY_COUNT];
uint16_t TapDance::pressed_state_;
uint16_t TapDance::triggered_state_;
uint16_t TapDance::release_next_state_;
Key TapDance::last_tap_dance_key_; Key TapDance::last_tap_dance_key_;
byte TapDance::last_tap_dance_row_; byte TapDance::last_tap_dance_row_;
byte TapDance::last_tap_dance_col_; byte TapDance::last_tap_dance_col_;
// --- helpers ---
#define isTapDance(k) (k.raw >= ranges::TD_FIRST && k.raw <= ranges::TD_LAST)
#define isInSeq(k) (last_tap_dance_key_.raw == k.raw)
#define stillHeld(idx) (tap_count_[idx])
#define isActive() (last_tap_dance_key_.raw != Key_NoKey.raw)
// --- actions --- // --- actions ---
void TapDance::interrupt(byte row, byte col) { void TapDance::interrupt(byte row, byte col) {
uint8_t idx = last_tap_dance_key_.raw - ranges::TD_FIRST; uint8_t idx = last_tap_dance_key_.raw - ranges::TD_FIRST;
tapDanceAction(idx, last_tap_dance_row_, last_tap_dance_col_, tap_count_[idx], Interrupt); tapDanceAction(idx, last_tap_dance_row_, last_tap_dance_col_, state_[idx].count, Interrupt);
bitWrite(triggered_state_, idx, 1); state_[idx].triggered = true;
end_time_ = 0; end_time_ = 0;
@ -53,7 +43,7 @@ void TapDance::interrupt(byte row, byte col) {
kaleidoscope::hid::sendKeyboardReport(); kaleidoscope::hid::sendKeyboardReport();
kaleidoscope::hid::releaseAllKeys(); kaleidoscope::hid::releaseAllKeys();
if (bitRead(pressed_state_, idx)) if (state_[idx].pressed)
return; return;
release(idx); release(idx);
@ -62,10 +52,10 @@ void TapDance::interrupt(byte row, byte col) {
void TapDance::timeout(void) { void TapDance::timeout(void) {
uint8_t idx = last_tap_dance_key_.raw - ranges::TD_FIRST; uint8_t idx = last_tap_dance_key_.raw - ranges::TD_FIRST;
tapDanceAction(idx, last_tap_dance_row_, last_tap_dance_col_, tap_count_[idx], Timeout); tapDanceAction(idx, last_tap_dance_row_, last_tap_dance_col_, state_[idx].count, Timeout);
bitWrite(triggered_state_, idx, 1); state_[idx].triggered = true;
if (bitRead(pressed_state_, idx)) if (state_[idx].pressed)
return; return;
last_tap_dance_key_.raw = Key_NoKey.raw; last_tap_dance_key_.raw = Key_NoKey.raw;
@ -77,18 +67,18 @@ void TapDance::release(uint8_t tap_dance_index) {
end_time_ = 0; end_time_ = 0;
last_tap_dance_key_.raw = Key_NoKey.raw; last_tap_dance_key_.raw = Key_NoKey.raw;
bitClear(pressed_state_, tap_dance_index); state_[tap_dance_index].pressed = false;
bitClear(triggered_state_, tap_dance_index); state_[tap_dance_index].triggered = false;
bitWrite(release_next_state_, tap_dance_index, 1); state_[tap_dance_index].release_next = true;
} }
void TapDance::tap(void) { void TapDance::tap(void) {
uint8_t idx = last_tap_dance_key_.raw - ranges::TD_FIRST; uint8_t idx = last_tap_dance_key_.raw - ranges::TD_FIRST;
tap_count_[idx]++; state_[idx].count++;
end_time_ = millis() + time_out; end_time_ = millis() + time_out;
tapDanceAction(idx, last_tap_dance_row_, last_tap_dance_col_, tap_count_[idx], Tap); tapDanceAction(idx, last_tap_dance_row_, last_tap_dance_col_, state_[idx].count, Tap);
} }
// --- api --- // --- api ---
@ -123,8 +113,8 @@ EventHandlerResult TapDance::onKeyswitchEvent(Key &mapped_key, byte row, byte co
if (keyState & INJECTED) if (keyState & INJECTED)
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (!isTapDance(mapped_key)) { if (mapped_key.raw < ranges::TD_FIRST || mapped_key.raw > ranges::TD_LAST) {
if (!isActive()) if (last_tap_dance_key_.raw == Key_NoKey.raw)
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (keyToggledOn(keyState)) if (keyToggledOn(keyState))
@ -140,11 +130,11 @@ EventHandlerResult TapDance::onKeyswitchEvent(Key &mapped_key, byte row, byte co
uint8_t tap_dance_index = mapped_key.raw - ranges::TD_FIRST; uint8_t tap_dance_index = mapped_key.raw - ranges::TD_FIRST;
if (keyToggledOff(keyState)) if (keyToggledOff(keyState))
bitClear(pressed_state_, tap_dance_index); state_[tap_dance_index].pressed = false;
if (!isInSeq(mapped_key)) { if (last_tap_dance_key_.raw != mapped_key.raw) {
if (!isActive()) { if (last_tap_dance_key_.raw == Key_NoKey.raw) {
if (bitRead(triggered_state_, tap_dance_index)) { if (state_[tap_dance_index].triggered) {
if (keyToggledOff(keyState)) { if (keyToggledOff(keyState)) {
release(tap_dance_index); release(tap_dance_index);
} }
@ -160,7 +150,7 @@ EventHandlerResult TapDance::onKeyswitchEvent(Key &mapped_key, byte row, byte co
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} else { } else {
if (keyToggledOff(keyState) && stillHeld(tap_dance_index)) { if (keyToggledOff(keyState) && state_[tap_dance_index].count) {
release(tap_dance_index); release(tap_dance_index);
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
@ -182,30 +172,30 @@ EventHandlerResult TapDance::onKeyswitchEvent(Key &mapped_key, byte row, byte co
last_tap_dance_key_.raw = mapped_key.raw; last_tap_dance_key_.raw = mapped_key.raw;
last_tap_dance_row_ = row; last_tap_dance_row_ = row;
last_tap_dance_col_ = col; last_tap_dance_col_ = col;
bitSet(pressed_state_, tap_dance_index); state_[tap_dance_index].pressed = true;
if (keyToggledOn(keyState)) { if (keyToggledOn(keyState)) {
tap(); tap();
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
if (bitRead(triggered_state_, tap_dance_index)) if (state_[tap_dance_index].triggered)
tapDanceAction(tap_dance_index, row, col, tap_count_[tap_dance_index], Hold); tapDanceAction(tap_dance_index, row, col, state_[tap_dance_index].count, Hold);
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
EventHandlerResult TapDance::afterEachCycle() { EventHandlerResult TapDance::afterEachCycle() {
for (uint8_t i = 0; i < 16; i++) { for (uint8_t i = 0; i < TAPDANCE_KEY_COUNT; i++) {
if (!bitRead(release_next_state_, i)) if (!state_[i].release_next)
continue; continue;
tapDanceAction(i, last_tap_dance_row_, last_tap_dance_col_, tap_count_[i], Release); tapDanceAction(i, last_tap_dance_row_, last_tap_dance_col_, state_[i].count, Release);
tap_count_[i] = 0; state_[i].count = 0;
bitClear(release_next_state_, i); state_[i].release_next = false;
} }
if (!isActive()) if (last_tap_dance_key_.raw == Key_NoKey.raw)
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (end_time_ && millis() > end_time_) if (end_time_ && millis() > end_time_)

@ -50,11 +50,16 @@ class TapDance : public kaleidoscope::Plugin {
EventHandlerResult afterEachCycle(); EventHandlerResult afterEachCycle();
private: private:
static constexpr uint8_t TAPDANCE_KEY_COUNT = 16;
struct TapDanceState {
bool pressed: 1;
bool triggered: 1;
bool release_next: 1;
uint8_t count;
};
static TapDanceState state_[TAPDANCE_KEY_COUNT];
static uint32_t end_time_; static uint32_t end_time_;
static uint8_t tap_count_[16];
static uint16_t pressed_state_;
static uint16_t triggered_state_;
static uint16_t release_next_state_;
static Key last_tap_dance_key_; static Key last_tap_dance_key_;
static byte last_tap_dance_row_; static byte last_tap_dance_row_;
static byte last_tap_dance_col_; static byte last_tap_dance_col_;

Loading…
Cancel
Save