CharShift: Migrate to a pluggable storage sub-class

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

@ -32,20 +32,21 @@ 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;
charshift::Storage * CharShift::storage_;
bool CharShift::reverse_shift_state_{false};
static auto progmem_storage_ = charshift::ProgmemStorage();
// =============================================================================
// Event handlers
// -----------------------------------------------------------------------------
EventHandlerResult CharShift::onSetup() {
if (!storage_) {
storage_ = &progmem_storage_;
}
return EventHandlerResult::OK;
}
EventHandlerResult CharShift::onNameQuery() {
return ::Focus.sendName(F("CharShift"));
}
@ -121,20 +122,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 < storage_->numKeyPairs()) {
return storage_->readKeyPair(i);
}
return {Key_NoKey, Key_NoKey};
}
uint8_t CharShift::numProgmemKeyPairs() {
return num_keypairs_;
}
CharShift::KeyPair CharShift::readKeyPairFromProgmem(uint8_t n) {
return cloneFromProgmem(progmem_keypairs_[n]);
}
} // namespace plugin
} // namespace kaleidoscope

@ -24,6 +24,10 @@
namespace kaleidoscope {
namespace plugin {
namespace charshift {
class Storage;
}
// =============================================================================
/// Kaleidoscope plugin for independently assigning shifted symbols
///
@ -36,9 +40,11 @@ namespace plugin {
class CharShift : public Plugin {
public:
EventHandlerResult onSetup();
EventHandlerResult onNameQuery();
EventHandlerResult onKeyEvent(KeyEvent &event);
EventHandlerResult beforeReportingState(const KeyEvent &event);
EventHandlerResult onFocusEvent(const char *command);
// ---------------------------------------------------------------------------
/// A structure that stores CharShift key pair values
@ -63,39 +69,15 @@ class CharShift : public Plugin {
KeyPair() = default;
};
/// Configure the KeyPairs array in PROGMEM
///
/// This function configures the PROGMEM array of `KeyPair` objects,
/// automatically setting the internal count variable from the size of the
/// `keypairs` array given, which must be a fixed-sized array, not a pointer.
/// Generally, it will be called via the `KEYPAIRS()` preprocessor macro, not
/// directly by user code.
template <uint8_t _num_keypairs>
static void setProgmemKeyPairs(KeyPair const(&keypairs)[_num_keypairs]) {
progmem_keypairs_ = keypairs;
num_keypairs_ = _num_keypairs;
charshift::Storage &storage() {
return *storage_;
}
typedef uint8_t (*GetNumKeyPairsFunction)();
typedef KeyPair (*ReadKeyPairFunction)(uint8_t n);
static void setNumKeyPairsFunction(GetNumKeyPairsFunction f) {
numKeyPairs_ = f;
void setStorage(charshift::Storage *st) {
storage_ = st;
}
static void setReadKeyPairFunction(ReadKeyPairFunction f) {
readKeyPair_ = f;
}
friend class CharShiftConfig;
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_;
static charshift::Storage *storage_;
// If a `shift` key needs to be suppressed in `beforeReportingState()`
static bool reverse_shift_state_;
@ -105,41 +87,68 @@ class CharShift : public Plugin {
/// Look up the `KeyPair` specified by the given keymap entry
static KeyPair decodeCharShiftKey(Key key);
// Default for `keypairsCount()`: size of the PROGMEM array
static uint8_t numProgmemKeyPairs();
// Default for `readKeypair(i)`: fetch the value from PROGMEM
static KeyPair readKeyPairFromProgmem(uint8_t n);
};
class CharShiftConfig: public Plugin {
namespace charshift {
class Storage {
public:
CharShiftConfig() {}
Storage() {}
EventHandlerResult onNameQuery();
EventHandlerResult onFocusEvent(const char *command);
static uint8_t numKeyPairs() {
return num_keypairs_;
}
static CharShift::KeyPair readKeyPair(uint8_t n) {
return CharShift::KeyPair(Key_NoKey, Key_NoKey);
}
static void setup(uint8_t dynamic_offset, uint8_t max_pairs);
EventHandlerResult onFocusEvent(const char *command) {
return EventHandlerResult::OK;
}
private:
static uint8_t max_pairs_;
static uint16_t storage_base_;
static uint8_t dynamic_offset_;
protected:
static uint8_t num_keypairs_;
};
class ProgmemStorage: public Storage {
public:
static CharShift::KeyPair readKeyPair(uint8_t n);
static uint8_t numEEPROMPairs() {
return max_pairs_;
/// Configure the KeyPairs array in PROGMEM
///
/// This function configures the PROGMEM array of `KeyPair` objects,
/// automatically setting the internal count variable from the size of the
/// `keypairs` array given, which must be a fixed-sized array, not a pointer.
/// Generally, it will be called via the `KEYPAIRS()` preprocessor macro, not
/// directly by user code.
template <uint8_t _num_keypairs>
static void setKeyPairs(CharShift::KeyPair const(&keypairs)[_num_keypairs]) {
keypairs_ = keypairs;
num_keypairs_ = _num_keypairs;
}
static CharShift::KeyPair readKeyPairFromEEPROM(uint8_t n);
static uint8_t numPairs();
private:
// A pointer to an array of `KeyPair` objects in PROGMEM
static CharShift::KeyPair const *keypairs_;
};
class EEPROMStorage: public Storage {
public:
void setup(uint8_t num_pairs);
static CharShift::KeyPair readKeyPair(uint8_t n);
EventHandlerResult onFocusEvent(const char *command);
private:
static uint16_t storage_base_;
};
}
} // namespace plugin
} // namespace kaleidoscope
extern kaleidoscope::plugin::CharShift CharShift;
extern kaleidoscope::plugin::CharShiftConfig CharShiftConfig;
/// Define an array of `KeyPair` objects in PROGMEM
///
@ -151,7 +160,7 @@ extern kaleidoscope::plugin::CharShiftConfig CharShiftConfig;
static kaleidoscope::plugin::CharShift::KeyPair const kp_table[] PROGMEM = { \
keypairs \
}; \
CharShift.setProgmemKeyPairs(kp_table); \
CharShift.storage().setKeyPairs(kp_table); \
}
/// Define an `KeyPair` entry in a keymap

@ -17,29 +17,42 @@
#include "kaleidoscope/plugin/CharShift.h"
#include <Kaleidoscope-Ranges.h>
#include <Kaleidoscope-FocusSerial.h>
#include <Kaleidoscope-EEPROM-Settings.h>
#include "kaleidoscope/KeyAddr.h"
#include "kaleidoscope/key_defs.h"
#include "kaleidoscope/KeyEvent.h"
#include "kaleidoscope/keyswitch_state.h"
#include "kaleidoscope/progmem_helpers.h"
#include "kaleidoscope/Runtime.h"
namespace kaleidoscope {
namespace plugin {
namespace charshift {
// =============================================================================
// CharShiftConfig class variables
// Storage class variables
uint8_t Storage::num_keypairs_;
CharShift::KeyPair const * ProgmemStorage::keypairs_{nullptr};
uint16_t EEPROMStorage::storage_base_;
uint8_t CharShiftConfig::max_pairs_;
uint8_t CharShiftConfig::dynamic_offset_;
uint16_t CharShiftConfig::storage_base_;
// =============================================================================
// Event handlers
// Progmem Storage
EventHandlerResult CharShiftConfig::onNameQuery() {
return ::Focus.sendName(F("CharShiftConfig"));
CharShift::KeyPair ProgmemStorage::readKeyPair(uint8_t n) {
return cloneFromProgmem(keypairs_[n]);
}
EventHandlerResult CharShiftConfig::onFocusEvent(const char *command) {
// =============================================================================
// EEPROM Storage
EventHandlerResult EEPROMStorage::onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("charshift.map")))
return EventHandlerResult::OK;
@ -49,7 +62,7 @@ EventHandlerResult CharShiftConfig::onFocusEvent(const char *command) {
if (::Focus.isEOL()) {
// We dump key by key, rather than pairs, because the end result is the
// same, and dumping one by one is less code.
for (uint16_t i = 0; i < max_pairs_ * 2; i += 2) {
for (uint16_t i = 0; i < num_keypairs_ * 2; i += 2) {
Key k;
Runtime.storage().get(storage_base_ + i, k);
@ -73,19 +86,13 @@ EventHandlerResult CharShiftConfig::onFocusEvent(const char *command) {
return EventHandlerResult::EVENT_CONSUMED;
}
// =============================================================================
// Support functions
void CharShiftConfig::setup(uint8_t dynamic_offset, uint8_t max_pairs) {
dynamic_offset_ = dynamic_offset;
max_pairs_ = max_pairs;
void EEPROMStorage::setup(uint8_t num_keypairs) {
num_keypairs_ = num_keypairs;
storage_base_ = ::EEPROMSettings.requestSlice(max_pairs * 4);
::CharShift.setNumKeyPairsFunction(numPairs);
::CharShift.setReadKeyPairFunction(readKeyPair);
storage_base_ = ::EEPROMSettings.requestSlice(num_keypairs * 4);
}
CharShift::KeyPair CharShiftConfig::readKeyPairFromEEPROM(uint8_t n) {
CharShift::KeyPair EEPROMStorage::readKeyPair(uint8_t n) {
uint16_t pos = storage_base_ + n * 4; // 4: Size of a keypair.
uint16_t raw_lower = Runtime.storage().read(pos);
uint16_t raw_upper = Runtime.storage().read(pos + 2);
@ -93,18 +100,8 @@ CharShift::KeyPair CharShiftConfig::readKeyPairFromEEPROM(uint8_t n) {
return CharShift::KeyPair(Key(raw_lower), Key(raw_upper));
}
uint8_t CharShiftConfig::numPairs() {
return CharShift::numProgmemKeyPairs() + numEEPROMPairs();
}
CharShift::KeyPair CharShiftConfig::readKeyPair(uint8_t n) {
if (n < dynamic_offset_) {
return CharShift::readKeyPairFromProgmem(n);
}
return readKeyPairFromEEPROM(n - dynamic_offset_);
}
} // namespace plugin
} // namespace kaleidoscope
kaleidoscope::plugin::CharShiftConfig CharShiftConfig;
Loading…
Cancel
Save