Add `remove(n)` method to `KeyAddrEventQueue` class

This will be necessary to support the forthcoming Qukeys tap-repeat feature.

Signed-off-by: Michael Richters <gedankenexperimenter@gmail.com>
pull/1024/head
Michael Richters 4 years ago
parent 0db11e3e66
commit d97221c5a1
No known key found for this signature in database
GPG Key ID: 1288FD13E4EEF0C0

@ -1,6 +1,6 @@
// -*- mode: c++ -*- // -*- mode: c++ -*-
/* Kaleidoscope - Firmware for computer input devices /* Kaleidoscope - Firmware for computer input devices
* Copyright (C) 2013-2019 Keyboard.io, Inc. * Copyright (C) 2013-2020 Keyboard.io, Inc.
* *
* This program is free software: you can redistribute it and/or modify it under * This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software * the terms of the GNU General Public License as published by the Free Software
@ -90,18 +90,31 @@ class KeyAddrEventQueue {
++length_; ++length_;
} }
// Remove the first event from the head of the queue, shifting the // Remove an event from the head of the queue, shifting the subsequent
// others. This function actually shifts the queue by copying element values, // ones. This function actually shifts the queue by copying element values,
// rather than using a ring buffer because we expect it will be called much // rather than using a ring buffer because we expect it will be called much
// less often than the queue is searched via a for loop. // less often than the queue is searched via a for loop.
void shift() { void remove(uint8_t n = 0) {
// assert(length > 0); // assert(length > n);
--length_; --length_;
for (uint8_t i{0}; i < length_; ++i) { for (uint8_t i{n}; i < length_; ++i) {
addrs_[i] = addrs_[i + 1]; addrs_[i] = addrs_[i + 1];
timestamps_[i] = timestamps_[i + 1]; timestamps_[i] = timestamps_[i + 1];
} }
// mask = all ones for bits >= n, zeros otherwise
_Bitfield mask = _Bitfield(~0) << n;
// use the inverse mask to get just the low bits (that won't be shifted)
_Bitfield low_bits = release_event_bits_ & ~mask;
// shift the event bits
release_event_bits_ >>= 1; release_event_bits_ >>= 1;
// use the mask to zero the low bits, leaving only the shifted high bits
release_event_bits_ &= mask;
// add the low bits back in
release_event_bits_ |= low_bits;
}
void shift() {
remove(0);
} }
void shift(uint8_t n) { void shift(uint8_t n) {

Loading…
Cancel
Save