#ifndef FLASH_STORAGE_H
#define FLASH_STORAGE_H

#include "hardware/flash.h"
#include "hardware/sync.h"
#include <string.h>

extern uint8_t __flash_binary_end;

//#define FLASH_SECTOR_SIZE 4096
//#define FLASH_PAGE_SIZE   256

#define FLASH_TARGET_OFFSET ( \
    (((uint32_t)&__flash_binary_end - XIP_BASE) + FLASH_SECTOR_SIZE - 1) \
    & ~(FLASH_SECTOR_SIZE - 1) \
)

#define FLASH_MAGIC   0x5049434F  // 'PICO'
#define FLASH_VERSION 1

typedef struct {
    uint32_t magic;
    uint32_t version;

    float fan_on_temp; // If the MOSFET temperature exceeds this threshold, the fan starts
    float fan_off_temp; // If the MOSFET temperature falls below this threshold, the fan stops
    float shutoff_temp; // If the MOSFET temperature exceeds this threshold, the device is turned off

    uint32_t crc;
} flash_data_t;

bool flash_load(flash_data_t *out);
void flash_save(const flash_data_t *in);


#endif
