-
Notifications
You must be signed in to change notification settings - Fork 0
/
Settings.ino
130 lines (108 loc) · 2.96 KB
/
Settings.ino
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
void fileSystemCheck()
{
if (SPIFFS.begin()) {
fs::File f = SPIFFS.open("settings.dat", "r");
if (!f)
{
ResetFactory();
}
}
}
void ResetFactory()
{
// Direct Serial is allowed here, since this is only an emergency task.
Serial.println(F("Resetting factory defaults..."));
delay(1000);
//always format on factory reset, in case of corrupt SPIFFS
SPIFFS.end();
Serial.println(F("formatting..."));
SPIFFS.format();
Serial.println(F("formatting done..."));
if (!SPIFFS.begin())
{
Serial.println(F("FORMATTING SPIFFS FAILED!"));
return;
}
fs::File f = SPIFFS.open("settings.dat", "w");
if (f)
{
for (int x = 0; x < sizeof(struct SettingsStr); x++)
f.write(0);
f.close();
}
LoadSettings();
// now we set all parameters that need to be non-zero as default value
Settings.settings_version = SETTINGS_VERSION;
Settings.Version = VERSION;
#if DEFAULT_USE_STATIC_IP
str2ip((char*)DEFAULT_IP, Settings.IP);
str2ip((char*)DEFAULT_GW, Settings.Gateway);
str2ip((char*)DEFAULT_SUBNET, Settings.Subnet);
str2ip((char*)DEFAULT_DNS, Settings.DNS);
#endif
strcpy_P(Settings.WifiSSID, PSTR(DEFAULT_SSID));
strcpy_P(Settings.WifiKey, PSTR(DEFAULT_KEY));
strcpy_P(Settings.Name, PSTR(DEFAULT_NAME));
strcpy_P(Settings.Password, PSTR(DEFAULT_PASSWORD));
Settings.led = true;
for (byte x = 0; x < 3; x++)
{
Settings.chi[x] = 0;
Settings.cho[x] = 0;
}
SaveSettings();
Serial.println("Factory reset succesful, rebooting...");
delay(1000);
// WiFi.persistent(true); // use SDK storage of SSID/WPA parameters
// WiFi.disconnect(); // this will store empty ssid/wpa into sdk storage
// WiFi.persistent(false); // Do not use SDK storage of SSID/WPA parameters
ESP.eraseConfig();
delay(1000);
pinMode(0, OUTPUT);
digitalWrite(0, false);
delay(1000);
ESP.restart();
}
boolean SaveSettings()
{
return SaveToFile((char*)"settings.dat", 0, (byte*)&Settings, sizeof(struct SettingsStr));
}
boolean LoadSettings()
{
return LoadFromFile((char*)"settings.dat", 0, (byte*)&Settings, sizeof(struct SettingsStr));
}
boolean LoadFromFile(char* fname, int index, byte* memAddress, int datasize)
{
boolean success = false;
fs::File f = SPIFFS.open(fname, "r+");
if (f) {
f.seek(index, fs::SeekSet);
byte *pointerToByteToRead = memAddress;
for (int x = 0; x < datasize; x++)
{
*pointerToByteToRead = f.read();
pointerToByteToRead++;// next byte
}
f.close();
success = true;
}
return success;
}
boolean SaveToFile(char* fname, int index, byte* memAddress, int datasize)
{
boolean success = false;
fs::File f = SPIFFS.open(fname, "r+");
if (f) {
f.seek(index, fs::SeekSet);
byte *pointerToByteToSave = memAddress;
for (int x = 0; x < datasize ; x++)
{
f.write(*pointerToByteToSave);
pointerToByteToSave++;
}
f.close();
Serial.println("FILE : File saved");
success = true;
}
return success;
}