plugins: Convert some of the plugins to use Storage::isSliceUninitialized

Three plugins (`AutoShiftConfig`, `EscapeOneShotConfig` and `TypingBreaks`) that
used the same checker pattern to see if their storage slice is uninitialized now
use the new `storage().isSliceUninitialized()` method instead.

This reduces code duplication, among other things.

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

@ -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();
} }

Loading…
Cancel
Save