Merge pull request #642 from gedankenexperimenter/standardized-timers

Standardize plugin timers
pull/659/head
Jesse Vincent 6 years ago committed by GitHub
commit 0ee41c85b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -20,12 +20,12 @@
namespace kaleidoscope { namespace kaleidoscope {
namespace plugin { namespace plugin {
uint32_t CycleTimeReport::next_report_time_; uint16_t CycleTimeReport::last_report_time_;
uint32_t CycleTimeReport::loop_start_time_; uint32_t CycleTimeReport::loop_start_time_;
uint32_t CycleTimeReport::average_loop_time; uint32_t CycleTimeReport::average_loop_time;
EventHandlerResult CycleTimeReport::onSetup() { EventHandlerResult CycleTimeReport::onSetup() {
next_report_time_ = millis() + 1000; last_report_time_ = Kaleidoscope.millisAtCycleStart();
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
@ -42,11 +42,11 @@ EventHandlerResult CycleTimeReport::afterEachCycle() {
else else
average_loop_time = loop_time; average_loop_time = loop_time;
if (millis() >= next_report_time_) { if (Kaleidoscope.hasTimeExpired(last_report_time_, uint16_t(1000))) {
cycleTimeReport(); cycleTimeReport();
average_loop_time = 0; average_loop_time = 0;
next_report_time_ = millis() + 1000; last_report_time_ = Kaleidoscope.millisAtCycleStart();
} }
return EventHandlerResult::OK; return EventHandlerResult::OK;

@ -32,7 +32,7 @@ class CycleTimeReport : public kaleidoscope::Plugin {
static uint32_t average_loop_time; static uint32_t average_loop_time;
private: private:
static uint32_t next_report_time_; static uint16_t last_report_time_;
static uint32_t loop_start_time_; static uint32_t loop_start_time_;
}; };
} }

@ -46,11 +46,11 @@ EventHandlerResult GhostInTheFirmware::beforeReportingState() {
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
is_pressed_ = true; is_pressed_ = true;
start_time_ = millis(); start_time_ = Kaleidoscope.millisAtCycleStart();
} else { } else {
if (is_pressed_ && ((millis() - start_time_) > press_timeout_)) { if (is_pressed_ && Kaleidoscope.hasTimeExpired(start_time_, press_timeout_)) {
is_pressed_ = false; is_pressed_ = false;
start_time_ = millis(); start_time_ = Kaleidoscope.millisAtCycleStart();
byte row = pgm_read_byte(&(ghost_keys[current_pos_].row)); byte row = pgm_read_byte(&(ghost_keys[current_pos_].row));
byte col = pgm_read_byte(&(ghost_keys[current_pos_].col)); byte col = pgm_read_byte(&(ghost_keys[current_pos_].col));
@ -61,7 +61,7 @@ EventHandlerResult GhostInTheFirmware::beforeReportingState() {
byte col = pgm_read_byte(&(ghost_keys[current_pos_].col)); byte col = pgm_read_byte(&(ghost_keys[current_pos_].col));
handleKeyswitchEvent(Key_NoKey, row, col, IS_PRESSED); handleKeyswitchEvent(Key_NoKey, row, col, IS_PRESSED);
} else if ((millis() - start_time_) > delay_timeout_) { } else if (Kaleidoscope.hasTimeExpired(start_time_, delay_timeout_)) {
current_pos_++; current_pos_++;
press_timeout_ = 0; press_timeout_ = 0;
} }

@ -38,8 +38,8 @@ Heatmap::TransientLEDMode::TransientLEDMode(const Heatmap *parent)
heatmap_{}, heatmap_{},
// max of heatmap_ (we divide by it so we start at 1) // max of heatmap_ (we divide by it so we start at 1)
highest_(1), highest_(1),
// next heatmap computation time // last heatmap computation time
next_heatmap_comp_time_(0) last_heatmap_comp_time_(Kaleidoscope.millisAtCycleStart())
{} {}
cRGB Heatmap::TransientLEDMode::computeColor(float v) { cRGB Heatmap::TransientLEDMode::computeColor(float v) {
@ -211,14 +211,14 @@ void Heatmap::TransientLEDMode::update(void) {
// this methode is called frequently by the LEDControl::loopHook // this methode is called frequently by the LEDControl::loopHook
// do nothing if we didn't reach next_heatmap_comp_time_ yet // do nothing if the update interval hasn't elapsed since the previous update
if (next_heatmap_comp_time_ && (millis() < next_heatmap_comp_time_)) if (!Kaleidoscope.hasTimeExpired(last_heatmap_comp_time_, update_delay))
return; return;
// do the heatmap computing // do the heatmap computing
// (we reach next_heatmap_comp_time_ or next_heatmap_comp_time_ was never scheduled) // (update_delay milliseconds elapsed since last_heatmap_comp_time)
// schedule the next heatmap computing // schedule the next heatmap computing
next_heatmap_comp_time_ = millis() + update_delay; last_heatmap_comp_time_ = Kaleidoscope.millisAtCycleStart();
// for each key // for each key
for (uint8_t r = 0; r < ROWS; r++) { for (uint8_t r = 0; r < ROWS; r++) {

@ -61,7 +61,7 @@ class Heatmap : public Plugin,
uint16_t heatmap_[ROWS][COLS]; uint16_t heatmap_[ROWS][COLS];
uint16_t highest_; uint16_t highest_;
uint32_t next_heatmap_comp_time_; uint16_t last_heatmap_comp_time_;
void shiftStats(void); void shiftStats(void);
cRGB computeColor(float v); cRGB computeColor(float v);

@ -22,11 +22,11 @@ namespace kaleidoscope {
namespace plugin { namespace plugin {
uint16_t IdleLEDs::idle_time_limit = 600; // 10 minutes uint16_t IdleLEDs::idle_time_limit = 600; // 10 minutes
uint32_t IdleLEDs::end_time_; uint32_t IdleLEDs::start_time_ = 0;
EventHandlerResult IdleLEDs::beforeEachCycle() { EventHandlerResult IdleLEDs::beforeEachCycle() {
if (!::LEDControl.paused && if (!::LEDControl.paused &&
(Kaleidoscope.millisAtCycleStart() >= end_time_)) { Kaleidoscope.hasTimeExpired(start_time_, uint32_t(idle_time_limit * 1000))) {
::LEDControl.set_all_leds_to(CRGB(0, 0, 0)); ::LEDControl.set_all_leds_to(CRGB(0, 0, 0));
::LEDControl.syncLeds(); ::LEDControl.syncLeds();
@ -43,7 +43,7 @@ EventHandlerResult IdleLEDs::onKeyswitchEvent(Key &mapped_key, byte row, byte co
::LEDControl.refreshAll(); ::LEDControl.refreshAll();
} }
end_time_ = Kaleidoscope.millisAtCycleStart() + (uint32_t)idle_time_limit * 1000; start_time_ = Kaleidoscope.millisAtCycleStart();
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }

@ -28,15 +28,11 @@ class IdleLEDs: public kaleidoscope::Plugin {
static uint16_t idle_time_limit; static uint16_t idle_time_limit;
EventHandlerResult onSetup() {
end_time_ = millis() + idle_time_limit * 1000;
return EventHandlerResult::OK;
}
EventHandlerResult beforeEachCycle(); EventHandlerResult beforeEachCycle();
EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state); EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state);
private: private:
static uint32_t end_time_; static uint32_t start_time_;
}; };
} }
} }

@ -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;

@ -57,8 +57,7 @@ void StalkerEffect::TransientLEDMode::update(void) {
if (!parent_->variant) if (!parent_->variant)
return; return;
uint16_t elapsed = Kaleidoscope.millisAtCycleStart() - step_start_time_; if (!Kaleidoscope.hasTimeExpired(step_start_time_, parent_->step_length))
if (elapsed < parent_->step_length)
return; return;
for (byte r = 0; r < ROWS; r++) { for (byte r = 0; r < ROWS; r++) {

@ -81,14 +81,14 @@ void WavepoolEffect::TransientLEDMode::raindrop(uint8_t x, uint8_t y, int8_t *pa
uint8_t WavepoolEffect::TransientLEDMode::wp_rand() { uint8_t WavepoolEffect::TransientLEDMode::wp_rand() {
static uint16_t offset = 0x400; static uint16_t offset = 0x400;
offset = ((offset + 1) & 0x4fff) | 0x400; offset = ((offset + 1) & 0x4fff) | 0x400;
return (millis() / MS_PER_FRAME) + pgm_read_byte(offset); return (Kaleidoscope.millisAtCycleStart() / MS_PER_FRAME) + pgm_read_byte(offset);
} }
void WavepoolEffect::TransientLEDMode::update(void) { void WavepoolEffect::TransientLEDMode::update(void) {
// limit the frame rate; one frame every 64 ms // limit the frame rate; one frame every 64 ms
static uint8_t prev_time = 0; static uint8_t prev_time = 0;
uint8_t now = millis() / MS_PER_FRAME; uint8_t now = Kaleidoscope.millisAtCycleStart() / MS_PER_FRAME;
if (now != prev_time) { if (now != prev_time) {
prev_time = now; prev_time = now;
} else { } else {

@ -28,8 +28,8 @@ static constexpr uint8_t uninitialized_mode_id = 255;
uint8_t LEDControl::mode_id = uninitialized_mode_id; uint8_t LEDControl::mode_id = uninitialized_mode_id;
uint8_t LEDControl::num_led_modes_ = LEDModeManager::numLEDModes(); uint8_t LEDControl::num_led_modes_ = LEDModeManager::numLEDModes();
LEDMode *LEDControl::cur_led_mode_; LEDMode *LEDControl::cur_led_mode_;
uint16_t LEDControl::syncDelay = 32; uint8_t LEDControl::syncDelay = 32;
uint16_t LEDControl::syncTimer; uint16_t LEDControl::syncTimer = 0;
bool LEDControl::paused = false; bool LEDControl::paused = false;
LEDControl::LEDControl(void) { LEDControl::LEDControl(void) {
@ -125,8 +125,6 @@ kaleidoscope::EventHandlerResult LEDControl::onSetup() {
LEDModeManager::setupPersistentLEDModes(); LEDModeManager::setupPersistentLEDModes();
syncTimer = millis() + syncDelay;
if (mode_id == uninitialized_mode_id) { if (mode_id == uninitialized_mode_id) {
set_mode(0); set_mode(0);
} }
@ -153,12 +151,7 @@ kaleidoscope::EventHandlerResult LEDControl::beforeReportingState(void) {
if (paused) if (paused)
return kaleidoscope::EventHandlerResult::OK; return kaleidoscope::EventHandlerResult::OK;
// unsigned subtraction means that as syncTimer rolls over if (Kaleidoscope.hasTimeExpired(syncTimer, syncDelay)) {
// the same interval is kept
uint16_t elapsed = Kaleidoscope.millisAtCycleStart() - syncTimer;
// on some platforms, the subtraction in the comparison results in a signed
// operation, resulting in syncLeds() no longer getting called.
if (elapsed > syncDelay) {
syncLeds(); syncLeds();
syncTimer += syncDelay; syncTimer += syncDelay;
update(); update();

@ -95,7 +95,7 @@ class LEDControl : public kaleidoscope::Plugin {
// //
static void activate(LEDModeInterface *plugin); static void activate(LEDModeInterface *plugin);
static uint16_t syncDelay; static uint8_t syncDelay;
static bool paused; static bool paused;
kaleidoscope::EventHandlerResult onSetup(); kaleidoscope::EventHandlerResult onSetup();

@ -40,7 +40,6 @@ const uint8_t BootAnimationEffect::greeting_[11] PROGMEM = {
}; };
EventHandlerResult BootAnimationEffect::onSetup() { EventHandlerResult BootAnimationEffect::onSetup() {
start_time_ = Kaleidoscope.millisAtCycleStart();
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
@ -70,12 +69,12 @@ EventHandlerResult BootAnimationEffect::afterEachCycle() {
} }
} }
if ((Kaleidoscope.millisAtCycleStart() - start_time_) > timeout) { if (Kaleidoscope.hasTimeExpired(start_time_, timeout)) {
current_index_++; current_index_++;
if (current_index_ == sizeof(greeting_)) if (current_index_ == sizeof(greeting_))
done_ = true; done_ = true;
start_time_ = Kaleidoscope.millisAtCycleStart(); start_time_ += timeout;
if (row != 255 && col != 255) if (row != 255 && col != 255)
::LEDControl.refreshAt(row, col); ::LEDControl.refreshAt(row, col);
return EventHandlerResult::OK; return EventHandlerResult::OK;

@ -60,6 +60,11 @@ void BootGreetingEffect::findLed(void) {
done_ = true; done_ = true;
} }
EventHandlerResult BootGreetingEffect::onSetup() {
findLed();
return EventHandlerResult::OK;
}
EventHandlerResult BootGreetingEffect::afterEachCycle() { EventHandlerResult BootGreetingEffect::afterEachCycle() {
if (!Kaleidoscope.has_leds) if (!Kaleidoscope.has_leds)
return EventHandlerResult::OK; return EventHandlerResult::OK;
@ -69,17 +74,8 @@ EventHandlerResult BootGreetingEffect::afterEachCycle() {
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
//If the start time isn't set, set the start time and
//find the LEDs.
if (start_time == 0) {
start_time = millis();
findLed();
//the first time, don't do anything.
return EventHandlerResult::OK;
}
//Only run for 'timeout' milliseconds //Only run for 'timeout' milliseconds
if ((millis() - start_time) > timeout) { if (Kaleidoscope.hasTimeExpired(start_time, timeout)) {
done_ = true; done_ = true;
::LEDControl.refreshAt(row_, col_); ::LEDControl.refreshAt(row_, col_);
return EventHandlerResult::OK; return EventHandlerResult::OK;

@ -32,6 +32,7 @@ class BootGreetingEffect : public kaleidoscope::Plugin {
static uint8_t hue; static uint8_t hue;
static uint16_t timeout; static uint16_t timeout;
EventHandlerResult onSetup();
EventHandlerResult afterEachCycle(); EventHandlerResult afterEachCycle();
private: private:

@ -16,18 +16,15 @@
#include "Kaleidoscope-LEDEffect-Breathe.h" #include "Kaleidoscope-LEDEffect-Breathe.h"
#define UPDATE_INTERVAL 50 // milliseconds between two LED updates to avoid overloading; 20 fps
namespace kaleidoscope { namespace kaleidoscope {
namespace plugin { namespace plugin {
void LEDBreatheEffect::TransientLEDMode::update(void) { void LEDBreatheEffect::TransientLEDMode::update(void) {
if (!Kaleidoscope.has_leds) if (!Kaleidoscope.has_leds)
return; return;
uint16_t now = Kaleidoscope.millisAtCycleStart(); if (!Kaleidoscope.hasTimeExpired(last_update_, update_interval_))
if ((now - last_update_) < UPDATE_INTERVAL)
return; return;
last_update_ = now; last_update_ += update_interval_;
cRGB color = breath_compute(parent_->hue, parent_->saturation); cRGB color = breath_compute(parent_->hue, parent_->saturation);
::LEDControl.set_all_leds_to(color); ::LEDControl.set_all_leds_to(color);

@ -46,7 +46,9 @@ class LEDBreatheEffect : public Plugin,
private: private:
const LEDBreatheEffect *parent_; const LEDBreatheEffect *parent_;
uint16_t last_update_ = 0;
static constexpr uint8_t update_interval_ = 50;
uint8_t last_update_ = 0;
}; };
}; };
} }

@ -23,11 +23,10 @@ void LEDChaseEffect::TransientLEDMode::update(void) {
if (!Kaleidoscope.has_leds) if (!Kaleidoscope.has_leds)
return; return;
uint16_t now = Kaleidoscope.millisAtCycleStart(); if (!Kaleidoscope.hasTimeExpired(last_update_, parent_->update_delay_)) {
if ((now - last_update_) < parent_->update_delay_) {
return; return;
} }
last_update_ = now; last_update_ += parent_->update_delay_;
// The red LED is at `pos_`; the blue one follows behind. `direction_` is // The red LED is at `pos_`; the blue one follows behind. `direction_` is
// either +1 or -1; `distance_` is the gap between them. // either +1 or -1; `distance_` is the gap between them.

@ -25,10 +25,10 @@ class LEDChaseEffect : public Plugin,
public: public:
LEDChaseEffect(void) {} LEDChaseEffect(void) {}
uint16_t update_delay() { uint8_t update_delay() {
return update_delay_; return update_delay_;
} }
void update_delay(uint16_t delay) { void update_delay(uint8_t delay) {
update_delay_ = delay; update_delay_ = delay;
} }
uint8_t distance() { uint8_t distance() {
@ -48,7 +48,7 @@ class LEDChaseEffect : public Plugin,
// members of their parent class. Most LED modes can do without. // members of their parent class. Most LED modes can do without.
// //
TransientLEDMode(const LEDChaseEffect *parent) TransientLEDMode(const LEDChaseEffect *parent)
: parent_(parent) {} : parent_(parent), last_update_(Kaleidoscope.millisAtCycleStart()) {}
protected: protected:
@ -60,12 +60,12 @@ class LEDChaseEffect : public Plugin,
int8_t pos_ = 0; int8_t pos_ = 0;
int8_t direction_ = 1; int8_t direction_ = 1;
uint16_t last_update_ = 0; uint16_t last_update_;
}; };
private: private:
uint8_t distance_ = 5; uint8_t distance_ = 5;
uint16_t update_delay_ = 150; uint8_t update_delay_ = 150;
}; };
} }
} }

@ -23,11 +23,11 @@ void LEDRainbowEffect::TransientLEDMode::update(void) {
if (!Kaleidoscope.has_leds) if (!Kaleidoscope.has_leds)
return; return;
uint16_t now = millis(); if (!Kaleidoscope.hasTimeExpired(rainbow_last_update,
if ((now - rainbow_last_update) < parent_->rainbow_update_delay) { parent_->rainbow_update_delay)) {
return; return;
} else { } else {
rainbow_last_update = now; rainbow_last_update += parent_->rainbow_update_delay;
} }
cRGB rainbow = hsvToRgb(rainbow_hue, rainbow_saturation, parent_->rainbow_value); cRGB rainbow = hsvToRgb(rainbow_hue, rainbow_saturation, parent_->rainbow_value);
@ -54,11 +54,11 @@ void LEDRainbowWaveEffect::TransientLEDMode::update(void) {
if (!Kaleidoscope.has_leds) if (!Kaleidoscope.has_leds)
return; return;
uint16_t now = millis(); if (!Kaleidoscope.hasTimeExpired(rainbow_last_update,
if ((now - rainbow_last_update) < parent_->rainbow_update_delay) { parent_->rainbow_update_delay)) {
return; return;
} else { } else {
rainbow_last_update = now; rainbow_last_update += parent_->rainbow_update_delay;
} }
for (int8_t i = 0; i < LED_COUNT; i++) { for (int8_t i = 0; i < LED_COUNT; i++) {

@ -55,13 +55,13 @@ class LEDRainbowEffect : public Plugin,
uint16_t rainbow_hue = 0; // stores 0 to 614 uint16_t rainbow_hue = 0; // stores 0 to 614
uint8_t rainbow_steps = 1; // number of hues we skip in a 360 range per update uint8_t rainbow_steps = 1; // number of hues we skip in a 360 range per update
uint16_t rainbow_last_update = 0; uint8_t rainbow_last_update = 0;
byte rainbow_saturation = 255; byte rainbow_saturation = 255;
}; };
private: private:
uint16_t rainbow_update_delay = 40; // delay between updates (ms) uint8_t rainbow_update_delay = 40; // delay between updates (ms)
byte rainbow_value = 50; byte rainbow_value = 50;
}; };
@ -100,12 +100,12 @@ class LEDRainbowWaveEffect : public Plugin, public LEDModeInterface {
uint16_t rainbow_hue = 0; // stores 0 to 614 uint16_t rainbow_hue = 0; // stores 0 to 614
uint8_t rainbow_wave_steps = 1; // number of hues we skip in a 360 range per update uint8_t rainbow_wave_steps = 1; // number of hues we skip in a 360 range per update
uint16_t rainbow_last_update = 0; uint8_t rainbow_last_update = 0;
byte rainbow_saturation = 255; byte rainbow_saturation = 255;
}; };
uint16_t rainbow_update_delay = 40; // delay between updates (ms) uint8_t rainbow_update_delay = 40; // delay between updates (ms)
byte rainbow_value = 50; byte rainbow_value = 50;
}; };
} }

@ -23,7 +23,7 @@ namespace plugin {
// --- state --- // --- state ---
Key Leader::sequence_[LEADER_MAX_SEQUENCE_LENGTH + 1]; Key Leader::sequence_[LEADER_MAX_SEQUENCE_LENGTH + 1];
uint8_t Leader::sequence_pos_; uint8_t Leader::sequence_pos_;
uint32_t Leader::end_time_; uint16_t Leader::start_time_ = 0;
uint16_t Leader::time_out = 1000; uint16_t Leader::time_out = 1000;
const Leader::dictionary_t *Leader::dictionary; const Leader::dictionary_t *Leader::dictionary;
@ -93,7 +93,7 @@ EventHandlerResult Leader::onKeyswitchEvent(Key &mapped_key, byte row, byte col,
if (keyToggledOff(keyState)) { if (keyToggledOff(keyState)) {
// not active, but a leader key = start the sequence on key release! // not active, but a leader key = start the sequence on key release!
end_time_ = millis() + time_out; start_time_ = Kaleidoscope.millisAtCycleStart();
sequence_pos_ = 0; sequence_pos_ = 0;
sequence_[sequence_pos_].raw = mapped_key.raw; sequence_[sequence_pos_].raw = mapped_key.raw;
} }
@ -112,7 +112,7 @@ EventHandlerResult Leader::onKeyswitchEvent(Key &mapped_key, byte row, byte col,
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
end_time_ = millis() + time_out; start_time_ = Kaleidoscope.millisAtCycleStart();
sequence_[sequence_pos_].raw = mapped_key.raw; sequence_[sequence_pos_].raw = mapped_key.raw;
action_index = lookup(); action_index = lookup();
@ -142,7 +142,7 @@ EventHandlerResult Leader::afterEachCycle() {
if (!isActive()) if (!isActive())
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (millis() >= end_time_) if (Kaleidoscope.hasTimeExpired(start_time_, time_out))
reset(); reset();
return EventHandlerResult::OK; return EventHandlerResult::OK;

@ -52,7 +52,7 @@ class Leader : public kaleidoscope::Plugin {
private: private:
static Key sequence_[LEADER_MAX_SEQUENCE_LENGTH + 1]; static Key sequence_[LEADER_MAX_SEQUENCE_LENGTH + 1];
static uint8_t sequence_pos_; static uint8_t sequence_pos_;
static uint32_t end_time_; static uint16_t start_time_;
static int8_t lookup(void); static int8_t lookup(void);
}; };

@ -21,7 +21,7 @@ namespace kaleidoscope {
namespace plugin { namespace plugin {
uint16_t MagicCombo::min_interval = 500; uint16_t MagicCombo::min_interval = 500;
uint32_t MagicCombo::end_time_; uint16_t MagicCombo::start_time_ = 0;
EventHandlerResult MagicCombo::beforeReportingState() { EventHandlerResult MagicCombo::beforeReportingState() {
for (byte i = 0; i < magiccombo::combos_length; i++) { for (byte i = 0; i < magiccombo::combos_length; i++) {
@ -42,11 +42,11 @@ EventHandlerResult MagicCombo::beforeReportingState() {
if (j != KeyboardHardware.pressedKeyswitchCount()) if (j != KeyboardHardware.pressedKeyswitchCount())
match = false; match = false;
if (match && (millis() >= end_time_)) { if (match && Kaleidoscope.hasTimeExpired(start_time_, min_interval)) {
ComboAction action = (ComboAction) pgm_read_ptr(&(magiccombo::combos[i].action)); ComboAction action = (ComboAction) pgm_read_ptr(&(magiccombo::combos[i].action));
(*action)(i); (*action)(i);
end_time_ = millis() + min_interval; start_time_ = Kaleidoscope.millisAtCycleStart();
} }
} }

@ -51,7 +51,7 @@ class MagicCombo : public kaleidoscope::Plugin {
EventHandlerResult beforeReportingState(); EventHandlerResult beforeReportingState();
private: private:
static uint32_t end_time_; static uint16_t start_time_;
}; };
namespace magiccombo { namespace magiccombo {

@ -33,9 +33,9 @@ uint16_t MouseKeys_::accelDelay = 64;
uint8_t MouseKeys_::wheelSpeed = 1; uint8_t MouseKeys_::wheelSpeed = 1;
uint16_t MouseKeys_::wheelDelay = 50; uint16_t MouseKeys_::wheelDelay = 50;
uint32_t MouseKeys_::accelEndTime; uint16_t MouseKeys_::move_start_time_;
uint32_t MouseKeys_::endTime; uint16_t MouseKeys_::accel_start_time_;
uint32_t MouseKeys_::wheelEndTime; uint16_t MouseKeys_::wheel_start_time_;
void MouseKeys_::setWarpGridSize(uint8_t grid_size) { void MouseKeys_::setWarpGridSize(uint8_t grid_size) {
MouseWrapper.warp_grid_size = grid_size; MouseWrapper.warp_grid_size = grid_size;
@ -46,10 +46,10 @@ void MouseKeys_::setSpeedLimit(uint8_t speed_limit) {
} }
void MouseKeys_::scrollWheel(uint8_t keyCode) { void MouseKeys_::scrollWheel(uint8_t keyCode) {
if (millis() < wheelEndTime) if (!Kaleidoscope.hasTimeExpired(wheel_start_time_, wheelDelay))
return; return;
wheelEndTime = millis() + wheelDelay; wheel_start_time_ = Kaleidoscope.millisAtCycleStart();
if (keyCode & KEY_MOUSE_UP) if (keyCode & KEY_MOUSE_UP)
kaleidoscope::hid::moveMouse(0, 0, wheelSpeed); kaleidoscope::hid::moveMouse(0, 0, wheelSpeed);
@ -72,23 +72,21 @@ EventHandlerResult MouseKeys_::afterEachCycle() {
EventHandlerResult MouseKeys_::beforeReportingState() { EventHandlerResult MouseKeys_::beforeReportingState() {
if (mouseMoveIntent == 0) { if (mouseMoveIntent == 0) {
MouseWrapper.accelStep = 0; MouseWrapper.accelStep = 0;
endTime = 0;
accelEndTime = 0;
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
if (millis() < endTime) if (!Kaleidoscope.hasTimeExpired(move_start_time_, speedDelay))
return EventHandlerResult::OK; return EventHandlerResult::OK;
endTime = millis() + speedDelay; move_start_time_ = Kaleidoscope.millisAtCycleStart();
int8_t moveX = 0, moveY = 0; int8_t moveX = 0, moveY = 0;
if (millis() >= accelEndTime) { if (Kaleidoscope.hasTimeExpired(accel_start_time_, accelDelay)) {
if (MouseWrapper.accelStep < 255 - accelSpeed) { if (MouseWrapper.accelStep < 255 - accelSpeed) {
MouseWrapper.accelStep += accelSpeed; MouseWrapper.accelStep += accelSpeed;
} }
accelEndTime = millis() + accelDelay; accel_start_time_ = Kaleidoscope.millisAtCycleStart();
} }
if (mouseMoveIntent & KEY_MOUSE_UP) if (mouseMoveIntent & KEY_MOUSE_UP)
@ -126,9 +124,9 @@ EventHandlerResult MouseKeys_::onKeyswitchEvent(Key &mappedKey, byte row, byte c
} }
} else if (!(mappedKey.keyCode & KEY_MOUSE_WARP)) { } else if (!(mappedKey.keyCode & KEY_MOUSE_WARP)) {
if (keyToggledOn(keyState)) { if (keyToggledOn(keyState)) {
endTime = millis() + speedDelay; move_start_time_ = Kaleidoscope.millisAtCycleStart();
accelEndTime = millis() + accelDelay; accel_start_time_ = Kaleidoscope.millisAtCycleStart();
wheelEndTime = 0; wheel_start_time_ = Kaleidoscope.millisAtCycleStart() - wheelDelay;
} }
if (keyIsPressed(keyState)) { if (keyIsPressed(keyState)) {
if (mappedKey.keyCode & KEY_MOUSE_WHEEL) { if (mappedKey.keyCode & KEY_MOUSE_WHEEL) {

@ -44,9 +44,9 @@ class MouseKeys_ : public kaleidoscope::Plugin {
private: private:
static uint8_t mouseMoveIntent; static uint8_t mouseMoveIntent;
static uint32_t endTime; static uint16_t move_start_time_;
static uint32_t accelEndTime; static uint16_t accel_start_time_;
static uint32_t wheelEndTime; static uint16_t wheel_start_time_;
static void scrollWheel(uint8_t keyCode); static void scrollWheel(uint8_t keyCode);
}; };

@ -22,7 +22,7 @@ namespace plugin {
// ---- state --------- // ---- state ---------
uint32_t OneShot::start_time_ = 0; uint16_t OneShot::start_time_ = 0;
uint16_t OneShot::time_out = 2500; uint16_t OneShot::time_out = 2500;
uint16_t OneShot::hold_time_out = 250; uint16_t OneShot::hold_time_out = 250;
int16_t OneShot::double_tap_time_out = -1; int16_t OneShot::double_tap_time_out = -1;
@ -113,7 +113,7 @@ EventHandlerResult OneShot::onKeyswitchEvent(Key &mapped_key, byte row, byte col
} else { } else {
if (keyToggledOff(keyState)) { if (keyToggledOff(keyState)) {
state_[idx].pressed = false; state_[idx].pressed = false;
if ((Kaleidoscope.millisAtCycleStart() - start_time_) >= hold_time_out) { if (Kaleidoscope.hasTimeExpired(start_time_, hold_time_out)) {
cancelOneShot(idx); cancelOneShot(idx);
should_cancel_ = false; should_cancel_ = false;
} }
@ -123,8 +123,8 @@ EventHandlerResult OneShot::onKeyswitchEvent(Key &mapped_key, byte row, byte col
state_[idx].pressed = true; state_[idx].pressed = true;
if (prev_key_ == mapped_key && isStickable(mapped_key)) { if (prev_key_ == mapped_key && isStickable(mapped_key)) {
if ((Kaleidoscope.millisAtCycleStart() - start_time_) <= uint16_t dtto = (double_tap_time_out == -1) ? time_out : double_tap_time_out;
uint16_t((double_tap_time_out == -1) ? time_out : double_tap_time_out)) { if (!Kaleidoscope.hasTimeExpired(start_time_, dtto)) {
state_[idx].sticky = true; state_[idx].sticky = true;
prev_key_ = mapped_key; prev_key_ = mapped_key;
} }

@ -90,7 +90,7 @@ class OneShot : public kaleidoscope::Plugin {
} key_state_t; } key_state_t;
static key_state_t state_[ONESHOT_KEY_COUNT]; static key_state_t state_[ONESHOT_KEY_COUNT];
static uint32_t start_time_; static uint16_t start_time_;
static Key prev_key_; static Key prev_key_;
static bool should_cancel_; static bool should_cancel_;
static bool should_cancel_stickies_; static bool should_cancel_stickies_;
@ -103,7 +103,7 @@ class OneShot : public kaleidoscope::Plugin {
return key.raw >= ranges::OS_FIRST && key.raw <= ranges::OS_LAST; return key.raw >= ranges::OS_FIRST && key.raw <= ranges::OS_LAST;
} }
static bool hasTimedOut() { static bool hasTimedOut() {
return Kaleidoscope.millisAtCycleStart() - start_time_ >= time_out; return Kaleidoscope.hasTimeExpired(start_time_, time_out);
} }
}; };
} }

@ -119,7 +119,7 @@ EventHandlerResult SpaceCadet::onKeyswitchEvent(Key &mapped_key, byte row, byte
if (mapped_key.raw == map[i].input.raw) { if (mapped_key.raw == map[i].input.raw) {
//The keypress was valid and a match. Mark it as flagged and reset the counter //The keypress was valid and a match. Mark it as flagged and reset the counter
map[i].flagged = true; map[i].flagged = true;
map[i].start_time = millis(); map[i].start_time = Kaleidoscope.millisAtCycleStart();
//yes, we found a valid key //yes, we found a valid key
valid_key = true; valid_key = true;
@ -199,7 +199,7 @@ EventHandlerResult SpaceCadet::onKeyswitchEvent(Key &mapped_key, byte row, byte
} }
//Check to determine if we have surpassed our timeout for holding this key //Check to determine if we have surpassed our timeout for holding this key
if ((millis() - map[index].start_time) >= current_timeout) { if (Kaleidoscope.hasTimeExpired(map[index].start_time, current_timeout)) {
// if we timed out, that means we need to keep pressing the mapped // if we timed out, that means we need to keep pressing the mapped
// key, but we won't need to send the alternative key in the end // key, but we won't need to send the alternative key in the end
map[index].flagged = false; map[index].flagged = false;

@ -52,7 +52,7 @@ class SpaceCadet : public kaleidoscope::Plugin {
//The flag (set to 0) //The flag (set to 0)
bool flagged = false; bool flagged = false;
//the start time for this key press //the start time for this key press
uint32_t start_time = 0; uint16_t start_time = 0;
}; };
SpaceCadet(void); SpaceCadet(void);

@ -22,10 +22,10 @@ namespace kaleidoscope {
namespace plugin { namespace plugin {
// --- state --- // --- state ---
uint32_t TapDance::end_time_; uint16_t TapDance::start_time_;
uint16_t TapDance::time_out = 200; uint16_t TapDance::time_out = 200;
TapDance::TapDanceState TapDance::state_[TapDance::TAPDANCE_KEY_COUNT]; TapDance::TapDanceState TapDance::state_[TapDance::TAPDANCE_KEY_COUNT];
Key TapDance::last_tap_dance_key_; Key TapDance::last_tap_dance_key_ = Key_NoKey;
byte TapDance::last_tap_dance_row_; byte TapDance::last_tap_dance_row_;
byte TapDance::last_tap_dance_col_; byte TapDance::last_tap_dance_col_;
@ -37,7 +37,7 @@ void TapDance::interrupt(byte row, byte col) {
tapDanceAction(idx, last_tap_dance_row_, last_tap_dance_col_, state_[idx].count, Interrupt); tapDanceAction(idx, last_tap_dance_row_, last_tap_dance_col_, state_[idx].count, Interrupt);
state_[idx].triggered = true; state_[idx].triggered = true;
end_time_ = 0; last_tap_dance_key_ = Key_NoKey;
KeyboardHardware.maskKey(row, col); KeyboardHardware.maskKey(row, col);
kaleidoscope::hid::sendKeyboardReport(); kaleidoscope::hid::sendKeyboardReport();
@ -64,7 +64,6 @@ void TapDance::timeout(void) {
} }
void TapDance::release(uint8_t tap_dance_index) { void TapDance::release(uint8_t tap_dance_index) {
end_time_ = 0;
last_tap_dance_key_.raw = Key_NoKey.raw; last_tap_dance_key_.raw = Key_NoKey.raw;
state_[tap_dance_index].pressed = false; state_[tap_dance_index].pressed = false;
@ -76,7 +75,7 @@ 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;
state_[idx].count++; state_[idx].count++;
end_time_ = millis() + time_out; start_time_ = Kaleidoscope.millisAtCycleStart();
tapDanceAction(idx, last_tap_dance_row_, last_tap_dance_col_, state_[idx].count, Tap); tapDanceAction(idx, last_tap_dance_row_, last_tap_dance_col_, state_[idx].count, Tap);
} }
@ -198,7 +197,7 @@ EventHandlerResult TapDance::afterEachCycle() {
if (last_tap_dance_key_.raw == Key_NoKey.raw) if (last_tap_dance_key_.raw == Key_NoKey.raw)
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (end_time_ && millis() > end_time_) if (Kaleidoscope.hasTimeExpired(start_time_, time_out))
timeout(); timeout();
return EventHandlerResult::OK; return EventHandlerResult::OK;

@ -59,7 +59,7 @@ class TapDance : public kaleidoscope::Plugin {
}; };
static TapDanceState state_[TAPDANCE_KEY_COUNT]; static TapDanceState state_[TAPDANCE_KEY_COUNT];
static uint32_t end_time_; static uint16_t start_time_;
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_;

@ -30,6 +30,7 @@ TypingBreaks::settings_t TypingBreaks::settings = {
.right_hand_max_keys = 0 .right_hand_max_keys = 0
}; };
bool TypingBreaks::keyboard_locked_{false};
uint32_t TypingBreaks::session_start_time_; uint32_t TypingBreaks::session_start_time_;
uint32_t TypingBreaks::last_key_time_; uint32_t TypingBreaks::last_key_time_;
uint32_t TypingBreaks::lock_start_time_; uint32_t TypingBreaks::lock_start_time_;
@ -42,22 +43,17 @@ EventHandlerResult TypingBreaks::onKeyswitchEvent(Key &mapped_key, byte row, byt
uint32_t idle_time_limit = settings.idle_time_limit * 1000; uint32_t idle_time_limit = settings.idle_time_limit * 1000;
uint32_t lock_time_out = settings.lock_time_out * 1000; uint32_t lock_time_out = settings.lock_time_out * 1000;
// If we are locked, and didn't time out yet, no key has to be pressed.
if (lock_start_time_ && (millis() - lock_start_time_ <= lock_length)) {
return EventHandlerResult::EVENT_CONSUMED;
}
// If we are locked... // If we are locked...
if (lock_start_time_) { if (keyboard_locked_) {
// ...and the lock has not expired yet // ...and the lock has not expired yet
if (millis() - lock_start_time_ <= lock_length) { if (!Kaleidoscope.hasTimeExpired(lock_start_time_, lock_length)) {
return EventHandlerResult::EVENT_CONSUMED; // remain locked return EventHandlerResult::EVENT_CONSUMED; // remain locked
} }
// ...otherwise clear the lock // ...otherwise clear the lock
lock_start_time_ = 0; keyboard_locked_ = false;
left_hand_keys_ = right_hand_keys_ = 0; left_hand_keys_ = right_hand_keys_ = 0;
session_start_time_ = millis(); session_start_time_ = Kaleidoscope.millisAtCycleStart();
TypingBreak(false); TypingBreak(false);
} }
@ -65,31 +61,26 @@ EventHandlerResult TypingBreaks::onKeyswitchEvent(Key &mapped_key, byte row, byt
// Any other case, we are not locked yet! (or we just unlocked) // Any other case, we are not locked yet! (or we just unlocked)
// Are we still in the same session? // Are we still in the same session?
if (last_key_time_ && (millis() - last_key_time_) >= idle_time_limit) { if (Kaleidoscope.hasTimeExpired(last_key_time_, idle_time_limit)) {
// No, we are not. Clear timers and start over. // No, we are not. Clear timers and start over.
lock_start_time_ = 0;
left_hand_keys_ = right_hand_keys_ = 0; left_hand_keys_ = right_hand_keys_ = 0;
session_start_time_ = millis(); session_start_time_ = Kaleidoscope.millisAtCycleStart();
}
// If we have a limit on the left hand, and we reached it, lock up!
if (settings.left_hand_max_keys && left_hand_keys_ >= settings.left_hand_max_keys) {
lock_start_time_ = millis();
TypingBreak(true);
return EventHandlerResult::EVENT_CONSUMED;
} }
// If we have a limit on the right hand, and we reached it, lock up! // If we have a limit on the either hand, and we reached it, lock up!
if (settings.right_hand_max_keys && right_hand_keys_ >= settings.right_hand_max_keys) { if ((settings.left_hand_max_keys && left_hand_keys_ >= settings.left_hand_max_keys) ||
lock_start_time_ = millis(); (settings.right_hand_max_keys && right_hand_keys_ >= settings.right_hand_max_keys)) {
keyboard_locked_ = true;
lock_start_time_ = last_key_time_;
TypingBreak(true); TypingBreak(true);
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
if (lock_time_out) { if (lock_time_out) {
// Is the session longer than lock_time_out? // Is the session longer than lock_time_out?
if (millis() - session_start_time_ >= lock_time_out) { if (Kaleidoscope.hasTimeExpired(session_start_time_, lock_time_out)) {
// Yeah, it is. // Yeah, it is.
keyboard_locked_ = true;
lock_start_time_ = last_key_time_; lock_start_time_ = last_key_time_;
TypingBreak(true); TypingBreak(true);
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
@ -104,7 +95,7 @@ EventHandlerResult TypingBreaks::onKeyswitchEvent(Key &mapped_key, byte row, byt
left_hand_keys_++; left_hand_keys_++;
else else
right_hand_keys_++; right_hand_keys_++;
last_key_time_ = millis(); last_key_time_ = Kaleidoscope.millisAtCycleStart();
} }
return EventHandlerResult::OK; return EventHandlerResult::OK;

@ -41,6 +41,7 @@ class TypingBreaks : public kaleidoscope::Plugin {
EventHandlerResult onSetup(); EventHandlerResult onSetup();
private: private:
static bool keyboard_locked_;
static uint32_t session_start_time_; static uint32_t session_start_time_;
static uint32_t lock_start_time_; static uint32_t lock_start_time_;
static uint32_t last_key_time_; static uint32_t last_key_time_;

Loading…
Cancel
Save