From 778cb705bcd33514e1238aa8db13d3059436703d Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Mon, 18 Dec 2017 22:36:49 -0600 Subject: [PATCH] Correct for timer overflow When the value returned by `millis()` overflows (after ~two months of runtime), a straight comparison to the end time would fail. This wasn't a really big deal, but it is possible to do it correctly, and in the process, reduce the size of the time values stored from 32 bits to 16 bits (~one minute), since the largest conceivable useful timeout is measured in seconds (at most). --- src/Kaleidoscope/Qukeys.cpp | 8 ++++---- src/Kaleidoscope/Qukeys.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Kaleidoscope/Qukeys.cpp b/src/Kaleidoscope/Qukeys.cpp index ecabebad..0eb95ddd 100644 --- a/src/Kaleidoscope/Qukeys.cpp +++ b/src/Kaleidoscope/Qukeys.cpp @@ -72,7 +72,7 @@ void Qukeys::enqueue(uint8_t key_addr) { flushQueue(); } key_queue_[key_queue_length_].addr = key_addr; - key_queue_[key_queue_length_].flush_time = millis() + time_limit_; + key_queue_[key_queue_length_].start_time = (uint16_t)millis(); key_queue_length_++; addr::mask(key_addr); } @@ -235,11 +235,11 @@ Key Qukeys::keyScanHook(Key mapped_key, byte row, byte col, uint8_t key_state) { void Qukeys::preReportHook(void) { // If the qukey has been held longer than the time limit, set its // state to the alternate keycode and add it to the report - uint32_t current_time = millis(); + uint16_t current_time = (uint16_t)millis(); while (key_queue_length_ > 0) { if (lookupQukey(key_queue_[0].addr) == QUKEY_NOT_FOUND) { flushKey(QUKEY_STATE_PRIMARY, IS_PRESSED | WAS_PRESSED); - } else if (current_time > key_queue_[0].flush_time) { + } else if ((current_time - key_queue_[0].start_time) > time_limit_) { flushKey(QUKEY_STATE_ALTERNATE, IS_PRESSED | WAS_PRESSED); } else { break; @@ -256,7 +256,7 @@ void Qukeys::begin() { // initializing the key_queue seems unnecessary, actually for (int8_t i = 0; i < QUKEYS_QUEUE_MAX; i++) { key_queue_[i].addr = QUKEY_UNKNOWN_ADDR; - key_queue_[i].flush_time = 0; + key_queue_[i].start_time = 0; } key_queue_length_ = 0; diff --git a/src/Kaleidoscope/Qukeys.h b/src/Kaleidoscope/Qukeys.h index 2e920e69..2994bdff 100644 --- a/src/Kaleidoscope/Qukeys.h +++ b/src/Kaleidoscope/Qukeys.h @@ -57,7 +57,7 @@ struct Qukey { // Data structure for an entry in the key_queue struct QueueItem { uint8_t addr; // keyswitch coordinates - uint32_t flush_time; // time past which a qukey gets flushed + uint16_t start_time; // time a queued key was pressed }; // The plugin itself