InverseRE / antiflood

domestic automation
GNU General Public License v3.0
2 stars 0 forks source link

Configuration & user settings. #5

Closed InverseRE closed 5 years ago

InverseRE commented 5 years ago

Data structure, load/store operations of the custom settings.

InverseRE commented 5 years ago
#include <stdio.h>
#include <stdint.h>

static uint8_t EEPROM[1024];
static const int EEPROM_SIZE = sizeof(eeprom);

/*
 * format
 * save
 * load
 * clear
 */

void st_format(void);
int st_defrag(void);
template <typename T> int st_save(Tag tag, const T& value);
template <typename T> int st_load(Tag tag, T& value);
int st_clear(Tag tag);

enum Tag : uint8_t {
    EMPTY = 0xff
};

struct Head {
    Tag tag;
    uint8_t len;
    uint8_t crc;

    int operator +(const int addr) {
        return addr + 3 + len;
    }
};

void st_format(void)
{
    const Head empty{EMPTY, 0, 0};
    const uint8_t max_len = 256 - sizeof(head);
    int addr = 0;

    while (addr < EEPROM_SIZE) {
        int rem_size = EEPROM_SIZE - addr;
        empty.len = rem_size < max_len ? rem_size : max_len;
        memcpy(EEPROM + addr, &empty, sizeof(empty));
        addr = empty + addr;
    }
}