Skip to content

Commit

Permalink
feat: better eeprom save struct (now always in memory)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipton committed Jun 6, 2024
1 parent c3c27a8 commit cc6cb0c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
2 changes: 1 addition & 1 deletion firmware/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void setup() {
Logger.begin(&Serial);
EEPROM.begin(128);
Wire.begin(LCD_SDA, LCD_SCL);
readState(true);
readState();
clearDisplay(0);
lcdInit();

Expand Down
65 changes: 31 additions & 34 deletions firmware/src/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,64 +72,61 @@ struct EEPROMState {
int penalty;

float batteryOffset;
};
} eeprom_state;

void stateDefault() {
uuid.generate();
strcpy(state.solveSessionId, uuid.toCharArray());
}

void saveState() {
EEPROMState s = {0};
strcpy(s.solveSessionId, state.solveSessionId);
s.solveTime = state.solveTime;
s.penalty = state.penalty;
s.competitorCardId = state.competitorCardId;
s.inspectionStarted = state.inspectionStarted;
s.inspectionEnded = state.inspectionEnded;
s.saveTime = getEpoch();
s.batteryOffset = batteryVoltageOffset;
strcpy(eeprom_state.solveSessionId, state.solveSessionId);
eeprom_state.solveTime = state.solveTime;
eeprom_state.penalty = state.penalty;
eeprom_state.competitorCardId = state.competitorCardId;
eeprom_state.inspectionStarted = state.inspectionStarted;
eeprom_state.inspectionEnded = state.inspectionEnded;
eeprom_state.saveTime = getEpoch();
eeprom_state.batteryOffset = batteryVoltageOffset;

EEPROM.write(0, (uint8_t)sizeof(EEPROMState));
EEPROM.put(1, s);
EEPROM.put(1, eeprom_state);
EEPROM.commit();
}

void readState(bool skipEpoch = false) {
void readState() {
uint8_t size = EEPROM.read(0);

if (size != sizeof(EEPROMState) && !skipEpoch) {
if (size != sizeof(EEPROMState)) {
Logger.println("Loading default state...");
stateDefault();
return;
}

EEPROMState _state = {0};
EEPROM.get(1, _state);

if(_state.batteryOffset > -3 && _state.batteryOffset < 3) {
batteryVoltageOffset = _state.batteryOffset;
}

if (skipEpoch) return;
unsigned long currentEpoch = getEpoch();
if (currentEpoch - _state.saveTime > SAVE_TIME_RESET) {
return;
EEPROM.get(1, eeprom_state);
if(eeprom_state.batteryOffset > -3 && eeprom_state.batteryOffset < 3) {
batteryVoltageOffset = eeprom_state.batteryOffset;
}

strcpy(state.solveSessionId, _state.solveSessionId);
state.solveTime = _state.solveTime;
state.lastSolveTime = _state.solveTime;
state.penalty = _state.penalty;
state.competitorCardId = _state.competitorCardId;
state.inspectionStarted = _state.inspectionStarted;
state.inspectionEnded = _state.inspectionEnded;
}

void initState() {
uuid.seed(getEpoch(), getEspId());
unsigned long currentEpoch = 0;
while((currentEpoch = getEpoch()) == 0) {
webSocket.loop();
delay(5);
}

uuid.seed(currentEpoch, getEspId());
if(currentEpoch - eeprom_state.saveTime < SAVE_TIME_RESET) {
strcpy(state.solveSessionId, eeprom_state.solveSessionId);
state.solveTime = eeprom_state.solveTime;
state.lastSolveTime = eeprom_state.solveTime;
state.penalty = eeprom_state.penalty;
state.competitorCardId = eeprom_state.competitorCardId;
state.inspectionStarted = eeprom_state.inspectionStarted;
state.inspectionEnded = eeprom_state.inspectionEnded;
}

readState();
if (state.solveTime > 0) {
state.currentScene = SCENE_FINISHED_TIME;
} else {
Expand Down

0 comments on commit cc6cb0c

Please sign in to comment.