From c69efc81035c276fa478bb3d68512396184a2f07 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Tue, 30 Jun 2020 11:57:01 +0200 Subject: [PATCH] keyboardio/Imago: Stop using opaque macros Instead of using opaque macros which aren't even extensible, just expand them, and use the raw data structures for the hardware definition. While this is more verbose than the macros, it is more future proof, and clearer for the reader too, because they don't need to understand the magic macros. Signed-off-by: Gergely Nagy --- src/kaleidoscope/device/keyboardio/Imago.cpp | 38 +++++++++++++++++++- src/kaleidoscope/device/keyboardio/Imago.h | 11 +++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/kaleidoscope/device/keyboardio/Imago.cpp b/src/kaleidoscope/device/keyboardio/Imago.cpp index 35d1c019..789a8964 100644 --- a/src/kaleidoscope/device/keyboardio/Imago.cpp +++ b/src/kaleidoscope/device/keyboardio/Imago.cpp @@ -28,6 +28,12 @@ extern "C" { #define LED_DRIVER_ADDR 0x30 +// Here, we set up aliases to the device's KeyScanner and KeyScannerProps +// in the global namespace within the scope of this file. We'll use these +// aliases to simplify some template initialization code below. +using KeyScannerProps = typename kaleidoscope::device::keyboardio::ImagoProps::KeyScannerProps; +using KeyScanner = typename kaleidoscope::device::keyboardio::ImagoProps::KeyScanner; + namespace kaleidoscope { namespace device { namespace keyboardio { @@ -53,7 +59,37 @@ static constexpr uint8_t LED_REGISTER_DATA1_SIZE = 0xAB; static constexpr uint8_t LED_REGISTER_DATA_LARGEST = LED_REGISTER_DATA0_SIZE; -ATMEGA_KEYSCANNER_BOILERPLATE +// `KeyScannerProps` here refers to the alias set up above. We do not need to +// prefix the `matrix_rows` and `matrix_columns` names within the array +// declaration, because those are resolved within the context of the class, so +// the `matrix_rows` in `KeyScannerProps::matrix_row_pins[matrix_rows]` gets +// resolved as `KeyScannerProps::matrix_rows`. +const uint8_t KeyScannerProps::matrix_rows; +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<> uint8_t KeyScanner::debounce_matrix_[KeyScannerProps::matrix_rows][KeyScannerProps::matrix_columns] = {}; + +// 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. +// +// Timer1 is responsible for setting a property on the KeyScanner, which will +// tell it to do a scan. We use this to make sure that scans happen at roughly +// the intervals we want. We do the scan outside of the interrupt scope for +// practical reasons: guarding every codepath against interrupts that can be +// reached from the scan is far too tedious, for very little gain. +ISR(TIMER1_OVF_vect) { + Runtime.device().keyScanner().do_scan_ = true; +} bool ImagoLEDDriver::isLEDChanged = true; cRGB ImagoLEDDriver::led_data[]; diff --git a/src/kaleidoscope/device/keyboardio/Imago.h b/src/kaleidoscope/device/keyboardio/Imago.h index ccf45943..99841fb2 100644 --- a/src/kaleidoscope/device/keyboardio/Imago.h +++ b/src/kaleidoscope/device/keyboardio/Imago.h @@ -85,10 +85,13 @@ class ImagoLEDDriver; struct ImagoProps : kaleidoscope::device::ATmega32U4KeyboardProps { struct KeyScannerProps : public kaleidoscope::driver::keyscanner::ATmegaProps { - ATMEGA_KEYSCANNER_PROPS( - ROW_PIN_LIST({ PIN_F6, PIN_F5, PIN_F4, PIN_F1, PIN_F0}), - COL_PIN_LIST({ PIN_B2, PIN_B7, PIN_E2, PIN_C7, PIN_C6, PIN_B6, PIN_B5, PIN_B4, PIN_D7, PIN_D6, PIN_D4, PIN_D5, PIN_D3, PIN_D2, PIN_E6, PIN_F7}) - ); + static constexpr uint8_t matrix_rows = 5; + static constexpr uint8_t matrix_columns = 16; + typedef MatrixAddr KeyAddr; +#ifndef KALEIDOSCOPE_VIRTUAL_BUILD + static constexpr uint8_t matrix_row_pins[matrix_rows] = {PIN_F6, PIN_F5, PIN_F4, PIN_F1, PIN_F0}; + static constexpr uint8_t matrix_col_pins[matrix_columns] = {PIN_B2, PIN_B7, PIN_E2, PIN_C7, PIN_C6, PIN_B6, PIN_B5, PIN_B4, PIN_D7, PIN_D6, PIN_D4, PIN_D5, PIN_D3, PIN_D2, PIN_E6, PIN_F7}; +#endif // ifndef KALEIDOSCOPE_VIRTUAL_BUILD }; typedef kaleidoscope::driver::keyscanner::ATmega KeyScanner; typedef ImagoLEDDriverProps LEDDriverProps;