keyscanner::ATmega: Merge the static props into a struct

This makes it easier to initialize them in the cpp (shorter too!), and reduces
code size as well. It's also a bit simpler to understand the initialization
part, because it's no different from the props init.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/877/head
Gergely Nagy 4 years ago
parent 985facf111
commit adb93ed152
No known key found for this signature in database
GPG Key ID: AC1E90BAC433F68F

@ -43,15 +43,9 @@ const uint8_t KeyScannerProps::matrix_columns;
constexpr uint8_t KeyScannerProps::matrix_row_pins[matrix_rows];
constexpr uint8_t KeyScannerProps::matrix_col_pins[matrix_columns];
// Resolving is a bit different in case of templates, however: the name of the
// array is resolved within the scope of the namespace and the class, but the
// array size is not - because it is a template. Therefore, we need a fully
// qualified name there - or an alias in the global scope, which we set up just
// above.
template<> uint16_t KeyScanner::previousKeyState_[KeyScannerProps::matrix_rows] = {};
template<> uint16_t KeyScanner::keyState_[KeyScannerProps::matrix_rows] = {};
template<> uint16_t KeyScanner::masks_[KeyScannerProps::matrix_rows] = {};
template<> KeyScanner::debounce_t KeyScanner::db[KeyScannerProps::matrix_rows] = {};
// `KeyScanner` here refers to the alias set up above, just like in the
// `KeyScannerProps` case above.
template<> KeyScanner::state_t KeyScanner::state_ = {};
// We set up the TIMER1 interrupt vector here. Due to dependency reasons, this
// cannot be in a header-only driver, and must be placed here.

@ -116,11 +116,11 @@ class ATmega: public kaleidoscope::driver::keyscanner::Base<_KeyScannerProps> {
OUTPUT_TOGGLE(_KeyScannerProps::matrix_row_pins[current_row]);
OUTPUT_TOGGLE(_KeyScannerProps::matrix_row_pins[(current_row + 1) % _KeyScannerProps::matrix_rows]);
any_debounced_changes |= debounce(hot_pins, db + current_row);
any_debounced_changes |= debounce(hot_pins, state_.db + current_row);
if (any_debounced_changes) {
for (uint8_t current_row = 0; current_row < _KeyScannerProps::matrix_rows; current_row++) {
keyState_[current_row] = db[current_row].state;
state_.keyState[current_row] = state_.db[current_row].state;
}
}
}
@ -136,12 +136,12 @@ class ATmega: public kaleidoscope::driver::keyscanner::Base<_KeyScannerProps> {
void __attribute__((optimize(3))) actOnMatrixScan() {
for (byte row = 0; row < _KeyScannerProps::matrix_rows; row++) {
for (byte col = 0; col < _KeyScannerProps::matrix_columns; col++) {
uint8_t keyState = (bitRead(previousKeyState_[row], col) << 0) | (bitRead(keyState_[row], col) << 1);
uint8_t keyState = (bitRead(state_.previousKeyState[row], col) << 0) | (bitRead(state_.keyState[row], col) << 1);
if (keyState) {
ThisType::handleKeyswitchEvent(Key_NoKey, typename _KeyScannerProps::KeyAddr(row, col), keyState);
}
}
previousKeyState_[row] = keyState_[row];
state_.previousKeyState[row] = state_.keyState[row];
}
}
@ -149,24 +149,24 @@ class ATmega: public kaleidoscope::driver::keyscanner::Base<_KeyScannerProps> {
uint8_t count = 0;
for (int8_t r = 0; r < _KeyScannerProps::matrix_rows; r++) {
count += __builtin_popcount(keyState_[r]);
count += __builtin_popcount(state_.keyState[r]);
}
return count;
}
bool isKeyswitchPressed(typename _KeyScannerProps::KeyAddr key_addr) {
return (bitRead(keyState_[key_addr.row()], key_addr.col()) != 0);
return (bitRead(state_.keyState[key_addr.row()], key_addr.col()) != 0);
}
uint8_t previousPressedKeyswitchCount() {
uint8_t count = 0;
for (int8_t r = 0; r < _KeyScannerProps::matrix_rows; r++) {
count += __builtin_popcount(previousKeyState_[r]);
count += __builtin_popcount(state_.previousKeyState[r]);
}
return count;
}
bool wasKeyswitchPressed(typename _KeyScannerProps::KeyAddr key_addr) {
return (bitRead(previousKeyState_[key_addr.row()],
return (bitRead(state_.previousKeyState[key_addr.row()],
key_addr.col()) != 0);
}
@ -174,19 +174,19 @@ class ATmega: public kaleidoscope::driver::keyscanner::Base<_KeyScannerProps> {
if (!key_addr.isValid())
return;
bitWrite(masks_[key_addr.row()], key_addr.col(), 1);
bitWrite(state_.masks[key_addr.row()], key_addr.col(), 1);
}
void unMaskKey(typename _KeyScannerProps::KeyAddr key_addr) {
if (!key_addr.isValid())
return;
bitWrite(masks_[key_addr.row()], key_addr.col(), 0);
bitWrite(state_.masks[key_addr.row()], key_addr.col(), 0);
}
bool isKeyMasked(typename _KeyScannerProps::KeyAddr key_addr) {
if (!key_addr.isValid())
return false;
return bitRead(masks_[key_addr.row()],
return bitRead(state_.masks[key_addr.row()],
key_addr.col());
}
@ -206,13 +206,16 @@ class ATmega: public kaleidoscope::driver::keyscanner::Base<_KeyScannerProps> {
uint8_t state; // debounced state
};
struct state_t {
uint16_t previousKeyState[_KeyScannerProps::matrix_rows];
uint16_t keyState[_KeyScannerProps::matrix_rows];
uint16_t masks[_KeyScannerProps::matrix_rows];
debounce_t db[_KeyScannerProps::matrix_rows];
};
private:
typedef _KeyScannerProps KeyScannerProps_;
static uint16_t previousKeyState_[_KeyScannerProps::matrix_rows];
static uint16_t keyState_[_KeyScannerProps::matrix_rows];
static uint16_t masks_[_KeyScannerProps::matrix_rows];
static debounce_t db[_KeyScannerProps::matrix_rows];
static state_t state_;
static inline uint8_t debounce(uint8_t sample, debounce_t *debouncer) {
uint8_t delta, changes;

Loading…
Cancel
Save