Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centralize eeprom usage #264

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Bruce3_*.bin
bruce.conf
_*.sh
tmp
ISSUES.md
171 changes: 171 additions & 0 deletions src/core/eeprom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#include "eeprom.h"
#include "core/globals.h"
#include "core/settings.h"


/*********************************************************************
** Function: load_eeprom
** Load EEPROM data
*********************************************************************/
void load_eeprom() {
EEPROM.begin(EEPROMSIZE); // open eeprom

rotation = EEPROM.read(EEPROM_ROT);
dimmerSet = EEPROM.read(EEPROM_DIMMER);
bright = EEPROM.read(EEPROM_BRIGHT);
IrTx = EEPROM.read(EEPROM_IR_TX);
IrRx = EEPROM.read(EEPROM_IR_RX);
RfTx = EEPROM.read(EEPROM_RF_TX);
RfRx = EEPROM.read(EEPROM_RF_RX);
tmz = EEPROM.read(EEPROM_TMZ);
FGCOLOR = EEPROM.read(EEPROM_FGCOLOR0) << 8 | EEPROM.read(EEPROM_FGCOLOR1);
RfModule = EEPROM.read(EEPROM_RF_MODULE);
RfidModule = EEPROM.read(EEPROM_RFID_MODULE);

log_i("\
\n*-*EEPROM Settings*-* \
\n- rotation =%03d, \
\n- dimmerSet =%03d, \
\n- Brightness=%03d, \
\n- IR Tx Pin =%03d, \
\n- IR Rx Pin =%03d, \
\n- RF Tx Pin =%03d, \
\n- RF Rx Pin =%03d, \
\n- Time Zone =%03d, \
\n- FGColor =0x%04X \
\n- RfModule =%03d, \
\n- RfidModule=%03d, \
\n*-*-*-*-*-*-*-*-*-*-*",
rotation, dimmerSet, bright,IrTx, IrRx, RfTx, RfRx, tmz, FGCOLOR, RfModule, RfidModule
);

if (
rotation > 3
|| dimmerSet > 60
|| bright > 100
|| IrTx > 100
|| IrRx > 100
|| RfRx > 100
|| RfTx > 100
|| tmz > 24
) {
rotation = ROTATION;
dimmerSet = 10;
bright = 100;
IrTx = LED;
IrRx = GROVE_SCL;
RfTx = GROVE_SDA;
RfRx = GROVE_SCL;
FGCOLOR = 0xA80F;
tmz = 0;
RfModule = M5_RF_MODULE;
RfidModule = M5_RFID2_MODULE;

EEPROM.write(EEPROM_ROT, rotation);
EEPROM.write(EEPROM_DIMMER, dimmerSet);
EEPROM.write(EEPROM_BRIGHT, bright);
EEPROM.write(EEPROM_IR_TX, IrTx);
EEPROM.write(EEPROM_IR_RX, IrRx);
EEPROM.write(EEPROM_RF_TX, RfTx);
EEPROM.write(EEPROM_RF_RX, RfRx);
EEPROM.write(EEPROM_TMZ, tmz);
EEPROM.write(EEPROM_FGCOLOR0, int((FGCOLOR >> 8) & 0x00FF));
EEPROM.write(EEPROM_FGCOLOR1, int(FGCOLOR & 0x00FF));
EEPROM.write(EEPROM_RF_MODULE, RfModule);
EEPROM.write(EEPROM_RFID_MODULE, RfidModule);
EEPROM.writeString(20,"");

EEPROM.commit(); // Store data to EEPROM
log_w("One of the eeprom values is invalid");
}

EEPROM.end();

setBrightness(bright, false);
}


/*********************************************************************
** Function: write_eeprom
** write EEPROM data
*********************************************************************/
void write_eeprom(int address, uint8_t val) {
Serial.printf("Writing EEPROM address %d value %d\n", address, val);
EEPROM.begin(EEPROMSIZE); // open eeprom
EEPROM.write(address, val); //set the byte
EEPROM.commit(); // Store data to EEPROM
EEPROM.end(); // Free EEPROM memory
Serial.println("Write EEPROM success");
}


/*********************************************************************
** Function: write_eeprom_string
** write EEPROM string data
*********************************************************************/
void write_eeprom_string(int address, String val) {
Serial.printf("Writing EEPROM address %d value %d\n", address, val);
EEPROM.begin(EEPROMSIZE); // open eeprom
EEPROM.writeString(address, val); //set the byte
EEPROM.commit(); // Store data to EEPROM
EEPROM.end(); // Free EEPROM memory
Serial.println("Write EEPROM success");
}


/*********************************************************************
** Function: read_eeprom
** read EEPROM data
*********************************************************************/
uint8_t read_eeprom(int address) {
Serial.printf("Reading EEPROM address %d\n", address);
EEPROM.begin(EEPROMSIZE); // open eeprom
uint8_t value = EEPROM.read(address); //set the byte
EEPROM.end(); // Free EEPROM memory
Serial.printf("EEPROM value = %d\n", value);
return value;
}


/*********************************************************************
** Function: read_eeprom_string
** read EEPROM string data
*********************************************************************/
String read_eeprom_string(int address) {
Serial.printf("Reading EEPROM address %d\n", address);
EEPROM.begin(EEPROMSIZE); // open eeprom
String value = EEPROM.readString(address); //set the byte
EEPROM.end(); // Free EEPROM memory
Serial.println("EEPROM value = "+value);
return value;
}


void sync_eeprom_values(void) {
int count = 0;

EEPROM.begin(EEPROMSIZE); // open eeprom

if(EEPROM.read(EEPROM_ROT) != rotation) { EEPROM.write(EEPROM_ROT, rotation); count++; }
if(EEPROM.read(EEPROM_DIMMER) != dimmerSet) { EEPROM.write(EEPROM_DIMMER, dimmerSet); count++; }
if(EEPROM.read(EEPROM_BRIGHT) != bright) { EEPROM.write(EEPROM_BRIGHT, bright); count++; }

if(EEPROM.read(EEPROM_IR_TX) != IrTx) { EEPROM.write(EEPROM_IR_TX, IrTx); count++; }
if(EEPROM.read(EEPROM_IR_RX) != IrRx) { EEPROM.write(EEPROM_IR_RX, IrRx); count++; }
if(EEPROM.read(EEPROM_RF_TX) != RfTx) { EEPROM.write(EEPROM_RF_TX, RfTx); count++; }
if(EEPROM.read(EEPROM_RF_RX) != RfRx) { EEPROM.write(EEPROM_RF_RX, RfRx); count++; }
if(EEPROM.read(EEPROM_TMZ) != tmz) { EEPROM.write(EEPROM_TMZ, tmz); count++; }
if(EEPROM.read(EEPROM_FGCOLOR0) !=(int((FGCOLOR >> 8) & 0x00FF))) {EEPROM.write(EEPROM_FGCOLOR0, int((FGCOLOR >> 8) & 0x00FF)); count++; }
if(EEPROM.read(EEPROM_FGCOLOR1) != int(FGCOLOR & 0x00FF)) { EEPROM.write(EEPROM_FGCOLOR1, int(FGCOLOR & 0x00FF)); count++; }
if(EEPROM.read(EEPROM_RF_MODULE) != RfModule) { EEPROM.write(EEPROM_RF_MODULE, RfModule); count++; }
if(EEPROM.read(EEPROM_RFID_MODULE) != RfidModule) { EEPROM.write(EEPROM_RFID_MODULE, RfidModule); count++; }
// TODO: add RfFreq

//If something changed, saves the changes on EEPROM.
if(count > 0) {
if(!EEPROM.commit()) log_i("fail to write EEPROM"); // Store data to EEPROM
else log_i("Wrote new conf to EEPROM");
}

EEPROM.end();
}
58 changes: 58 additions & 0 deletions src/core/eeprom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef __BRUCE_EEPROM_H__
#define __BRUCE_EEPROM_H__

#include <EEPROM.h>
#include <Arduino.h>


/*
EEPROM ADDRESSES MAP

0 Rotation 16 32 Pass 48 Pass 64 Pass 80 Pass 96 112
1 Dim(N/L) 17 33 Pass 49 Pass 65 Pass 81 Pass 97 113
2 Bright 18 34 Pass 50 Pass 66 Pass 82 Pass 98 114
3 - 19 35 Pass 51 Pass 67 Pass 83 Pass 99 115
4 - 20 Pass 36 Pass 52 Pass 68 Pass 84 Pass 100 116
5 - 21 Pass 37 Pass 53 Pass 69 Pass 85 101 117
6 IrTX 22 Pass 38 Pass 54 Pass 70 Pass 86 102 118 (L-odd)
7 IrRx 23 Pass 39 Pass 55 Pass 71 Pass 87 103 119 (L-odd)
8 RfTX 24 Pass 40 Pass 56 Pass 72 Pass 88 104 120 (L-even)
9 RfRx 25 Pass 41 Pass 57 Pass 73 Pass 89 105 121 (L-even)
10 TimeZone 26 Pass 42 Pass 58 Pass 74 Pass 90 106 122 (L-BGCOLOR)
11 FGCOLOR 27 Pass 43 Pass 59 Pass 75 Pass 91 107 123 (L-BGCOLOR)
12 FGCOLOR 28 Pass 44 Pass 60 Pass 76 Pass 92 108 124 (L-FGCOLOR)
13 RfModule 29 Pass 45 Pass 61 Pass 77 Pass 93 109 125 (L-FGCOLOR)
14 RfidModule 30 Pass 46 Pass 62 Pass 78 Pass 94 110 126 (L-AskSpiffs)
15 31 Pass 47 Pass 63 Pass 79 Pass 95 111 127 (L-OnlyBins)

From 1 to 5: Nemo shared addresses
(L -*) stands for Launcher addresses
*/


#define EEPROM_ROT (0)
#define EEPROM_DIMMER (1)
#define EEPROM_BRIGHT (2)
#define EEPROM_IR_TX (6)
#define EEPROM_IR_RX (7)
#define EEPROM_RF_TX (8)
#define EEPROM_RF_RX (9)
#define EEPROM_TMZ (10)
#define EEPROM_FGCOLOR0 (11)
#define EEPROM_FGCOLOR1 (12)
#define EEPROM_RF_MODULE (13)
#define EEPROM_RFID_MODULE (14)
#define EEPROM_PWD (20)


void load_eeprom(void);

void write_eeprom(int address, uint8_t val);
void write_eeprom_string(int address, String val);

uint8_t read_eeprom(int address);
String read_eeprom_string(int address);

void sync_eeprom_values(void);

#endif
44 changes: 1 addition & 43 deletions src/core/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,7 @@ while(1) {
loopOptions(options);
...
}

/*********************************************************************
** Function: readFGColorFromEEPROM
** reads the foreground color from EEPROM
** if the value is not set, it will use the default value
**********************************************************************/
void readFGCOLORFromEEPROM() {
int colorEEPROM;

EEPROM.begin(EEPROMSIZE);
EEPROM.get(5, colorEEPROM);

switch(colorEEPROM){
case 0:
FGCOLOR = TFT_PURPLE+0x3000;
break;
case 1:
FGCOLOR = TFT_WHITE;
break;
case 2:
FGCOLOR = TFT_RED;
break;
case 3:
FGCOLOR = TFT_DARKGREEN;
break;
case 4:
FGCOLOR = TFT_BLUE;
break;
case 5:
FGCOLOR = TFT_YELLOW;
break;
case 7:
FGCOLOR = TFT_ORANGE;
break;
default:
FGCOLOR = TFT_PURPLE+0x3000;
EEPROM.put(5, 0);
EEPROM.commit();
break;

}
EEPROM.end(); // Free EEPROM memory
}
*/


void backToMenu() {
Expand Down
8 changes: 5 additions & 3 deletions src/core/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ extern char16_t FGCOLOR;
#include <NTPClient.h>
#include <Timezone.h>
#include <ESP32Time.h>
#include <EEPROM.h>
#include <ArduinoJson.h>

#if defined (STICK_C_PLUS) || defined (STICK_C)
Expand Down Expand Up @@ -136,8 +135,6 @@ extern int dimmerSet;
extern int devMode;
extern int soundEnabled;

void readFGCOLORFromEEPROM();

void backToMenu();

void updateTimeStr(struct tm timeInfo);
Expand All @@ -157,5 +154,10 @@ enum RFIDModules {
PN532_SPI_MODULE = 2,
};

enum RFModules {
M5_RF_MODULE = 0,
CC1101_SPI_MODULE = 1,
};

void setup_gpio();

Loading
Loading