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).
pull/389/head
Michael Richters 7 years ago
parent 71d2be1264
commit 778cb705bc

@ -72,7 +72,7 @@ void Qukeys::enqueue(uint8_t key_addr) {
flushQueue(); flushQueue();
} }
key_queue_[key_queue_length_].addr = key_addr; 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_++; key_queue_length_++;
addr::mask(key_addr); 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) { void Qukeys::preReportHook(void) {
// If the qukey has been held longer than the time limit, set its // If the qukey has been held longer than the time limit, set its
// state to the alternate keycode and add it to the report // 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) { while (key_queue_length_ > 0) {
if (lookupQukey(key_queue_[0].addr) == QUKEY_NOT_FOUND) { if (lookupQukey(key_queue_[0].addr) == QUKEY_NOT_FOUND) {
flushKey(QUKEY_STATE_PRIMARY, IS_PRESSED | WAS_PRESSED); 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); flushKey(QUKEY_STATE_ALTERNATE, IS_PRESSED | WAS_PRESSED);
} else { } else {
break; break;
@ -256,7 +256,7 @@ void Qukeys::begin() {
// initializing the key_queue seems unnecessary, actually // initializing the key_queue seems unnecessary, actually
for (int8_t i = 0; i < QUKEYS_QUEUE_MAX; i++) { for (int8_t i = 0; i < QUKEYS_QUEUE_MAX; i++) {
key_queue_[i].addr = QUKEY_UNKNOWN_ADDR; key_queue_[i].addr = QUKEY_UNKNOWN_ADDR;
key_queue_[i].flush_time = 0; key_queue_[i].start_time = 0;
} }
key_queue_length_ = 0; key_queue_length_ = 0;

@ -57,7 +57,7 @@ struct Qukey {
// Data structure for an entry in the key_queue // Data structure for an entry in the key_queue
struct QueueItem { struct QueueItem {
uint8_t addr; // keyswitch coordinates 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 // The plugin itself

Loading…
Cancel
Save