CharShift: Transition to using a function pointer instead of overridables

Instead of using overridable functions, use function pointers (+ setters) to
allow changing lookups and related things. This allows a bit more flexibility,
and makes the user sketch simpler, because there is no override necessary there.

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

@ -34,6 +34,10 @@ namespace plugin {
// CharShift class variables
CharShift::KeyPair const * CharShift::progmem_keypairs_{nullptr};
uint8_t CharShift::num_keypairs_{0};
CharShift::GetNumKeyPairsFunction CharShift::numKeyPairs_ =
CharShift::numProgmemKeyPairs;
CharShift::ReadKeyPairFunction CharShift::readKeyPair_ =
CharShift::readKeyPairFromProgmem;
bool CharShift::reverse_shift_state_{false};
@ -117,24 +121,12 @@ bool CharShift::isCharShiftKey(Key key) {
CharShift::KeyPair CharShift::decodeCharShiftKey(Key key) {
uint8_t i = key.getRaw() - ranges::CS_FIRST;
if (i < numKeyPairs()) {
return readKeyPair(i);
if (i < numKeyPairs_()) {
return readKeyPair_(i);
}
return {Key_NoKey, Key_NoKey};
}
// This should be overridden if the KeyPairs array is stored in EEPROM
__attribute__((weak))
uint8_t CharShift::numKeyPairs() {
return numProgmemKeyPairs();
}
// This should be overridden if the KeyPairs array is stored in EEPROM
__attribute__((weak))
CharShift::KeyPair CharShift::readKeyPair(uint8_t n) {
return readKeyPairFromProgmem(n);
}
uint8_t CharShift::numProgmemKeyPairs() {
return num_keypairs_;
}

@ -76,12 +76,25 @@ class CharShift : public Plugin {
num_keypairs_ = _num_keypairs;
}
typedef uint8_t (*GetNumKeyPairsFunction)();
typedef KeyPair (*ReadKeyPairFunction)(uint8_t n);
static void setNumKeyPairsFunction(GetNumKeyPairsFunction f) {
numKeyPairs_ = f;
}
static void setReadKeyPairFunction(ReadKeyPairFunction f) {
readKeyPair_ = f;
}
private:
// A pointer to an array of `KeyPair` objects in PROGMEM
static KeyPair const * progmem_keypairs_;
// The size of the PROGMEM array of `KeyPair` objects
static uint8_t num_keypairs_;
static GetNumKeyPairsFunction numKeyPairs_;
static ReadKeyPairFunction readKeyPair_;
// If a `shift` key needs to be suppressed in `beforeReportingState()`
static bool reverse_shift_state_;
@ -91,18 +104,6 @@ class CharShift : public Plugin {
/// Look up the `KeyPair` specified by the given keymap entry
static KeyPair decodeCharShiftKey(Key key);
/// Get the total number of KeyPairs defined
///
/// This function can be overridden in order to store the `KeyPair` array in
/// EEPROM instead of PROGMEM.
static uint8_t numKeyPairs();
/// Get the `KeyPair` at the specified index from the defined `KeyPair` array
///
/// This function can be overridden in order to store the `KeyPair` array in
/// EEPROM instead of PROGMEM.
static KeyPair readKeyPair(uint8_t n);
// Default for `keypairsCount()`: size of the PROGMEM array
static uint8_t numProgmemKeyPairs();
// Default for `readKeypair(i)`: fetch the value from PROGMEM

Loading…
Cancel
Save