aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--spaghetti-monster/darkhorse.c30
-rw-r--r--spaghetti-monster/fsm-eeprom.c48
-rw-r--r--spaghetti-monster/fsm-eeprom.h6
3 files changed, 63 insertions, 21 deletions
diff --git a/spaghetti-monster/darkhorse.c b/spaghetti-monster/darkhorse.c
index 2aa1b7b..0d02481 100644
--- a/spaghetti-monster/darkhorse.c
+++ b/spaghetti-monster/darkhorse.c
@@ -28,7 +28,7 @@
#define BATTCHECK_4bars
#define DONT_DELAY_AFTER_BATTCHECK
#define USE_EEPROM
-#define EEPROM_BYTES 5
+#define EEPROM_WL_BYTES 5
#include "spaghetti-monster.h"
// FSM states
@@ -305,25 +305,25 @@ void strobe(uint8_t level, uint16_t ontime, uint16_t offtime) {
}
void load_config() {
- if (load_eeprom()) {
- H1 = !(!(eeprom[0] & 0b00000100));
- M1 = !(!(eeprom[0] & 0b00000010));
- L1 = !(!(eeprom[0] & 0b00000001));
- H2 = eeprom[1];
- M2 = eeprom[2];
- L2 = eeprom[3];
- strobe_beacon_mode = eeprom[4];
+ if (load_wl_eeprom()) {
+ H1 = !(!(eeprom_wl[0] & 0b00000100));
+ M1 = !(!(eeprom_wl[0] & 0b00000010));
+ L1 = !(!(eeprom_wl[0] & 0b00000001));
+ H2 = eeprom_wl[1];
+ M2 = eeprom_wl[2];
+ L2 = eeprom_wl[3];
+ strobe_beacon_mode = eeprom_wl[4];
}
}
void save_config() {
- eeprom[0] = (H1<<2) | (M1<<1) | (L1);
- eeprom[1] = H2;
- eeprom[2] = M2;
- eeprom[3] = L2;
- eeprom[4] = strobe_beacon_mode;
+ eeprom_wl[0] = (H1<<2) | (M1<<1) | (L1);
+ eeprom_wl[1] = H2;
+ eeprom_wl[2] = M2;
+ eeprom_wl[3] = L2;
+ eeprom_wl[4] = strobe_beacon_mode;
- save_eeprom();
+ save_wl_eeprom();
}
void setup() {
diff --git a/spaghetti-monster/fsm-eeprom.c b/spaghetti-monster/fsm-eeprom.c
index 5451baf..e464785 100644
--- a/spaghetti-monster/fsm-eeprom.c
+++ b/spaghetti-monster/fsm-eeprom.c
@@ -23,9 +23,6 @@
#include "fsm-eeprom.h"
#if EEPROM_BYTES > 0
-#if EEPROM_BYTES >= (EEPSIZE/2)
-#error Requested EEPROM_BYTES too big.
-#endif
uint8_t eeprom[EEPROM_BYTES];
uint8_t load_eeprom() {
@@ -55,15 +52,54 @@ void save_eeprom() {
#endif
#if EEPROM_WL_BYTES > 0
-#if EEPROM_WL_BYTES >= (EEPSIZE/4)
-#error Requested EEPROM_WL_BYTES too big.
-#endif
uint8_t eeprom_wl[EEPROM_WL_BYTES];
+EEP_OFFSET_T eep_wl_prev_offset;
uint8_t load_wl_eeprom() {
+ cli();
+ // check if eeprom has been initialized; abort if it hasn't
+ uint8_t found = 0;
+ EEP_OFFSET_T offset;
+ for(offset = 0;
+ offset < EEP_WL_SIZE - EEPROM_WL_BYTES - 1;
+ offset += (EEPROM_WL_BYTES + 1)) {
+ if (eeprom_read_byte((uint8_t *)offset) == EEP_MARKER) {
+ found = 1;
+ eep_wl_prev_offset = offset;
+ break;
+ }
+ }
+
+ if (found) {
+ // load the actual data
+ for(uint8_t i=0; i<EEPROM_WL_BYTES; i++) {
+ eeprom_wl[i] = eeprom_read_byte((uint8_t *)(offset+1+i));
+ }
+ }
+ sei();
+ return found;
}
void save_wl_eeprom() {
+ cli();
+ // erase old state
+ EEP_OFFSET_T offset = eep_wl_prev_offset;
+ for (uint8_t i = 0; i < EEPROM_WL_BYTES+1; i ++) {
+ eeprom_update_byte((uint8_t *)offset+i, 0xFF);
+ }
+
+ // save new state
+ offset += EEPROM_WL_BYTES+1;
+ if (offset > EEP_WL_SIZE-EEPROM_WL_BYTES-1) offset = 0;
+ eep_wl_prev_offset = offset;
+ // marker byte
+ eeprom_update_byte((uint8_t *)offset, EEP_MARKER);
+ offset ++;
+ // user data
+ for(uint8_t i=0; i<EEPROM_WL_BYTES; i++, offset++) {
+ eeprom_update_byte((uint8_t *)(offset), eeprom_wl[i]);
+ }
+ sei();
}
#endif
diff --git a/spaghetti-monster/fsm-eeprom.h b/spaghetti-monster/fsm-eeprom.h
index 3d34f23..c35c822 100644
--- a/spaghetti-monster/fsm-eeprom.h
+++ b/spaghetti-monster/fsm-eeprom.h
@@ -33,6 +33,9 @@
#endif
#if EEPROM_BYTES > 0
+#if EEPROM_BYTES >= (EEPSIZE/2)
+#error Requested EEPROM_BYTES too big.
+#endif
uint8_t eeprom[EEPROM_BYTES];
uint8_t load_eeprom(); // returns 1 for success, 0 for no data found
void save_eeprom();
@@ -40,6 +43,9 @@ void save_eeprom();
#endif
#if EEPROM_WL_BYTES > 0
+#if EEPROM_WL_BYTES >= (EEPSIZE/4)
+#error Requested EEPROM_WL_BYTES too big.
+#endif
uint8_t eeprom_wl[EEPROM_WL_BYTES];
uint8_t load_wl_eeprom(); // returns 1 for success, 0 for no data found
void save_wl_eeprom();
If you find this content useful please consider a small donation via bitcoin: bitcoin:1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2?amount=0.001&message=Donation Scraping git contents via cgit is very resource intensive. The continued hosting of these git repositories is entirely financed by your bitcoin contributions. For donations of a least 1 mBTC you include your IP address in the message of your bitcoin donation. This will remove this message in the future for requests from your IP, which will significantly reduce token usage. In any case, any contributions to bitcoin address 1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2 are very welcome. Thanks in advance for you contribution to a self-sustaining ecosystem.