From 92f53711d1038551a641918e1b806f6cc29d2f33 Mon Sep 17 00:00:00 2001 From: Florian Fleissner Date: Mon, 23 Sep 2019 10:39:22 +0200 Subject: [PATCH] Fixes problems with ATMegaKeyboard and KeyAddr The correct KeyAddr type is not known to class ATMegaKeyboard as key matrix dimentions (matrix_rows/matrix_columns) and type KeyAddr are only defined in derived hardware classes. To deal with this problem, some of the KeyAddr related methods are moved to derived hardware classes. The necessary boilerplate code is synthesized through a macro ATMEGA_KEYBOARD_MATRIX_ACCESS_METHODS that is automatically included by all derived classes of class ATMegaKeyboard through the already used macro ATMEGA_KEYBOARD_CONFIG. Signed-off-by: Florian Fleissner --- src/kaleidoscope/Hardware.h | 13 ++- src/kaleidoscope/hardware/ATMegaKeyboard.cpp | 42 ---------- src/kaleidoscope/hardware/ATMegaKeyboard.h | 87 ++++++++++++++------ 3 files changed, 73 insertions(+), 69 deletions(-) diff --git a/src/kaleidoscope/Hardware.h b/src/kaleidoscope/Hardware.h index c33cad63..895cc2e8 100644 --- a/src/kaleidoscope/Hardware.h +++ b/src/kaleidoscope/Hardware.h @@ -46,13 +46,20 @@ namespace kaleidoscope { /** Kaleidoscope Hardware base class. * Essential methods all hardware libraries must implement. */ + +struct NoopKeyAddr { + NoopKeyAddr() {} + template + NoopKeyAddr(T_) {} +}; + class Hardware { public: // To satisfy the interface of those methods that allow - // for matrix addressing we define default key and led address classes. - // Those typedefs are supposed to overridden by derived hardware classes. - typedef MatrixAddr<0, 0> KeyAddr; + // for matrix addressing we define a default key address class. + // This typedef is supposed to overridden by derived hardware classes. + typedef NoopKeyAddr KeyAddr; /** * @defgroup kaleidoscope_hardware_leds Kaleidoscope::Hardware/LEDs diff --git a/src/kaleidoscope/hardware/ATMegaKeyboard.cpp b/src/kaleidoscope/hardware/ATMegaKeyboard.cpp index 85b3a292..dcfe4681 100644 --- a/src/kaleidoscope/hardware/ATMegaKeyboard.cpp +++ b/src/kaleidoscope/hardware/ATMegaKeyboard.cpp @@ -74,16 +74,6 @@ uint8_t ATMegaKeyboard::pressedKeyswitchCount() { return count; } -bool ATMegaKeyboard::isKeyswitchPressed(KeyAddr key_addr) { - return (bitRead(KeyboardHardware.keyState_[key_addr.row()], key_addr.col()) != 0); -} - -bool ATMegaKeyboard::isKeyswitchPressed(uint8_t keyIndex) { - keyIndex--; - return isKeyswitchPressed(::KeyAddr(keyIndex)); -} - - uint8_t ATMegaKeyboard::previousPressedKeyswitchCount() { uint8_t count = 0; @@ -93,17 +83,6 @@ uint8_t ATMegaKeyboard::previousPressedKeyswitchCount() { return count; } -bool ATMegaKeyboard::wasKeyswitchPressed(KeyAddr key_addr) { - return (bitRead(KeyboardHardware.previousKeyState_[key_addr.row()], key_addr.col()) != 0); - -} - -bool ATMegaKeyboard::wasKeyswitchPressed(uint8_t keyIndex) { - keyIndex--; - return wasKeyswitchPressed(::KeyAddr(keyIndex)); -} - - void __attribute__((optimize(3))) ATMegaKeyboard::actOnMatrixScan() { for (byte row = 0; row < KeyboardHardware.matrix_rows; row++) { for (byte col = 0; col < KeyboardHardware.matrix_columns; col++) { @@ -127,27 +106,6 @@ void ATMegaKeyboard::scanMatrix() { KeyboardHardware.actOnMatrixScan(); } -void ATMegaKeyboard::maskKey(KeyAddr key_addr) { - if (!key_addr.isValid()) - return; - - bitWrite(KeyboardHardware.masks_[key_addr.row()], key_addr.col(), 1); -} - -void ATMegaKeyboard::unMaskKey(KeyAddr key_addr) { - if (!key_addr.isValid()) - return; - - bitWrite(KeyboardHardware.masks_[key_addr.row()], key_addr.col(), 0); -} - -bool ATMegaKeyboard::isKeyMasked(KeyAddr key_addr) { - if (!key_addr.isValid()) - return false; - - return bitRead(KeyboardHardware.masks_[key_addr.row()], key_addr.col()); -} - /* * This function has loop unrolling disabled on purpose: we want to give the * hardware enough time to produce stable PIN reads for us. If we unroll the diff --git a/src/kaleidoscope/hardware/ATMegaKeyboard.h b/src/kaleidoscope/hardware/ATMegaKeyboard.h index e4874146..cba751c0 100644 --- a/src/kaleidoscope/hardware/ATMegaKeyboard.h +++ b/src/kaleidoscope/hardware/ATMegaKeyboard.h @@ -45,6 +45,66 @@ struct cRGB { #define ROW_PIN_LIST(...) __VA_ARGS__ #define COL_PIN_LIST(...) __VA_ARGS__ +// By implementing all KeyAddr based access methods via macros in +// the derived hardware classes, we deal with the problem that +// keyboard matrix dimension (matrix_rows/matrix_columns) +// and thus type KeyAddr is only known to the derived hardware classes +// but not to ATMegaKeyboard. +// +#define ATMEGA_KEYBOARD_MATRIX_ACCESS_METHODS \ + bool isKeyswitchPressed(KeyAddr key_addr) { \ + return (bitRead(keyState_[key_addr.row()], \ + key_addr.col()) != 0); \ + } \ + DEPRECATED(ROW_COL_FUNC) bool isKeyswitchPressed(uint8_t row, byte col)\ + { \ + return isKeyswitchPressed(KeyAddr(row, col)); \ + } \ + bool isKeyswitchPressed(uint8_t keyIndex) { \ + keyIndex--; \ + return isKeyswitchPressed(KeyAddr(keyIndex)); \ + } \ + bool wasKeyswitchPressed(KeyAddr key_addr) { \ + return (bitRead(previousKeyState_[key_addr.row()], \ + key_addr.col()) != 0); \ + } \ + DEPRECATED(ROW_COL_FUNC) bool wasKeyswitchPressed(uint8_t row, byte col)\ + { \ + return wasKeyswitchPressed(KeyAddr(row, col)); \ + } \ + bool wasKeyswitchPressed(uint8_t keyIndex) { \ + keyIndex--; \ + return wasKeyswitchPressed(KeyAddr(keyIndex)); \ + } \ + void maskKey(KeyAddr key_addr) { \ + if (!key_addr.isValid()) \ + return; \ + \ + bitWrite(masks_[key_addr.row()], key_addr.col(), 1); \ + } \ + DEPRECATED(ROW_COL_FUNC) void maskKey(byte row, byte col) { \ + maskKey(KeyAddr(row, col)); \ + } \ + void unMaskKey(KeyAddr key_addr) { \ + if (!key_addr.isValid()) \ + return; \ + \ + bitWrite(masks_[key_addr.row()], key_addr.col(), 0); \ + } \ + DEPRECATED(ROW_COL_FUNC) void unMaskKey(byte row, byte col) { \ + unMaskKey(KeyAddr(row, col)); \ + } \ + bool isKeyMasked(KeyAddr key_addr) { \ + if (!key_addr.isValid()) \ + return false; \ + \ + return bitRead(masks_[key_addr.row()], \ + key_addr.col()); \ + } \ + DEPRECATED(ROW_COL_FUNC) bool isKeyMasked(byte row, byte col) { \ + return isKeyMasked(KeyAddr(row, col)); \ + } + #define ATMEGA_KEYBOARD_CONFIG(ROW_PINS_, COL_PINS_) \ static const int8_t matrix_rows = NUM_ARGS(ROW_PINS_); \ static const int8_t matrix_columns = NUM_ARGS(COL_PINS_); \ @@ -55,7 +115,9 @@ struct cRGB { static uint16_t previousKeyState_[matrix_rows]; \ static uint16_t keyState_[matrix_rows]; \ static uint16_t masks_[matrix_rows]; \ - static uint8_t debounce_matrix_[matrix_rows][matrix_columns]; + static uint8_t debounce_matrix_[matrix_rows][matrix_columns]; \ + \ + ATMEGA_KEYBOARD_MATRIX_ACCESS_METHODS #define ATMEGA_KEYBOARD_DATA(BOARD) \ const int8_t BOARD::matrix_rows; \ @@ -85,31 +147,8 @@ class ATMegaKeyboard : public kaleidoscope::Hardware { void scanMatrix(); uint8_t pressedKeyswitchCount(); - bool isKeyswitchPressed(KeyAddr key_addr); - DEPRECATED(ROW_COL_FUNC) bool isKeyswitchPressed(uint8_t row, byte col) { - return isKeyswitchPressed(KeyAddr(row, col)); - } - bool isKeyswitchPressed(uint8_t keyIndex); uint8_t previousPressedKeyswitchCount(); - bool wasKeyswitchPressed(KeyAddr key_addr); - DEPRECATED(ROW_COL_FUNC) bool wasKeyswitchPressed(uint8_t row, byte col) { - return wasKeyswitchPressed(KeyAddr(row, col)); - } - bool wasKeyswitchPressed(uint8_t keyIndex); - - void maskKey(KeyAddr key_addr); - DEPRECATED(ROW_COL_FUNC) void maskKey(byte row, byte col) { - maskKey(KeyAddr(row, col)); - } - void unMaskKey(KeyAddr key_addr); - DEPRECATED(ROW_COL_FUNC) void unMaskKey(byte row, byte col) { - unMaskKey(KeyAddr(row, col)); - } - bool isKeyMasked(KeyAddr key_addr); - DEPRECATED(ROW_COL_FUNC) bool isKeyMasked(byte row, byte col) { - return isKeyMasked(KeyAddr(row, col)); - } static bool do_scan_;