Serve the ESP32 frontend over httpserver/WebSocket, drive eight WS2812 strips with PIO/DMA patterns on core1, and keep Wi-Fi control on core0. Co-authored-by: Cursor <cursoragent@cursor.com>
75 lines
2.0 KiB
C
75 lines
2.0 KiB
C
#include "flash_store.h"
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "hardware/flash.h"
|
|
#include "hardware/sync.h"
|
|
#include "pico/stdlib.h"
|
|
|
|
/* Two sectors near end of flash: buttons, presets */
|
|
#ifndef PICO_FLASH_SIZE_BYTES
|
|
#define PICO_FLASH_SIZE_BYTES (2 * 1024 * 1024)
|
|
#endif
|
|
|
|
#define STORE_MAGIC 0x484F4F50u /* HOOP */
|
|
|
|
typedef struct {
|
|
uint32_t magic;
|
|
uint32_t len;
|
|
char name[16];
|
|
char data[FLASH_STORE_MAX - 24];
|
|
} store_blob_t;
|
|
|
|
_Static_assert(sizeof(store_blob_t) <= FLASH_SECTOR_SIZE, "blob too big");
|
|
|
|
static uint32_t slot_offset(const char *name) {
|
|
uint32_t base = PICO_FLASH_SIZE_BYTES - 2 * FLASH_SECTOR_SIZE;
|
|
if (strcmp(name, "buttons") == 0) {
|
|
return base;
|
|
}
|
|
return base + FLASH_SECTOR_SIZE; /* presets */
|
|
}
|
|
|
|
bool flash_store_load(const char *name, char *out, size_t out_len, size_t *out_size) {
|
|
if (!name || !out || out_len == 0) {
|
|
return false;
|
|
}
|
|
const store_blob_t *blob = (const store_blob_t *)(XIP_BASE + slot_offset(name));
|
|
if (blob->magic != STORE_MAGIC || blob->len == 0 || blob->len >= sizeof(blob->data)) {
|
|
return false;
|
|
}
|
|
if (strncmp(blob->name, name, sizeof(blob->name)) != 0) {
|
|
return false;
|
|
}
|
|
size_t n = blob->len;
|
|
if (n >= out_len) {
|
|
n = out_len - 1;
|
|
}
|
|
memcpy(out, blob->data, n);
|
|
out[n] = '\0';
|
|
if (out_size) {
|
|
*out_size = n;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool flash_store_save(const char *name, const char *data, size_t len) {
|
|
if (!name || !data || len >= FLASH_STORE_MAX - 24) {
|
|
return false;
|
|
}
|
|
store_blob_t blob;
|
|
memset(&blob, 0, sizeof(blob));
|
|
blob.magic = STORE_MAGIC;
|
|
blob.len = (uint32_t)len;
|
|
snprintf(blob.name, sizeof(blob.name), "%s", name);
|
|
memcpy(blob.data, data, len);
|
|
|
|
uint32_t off = slot_offset(name);
|
|
uint32_t ints = save_and_disable_interrupts();
|
|
flash_range_erase(off, FLASH_SECTOR_SIZE);
|
|
flash_range_program(off, (const uint8_t *)&blob, sizeof(blob));
|
|
restore_interrupts(ints);
|
|
return true;
|
|
}
|