62 lines
1.4 KiB
C
62 lines
1.4 KiB
C
|
/*
|
||
|
* config.c
|
||
|
*
|
||
|
* Created on: Jul 27, 2024
|
||
|
* Author: true
|
||
|
*/
|
||
|
|
||
|
#include "config.h"
|
||
|
#include "eeprom.h"
|
||
|
|
||
|
|
||
|
|
||
|
struct UserConf userconf;
|
||
|
|
||
|
|
||
|
|
||
|
static uint16_t checksum()
|
||
|
{
|
||
|
uint16_t i;
|
||
|
uint16_t sum = 0;
|
||
|
|
||
|
uint8_t *uc = (uint8_t *)&userconf;
|
||
|
|
||
|
// calculate checksum
|
||
|
for (i = 0; i < sizeof(userconf) - 6; i++) {
|
||
|
sum += *uc++;
|
||
|
}
|
||
|
|
||
|
return sum;
|
||
|
}
|
||
|
|
||
|
void userconf_load()
|
||
|
{
|
||
|
uint8_t csum;
|
||
|
|
||
|
eeprom_read_bytes(0, 0, (uint8_t *)&userconf, sizeof(userconf));
|
||
|
csum = checksum();
|
||
|
|
||
|
if ((userconf.checkval != CHECKVAL) || (userconf.checksum != csum)) {
|
||
|
// config is invalid; reset to default
|
||
|
userconf.cursor_color = CONF_CURSOR_WHITE;
|
||
|
userconf.cursor_flash = 4; // default flash rate
|
||
|
userconf.ledprog_ena_mask = 0; // no programs enabled
|
||
|
|
||
|
userconf.ledprog_setting[0][0] = 2; // rainbow: angle top left to bottom right
|
||
|
userconf.ledprog_setting[0][1] = 25; // rainbow: spacing
|
||
|
|
||
|
userconf.checksum = checksum();
|
||
|
userconf.checkval = CHECKVAL;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void userconf_save()
|
||
|
{
|
||
|
userconf.checksum = checksum();
|
||
|
userconf.checkval = CHECKVAL;
|
||
|
|
||
|
// this eeprom writes in 16-byte pages only, so split up the data
|
||
|
eeprom_write_bytes(0, 0x00, (uint8_t *)&userconf + 0x00, 16);
|
||
|
eeprom_write_bytes(0, 0x10, (uint8_t *)&userconf + 0x10, 16);
|
||
|
}
|