read/write works
This commit is contained in:
parent
a40d9e0e5a
commit
e848ca8f92
3 changed files with 44 additions and 19 deletions
|
@ -5,26 +5,35 @@
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
stdio_init_all();
|
stdio_init_all();
|
||||||
|
|
||||||
sleep_ms(1000);
|
sleep_ms(1000);
|
||||||
|
gpio_init(LED_PIN);
|
||||||
puts("Hello, world!");
|
gpio_set_dir(LED_PIN, GPIO_OUT);
|
||||||
|
gpio_put(LED_PIN, 0);
|
||||||
|
|
||||||
Settings settings;
|
Settings settings;
|
||||||
settings.mode = Mode::IAMBIC_A;
|
settings.mode = Mode::IAMBIC_A;
|
||||||
store_settings(settings);
|
settings.wpm = 25;
|
||||||
printf("Iambic mode (stored): %d\n", static_cast<int>(settings.mode));
|
|
||||||
sleep_ms(1000);
|
|
||||||
Settings newSettings = read_settings();
|
|
||||||
printf("Iambic mode (loaded): %d\n", static_cast<int>(newSettings.mode));
|
|
||||||
|
|
||||||
while(true) {
|
//store_settings(settings);
|
||||||
|
|
||||||
|
Settings newSettings{read_settings()};
|
||||||
|
|
||||||
|
printf("Iambic mode (loaded): %d\n", static_cast<int>(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);
|
sleep_ms(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
23
settings.cpp
23
settings.cpp
|
@ -1,11 +1,14 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
#include <pico/stdlib.h>
|
||||||
|
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
constexpr const uint32_t FLASH_TARGET_OFFSET = PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE;
|
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)
|
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;
|
int writeSize = (sizeof(struct Settings) / FLASH_PAGE_SIZE) + 1;
|
||||||
|
|
||||||
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
|
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
|
||||||
flash_range_program(FLASH_TARGET_OFFSET, reinterpret_cast<uint8_t *>(settings), FLASH_PAGE_SIZE * writeSize);
|
flash_range_program(FLASH_TARGET_OFFSET, (uint8_t *)settings, FLASH_PAGE_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void store_settings(Settings &settings)
|
void store_settings(Settings &settings)
|
||||||
{
|
{
|
||||||
uint8_t *settingsAsBytes = reinterpret_cast<uint8_t *>(&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;
|
Settings settings;
|
||||||
memcpy(&settings, flash_target_contents, sizeof(struct Settings));
|
memcpy(&settings, flash_target_contents, sizeof(struct Settings));
|
||||||
return settings;
|
return settings;
|
||||||
|
|
|
@ -3,20 +3,21 @@
|
||||||
|
|
||||||
#include <pico/flash.h>
|
#include <pico/flash.h>
|
||||||
|
|
||||||
enum class Mode
|
enum class Mode : uint8_t
|
||||||
{
|
{
|
||||||
IAMBIC_A,
|
IAMBIC_A = 0,
|
||||||
IAMBIC_B,
|
IAMBIC_B,
|
||||||
// ULTIMATE
|
// ULTIMATE
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Settings
|
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);
|
void store_settings(Settings &settings);
|
||||||
Settings read_settings();
|
Settings read_settings();
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in a new issue