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
0 commit comments