-
Notifications
You must be signed in to change notification settings - Fork 5
/
Webrelay_eeprom.h
288 lines (236 loc) · 9.32 KB
/
Webrelay_eeprom.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
File to define the eeprom variable save/restor operations for the ASCOM switch web driver
*/
#ifndef _WEBRELAY_EEPROM_H_
#define _WEBRELAY_EEPROM_H_
#include "Webrelay_common.h"
#include "DebugSerial.h"
#include "eeprom.h"
#include "EEPROMAnything.h"
static const byte magic = '*';
//definitions
void setDefaults(void );
void saveToEeprom(void);
void setupFromEeprom(void);
/*
* Write default values into variables - don't save yet.
*/
void setDefaults( void )
{
int i=0;
DEBUGSL1( "Eeprom setDefaults: entered");
if ( myHostname != nullptr )
free ( myHostname );
myHostname = (char* )calloc( sizeof (char), MAX_NAME_LENGTH );
strcpy( myHostname, defaultHostname);
//MQTT thisID copied from hostname
if ( thisID != nullptr )
free ( thisID );
thisID = (char*) calloc( MAX_NAME_LENGTH, sizeof( char) );
strcpy ( thisID, myHostname );
udpPort = ALPACA_DISCOVERY_PORT;
if ( Location != nullptr )
free ( Location );
Location = (char*) calloc( MAX_NAME_LENGTH, sizeof( char) );
strcpy( Location, "Default Location" );
//Allocate storage for Number of Switch settings
numSwitches = defaultNumSwitches;
//Create array of pointers to switchEntry descriptor blocks
switchEntry = (SwitchEntry**) calloc( sizeof( SwitchEntry* ), numSwitches );
//Create individual descriptor blocks
for ( i=0; i< numSwitches ; i++ )
{
switchEntry[i] = (SwitchEntry*) calloc( sizeof( SwitchEntry ), 1 );
}
for ( i=0; i< numSwitches ; i++ )
{
String tempName = "Switch_";
switchEntry[i]->description = (char*) calloc( MAX_NAME_LENGTH, sizeof(char) );
strcpy( switchEntry[i]->description, "Default description" );
tempName.concat( i );
switchEntry[i]->switchName = (char*) calloc( MAX_NAME_LENGTH, sizeof(char) );
strcpy( switchEntry[i]->switchName, tempName.c_str() );
switchEntry[i]->writeable = true;
switchEntry[i]->type = SWITCH_RELAY_NO;
switchEntry[i]->pin = 0;
switchEntry[i]->min = 0.0F;
switchEntry[i]->max = 1.0F;
switchEntry[i]->step = 1.0F;
switchEntry[i]->value = 0.0F;
}
#if defined DEBUG_ESP_MH
//Read them back for checking - also available via status command.
Serial.printf( "Switches: %i \n" , numSwitches );
Serial.printf( "Hostname: %s \n" , myHostname );
Serial.printf( "Discovery port: %i \n" , udpPort );
for ( i=0;i < numSwitches; i++ )
{
String output;
Serial.printf( "Switch %i: \n" , i) ;
Serial.printf( "Desc: %s \n" , switchEntry[i]->description );
Serial.printf( "Name: %s \n" , switchEntry[i]->switchName );
Serial.printf( "Type: %i \n", switchEntry[i]->type );
Serial.printf( "Pin: %i \n", switchEntry[i]->pin );
Serial.printf( "Min: %2.2f \n", switchEntry[i]->min );
Serial.printf( "Max: %2.2f \n", switchEntry[i]->max );
Serial.printf( "Step: %2.2f \n", switchEntry[i]->step );
Serial.printf( "Value: %2.2f \n", switchEntry[i]->value );
Serial.printf( "Writeable: %i \n", switchEntry[i]->writeable );
}
#endif
DEBUGSL1( "setDefaults: exiting" );
}
/*
* Save current variables of interest into EEPROM
*/
void saveToEeprom( void )
{
int eepromAddr = 0;
DEBUGSL1( "savetoEeprom: Entered ");
//Num Switches
EEPROMWriteAnything( eepromAddr = 4, numSwitches );
eepromAddr += sizeof(int);
DEBUGS1( "Written numSwitches: ");DEBUGSL1( numSwitches );
//UDP Port
EEPROMWriteAnything( eepromAddr, udpPort );
eepromAddr += sizeof(int);
DEBUGS1( "Written udpPort: ");DEBUGSL1( udpPort );
//hostname
EEPROMWriteString( eepromAddr, myHostname, MAX_NAME_LENGTH );
eepromAddr += MAX_NAME_LENGTH;
DEBUGS1( "Written hostname: ");DEBUGSL1( myHostname );
//Mgmt Location
EEPROMWriteString( eepromAddr, Location, MAX_NAME_LENGTH );
eepromAddr += MAX_NAME_LENGTH;
DEBUGS1( "Written Location: ");DEBUGSL1( Location );
//Switch state
for ( int i = 0; i< numSwitches; i++ )
{
EEPROMWriteAnything( eepromAddr, (int) switchEntry[i]->type );
eepromAddr += sizeof( (int) switchEntry[i]->type );
EEPROMWriteAnything( eepromAddr, (int) switchEntry[i]->pin );
eepromAddr += sizeof( (int) switchEntry[i]->pin );
EEPROMWriteAnything( eepromAddr, (bool) switchEntry[i]->writeable );
eepromAddr += sizeof( (bool) switchEntry[i]->writeable );
EEPROMWriteAnything( eepromAddr, (float) switchEntry[i]->min );
eepromAddr += sizeof( (float) switchEntry[i]->min );
EEPROMWriteAnything( eepromAddr, (float) switchEntry[i]->max );
eepromAddr += sizeof( (float) switchEntry[i]->max );
EEPROMWriteAnything( eepromAddr, (float) switchEntry[i]->step );
eepromAddr += sizeof( (float) switchEntry[i]->step );
EEPROMWriteAnything( eepromAddr, (float) switchEntry[i]->value );
eepromAddr += sizeof( (float) switchEntry[i]->value );
//Name
EEPROMWriteString( eepromAddr, switchEntry[i]->switchName, MAX_NAME_LENGTH );
eepromAddr += (MAX_NAME_LENGTH * sizeof( char));
//Description
EEPROMWriteString( eepromAddr, switchEntry[i]->description, MAX_NAME_LENGTH );
eepromAddr += (MAX_NAME_LENGTH * sizeof( char));
}
//Magic number write for data write complete.
EEPROM.put( 0, magic );
EEPROM.commit();
DEBUGS1( "Wrote ");DEBUGS1(eepromAddr);DEBUGSL1( " bytes ");
//Test readback of contents
String input = "";
char ch;
int eepromReadLength = 4 + (numSwitches * MAX_NAME_LENGTH * 3) + (2* sizeof(int) ) + ( numSwitches * ( sizeof(SwitchEntry) + (2*MAX_NAME_LENGTH) ) ) + 10;
for ( int i = 0; i < eepromReadLength ; i++ )
{
ch = (char) EEPROM.read( i );
if ( ch == '\0' )
ch = '~';
if ( (i % 32 ) == 0 )
input.concat( "\n\r" );
input.concat( ch );
}
Serial.printf( "EEPROM contents after: \n %s \n", input.c_str() );
DEBUGSL1( "saveToEeprom: exiting ");
}
void setupFromEeprom( void )
{
int eepromAddr = 0;
int i = 0;
DEBUGSL1( "setUpFromEeprom: Entering ");
byte myMagic = '\0';
//Setup internal variables - read from EEPROM.
myMagic = EEPROM.read( 0 );
DEBUGS1( "Read magic: ");DEBUGSL1( (char) myMagic );
if ( (byte) myMagic != magic ) //initialise eeprom for first time use.
{
setDefaults();
saveToEeprom();
DEBUGSL1( "Failed to find init magic byte - wrote defaults & restarted.");
device.restart();
return;
}
//Num Switches
EEPROMReadAnything( eepromAddr = 4, numSwitches );
eepromAddr += sizeof(int);
DEBUGS1( "Read numSwitches: ");DEBUGSL1( numSwitches );
//UDP port
EEPROMReadAnything( eepromAddr, udpPort );
eepromAddr += sizeof(int);
DEBUGS1( "Read UDPport: ");DEBUGSL1( udpPort );
//hostname - directly into variable array
if( myHostname != nullptr )
free( myHostname );
myHostname = (char*) calloc( MAX_NAME_LENGTH, sizeof( char ) );
EEPROMReadString( eepromAddr, myHostname, MAX_NAME_LENGTH );
eepromAddr += MAX_NAME_LENGTH * sizeof(char);
DEBUGS1( "Read hostname: ");DEBUGSL1( myHostname );
//Setup MQTT client id based on hostname
if ( thisID != nullptr )
free ( thisID );
thisID = (char*) calloc( MAX_NAME_LENGTH, sizeof( char) );
strcpy ( thisID, myHostname );
DEBUGS1( "Read MQTT ID: ");DEBUGSL1( thisID );
if( Location != nullptr )
free( Location );
Location = (char*) calloc( MAX_NAME_LENGTH, sizeof( char ) );
EEPROMReadString( eepromAddr, Location, MAX_NAME_LENGTH );
eepromAddr += MAX_NAME_LENGTH * sizeof(char);
DEBUGS1( "Read Location: ");DEBUGSL1( Location );
//free old switch entries
if ( switchEntry != nullptr )
{
for ( i= 0; i < numSwitches; i++ )
if( switchEntry[i] != nullptr )
free ( switchEntry[i] );
free( switchEntry);
}
//Create new
switchEntry = (SwitchEntry**) calloc ( sizeof (SwitchEntry*), numSwitches );
for ( i=0; i< numSwitches; i++ )
{
switchEntry[i] = (SwitchEntry*) calloc( sizeof( SwitchEntry ), 1 );
if( switchEntry[i] == nullptr )
{
//something has gone wrong - probably run out of memory space.
//Alert somehow .
break;
}
EEPROMReadAnything( eepromAddr, switchEntry[i]->type );
eepromAddr += sizeof( switchEntry[i]->type );
EEPROMReadAnything( eepromAddr, switchEntry[i]->pin );
eepromAddr += sizeof( switchEntry[i]->pin );
EEPROMReadAnything( eepromAddr, switchEntry[i]->writeable );
eepromAddr += sizeof( switchEntry[i]->writeable );
EEPROMReadAnything( eepromAddr, switchEntry[i]->min );
eepromAddr += sizeof( switchEntry[i]->min );
EEPROMReadAnything( eepromAddr, switchEntry[i]->max );
eepromAddr += sizeof( switchEntry[i]->max );
EEPROMReadAnything( eepromAddr, switchEntry[i]->step );
eepromAddr += sizeof( switchEntry[i]->step );
EEPROMReadAnything( eepromAddr, switchEntry[i]->value );
eepromAddr += sizeof( switchEntry[i]->value );
switchEntry[i]->switchName = (char*) calloc( MAX_NAME_LENGTH, sizeof( char ) );
EEPROMReadString( eepromAddr, switchEntry[i]->switchName, MAX_NAME_LENGTH );
eepromAddr += MAX_NAME_LENGTH * sizeof( char);
switchEntry[i]->description = (char*) calloc( MAX_NAME_LENGTH, sizeof( char ) );
EEPROMReadString( eepromAddr, switchEntry[i]->description, MAX_NAME_LENGTH );
eepromAddr += MAX_NAME_LENGTH * sizeof( char);
}
DEBUGSL1( "setupFromEeprom: exiting" );
}
#endif