From a7a29bb658aa98758a560884d88aaec3149a2e05 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 28 Mar 2022 12:27:02 +0200 Subject: [PATCH 1/2] driver::storage::Base: Add an isSliceUninitialized() function First, we add an `uinitialized_byte` prop to `BaseProps`, so implementations that don't use `0xff` as default can set it to whatever they use. Do keep in mind that plenty of code still assumes `0xff` is the uninitialized byte, but this way we have a starting point. Then, we add an `isSliceUninitialized()` function, which checks whether a given slice is completely made up from uninitialized bytes or not. Fixes #1145. Signed-off-by: Gergely Nagy --- src/kaleidoscope/driver/storage/Base.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/kaleidoscope/driver/storage/Base.h b/src/kaleidoscope/driver/storage/Base.h index a2dae8d0..00cc481a 100644 --- a/src/kaleidoscope/driver/storage/Base.h +++ b/src/kaleidoscope/driver/storage/Base.h @@ -25,6 +25,7 @@ namespace storage { struct BaseProps { static constexpr uint16_t length = 0; + static constexpr uint8_t uninitialized_byte = 0xff; }; template @@ -48,6 +49,14 @@ class Base { 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() { return _StorageProps::length; } From b93b38da22d6c97b6d10407b8e0add27c6348025 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 28 Mar 2022 12:29:57 +0200 Subject: [PATCH 2/2] 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 --- .../src/kaleidoscope/plugin/AutoShiftConfig.cpp | 10 ++++------ .../src/kaleidoscope/plugin/Escape-OneShot-Config.cpp | 10 ++++------ .../src/kaleidoscope/plugin/TypingBreaks.cpp | 9 ++++----- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/plugins/Kaleidoscope-AutoShift/src/kaleidoscope/plugin/AutoShiftConfig.cpp b/plugins/Kaleidoscope-AutoShift/src/kaleidoscope/plugin/AutoShiftConfig.cpp index 859a1747..ac2b7085 100644 --- a/plugins/Kaleidoscope-AutoShift/src/kaleidoscope/plugin/AutoShiftConfig.cpp +++ b/plugins/Kaleidoscope-AutoShift/src/kaleidoscope/plugin/AutoShiftConfig.cpp @@ -37,13 +37,11 @@ uint16_t AutoShiftConfig::settings_base_; EventHandlerResult AutoShiftConfig::onSetup() { settings_base_ = ::EEPROMSettings.requestSlice(sizeof(AutoShift::settings_)); - uint32_t checker; - Runtime.storage().get(settings_base_, checker); - - // Check if we have an empty eeprom... - if (checker == 0xffffffff) { - // ...if the eeprom was empty, store the default settings. + if (Runtime.storage().isSliceUninitialized( + settings_base_, + sizeof(AutoShift::settings_))) { + // If our slice is uninitialized, set sensible defaults. Runtime.storage().put(settings_base_, AutoShift::settings_); Runtime.storage().commit(); } diff --git a/plugins/Kaleidoscope-Escape-OneShot/src/kaleidoscope/plugin/Escape-OneShot-Config.cpp b/plugins/Kaleidoscope-Escape-OneShot/src/kaleidoscope/plugin/Escape-OneShot-Config.cpp index 721f0332..59a29b61 100644 --- a/plugins/Kaleidoscope-Escape-OneShot/src/kaleidoscope/plugin/Escape-OneShot-Config.cpp +++ b/plugins/Kaleidoscope-Escape-OneShot/src/kaleidoscope/plugin/Escape-OneShot-Config.cpp @@ -34,13 +34,11 @@ uint16_t EscapeOneShotConfig::settings_base_; EventHandlerResult EscapeOneShotConfig::onSetup() { settings_base_ = ::EEPROMSettings.requestSlice(sizeof(EscapeOneShot::settings_)); - uint16_t checker; - Runtime.storage().get(settings_base_, checker); - - // Check if we have an empty eeprom... - if (checker == 0xffff) { - // ...if the eeprom was empty, store the default settings. + if (Runtime.storage().isSliceUninitialized( + settings_base_, + sizeof(EscapeOneShot::settings_))) { + // If our slice is uninitialized, set sensible defaults. Runtime.storage().put(settings_base_, EscapeOneShot::settings_); Runtime.storage().commit(); } diff --git a/plugins/Kaleidoscope-TypingBreaks/src/kaleidoscope/plugin/TypingBreaks.cpp b/plugins/Kaleidoscope-TypingBreaks/src/kaleidoscope/plugin/TypingBreaks.cpp index 9490ec43..bbf63ae6 100644 --- a/plugins/Kaleidoscope-TypingBreaks/src/kaleidoscope/plugin/TypingBreaks.cpp +++ b/plugins/Kaleidoscope-TypingBreaks/src/kaleidoscope/plugin/TypingBreaks.cpp @@ -121,11 +121,10 @@ EventHandlerResult TypingBreaks::onNameQuery() { EventHandlerResult TypingBreaks::onSetup() { settings_base_ = ::EEPROMSettings.requestSlice(sizeof(settings)); - // If idleTime is max, assume that EEPROM is uninitialized, and store the - // defaults. - uint32_t idle_time; - Runtime.storage().get(settings_base_, idle_time); - if (idle_time == 0xffffffff) { + if (Runtime.storage().isSliceUninitialized( + settings_base_, + sizeof(settings))) { + // If our slice is uninitialized, set sensible defaults. Runtime.storage().put(settings_base_, settings); Runtime.storage().commit(); }