forked from JurgenG/ArduinoSettings
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadsettings.ino
114 lines (92 loc) · 2.45 KB
/
readsettings.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
File confFile;
char confChar;
boolean getSettings()
{
// Open the settings file for reading:
String description = "";
confFile = SD.open("SETTINGS.TXT");
if (!confFile) {
return false;
}
// read from the file until there's nothing else in it:
while (confFile.available()) {
confChar = confFile.read();
if (confChar == '#') { // Comment - ignore this line
skipLine();
} else if (isalnum(confChar)) { // Add a confChar to th e description
description.concat(confChar);
} else if (confChar =='=') { // start checking the value for possible results
// First going to trim out all trailing white spaces
do {
confChar = confFile.read();
} while(confChar == ' ');
// Property list
if (description == "intValue") {
settings.intValue = getIntSetting(DEFAULT_INT_VALUE);
} else if (description == "boolValue") {
settings.boolValue = getBoolSetting(DEFAULT_BOOL_VALUE);
} else if (description == "stringValue") {
settings.stringValue = getStringSetting(DEFAULT_STRING_VALUE);
} else { // Unknown parameter - ignore this line
skipLine();
}
description = "";
} else {
// Ignore this confChar (could be space, tab, newline, carriage return or something else)
}
}
// close the file:
confFile.close();
return true;
}
void skipLine()
{
do {
confChar = confFile.read();
} while (confChar != '\n');
}
int getIntSetting(int defaultValue)
{
String value = "";
boolean valid = true;
while (confChar != '\n') {
if (isdigit(confChar) || confChar == '-') {
value.concat(confChar);
} else if (confChar != '\n') { // Use of invalid values
valid = false;
}
confChar = confFile.read();
}
if (valid) {
// Convert string to integer
char charBuf[value.length()+1];
value.toCharArray(charBuf,value.length()+1);
return atoi(charBuf);
} else {
// revert to default value for invalid entry in settings
return defaultValue;
}
}
bool getBoolSetting(bool defaultValue)
{
if (confChar == '1') {
return true;
} else if (confChar == '0') {
return false;
} else {
return defaultValue;
}
}
String getStringSetting(String defaultValue)
{
String value = "";
do {
value.concat(confChar);
confChar = confFile.read();
} while(confChar != '\n');
if (value != "") {
return value;
} else {
return defaultValue;
}
}