From e848ca8f92d670abf0d23d03809d9934a54c9bf6 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 7 Feb 2024 11:15:41 +0100 Subject: [PATCH] read/write works --- pico_keyer.cpp | 29 +++++++++++++++++++---------- settings.cpp | 25 ++++++++++++++++++++----- settings.h | 9 +++++---- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/pico_keyer.cpp b/pico_keyer.cpp index ada0dfb..c70877b 100644 --- a/pico_keyer.cpp +++ b/pico_keyer.cpp @@ -5,26 +5,35 @@ namespace { - + const uint LED_PIN = PICO_DEFAULT_LED_PIN; } int main() { stdio_init_all(); - sleep_ms(1000); - - puts("Hello, world!"); + gpio_init(LED_PIN); + gpio_set_dir(LED_PIN, GPIO_OUT); + gpio_put(LED_PIN, 0); Settings settings; settings.mode = Mode::IAMBIC_A; - store_settings(settings); - printf("Iambic mode (stored): %d\n", static_cast(settings.mode)); - sleep_ms(1000); - Settings newSettings = read_settings(); - printf("Iambic mode (loaded): %d\n", static_cast(newSettings.mode)); + settings.wpm = 25; - while(true) { + //store_settings(settings); + + Settings newSettings{read_settings()}; + + printf("Iambic mode (loaded): %d\n", static_cast(newSettings.mode)); + printf("WPM (loaded): %d\n", newSettings.wpm); + + if (settings.mode == Mode::IAMBIC_A && settings.wpm == 25) + { + gpio_put(LED_PIN, 1); + } + + while (true) + { sleep_ms(1000); } diff --git a/settings.cpp b/settings.cpp index 11926e3..9d9f2e9 100644 --- a/settings.cpp +++ b/settings.cpp @@ -1,11 +1,14 @@ #include +#include + #include "settings.h" namespace { constexpr const uint32_t FLASH_TARGET_OFFSET = PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE; - const uint8_t *flash_target_contents = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET); + const uint8_t *flash_target_contents = (const uint8_t *)(XIP_BASE + FLASH_TARGET_OFFSET); + const uint LED_PIN = PICO_DEFAULT_LED_PIN; } void flash_store_callback(void *settings) @@ -13,17 +16,29 @@ void flash_store_callback(void *settings) int writeSize = (sizeof(struct Settings) / FLASH_PAGE_SIZE) + 1; flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE); - flash_range_program(FLASH_TARGET_OFFSET, reinterpret_cast(settings), FLASH_PAGE_SIZE * writeSize); + flash_range_program(FLASH_TARGET_OFFSET, (uint8_t *)settings, FLASH_PAGE_SIZE); } void store_settings(Settings &settings) { - uint8_t *settingsAsBytes = reinterpret_cast(&settings); + uint8_t *settingsAsBytes = (uint8_t *)&settings; - flash_safe_execute(flash_store_callback, settingsAsBytes, 1000); + int result = flash_safe_execute(flash_store_callback, settingsAsBytes, 1000); + if (result == PICO_OK) + { + gpio_put(LED_PIN, 1); + 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 read_settings() +{ Settings settings; memcpy(&settings, flash_target_contents, sizeof(struct Settings)); return settings; diff --git a/settings.h b/settings.h index 654b70a..ce0b95f 100644 --- a/settings.h +++ b/settings.h @@ -3,20 +3,21 @@ #include -enum class Mode +enum class Mode : uint8_t { - IAMBIC_A, + IAMBIC_A = 0, IAMBIC_B, // ULTIMATE }; struct Settings { - Mode mode{Mode::IAMBIC_B}; + 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 }; void store_settings(Settings &settings); Settings read_settings(); - #endif \ No newline at end of file