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 <florian.fleissner@inpartik.de>
pull/690/head
Florian Fleissner 5 years ago
parent f8fa47610b
commit 92f53711d1

@ -46,13 +46,20 @@ namespace kaleidoscope {
/** Kaleidoscope Hardware base class. /** Kaleidoscope Hardware base class.
* Essential methods all hardware libraries must implement. * Essential methods all hardware libraries must implement.
*/ */
struct NoopKeyAddr {
NoopKeyAddr() {}
template<typename T_>
NoopKeyAddr(T_) {}
};
class Hardware { class Hardware {
public: public:
// To satisfy the interface of those methods that allow // To satisfy the interface of those methods that allow
// for matrix addressing we define default key and led address classes. // for matrix addressing we define a default key address class.
// Those typedefs are supposed to overridden by derived hardware classes. // This typedef is supposed to overridden by derived hardware classes.
typedef MatrixAddr<0, 0> KeyAddr; typedef NoopKeyAddr KeyAddr;
/** /**
* @defgroup kaleidoscope_hardware_leds Kaleidoscope::Hardware/LEDs * @defgroup kaleidoscope_hardware_leds Kaleidoscope::Hardware/LEDs

@ -74,16 +74,6 @@ uint8_t ATMegaKeyboard::pressedKeyswitchCount() {
return count; 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 ATMegaKeyboard::previousPressedKeyswitchCount() {
uint8_t count = 0; uint8_t count = 0;
@ -93,17 +83,6 @@ uint8_t ATMegaKeyboard::previousPressedKeyswitchCount() {
return count; 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() { void __attribute__((optimize(3))) ATMegaKeyboard::actOnMatrixScan() {
for (byte row = 0; row < KeyboardHardware.matrix_rows; row++) { for (byte row = 0; row < KeyboardHardware.matrix_rows; row++) {
for (byte col = 0; col < KeyboardHardware.matrix_columns; col++) { for (byte col = 0; col < KeyboardHardware.matrix_columns; col++) {
@ -127,27 +106,6 @@ void ATMegaKeyboard::scanMatrix() {
KeyboardHardware.actOnMatrixScan(); 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 * 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 * hardware enough time to produce stable PIN reads for us. If we unroll the

@ -45,6 +45,66 @@ struct cRGB {
#define ROW_PIN_LIST(...) __VA_ARGS__ #define ROW_PIN_LIST(...) __VA_ARGS__
#define COL_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_) \ #define ATMEGA_KEYBOARD_CONFIG(ROW_PINS_, COL_PINS_) \
static const int8_t matrix_rows = NUM_ARGS(ROW_PINS_); \ static const int8_t matrix_rows = NUM_ARGS(ROW_PINS_); \
static const int8_t matrix_columns = NUM_ARGS(COL_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 previousKeyState_[matrix_rows]; \
static uint16_t keyState_[matrix_rows]; \ static uint16_t keyState_[matrix_rows]; \
static uint16_t masks_[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) \ #define ATMEGA_KEYBOARD_DATA(BOARD) \
const int8_t BOARD::matrix_rows; \ const int8_t BOARD::matrix_rows; \
@ -85,31 +147,8 @@ class ATMegaKeyboard : public kaleidoscope::Hardware {
void scanMatrix(); void scanMatrix();
uint8_t pressedKeyswitchCount(); 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(); 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_; static bool do_scan_;

Loading…
Cancel
Save