Skip to content

Commit ac65493

Browse files
committed
EEPROM on SPIFFS
1 parent 0cba631 commit ac65493

13 files changed

+227
-30
lines changed

src/BrewLogger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ BrewLogger::BrewLogger(void){
379379
}
380380
#if SupportPressureTransducer
381381
// pressure, if any
382-
DBG_PRINTF("Pressure mode:%d _lastPressureReading:%d, current:%d\n",PressureMonitor.mode(),_lastPressureReading,PressureEncode(PressureMonitor.currentPsi()));
382+
//DBG_PRINTF("Pressure mode:%d _lastPressureReading:%d, current:%d\n",PressureMonitor.mode(),_lastPressureReading,PressureEncode(PressureMonitor.currentPsi()));
383383
if(PressureMonitor.mode() != PMModeOff){
384384
int16_t pressure = PressureEncode(PressureMonitor.currentPsi());
385385
if(pressure != _lastPressureReading){

src/BrewPiLess.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,10 @@ void brewpi_setup()
15791579
eepromAccess.set_manual_commit(false); // TODO - Move this where it should actually belong (a class constructor)
15801580
#endif
15811581

1582+
#if FS_EEPROM
1583+
eepromAccess.begin();
1584+
#endif
1585+
15821586
#if BREWPI_BUZZER
15831587
buzzer.init();
15841588
buzzer.beep(2, 500);

src/Config.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@
262262
#define SettableMinimumCoolTime true
263263
//#endif
264264

265+
#if ESP32
266+
#define FS_EEPROM true
267+
#endif
268+
265269
#define BPL_VERSION "3.7"
266270

267271
//////////////////////////////////////////////////////////////////////////
@@ -288,19 +292,19 @@
288292
#define PIN_SCL 22
289293

290294

291-
#define oneWirePin 5
295+
#define oneWirePin 23
292296

293297
#define actuatorPin1 26
294298
#define actuatorPin2 16
295299
#define actuatorPin3 17
296300
#define actuatorPin4 19
297301

298-
#define BuzzPin 23
302+
#define BuzzPin 18
299303

300304
// 34,35,26,39 input only
301305
#define rotaryAPin 32
302306
#define rotaryBPin 33
303-
#define rotarySwitchPin 27
307+
#define rotarySwitchPin 25
304308

305309
#else // #ifdef ESP32
306310
#define NODEMCU_PIN_A0 17 // Analog

src/ESPEepromAccess.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818

19-
#if !defined(ESP8266) && !defined(ESP32)
19+
#if !defined(ESP8266)
2020
// Generate an error if we have been incorrectly included in an Arduino build
2121
#error Incorrect processor type!
2222
#endif

src/EepromAccess.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
#ifdef ARDUINO
2626
// ARDUINO is defined for the ESP8266 implementation as well. Specifically break out
2727
// the ESP8266 implementation, then default to Arduino if we don't use it.
28+
#if defined(ESP32)
29+
#include "FSEepromAccess.h"
30+
typedef FSEepromAccess EepromAccess;
2831

29-
#if defined(ESP8266) || defined(ESP32)
32+
#elif defined(ESP8266)
3033
#include "ESPEepromAccess.h"
3134
typedef ESPEepromAccess EepromAccess;
3235
#else

src/EepromManager.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,25 @@ EepromManager::EepromManager()
3939

4040
bool EepromManager::hasSettings()
4141
{
42+
#if FS_EEPROM
43+
return eepromAccess.hasSettings();
44+
#else
4245
uint8_t version = eepromAccess.readByte(pointerOffset(version));
4346
return (version==EEPROM_FORMAT_VERSION);
47+
#endif
4448
}
4549

4650
void EepromManager::zapEeprom()
4751
{
52+
#if FS_EEPROM
53+
eepromAccess.zapData();
54+
#else
4855
for (uint16_t offset=0; offset<EepromFormat::MAX_EEPROM_SIZE; offset++)
4956
eepromAccess.writeByte(offset, 0xFF);
5057
#ifdef ESP8266
5158
eepromAccess.commit();
5259
#endif
53-
60+
#endif
5461
}
5562

5663

@@ -60,7 +67,10 @@ void EepromManager::initializeEeprom()
6067
// for (uint16_t offset=0; offset<EepromFormat::MAX_EEPROM_SIZE; offset++)
6168
// eepromAccess.writeByte(offset, 0);
6269
#ifdef ESP8266
70+
#if !FS_EEPROM
71+
6372
eepromAccess.set_manual_commit(true);
73+
#endif
6474
#endif
6575
zapEeprom();
6676

@@ -70,6 +80,11 @@ void EepromManager::initializeEeprom()
7080
tempControl.loadDefaultConstants();
7181
tempControl.loadDefaultSettings();
7282

83+
#if FS_EEPROM
84+
eepromAccess.initializeSetting();
85+
tempControl.storeConstants(0);
86+
tempControl.storeSettings(0);
87+
#else
7388
// write the default constants
7489
for (uint8_t c=0; c<EepromFormat::MAX_CHAMBERS; c++) {
7590
eptr_t pv = pointerOffset(chambers)+(c*sizeof(ChamberBlock)) ;
@@ -84,15 +99,19 @@ void EepromManager::initializeEeprom()
8499

85100
// set the version flag - so that storeDevice will work
86101
eepromAccess.writeByte(0, EEPROM_FORMAT_VERSION);
87-
102+
#endif
103+
88104
saveDefaultDevices();
89105
// set state to startup
90106
tempControl.init();
91107

92108
#ifdef ESP8266
109+
#if !FS_EEPROM
110+
93111
eepromAccess.set_manual_commit(false);
94112
eepromAccess.commit();
95113
#endif
114+
#endif
96115
}
97116

98117
uint8_t EepromManager::saveDefaultDevices()
@@ -156,16 +175,26 @@ void EepromManager::storeTempSettings()
156175
bool EepromManager::fetchDevice(DeviceConfig& config, uint8_t deviceIndex)
157176
{
158177
bool ok = (hasSettings() && deviceIndex<EepromFormat::MAX_DEVICES);
178+
#if FS_EEPROM
179+
if (ok)
180+
eepromAccess.readDeviceDefinition(config, deviceIndex, sizeof(DeviceConfig));
181+
#else
159182
if (ok)
160183
eepromAccess.readDeviceDefinition(config, pointerOffset(devices)+sizeof(DeviceConfig)*deviceIndex, sizeof(DeviceConfig));
184+
#endif
161185
return ok;
162186
}
163187

164188
bool EepromManager::storeDevice(const DeviceConfig& config, uint8_t deviceIndex)
165189
{
166190
bool ok = (hasSettings() && deviceIndex<EepromFormat::MAX_DEVICES);
191+
#if FS_EEPROM
192+
if (ok)
193+
eepromAccess.writeDeviceDefinition(deviceIndex, config, sizeof(DeviceConfig));
194+
#else
167195
if (ok)
168196
eepromAccess.writeDeviceDefinition(pointerOffset(devices)+sizeof(DeviceConfig)*deviceIndex, config, sizeof(DeviceConfig));
197+
#endif
169198
return ok;
170199
}
171200

src/EepromManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class EepromManager {
7676

7777
static uint8_t saveDefaultDevices();
7878
};
79-
79+
#if 0
8080
class EepromStream
8181
{
8282
eptr_t pv;
@@ -103,5 +103,5 @@ class EepromStream
103103
eepromAccess.writeDeviceDefinition(pv, source, size);
104104
}
105105
};
106-
106+
#endif
107107
extern EepromManager eepromManager;

src/FSEepromAccess.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "Arduino.h"
2+
#include "FSEepromAccess.h"
3+
4+
DeviceConfig FSEepromAccess::devices[MAX_DEVICE_SLOT];

src/FSEepromAccess.h

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2019 Vito Tai
3+
*
4+
* This is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this file. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
#include "Config.h"
18+
#include "EepromTypes.h"
19+
#include "EepromStructs.h"
20+
#include "EepromFormat.h"
21+
#ifndef FSEepromAccess_H
22+
#define FSEepromAccess_H
23+
#if defined(ESP32)
24+
#include <SPIFFS.h>
25+
#endif
26+
27+
#define File_ControlSettings "/eeprom_control_setting"
28+
#define File_ControlConstant "/eeprom_control_constant"
29+
#define File_DeviceDefinition "/eeprom_device_definition"
30+
31+
class FSEepromAccess
32+
{
33+
static bool exists(const char* file){
34+
return SPIFFS.exists(file);
35+
}
36+
static void remove(const char* file){
37+
if(exists(file)) SPIFFS.remove(file);
38+
}
39+
static bool readFromFile(const char* filename,char* target,size_t size){
40+
if(!exists(filename)) return false;
41+
42+
File file = SPIFFS.open(filename, "r");
43+
if (file) {
44+
file.readBytes(target, size);
45+
file.close();
46+
return true;
47+
}
48+
DBG_PRINTF("read %s error:%d\n",filename);
49+
return false;
50+
}
51+
static bool writeToFile(const char* filename,const uint8_t* source,size_t size){
52+
File file = SPIFFS.open(filename, "w+b");
53+
if (file) {
54+
file.write(source, size);
55+
file.close();
56+
return true;
57+
} else {
58+
DBG_PRINTF("read %s error:%d\n",filename);
59+
return false;
60+
}
61+
}
62+
63+
static DeviceConfig devices[MAX_DEVICE_SLOT];
64+
65+
static void saveDeviceDefinition(){
66+
writeToFile(File_DeviceDefinition, (const uint8_t*) devices,sizeof(devices));
67+
}
68+
69+
static void loadDeviceDefinition(){
70+
if(!readFromFile(File_DeviceDefinition,(char*)&devices,sizeof(devices)))
71+
memset((char*)&devices,0,sizeof(devices));
72+
}
73+
public:
74+
75+
static void begin(){
76+
loadDeviceDefinition();
77+
}
78+
79+
static bool hasSettings(void){
80+
return exists(File_ControlSettings) && exists(File_ControlConstant);
81+
// if hasSettings() returns false,
82+
// ControlConstant and ControlSettings will be set to ZERO.
83+
}
84+
85+
static void initializeSetting(){
86+
}
87+
88+
static void zapData(){
89+
remove(File_ControlSettings);
90+
remove(File_ControlConstant);
91+
remove(File_DeviceDefinition);
92+
}
93+
94+
static void readControlSettings(ControlSettings& target, eptr_t offset, uint16_t size) {
95+
if(!readFromFile(File_ControlSettings,(char*)&target,size))
96+
memset(&target,0,size);
97+
}
98+
99+
static void writeControlSettings(eptr_t target, ControlSettings& source, uint16_t size) {
100+
writeToFile(File_ControlSettings,(const uint8_t*)&source,size);
101+
DBG_PRINTF("Write Control Settings:%d\n",size);
102+
}
103+
104+
static void readControlConstants(ControlConstants& target, eptr_t offset, uint16_t size) {
105+
if(!readFromFile(File_ControlConstant,(char*)&target,size))
106+
memset(&target,0,size);
107+
}
108+
109+
static void writeControlConstants(eptr_t target, ControlConstants& source, uint16_t size) {
110+
writeToFile(File_ControlConstant,(const uint8_t*) &source, size);
111+
DBG_PRINTF("Write Control constants:%d\n",size);
112+
}
113+
114+
static void readDeviceDefinition(DeviceConfig& target, uint8_t deviceIndex, uint16_t size) {
115+
memcpy((void*)&target,(void*) &devices[deviceIndex],size);
116+
}
117+
118+
static void writeDeviceDefinition(uint8_t deviceIndex, const DeviceConfig& source, uint16_t size) {
119+
memcpy((void*) &devices[deviceIndex],(void*)&source,size);
120+
DBG_PRINTF("write device:%d size:\n",deviceIndex,size);
121+
saveDeviceDefinition();
122+
}
123+
};
124+
#endif

src/MqttRemoteControl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ MqttRemoteControl::MqttRemoteControl(){
4040
this->_onConnect();
4141
});
4242
_client.onDisconnect([this](AsyncMqttClientDisconnectReason reason){
43-
DBG_PRINTF("\n***MQTT:disc:%d\n",reason);
43+
DBG_PRINTF("\n***MQTT:disc:%d\n",(int)reason);
4444
this->_onDisconnect();
4545
});
4646
_client.onMessage([this](char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total){

0 commit comments

Comments
 (0)