Compare commits

...

4 Commits

Author SHA1 Message Date
Gergely Nagy 0cc060e4a4
wip: focus fix
3 years ago
Gergely Nagy ee36ed6929
CharShift: Migrate to a pluggable storage sub-class
3 years ago
Gergely Nagy 7d6ce8bf45
CharShift: WIP CharShiftConfig
3 years ago
Gergely Nagy f916c2c0d4
CharShift: Transition to using a function pointer instead of overridables
3 years ago

@ -32,19 +32,27 @@ namespace plugin {
// ============================================================================= // =============================================================================
// CharShift class variables // CharShift class variables
CharShift::KeyPair const * CharShift::progmem_keypairs_{nullptr}; charshift::Storage * CharShift::storage_;
uint8_t CharShift::num_keypairs_{0};
bool CharShift::reverse_shift_state_{false}; bool CharShift::reverse_shift_state_{false};
static auto progmem_storage_ = charshift::ProgmemStorage();
// ============================================================================= // =============================================================================
// Event handlers // Event handlers
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
EventHandlerResult CharShift::onSetup() {
if (!storage_) {
storage_ = &progmem_storage_;
}
return EventHandlerResult::OK;
}
EventHandlerResult CharShift::onNameQuery() { EventHandlerResult CharShift::onNameQuery() {
return ::Focus.sendName(F("CharShift")); return ::Focus.sendName(F("CharShift"));
} }
EventHandlerResult CharShift::onFocusEvent(const char *command) {
return storage_->onFocusEvent(command);
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
EventHandlerResult CharShift::onKeyEvent(KeyEvent &event) { EventHandlerResult CharShift::onKeyEvent(KeyEvent &event) {
@ -117,32 +125,12 @@ bool CharShift::isCharShiftKey(Key key) {
CharShift::KeyPair CharShift::decodeCharShiftKey(Key key) { CharShift::KeyPair CharShift::decodeCharShiftKey(Key key) {
uint8_t i = key.getRaw() - ranges::CS_FIRST; uint8_t i = key.getRaw() - ranges::CS_FIRST;
if (i < numKeyPairs()) { if (i < storage_->numKeyPairs()) {
return readKeyPair(i); return storage_->readKeyPair(i);
} }
return {Key_NoKey, Key_NoKey}; 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_;
}
CharShift::KeyPair CharShift::readKeyPairFromProgmem(uint8_t n) {
return cloneFromProgmem(progmem_keypairs_[n]);
}
} // namespace plugin } // namespace plugin
} // namespace kaleidoscope } // namespace kaleidoscope

@ -24,6 +24,10 @@
namespace kaleidoscope { namespace kaleidoscope {
namespace plugin { namespace plugin {
namespace charshift {
class Storage;
}
// ============================================================================= // =============================================================================
/// Kaleidoscope plugin for independently assigning shifted symbols /// Kaleidoscope plugin for independently assigning shifted symbols
/// ///
@ -36,9 +40,11 @@ namespace plugin {
class CharShift : public Plugin { class CharShift : public Plugin {
public: public:
EventHandlerResult onSetup();
EventHandlerResult onNameQuery(); EventHandlerResult onNameQuery();
EventHandlerResult onKeyEvent(KeyEvent &event); EventHandlerResult onKeyEvent(KeyEvent &event);
EventHandlerResult beforeReportingState(const KeyEvent &event); EventHandlerResult beforeReportingState(const KeyEvent &event);
EventHandlerResult onFocusEvent(const char *command);
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
/// A structure that stores CharShift key pair values /// A structure that stores CharShift key pair values
@ -63,24 +69,15 @@ class CharShift : public Plugin {
KeyPair() = default; KeyPair() = default;
}; };
/// Configure the KeyPairs array in PROGMEM charshift::Storage &storage() {
/// return *storage_;
/// This function configures the PROGMEM array of `KeyPair` objects, }
/// automatically setting the internal count variable from the size of the void setStorage(charshift::Storage *st) {
/// `keypairs` array given, which must be a fixed-sized array, not a pointer. storage_ = st;
/// 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;
} }
private: private:
// A pointer to an array of `KeyPair` objects in PROGMEM static charshift::Storage *storage_;
static KeyPair const * progmem_keypairs_;
// The size of the PROGMEM array of `KeyPair` objects
static uint8_t num_keypairs_;
// If a `shift` key needs to be suppressed in `beforeReportingState()` // If a `shift` key needs to be suppressed in `beforeReportingState()`
static bool reverse_shift_state_; static bool reverse_shift_state_;
@ -90,25 +87,64 @@ class CharShift : public Plugin {
/// Look up the `KeyPair` specified by the given keymap entry /// Look up the `KeyPair` specified by the given keymap entry
static KeyPair decodeCharShiftKey(Key key); static KeyPair decodeCharShiftKey(Key key);
};
/// Get the total number of KeyPairs defined namespace charshift {
///
/// This function can be overridden in order to store the `KeyPair` array in class Storage {
/// EEPROM instead of PROGMEM. public:
static uint8_t numKeyPairs(); Storage() {}
static uint8_t numKeyPairs() {
return num_keypairs_;
}
static CharShift::KeyPair readKeyPair(uint8_t n) {
return CharShift::KeyPair(Key_NoKey, Key_NoKey);
}
EventHandlerResult onFocusEvent(const char *command) {
return EventHandlerResult::OK;
}
protected:
static uint8_t num_keypairs_;
};
class ProgmemStorage: public Storage {
public:
static CharShift::KeyPair readKeyPair(uint8_t n);
/// Get the `KeyPair` at the specified index from the defined `KeyPair` array /// Configure the KeyPairs array in PROGMEM
/// ///
/// This function can be overridden in order to store the `KeyPair` array in /// This function configures the PROGMEM array of `KeyPair` objects,
/// EEPROM instead of PROGMEM. /// automatically setting the internal count variable from the size of the
static KeyPair readKeyPair(uint8_t n); /// `keypairs` array given, which must be a fixed-sized array, not a pointer.
/// Generally, it will be called via the `KEYPAIRS()` preprocessor macro, not
// Default for `keypairsCount()`: size of the PROGMEM array /// directly by user code.
static uint8_t numProgmemKeyPairs(); template <uint8_t _num_keypairs>
// Default for `readKeypair(i)`: fetch the value from PROGMEM static void setKeyPairs(CharShift::KeyPair const(&keypairs)[_num_keypairs]) {
static KeyPair readKeyPairFromProgmem(uint8_t n); keypairs_ = keypairs;
num_keypairs_ = _num_keypairs;
}
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 plugin
} // namespace kaleidoscope } // namespace kaleidoscope
@ -124,7 +160,7 @@ extern kaleidoscope::plugin::CharShift CharShift;
static kaleidoscope::plugin::CharShift::KeyPair const kp_table[] PROGMEM = { \ static kaleidoscope::plugin::CharShift::KeyPair const kp_table[] PROGMEM = { \
keypairs \ keypairs \
}; \ }; \
CharShift.setProgmemKeyPairs(kp_table); \ CharShift.storage().setKeyPairs(kp_table); \
} }
/// Define an `KeyPair` entry in a keymap /// Define an `KeyPair` entry in a keymap

@ -0,0 +1,107 @@
/* -*- mode: c++ -*-
* Kaleidoscope-CharShift -- Independently assign shifted and unshifted symbols
* Copyright (C) 2021 Keyboard.io, Inc
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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 {
// =============================================================================
// Storage class variables
uint8_t Storage::num_keypairs_;
CharShift::KeyPair const * ProgmemStorage::keypairs_{nullptr};
uint16_t EEPROMStorage::storage_base_;
// =============================================================================
// Progmem Storage
CharShift::KeyPair ProgmemStorage::readKeyPair(uint8_t n) {
return cloneFromProgmem(keypairs_[n]);
}
// =============================================================================
// EEPROM Storage
EventHandlerResult EEPROMStorage::onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("charshift.map")))
return EventHandlerResult::OK;
if (strcmp_P(command, PSTR("charshift.map")) != 0)
return EventHandlerResult::OK;
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 < num_keypairs_ * 2; i += 2) {
Key k;
Runtime.storage().get(storage_base_ + i, k);
::Focus.send(k);
}
} else {
uint16_t pos = 0;
// We read one key at a time, rather than a keypair, to better handle
// partials and failure, and to make the code simpler.
while (!::Focus.isEOL()) {
Key k;
::Focus.read(k);
Runtime.storage().put(storage_base_ + pos, k);
pos += 2;
}
Runtime.storage().commit();
}
return EventHandlerResult::EVENT_CONSUMED;
}
void EEPROMStorage::setup(uint8_t num_keypairs) {
num_keypairs_ = num_keypairs;
storage_base_ = ::EEPROMSettings.requestSlice(num_keypairs * 4);
}
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);
return CharShift::KeyPair(Key(raw_lower), Key(raw_upper));
}
}
} // namespace plugin
} // namespace kaleidoscope
Loading…
Cancel
Save