Only store settings if necessary.

This commit is contained in:
Martin Brodbeck 2024-02-07 11:25:38 +01:00
parent e848ca8f92
commit 7133d96c55
3 changed files with 31 additions and 5 deletions

View file

@ -20,8 +20,6 @@ int main()
settings.mode = Mode::IAMBIC_A;
settings.wpm = 25;
//store_settings(settings);
Settings newSettings{read_settings()};
printf("Iambic mode (loaded): %d\n", static_cast<int>(newSettings.mode));

View file

@ -34,12 +34,37 @@ void store_settings(Settings &settings)
sleep_ms(250);
gpio_put(LED_PIN, 0);
sleep_ms(250);
gpio_put(LED_PIN, 1);
sleep_ms(250);
gpio_put(LED_PIN, 0);
sleep_ms(250);
}
}
Settings read_settings()
{
Settings settings;
memcpy(&settings, flash_target_contents, sizeof(struct Settings));
if(settings.magic_number != MAGIC_NUMBER) {
settings = Settings();
gpio_put(LED_PIN, 1);
sleep_ms(1000);
gpio_put(LED_PIN, 0);
sleep_ms(250);
gpio_put(LED_PIN, 1);
sleep_ms(1000);
gpio_put(LED_PIN, 0);
sleep_ms(250);
gpio_put(LED_PIN, 1);
sleep_ms(1000);
gpio_put(LED_PIN, 0);
sleep_ms(250);
}
store_settings(settings);
return settings;
}

View file

@ -3,6 +3,8 @@
#include <pico/flash.h>
const uint16_t MAGIC_NUMBER = 2;
enum class Mode : uint8_t
{
IAMBIC_A = 0,
@ -12,9 +14,10 @@ enum class Mode : uint8_t
struct Settings
{
Mode mode{Mode::IAMBIC_B}; // Byte 1
uint8_t wpm{20}; // Byte 2
uint8_t dummy[FLASH_PAGE_SIZE - 2]{0}; // Fill up to next flash page size boundary
uint16_t magic_number; // Bytes: 2
Mode mode{Mode::IAMBIC_B}; // Bytes: 1
uint8_t wpm{20}; // Bytes: 1
uint8_t dummy[FLASH_PAGE_SIZE - 4]{0}; // Sum : 4
};
void store_settings(Settings &settings);