Merge pull request #1146 from keyboardio/eeprom/is-slice-empty

Add Storage.isSliceUninitialized(), and convert some of our plugins to use it.
pull/1143/head
Jesse Vincent 3 years ago committed by GitHub
commit f195690dd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -37,13 +37,11 @@ uint16_t AutoShiftConfig::settings_base_;
EventHandlerResult AutoShiftConfig::onSetup() { EventHandlerResult AutoShiftConfig::onSetup() {
settings_base_ = ::EEPROMSettings.requestSlice(sizeof(AutoShift::settings_)); settings_base_ = ::EEPROMSettings.requestSlice(sizeof(AutoShift::settings_));
uint32_t checker;
Runtime.storage().get(settings_base_, checker); if (Runtime.storage().isSliceUninitialized(
settings_base_,
// Check if we have an empty eeprom... sizeof(AutoShift::settings_))) {
if (checker == 0xffffffff) { // If our slice is uninitialized, set sensible defaults.
// ...if the eeprom was empty, store the default settings.
Runtime.storage().put(settings_base_, AutoShift::settings_); Runtime.storage().put(settings_base_, AutoShift::settings_);
Runtime.storage().commit(); Runtime.storage().commit();
} }

@ -34,13 +34,11 @@ uint16_t EscapeOneShotConfig::settings_base_;
EventHandlerResult EscapeOneShotConfig::onSetup() { EventHandlerResult EscapeOneShotConfig::onSetup() {
settings_base_ = ::EEPROMSettings.requestSlice(sizeof(EscapeOneShot::settings_)); settings_base_ = ::EEPROMSettings.requestSlice(sizeof(EscapeOneShot::settings_));
uint16_t checker;
Runtime.storage().get(settings_base_, checker); if (Runtime.storage().isSliceUninitialized(
settings_base_,
// Check if we have an empty eeprom... sizeof(EscapeOneShot::settings_))) {
if (checker == 0xffff) { // If our slice is uninitialized, set sensible defaults.
// ...if the eeprom was empty, store the default settings.
Runtime.storage().put(settings_base_, EscapeOneShot::settings_); Runtime.storage().put(settings_base_, EscapeOneShot::settings_);
Runtime.storage().commit(); Runtime.storage().commit();
} }

@ -121,11 +121,10 @@ EventHandlerResult TypingBreaks::onNameQuery() {
EventHandlerResult TypingBreaks::onSetup() { EventHandlerResult TypingBreaks::onSetup() {
settings_base_ = ::EEPROMSettings.requestSlice(sizeof(settings)); settings_base_ = ::EEPROMSettings.requestSlice(sizeof(settings));
// If idleTime is max, assume that EEPROM is uninitialized, and store the if (Runtime.storage().isSliceUninitialized(
// defaults. settings_base_,
uint32_t idle_time; sizeof(settings))) {
Runtime.storage().get(settings_base_, idle_time); // If our slice is uninitialized, set sensible defaults.
if (idle_time == 0xffffffff) {
Runtime.storage().put(settings_base_, settings); Runtime.storage().put(settings_base_, settings);
Runtime.storage().commit(); Runtime.storage().commit();
} }

@ -25,6 +25,7 @@ namespace storage {
struct BaseProps { struct BaseProps {
static constexpr uint16_t length = 0; static constexpr uint16_t length = 0;
static constexpr uint8_t uninitialized_byte = 0xff;
}; };
template <typename _StorageProps> template <typename _StorageProps>
@ -48,6 +49,14 @@ class Base {
void update(int idx, uint8_t val) {} void update(int idx, uint8_t val) {}
bool isSliceUninitialized(uint16_t offset, uint16_t size) {
for (uint16_t o = offset; o < offset + size; o++) {
if (read(o) != _StorageProps::uninitialized_byte)
return false;
}
return true;
}
const uint16_t length() { const uint16_t length() {
return _StorageProps::length; return _StorageProps::length;
} }

Loading…
Cancel
Save