code reorganized
This commit is contained in:
parent
991b587afb
commit
1d4d387299
5 changed files with 7 additions and 4 deletions
4
src/CMakeLists.txt
Normal file
4
src/CMakeLists.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
target_sources(pico_keyer PRIVATE
|
||||
pico_keyer.cpp
|
||||
settings.cpp
|
||||
)
|
30
src/pico_keyer.cpp
Normal file
30
src/pico_keyer.cpp
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include <stdio.h>
|
||||
#include <pico/stdlib.h>
|
||||
|
||||
#include "settings.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
stdio_init_all();
|
||||
sleep_ms(1000);
|
||||
gpio_init(LED_PIN);
|
||||
gpio_set_dir(LED_PIN, GPIO_OUT);
|
||||
gpio_put(LED_PIN, 0);
|
||||
|
||||
Settings settings{read_settings()};
|
||||
|
||||
printf("Iambic mode (loaded): %d\n", static_cast<int>(settings.mode));
|
||||
printf("WPM (loaded): %d\n", settings.wpm);
|
||||
|
||||
while (true)
|
||||
{
|
||||
sleep_ms(1000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
71
src/settings.cpp
Normal file
71
src/settings.cpp
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include <cstring>
|
||||
|
||||
#include <pico/stdlib.h>
|
||||
|
||||
#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 uint LED_PIN = PICO_DEFAULT_LED_PIN;
|
||||
}
|
||||
|
||||
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, (uint8_t *)settings, FLASH_PAGE_SIZE);
|
||||
}
|
||||
|
||||
void store_settings(Settings &settings)
|
||||
{
|
||||
uint8_t *settingsAsBytes = (uint8_t *)&settings;
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
26
src/settings.h
Normal file
26
src/settings.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#ifndef SETTINGS_H
|
||||
#define SETTINGS_H
|
||||
|
||||
#include <pico/flash.h>
|
||||
|
||||
const uint16_t MAGIC_NUMBER = 2;
|
||||
|
||||
enum class Mode : uint8_t
|
||||
{
|
||||
IAMBIC_A = 0,
|
||||
IAMBIC_B,
|
||||
// ULTIMATE
|
||||
};
|
||||
|
||||
struct Settings
|
||||
{
|
||||
uint16_t magic_number{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);
|
||||
Settings read_settings();
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue