Use standard timeout checker for LED-AlphaSquare

Also, instead of using `end_time_ == 0` as a special value to indicate that no
timers need to be checked, us `last_key_left_` & `last_key_right_`. This avoids
the bug that could occur when `millis()` returns 0 (which is unlikely, but
possible).

Signed-off-by: Michael Richters <gedankenexperimenter@gmail.com>
pull/642/head
Michael Richters 6 years ago
parent fa19951d56
commit 0ce81a3e85

@ -23,23 +23,23 @@ namespace plugin {
uint16_t AlphaSquareEffect::length = 1000; uint16_t AlphaSquareEffect::length = 1000;
AlphaSquareEffect::TransientLEDMode::TransientLEDMode(AlphaSquareEffect */*parent*/) AlphaSquareEffect::TransientLEDMode::TransientLEDMode(AlphaSquareEffect */*parent*/)
: end_time_left_(0), : last_key_left_(Key_NoKey),
end_time_right_(0), last_key_right_(Key_NoKey)
last_key_left_(Key{}),
last_key_right_(Key{})
{} {}
void AlphaSquareEffect::TransientLEDMode::update(void) { void AlphaSquareEffect::TransientLEDMode::update(void) {
if (!Kaleidoscope.has_leds) if (!Kaleidoscope.has_leds)
return; return;
if (end_time_left_ && millis() > end_time_left_) { if (last_key_left_ != Key_NoKey &&
Kaleidoscope.hasTimeExpired(start_time_left_, length)) {
::AlphaSquare.clear(last_key_left_); ::AlphaSquare.clear(last_key_left_);
end_time_left_ = 0; last_key_left_ = Key_NoKey;
} }
if (end_time_right_ && millis() > end_time_right_) { if (last_key_right_ != Key_NoKey &&
Kaleidoscope.hasTimeExpired(start_time_right_, length)) {
::AlphaSquare.clear(last_key_right_, 10); ::AlphaSquare.clear(last_key_right_, 10);
end_time_right_ = 0; last_key_right_ = Key_NoKey;
} }
} }
@ -49,11 +49,11 @@ void AlphaSquareEffect::TransientLEDMode::refreshAt(byte row, byte col) {
Key key = last_key_left_; Key key = last_key_left_;
if (col < COLS / 2) { if (col < COLS / 2) {
timed_out = !end_time_left_ || (end_time_left_ && millis() > end_time_left_); timed_out = Kaleidoscope.hasTimeExpired(start_time_left_, length);
} else { } else {
key = last_key_right_; key = last_key_right_;
display_col = 10; display_col = 10;
timed_out = !end_time_right_ || (end_time_right_ && millis() > end_time_right_); timed_out = Kaleidoscope.hasTimeExpired(start_time_right_, length);
} }
if (!::AlphaSquare.isSymbolPart(key, 0, display_col, row, col) || timed_out) if (!::AlphaSquare.isSymbolPart(key, 0, display_col, row, col) || timed_out)
@ -83,11 +83,11 @@ EventHandlerResult AlphaSquareEffect::onKeyswitchEvent(Key &mappedKey, byte row,
if (col < COLS / 2) { if (col < COLS / 2) {
this_led_mode->last_key_left_ = mappedKey; this_led_mode->last_key_left_ = mappedKey;
this_led_mode->end_time_left_ = millis() + length; this_led_mode->start_time_left_ = Kaleidoscope.millisAtCycleStart();
} else { } else {
prev_key = this_led_mode->last_key_right_; prev_key = this_led_mode->last_key_right_;
this_led_mode->last_key_right_ = mappedKey; this_led_mode->last_key_right_ = mappedKey;
this_led_mode->end_time_right_ = millis() + length; this_led_mode->start_time_right_ = Kaleidoscope.millisAtCycleStart();
display_col = 10; display_col = 10;
} }

@ -43,7 +43,7 @@ class AlphaSquareEffect : public Plugin,
void refreshAt(byte row, byte col) final; void refreshAt(byte row, byte col) final;
private: private:
uint32_t end_time_left_, end_time_right_; uint16_t start_time_left_, start_time_right_;
Key last_key_left_, last_key_right_; Key last_key_left_, last_key_right_;
friend class AlphaSquareEffect; friend class AlphaSquareEffect;

Loading…
Cancel
Save