Skip to content

Commit 30b85e4

Browse files
committed
Extracting write/read settings to ElocConfig.cpp
* Mostly no code changes only formating * only code change: remove vcal support: this seems to does not make any sense currently the offset of the battery against "low= 3.18V) is measured and stored. However this does not make sense as battery level is unknown. If reintroduced it must be calibrated against a defined value, e.g. by measuring with a external multimeter
1 parent ffc980d commit 30b85e4

File tree

3 files changed

+304
-326
lines changed

3 files changed

+304
-326
lines changed

eloc610LowPowerPartition/src/ElocConfig.cpp

+296-36
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2222
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2323
*/
24+
#include <esp_log.h>
2425

25-
26+
#include "Esp.h"
2627
#include <FS.h>
2728
#include "SPIFFS.h"
29+
#include "ElocConfig.hpp"
30+
31+
const char* TAG = "CONFIG";
2832

2933
//BUGME: encapsulate these in a struct & implement a getter
3034
String gMicType="ns";
@@ -35,55 +39,311 @@ String gMicHeight="ns";
3539
String gMicMountType="ns";
3640
String gMicBluetoothOnOrOff="on";
3741

38-
39-
4042
void writeMicInfo() {
41-
File file2 = SPIFFS.open("/micinfo.txt", FILE_WRITE);
42-
43-
file2.print(gMicType+'\n');
44-
file2.print(gMicBitShift+'\n');
45-
file2.print(gMicGPSCoords+'\n');
46-
file2.print(gMicPointingDirectionDegrees+'\n');
47-
file2.print(gMicHeight+'\n');
48-
file2.print(gMicMountType+'\n');
49-
file2.print(gMicBluetoothOnOrOff+'\n');
50-
file2.close();
51-
Serial.println("micinfo: "+gMicType+" "+gMicBitShift+" "+gMicGPSCoords+" "+gMicPointingDirectionDegrees+" "+gMicHeight+" "+gMicMountType+" "+gMicBluetoothOnOrOff);
52-
53-
54-
55-
43+
File file2 = SPIFFS.open("/micinfo.txt", FILE_WRITE);
5644

45+
file2.print(gMicType + '\n');
46+
file2.print(gMicBitShift + '\n');
47+
file2.print(gMicGPSCoords + '\n');
48+
file2.print(gMicPointingDirectionDegrees + '\n');
49+
file2.print(gMicHeight + '\n');
50+
file2.print(gMicMountType + '\n');
51+
file2.print(gMicBluetoothOnOrOff + '\n');
52+
file2.close();
53+
Serial.println("micinfo: " + gMicType + " " + gMicBitShift + " " + gMicGPSCoords + " " + gMicPointingDirectionDegrees + " " + gMicHeight + " " + gMicMountType + " " + gMicBluetoothOnOrOff);
5754
}
5855

59-
60-
void readMicInfo() {
61-
if(!(SPIFFS.exists("/micinfo.txt"))){
62-
63-
printf("micinfo.txt not exist");
64-
writeMicInfo();
65-
66-
67-
56+
void readMicInfo()
57+
{
58+
if (!(SPIFFS.exists("/micinfo.txt"))) {
59+
printf("micinfo.txt not exist");
60+
writeMicInfo();
6861
}
6962
File file2 = SPIFFS.open("/micinfo.txt", FILE_READ);
70-
gMicType=file2.readStringUntil('\n');
63+
gMicType = file2.readStringUntil('\n');
7164
gMicType.trim();
72-
gMicBitShift=file2.readStringUntil('\n');
65+
gMicBitShift = file2.readStringUntil('\n');
7366
gMicBitShift.trim();
74-
gMicGPSCoords=file2.readStringUntil('\n');
67+
gMicGPSCoords = file2.readStringUntil('\n');
7568
gMicGPSCoords.trim();
76-
gMicPointingDirectionDegrees=file2.readStringUntil('\n');
69+
gMicPointingDirectionDegrees = file2.readStringUntil('\n');
7770
gMicPointingDirectionDegrees.trim();
78-
gMicHeight=file2.readStringUntil('\n');
71+
gMicHeight = file2.readStringUntil('\n');
7972
gMicHeight.trim();
80-
gMicMountType=file2.readStringUntil('\n');
73+
gMicMountType = file2.readStringUntil('\n');
8174
gMicMountType.trim();
82-
gMicBluetoothOnOrOff=file2.readStringUntil('\n');
75+
gMicBluetoothOnOrOff = file2.readStringUntil('\n');
8376
gMicBluetoothOnOrOff.trim();
8477

8578
file2.close();
86-
Serial.println("micinfo: "+gMicType+" "+gMicBitShift+" "+gMicGPSCoords+" "+gMicPointingDirectionDegrees+" "+gMicHeight+" "+gMicMountType+" "+gMicBluetoothOnOrOff);
79+
Serial.println("micinfo: " + gMicType + " " + gMicBitShift + " " + gMicGPSCoords + " " + gMicPointingDirectionDegrees + " " + gMicHeight + " " + gMicMountType + " " + gMicBluetoothOnOrOff);
80+
}
81+
82+
//BUGME: encapsulate these in a struct & implement a getter
83+
uint32_t gSampleRate;
84+
int gSecondsPerFile= 60;
85+
String gLocation = "not_set";
86+
87+
//BUGME: these function should be part of some bluetooth handler
88+
// settings should only return settings as string, not directly send it
89+
void btwrite(String theString);
90+
void sendElocStatus();
91+
92+
void sendSettings()
93+
{
94+
btwrite("#" + String(gSampleRate) + "#" + String(gSecondsPerFile) + "#" + gLocation);
95+
vTaskDelay(pdMS_TO_TICKS(100));
96+
// btwrite("elocName: "+readNodeName() + " "+gFirmwareVersion);
97+
vTaskDelay(pdMS_TO_TICKS(100));
98+
// btwrite(String(gFreeSpace)+ " GB free");
99+
}
100+
101+
void writeSettings(String settings)
102+
{
103+
settings.trim();
104+
105+
if (settings.endsWith("getstats"))
106+
{
107+
btwrite("\n\n");
108+
sendElocStatus();
109+
btwrite("\n\n");
110+
delay(500);
111+
sendSettings();
112+
return;
113+
}
114+
115+
if (settings.endsWith("vcal"))
116+
{
117+
ESP_LOGW(TAG, "vcal is not supported! Usefullness is questionable");
118+
/*********** vcal is not supported currently ***********
119+
* If reintroduced it must be calibrated against a defined value, e.g. by measuring with a external multimeter
120+
121+
btwrite("\n\nCalibrating voltage with VLow="+String(gvLow)+ " volts\n");
122+
calculateVoltageOffset();
123+
btwrite("voltage offset is now "+String(gVoltageOffset));
124+
125+
File file = SPIFFS.open("/voltageoffset.txt", FILE_WRITE);
126+
file.print(gVoltageOffset);
127+
file.close();
128+
gVoltageCalibrationDone=true;
129+
delay(5000);
130+
**************************************************************/
131+
sendSettings();
132+
return;
133+
}
134+
135+
if (settings.endsWith("bton"))
136+
{
137+
gMicBluetoothOnOrOff = "on";
138+
btwrite("\n\nbluetooth ON while recording. Use phone to stop record.\n\n");
139+
writeMicInfo();
140+
sendSettings();
141+
return;
142+
}
143+
144+
if (settings.endsWith("btoff"))
145+
{
146+
gMicBluetoothOnOrOff = "off";
147+
btwrite("\n\nbluetooth OFF while recording. Use button to stop record.\n\n");
148+
149+
writeMicInfo();
150+
sendSettings();
151+
return;
152+
}
153+
154+
if (settings.endsWith("micinfo"))
155+
{
156+
157+
btwrite("****** micinfo: ******** \nTYPE: " + gMicType + "\nGAIN: " + gMicBitShift + "\nGPSCoords: " + gMicGPSCoords + "\nDIRECTION: " + gMicPointingDirectionDegrees + "\nHEIGHT: " + gMicHeight + "\nMOUNT: " + gMicMountType + "\nBluetooth when record: " + gMicBluetoothOnOrOff);
158+
btwrite("\n");
159+
sendSettings();
160+
return;
161+
}
162+
163+
if (settings.endsWith("help"))
164+
{
165+
166+
// btwrite("\n***commands***\nXXsetgain (11=forest, 14=Mahout)\nXXXXsettype (set mic type)\nXXXXsetname (set eloc bt name)\nupdate (reboot + upgrade firmware)\nbtoff BT off when record\nbton BT on when record\ndelete (don't use)\n\n");
167+
sendSettings();
168+
return;
169+
}
170+
171+
if (settings.endsWith("settype"))
172+
{
173+
gMicType = settings.substring(settings.lastIndexOf('#') + 1, settings.length() - 7);
174+
gMicType.trim();
175+
if (gMicType.length() == 0)
176+
gMicType = "ns";
177+
writeMicInfo();
178+
btwrite("Mic Type is now " + gMicType);
179+
sendSettings();
180+
return;
181+
}
182+
183+
if (settings.endsWith("setgain"))
184+
{
185+
gMicBitShift = settings.substring(settings.lastIndexOf('#') + 1, settings.length() - 7);
186+
gMicBitShift.trim();
187+
btwrite(gMicBitShift);
188+
if (gMicBitShift == "11" || gMicBitShift == "12" || gMicBitShift == "13" || gMicBitShift == "14" || gMicBitShift == "15" || gMicBitShift == "16")
189+
{
190+
}
191+
else
192+
{
193+
btwrite("Error, mic gain out of range. (11 to 16) ");
194+
gMicBitShift = "11";
195+
}
196+
197+
writeMicInfo();
198+
// int temp=gMicBitShift.toInt();
199+
btwrite("Mic gain is now " + gMicBitShift);
200+
sendSettings();
201+
return;
202+
}
203+
204+
if (settings.endsWith("update"))
205+
{
206+
// updateFirmware();
207+
File temp = SPIFFS.open("/update.txt", "w");
208+
temp.close();
209+
210+
btwrite("\nEloc will restart for firmware update. Please re-connect in 1 minute.\n");
211+
delay(1000);
212+
ESP.restart();
213+
return;
214+
}
215+
216+
if (settings.endsWith("setname"))
217+
{
218+
String temp;
219+
temp = settings.substring(settings.lastIndexOf('#') + 1, settings.length() - 7);
220+
temp.trim();
221+
// temp=settings.lastIndexOf('#');
222+
223+
File file = SPIFFS.open("/nodename.txt", FILE_WRITE);
224+
file.print(temp);
225+
226+
file.close();
227+
Serial.println("new name: " + temp);
228+
btwrite("new name " + temp + "\n\n--- Restarting ELOC ----");
229+
vTaskDelay(pdMS_TO_TICKS(100));
230+
sendSettings();
231+
// readSettings();
232+
vTaskDelay(pdMS_TO_TICKS(500));
233+
ESP.restart();
234+
return;
235+
}
236+
237+
// if (settings.endsWith("sync")) {
238+
239+
// // btwrite("syncnow");
240+
// // btwrite("syncing with time.google.com");
241+
// // vTaskDelay(pdMS_TO_TICKS(5200));
242+
// sendSettings();
243+
244+
// return;
245+
// }
246+
247+
if (settings.endsWith("delete"))
248+
{
249+
250+
// SPIFFS.
251+
SPIFFS.remove("/settings.txt");
252+
SPIFFS.remove("/nodename.txt");
253+
SPIFFS.remove("/micinfo.txt");
254+
255+
btwrite("spiffs settings removed");
256+
vTaskDelay(pdMS_TO_TICKS(100));
257+
sendSettings();
258+
259+
return;
260+
}
261+
262+
File file = SPIFFS.open("/settings.txt", FILE_WRITE);
263+
264+
if (!file)
265+
{
266+
printf("There was an error opening the file for writing");
267+
return;
268+
}
269+
270+
/*if(file.print(settings)){
271+
printf("File was written");;
272+
} else {
273+
printf("File write failed");
274+
}*/
275+
276+
file.print(settings);
277+
278+
file.close();
279+
}
280+
281+
String getSubstring(String data, char separator, int index) {
282+
int found = 0;
283+
int strIndex[] = { 0, -1 };
284+
int maxIndex = data.length() - 1;
285+
286+
for (int i = 0; i <= maxIndex && found <= index; i++) {
287+
if (data.charAt(i) == separator || i == maxIndex) {
288+
found++;
289+
strIndex[0] = strIndex[1] + 1;
290+
strIndex[1] = (i == maxIndex) ? i+1 : i;
291+
}
292+
}
293+
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
294+
}
295+
296+
297+
void readSettings() {
298+
299+
//SPIFFS.remove("/settings.txt");
300+
//vTaskDelay(pdMS_TO_TICKS(100));
301+
302+
if (!(SPIFFS.exists("/settings.txt"))) {
303+
writeSettings("#settings#"+String(gSampleRate)+"#"+String(gSecondsPerFile)+"#"+gLocation);
304+
printf("wrote settings to spiffs");
305+
vTaskDelay(pdMS_TO_TICKS(100));
306+
307+
}
308+
309+
310+
File file2 = SPIFFS.open("/settings.txt");
311+
312+
if(!file2){
87313

314+
printf("Failed to open file for reading");
315+
return;
316+
317+
318+
}
319+
320+
321+
322+
//String temp = file2.readStringUntil('\n');
323+
324+
String temp = file2.readString();
325+
temp.trim();
326+
327+
328+
gSampleRate=getSubstring(temp, '#', 2).toInt();
329+
//temp
330+
//if (gSampleRate==44100) gSampleRate=48000;
331+
gSecondsPerFile=getSubstring(temp, '#', 3).toInt();
332+
gLocation= getSubstring(temp, '#', 4);
333+
gLocation.trim();
334+
335+
Serial.println("settings read: "+temp);
336+
337+
/*printf("File Content:");
338+
339+
while(file2.available()){
340+
341+
Serial.write(file2.read());
342+
}*/
343+
344+
345+
file2.close();
346+
347+
}
348+
88349

89-
}

eloc610LowPowerPartition/src/ElocConfig.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,12 @@ extern String gMicBluetoothOnOrOff;
4040
void writeMicInfo();
4141
void readMicInfo();
4242

43+
//BUGME: encapsulate these in a struct & implement a getter
44+
extern uint32_t gSampleRate;
45+
extern int gSecondsPerFile;
46+
extern String gLocation;
47+
48+
void writeSettings(String settings);
49+
void readSettings();
50+
4351
#endif // ELOCCONFIG_HPP_

0 commit comments

Comments
 (0)