Compare commits

..

2 Commits

Author SHA1 Message Date
true ea3f6ae9e0 implement factory reset, fix eeprom write routine 2024-10-27 04:14:37 -07:00
true 36e091a949 clean up menu code; use clearer naming 2024-10-27 00:15:54 -07:00
17 changed files with 219 additions and 90 deletions

View File

@ -22,9 +22,9 @@
* - there is no ack on address. code always assumes slave is responding.
*/
#define i2c_init() i2cm_init()
#define i2c_start() SetSysClock(SYSCLK_FREQ_USEI2C); i2cm_start()
#define i2c_start() { SetSysClock(SYSCLK_FREQ_USEI2C); i2cm_start(); }
#define i2c_restart() i2cm_restart()
#define i2c_stop() SetSysClock(SYSCLK_FREQ_NORMAL); i2cm_stop()
#define i2c_stop() { i2cm_stop(); SetSysClock(SYSCLK_FREQ_NORMAL); }
#define i2c_rd(ack) i2cm_rd(ack)
#define i2c_wr(dat) i2cm_wr(dat)
#define i2c_addr(a, w) i2c_start(); i2cm_addr(a, w)

View File

@ -184,8 +184,9 @@ uint8_t i2cm_wr(uint8_t dat)
SCL_OUTLO(); bit_delay_lo();
}
SDA_IN_HI();
SDA_IN_HI(); __nop(); // slave will now try to ack
SCL_IN_HI(); bit_delay_hi();
while (!SCL_GET()); // nothing should stretch here, but...
ack = SDA_GET();

View File

@ -58,7 +58,7 @@ uint8_t eep16_is_ready()
uint8_t ack;
i2c_start();
ack = i2c_wr(EEPROM_I2C_ADDR | 1);
ack = i2c_wr(EEPROM_I2C_ADDR | 0);
i2c_stop();
if (ack) return 0;

View File

@ -85,6 +85,21 @@ void btn_commit_hold()
ch32sub_write(REG_BTN1_REPEAT_HI, val, sizeof(val));
}
int8_t btn_get_idx(uint8_t map_idx)
{
uint8_t *map;
if (map_idx >= BTN_COUNT) return -1;
if (sysflags & SYS_OLED_ROTATE_X) {
map = btn_map_rot;
} else {
map = btn_map_upr;
}
return map[map_idx] & 0x7f;
}
__HIGH_CODE
void btn_intr()

View File

@ -39,6 +39,8 @@ extern uint8_t btn_held;
void btn_intr();
void btn_commit_hold();
int8_t btn_get_idx(uint8_t map_idx);
#endif /* USER_UI_BTN_H_ */

View File

@ -1,5 +1,5 @@
/*
* $Id: menu.h 495 2021-07-22 20:53:39Z true $
* menu.h
* begin 20190527 true
*/
@ -82,12 +82,12 @@ void menu_0_disp(uint8_t idx);
void menu_0_enter(uint8_t idx);
// menu_1_name
extern const MenuItem menu_1;
extern const MenuItem menu_1_name_setup;
void menu_1_disp(uint8_t idx);
void menu_1_enter(uint8_t idx);
// menu_2_led
extern const MenuItem menu_2;
extern const MenuItem menu_2_led_setup;
void menu_2_disp(uint8_t idx);
void menu_2_enter(uint8_t idx);
@ -96,12 +96,12 @@ extern const MenuItem menu_3_snek;
void snek_disp(uint8_t idx);
// menu_5_options
extern const MenuItem menu_5;
extern const MenuItem menu_5_options;
void menu_5_disp(uint8_t idx);
void menu_5_enter(uint8_t idx);
// menu_6_about
extern const MenuItem menu_6;
extern const MenuItem menu_6_about;
void menu_6_disp(uint8_t idx);

View File

@ -1,8 +1,9 @@
/*
* menu_entry_0.c
* menu0_main.c
* begin 20190527 true
*
* main menu functions
* nametag display
*/
#include "menu.h"
@ -19,6 +20,7 @@
#include <stdio.h>
#define ROTATE_TARGET_WIDTH 40
#define ROTATE_TARGET_HEIGHT 32
@ -35,6 +37,7 @@
#define DEMOWAVE_ADD2 25
uint8_t rotsrc_fb[(ROTATE_TARGET_WIDTH * (ROTATE_TARGET_HEIGHT >> 3)) + 1];
uint8_t rotdst_fb[(ROTATE_TARGET_WIDTH * (ROTATE_TARGET_HEIGHT >> 3)) + 1];
@ -46,6 +49,7 @@ uint8_t wiggle = 1;
int8_t waves = 0;
static void menu_none_init()
{
rotsrc.width = rotdst.width = ROTATE_TARGET_WIDTH;
@ -316,7 +320,7 @@ __HIGH_CODE
void menu_0_disp(uint8_t idx)
{
char txt[14];
uint8_t w;
// uint8_t w;
ssd1306fb_set_color(SSD1306_STATE_SET_PIXEL);
@ -329,14 +333,25 @@ void menu_0_disp(uint8_t idx)
case 0: strcpy(txt, "Nametag!"); break;
case 1: strcpy(txt, "Name Setup"); break;
case 2: strcpy(txt, "RGB Setup"); break;
case 3: strcpy(txt, "Snek"); break;
case 4: strcpy(txt, "Morble"); break;
case 3: strcpy(txt, "Messages"); break;
case 4: {
ssd1306fb_set_cursor(32, 9);
ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, "I", 1);
ssd1306fb_set_cursor(39, 8);
ssd1306fb_draw_str(font_Dialog_plain_8, "2", 1);
ssd1306fb_set_cursor(45, 9);
strcpy(txt, "C Sniffer");
break;
}
case 5: strcpy(txt, "Options"); break;
case 6: strcpy(txt, "About & Dbg"); break;
}
ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, txt, 1);
// disabled / incomplete entries
/*
switch (idx) {
case 4: {
w = ssd1306fb_get_str_width(font_DejaVu_Sans_Mono_Bold_11, txt, strlen(txt), 1);
@ -344,6 +359,7 @@ void menu_0_disp(uint8_t idx)
ssd1306fb_draw_hline(32, w + 32, 17);
}
}
*/
// draw extras
menu_draw_tabs(idx);
@ -358,29 +374,28 @@ void menu_0_enter(uint8_t idx)
return;
}
case 1: {
menu = (MenuItem *)&menu_1;
menu = (MenuItem *)&menu_1_name_setup;
menu_idx = 0;
return;
}
case 2: {
menu = (MenuItem *)&menu_2;
menu = (MenuItem *)&menu_2_led_setup;
menu_idx = 0;
return;
}
case 3: {
menu = (MenuItem *)&menu_3_snek;
return;
}
case 4: {
return;
}
case 5: {
menu = (MenuItem *)&menu_5;
menu = (MenuItem *)&menu_5_options;
menu_idx = 0;
return;
}
case 6: {
menu = (MenuItem *)&menu_6;
menu = (MenuItem *)&menu_6_about;
menu_idx = 0;
return;
}

View File

@ -1,5 +1,5 @@
/*
* menu_entry_5.c
* menu5_options.c
* begin 20190613 true
*
* settings menu functions
@ -19,18 +19,20 @@
#define LI_ENABLE_LEDS 0
#define LI_NO_MOVE_TIMEOUT 1
#define LI_NO_MOVE_THRESH 2
#define LI_WAKEUP_THRESH 3
#define LI_LSENS_DARK_CAL 4
#define LI_FACTORY_RESET 5
#define LI_DEBUG_SHOW_CPU 6
#define LI_DEBUG_SHOW_ACCEL 7
#define LI_NO_MOVE_SLP_TIME 1
#define LI_NO_MOVE_DIM_TIME 2
#define LI_NO_MOVE_THRESH 3
#define LI_WAKEUP_THRESH 4
#define LI_LSENS_DARK_CAL 5
#define LI_FACTORY_RESET 6
#define LI_DEBUG_SHOW_CPU 7
#define LI_DEBUG_SHOW_ACCEL 8
const uint16_t sleep_times[] = {
0,
60,
300,
600,
900,
@ -44,7 +46,6 @@ static uint8_t factory_reset;
__HIGH_CODE
void menu_5_disp(uint8_t idx)
{
int8_t w, x;
@ -61,7 +62,7 @@ void menu_5_disp(uint8_t idx)
strcpy(txt, (uconf.flags & UCONF_FLAGS_LEDS_ENABLE) ? "On" : "Off");
break;
}
case LI_NO_MOVE_TIMEOUT: {
case LI_NO_MOVE_SLP_TIME: {
ssd1306fb_draw_str(font_table[0].font, "No Movement Sleep Time", 0);
if (uconf.sleep_timeout) {
sprintf(txt, "%dmin", (uconf.sleep_timeout / 60));
@ -70,6 +71,15 @@ void menu_5_disp(uint8_t idx)
}
break;
}
case LI_NO_MOVE_DIM_TIME: {
ssd1306fb_draw_str(font_table[0].font, "No Move LED Autodim Time", 0);
if (uconf.dim_timeout) {
sprintf(txt, "%dmin", (uconf.dim_timeout / 60));
} else {
strcpy(txt, "Off");
}
break;
}
case LI_NO_MOVE_THRESH: {
ssd1306fb_draw_str(font_table[0].font, "No Movement Threshold", 0);
strcpy(txt, "Normal");
@ -91,20 +101,47 @@ void menu_5_disp(uint8_t idx)
break;
}
case LI_FACTORY_RESET: {
uint32_t iter;
// todo: implement manual button checking here after getting button orientation code working
// yes, this means reset speed depends on framerate lol
if (btn_pushed & (1 << 2)) {
factory_reset++;
} else factory_reset = 0;
if (factory_reset >= 32) {
// do the factory reset
iter = uconf.iter;
uconf_defaults();
// todo: reboot?
// we do store the old iteration value so long as it isn't
// too high in order to keep track of nvmem writes.
// if the value is unnaturally high, we'll reset to zero.
// if it's corrupt in any way, enjoy your false write count.
if (iter < 0xffffff) {
uconf.iter = iter;
}
uconf_clear();
// while it isn't necesasry to write an initial config
// as the code will determine there is no valid config
// and load defaults if the nvmem is empty,
// we're doing it to write the iter value.
uconf_write();
// aaaand it's gone
SYS_ResetExecute();
while (1);
}
ssd1306fb_draw_str(font_table[0].font, "Hold Set to Factory Reset...", 0);
txt[0] = 0;
ssd1306fb_draw_rect(50, 18, 86, 31);
for (w = 0; w < (factory_reset >> 1); w++) {
ssd1306fb_draw_vline(50 + w, 18, 31);
ssd1306fb_draw_rect(52, 18, 52 + 31, 31);
for (w = 0; w < factory_reset; w++) {
ssd1306fb_draw_vline(53 + w, 18, 31);
}
break;
}
@ -139,6 +176,7 @@ void menu_5_disp(uint8_t idx)
void menu_5_enter(uint8_t idx)
{
int i, j;
uint16_t *t;
switch (idx) {
case LI_ENABLE_LEDS: {
@ -149,13 +187,16 @@ void menu_5_enter(uint8_t idx)
}
break;
}
case LI_NO_MOVE_TIMEOUT: {
case LI_NO_MOVE_SLP_TIME:
case LI_NO_MOVE_DIM_TIME: {
t = (idx == LI_NO_MOVE_SLP_TIME) ? &uconf.sleep_timeout : &uconf.dim_timeout;
j = sizeof(sleep_times) / sizeof(sleep_times[0]);
for (i = 0; i < j; i++) {
if (uconf.sleep_timeout == sleep_times[i]) {
if (*t == sleep_times[i]) {
i++;
i %= j;
uconf.sleep_timeout = sleep_times[i];
*t = sleep_times[i];
j = 0xff;
break;
}
@ -163,7 +204,7 @@ void menu_5_enter(uint8_t idx)
// couldn't find a match, so set default
if (j != 0xff) {
uconf.sleep_timeout = sleep_times[4];
*t = sleep_times[4];
}
break;
}

View File

@ -27,6 +27,19 @@
#define MCU_SRAM 24+2
#define LI_ABOUT 0
#define LI_CONTACT 1
#define LI_LED_1_4 2
#define LI_LED_5_8 3
#define LI_LED_9_12 4
#define LI_ACCELEROMETER 5
#define LI_CPU_USAGE 6
#define LI_MISC_STATS 7
#define LI_ERROR_COUNTERS 8
#define LI_FONT_GLYPH_TEST 9
uint8_t font_index = 0;
uint8_t font_glyph = 0;
@ -78,7 +91,7 @@ void menu_6_font_prev(uint8_t idx)
*/
}
void menu_6_accel_reset(uint8_t idx)
static void menu_6_accel_reset(uint8_t idx)
{
// todo: figure out what this does
movement_worst = 0;
@ -114,7 +127,7 @@ void menu_6_disp(uint8_t idx)
// which item selected?
switch (idx) {
case 0: { // about
case LI_ABOUT: { // about
ssd1306fb_set_cursor(11, 0);
ssd1306fb_draw_str(font_DSEG14_Classic_18, "GAT", 1);
ssd1306fb_set_cursor(11, 22);
@ -136,7 +149,7 @@ void menu_6_disp(uint8_t idx)
break;
}
case 1: { // manual
case LI_CONTACT: { // manual
ssd1306fb_set_cursor(11, 0);
ssd1306fb_draw_str(font_Dialog_plain_8, "Manual and code at", 1);
ssd1306fb_set_cursor(11, 11);
@ -146,7 +159,7 @@ void menu_6_disp(uint8_t idx)
break;
}
case 2: { // led 1-4
case LI_LED_1_4: { // led 1-4
ssd1306fb_set_cursor(54, 4);
ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, "LED", 1);
ssd1306fb_set_cursor(54, 15);
@ -163,7 +176,7 @@ void menu_6_disp(uint8_t idx)
}
break;
}
case 3: { // led 5-8
case LI_LED_5_8: { // led 5-8
ssd1306fb_set_cursor(54, 4);
ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, "LED", 1);
ssd1306fb_set_cursor(54, 15);
@ -180,7 +193,7 @@ void menu_6_disp(uint8_t idx)
}
break;
}
case 4: { // led 9-12
case LI_LED_9_12: { // led 9-12
ssd1306fb_set_cursor(54, 4);
ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, "LED", 1);
ssd1306fb_set_cursor(53, 15);
@ -201,7 +214,7 @@ void menu_6_disp(uint8_t idx)
}
break;
}
case 5: { // accelerometer
case LI_ACCELEROMETER: { // accelerometer
ssd1306fb_set_cursor(10, -1);
ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, "Accelerometer", 1);
@ -229,7 +242,7 @@ void menu_6_disp(uint8_t idx)
break;
}
case 6: { // cpu stats
case LI_CPU_USAGE: { // cpu stats
sprintf(txt, "CPU Load: %3u%%", cpu_pct);
ssd1306fb_set_cursor(10, -1);
ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, txt, 1);
@ -243,20 +256,20 @@ void menu_6_disp(uint8_t idx)
ssd1306fb_draw_str(font_Dialog_plain_8, txt, 0);
break;
}
case 7: { // misc stats
case LI_MISC_STATS: { // misc stats
ssd1306fb_set_cursor(10, -1);
ssd1306fb_draw_str(font_Dialog_plain_8, "Light: ", 1);
oled.cursor_x = 39;
sprintf(txt, "%d lo, %02d + %d", lsens_get_dark_threshold(), lsens_get_coarse(), lsens_get_fine());
ssd1306fb_draw_str(font_Dialog_plain_8, txt, 0);
//sprintf(txt, "%d lo, %02d + %d", lsens_get_dark_threshold(), lsens_get_coarse(), lsens_get_fine());
//ssd1306fb_draw_str(font_Dialog_plain_8, txt, 0);
ssd1306fb_draw_str(font_Dialog_plain_8, "not yet implemented", 0);
ssd1306fb_set_cursor(10, 7);
ssd1306fb_draw_str(font_Dialog_plain_8, "Temp: Batt:", 1);
ssd1306fb_draw_str(font_Dialog_plain_8, "Temp:", 1);
oled.cursor_x = 42;
// todo: implement temperature sensing
// sprintf(txt, "%d.%dC", temp_degc, temp_degc_decimal);
ssd1306fb_draw_str(font_Dialog_plain_8, txt, 0);
ssd1306fb_draw_str(font_Dialog_plain_8, "not yet implemented", 0);
oled.cursor_x = 98;
// battery reading support is not supported on this target
@ -265,18 +278,18 @@ void menu_6_disp(uint8_t idx)
ssd1306fb_draw_str(font_Dialog_plain_8, txt, 0);
ssd1306fb_set_cursor(10, 16);
ssd1306fb_draw_str(font_Dialog_plain_8, "Flash Conf Writes: ", 0);
ssd1306fb_draw_str(font_Dialog_plain_8, "EEPROM Conf Writes: ", 0);
sprintf(txt, "%lu", uconf.iter);
ssd1306fb_draw_str(font_Dialog_plain_8, txt, 1);
ssd1306fb_set_cursor(10, 24);
ssd1306fb_draw_str(font_Dialog_plain_8, "MCU: ", 1);
sprintf(txt, "%dK / %dK", MCU_FLASH, MCU_SRAM);
ssd1306fb_draw_str(font_Dialog_plain_8, txt, 1);
ssd1306fb_draw_str(font_Dialog_plain_8, "CH592 ", 0);
sprintf(txt, "F%dK / R%dK", MCU_FLASH, MCU_SRAM);
ssd1306fb_draw_str(font_Dialog_plain_8, txt, 0);
break;
}
case 8: { // error counters
case LI_ERROR_COUNTERS: { // error counters
ssd1306fb_set_cursor(10, -1);
ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, "Error Counters", 1);
@ -300,7 +313,7 @@ void menu_6_disp(uint8_t idx)
break;
}
case 9: {
case LI_FONT_GLYPH_TEST: {
ssd1306fb_set_cursor(10, -1);
ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, "Font Test", 1);
@ -325,20 +338,21 @@ void menu_6_disp(uint8_t idx)
// buttons
switch (idx) {
case 0:
case 1:
case 2:
case 3:
case 4: {
case LI_ABOUT:
case LI_CONTACT:
case LI_LED_1_4:
case LI_LED_5_8:
case LI_LED_9_12:
case LI_CPU_USAGE: {
menu_btn_use_std();
break;
}
case 5: {
case LI_ACCELEROMETER: {
menu_btn_use_std();
btn[2].cb_push = menu_6_accel_reset;
break;
}
case 9: {
case LI_FONT_GLYPH_TEST: {
menu_6_btn_use();
menu_draw_buttons(MENU_BTNSTYLE_ABOUT, 0x0c);
break;

View File

@ -26,10 +26,10 @@ void menu_tick()
menu->dispfn(menu_idx);
// // do we flip the display?
if ((menu == &menu_6) && (menu_idx == 5)) { // about > accelerometer
if ((menu == &menu_6_about) && (menu_idx == 5)) { // about > accelerometer
// accelerometer page never flips
ssd1306_set_flipmirror(0);
} else if (menu != &menu_none) { // not nametag
} else if (menu != &menu_none) { // not nametag
if (sysflags & SYS_OLED_ROTATE_X) {
ssd1306_set_flipmirror(1);
} else {
@ -173,8 +173,8 @@ void menu_draw_buttons(uint8_t mode, uint8_t mask)
case MENU_BTNSTYLE_MAIN: {
switch (mask) {
case 0: strcpy(button_txt[1], "Resume"); break;
case 3:
case 4: strcpy(button_txt[1], "Play"); break;
//case 3:
//case 4: strcpy(button_txt[1], "Play"); break;
default: strcpy(button_txt[1], "Select"); break;
}
strcpy(button_txt[3], " ");

View File

@ -15,31 +15,31 @@
const MenuItem menu_none = {1, 0, 0, 0, &menu_none_disp, &menu_start};
// root menu
// exit, snek, morble, set name, leds, options, about
// exit (nametag), name setup, led setup, messages, i2c sniffer, options, about
const MenuItem menu_0 = {7, MENU_FLAG_SCROLL,
0, 0, &menu_0_disp, &menu_0_enter};
// name menu
// set name, display mode, flip mode, font select, char spacing, y-offset, half-width, color invert
const MenuItem menu_1 = {8, MENU_FLAG_SCROLL | MENU_FLAG_SAVE_ON_EXIT,
const MenuItem menu_1_name_setup = {8, MENU_FLAG_SCROLL | MENU_FLAG_SAVE_ON_EXIT,
(MenuItem *)&menu_0, 1, &menu_1_disp, &menu_1_enter};
// led menu
// edge mode, edge setup, eyes mode, eyes setup, favcolor hue, sat, val, altcolor hue, sat, val
const MenuItem menu_2 = {10, MENU_FLAG_SCROLL | MENU_FLAG_SAVE_ON_EXIT,
const MenuItem menu_2_led_setup = {10, MENU_FLAG_SCROLL | MENU_FLAG_SAVE_ON_EXIT,
(MenuItem *)&menu_0, 2, &menu_2_disp, &menu_2_enter};
// snek menu
const MenuItem menu_3_snek = {1, MENU_FLAG_NONE, //MENU_FLAG_SAVE_ON_EXIT,
(MenuItem *)&menu_0, 3, &snek_disp, NULL};
//const MenuItem menu_3_snek = {1, MENU_FLAG_NONE, //MENU_FLAG_SAVE_ON_EXIT,
// (MenuItem *)&menu_0, 3, &snek_disp, NULL};
// options menu
// leds on/off, sleep on/off, sleep threshold, wake threshold, recal lightsense, factory reset, show cpu usage, show accel "angle",
const MenuItem menu_5 = {8, MENU_FLAG_SCROLL | MENU_FLAG_SAVE_ON_EXIT,
// leds on/off, sleep time, dim time, sleep threshold, wake threshold, recal lightsense, factory reset, show cpu usage, show accel "angle",
const MenuItem menu_5_options = {9, MENU_FLAG_SCROLL | MENU_FLAG_SAVE_ON_EXIT,
(MenuItem *)&menu_0, 5, &menu_5_disp, &menu_5_enter};
// about menu
// credits, leds, leds, leds, accel, cpu usage/uptime, light/temp, errors, font test
const MenuItem menu_6 = {10, MENU_FLAG_SCROLL,
const MenuItem menu_6_about = {9, MENU_FLAG_SCROLL,
(MenuItem *)&menu_0, 6, &menu_6_disp, 0};

View File

@ -11,6 +11,7 @@
#include "led/rgbled.h"
#include <string.h>
#include <stdio.h>
@ -36,14 +37,14 @@ void uconf_defaults()
{
int i;
memset(&uconf, 0x00, sizeof(uconf));
uconf.ver = UCONF_VER;
uconf.flags = UCONF_FLAGS_LEDS_ENABLE;
uconf.framemod = UCONF_FRAMERATE_FULL;
uconf.nameconf = UCONF_NAME_DISP_DEMOWAVE1 | UCONF_NAME_MODE_AUTOROTATE | UCONF_NAME_MODE_COLOR_INVERT;
// UCONF_NAME_DISP_DEMOWAVES1 | UCONF_NAME_MODE_AUTOROTATE | UCONF_NAME_MODE_COLOR_INVERT;
if (uconf_flash_offset == 0xf0) {
uconf.iter = 0;
}
uconf.font_idx = 5;
uconf.char_spacing = 2;
uconf.y_offset = 0;
@ -74,6 +75,7 @@ void uconf_defaults()
uconf.checksum = checksum_gen((uint8_t *)&uconf, sizeof(uconf) - 2);
}
// returns 0 if valid
static int8_t uconf_validate()
{
// blank check
@ -98,21 +100,56 @@ static int8_t uconf_validate()
void uconf_load()
{
uint8_t i;
uint32_t iter = 0;
uconf_flash_offset = FLASH_RSVD_PAGES;
for (i = 0; i < FLASH_RSVD_PAGES; i++) {
// find last page of valid config from flash
uconf_flash_offset = (FLASH_RSVD_PAGES - 1) - i;
eep16_read(uconf_flash_offset * FLASH_UCONF_BYTES, (uint8_t *)&uconf, FLASH_UCONF_BYTES);
// search nv data for latest user config
eep16_read(i * FLASH_UCONF_BYTES, (uint8_t *)&uconf, FLASH_UCONF_BYTES);
if (!uconf_validate()) {
// valid data
return;
if (uconf.iter > iter) {
// this page has the highest iter value, so is the latest we've found so far
uconf_flash_offset = i;
iter = uconf.iter;
}
}
}
// flash has no valid data whatsoever
// don't worry about flash setup; that is done during writing
uconf_flash_offset = 0xf0;
uconf_defaults();
if (uconf_flash_offset >= FLASH_RSVD_PAGES) {
// flash has no valid data whatsoever
// load some defaults into RAM and ignore flash for now
uconf_defaults();
} else {
// reload the latest save data found
eep16_read(uconf_flash_offset * FLASH_UCONF_BYTES, (uint8_t *)&uconf, FLASH_UCONF_BYTES);
}
}
void uconf_clear()
{
uint8_t i;
uint8_t c[128];
uint16_t fsize;
uint16_t faddr;
memset(&c, 0xff, sizeof(c));
uconf_flash_offset = FLASH_RSVD_PAGES;
for (i = 0; i < FLASH_RSVD_PAGES; i++) {
// write config data
fsize = FLASH_UCONF_BYTES;
faddr = i * FLASH_UCONF_BYTES;
while (fsize) {
while (!eep16_is_ready());
eep16_write(faddr, c, (fsize > 128) ? 128 : fsize);
if (fsize <= 128) break;
fsize -= 128;
faddr += 128;
}
}
}
void uconf_write()

View File

@ -84,9 +84,10 @@ typedef struct UserConf {
uint8_t padding0; // 52
uint8_t ledprog_rgb[16]; // 68
uint8_t ledprog_rgb_data[16][8]; // 196
uint8_t padding1[50]; // 246
uint16_t lsens_dark_thresh; // 248
uint16_t menu_timeout; // 250
uint8_t padding1[48]; // 244
uint16_t lsens_dark_thresh; // 246
uint16_t menu_timeout; // 248
uint16_t dim_timeout; // 250
uint16_t sleep_timeout; // 252
uint16_t tempcx10_offset; // 253-254
uint16_t checksum; // 255-256
@ -105,9 +106,12 @@ extern uint8_t temp_degc_decimal;
// non-volatile functions
void uconf_load();
void uconf_write();
void uconf_clear();
// volatile functions
void uconf_defaults();