-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgPrefs.cpp
232 lines (200 loc) · 5.77 KB
/
gPrefs.cpp
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
//-------------------------------
// SPIFFS based preferences
//-------------------------------
#include "gPrefs.h"
#include "gApp.h"
#include <FS.h>
#include <SPIFFS.h>
#define DEBUG_PREFS 0
#define PREF_TYPE_INT 0
#define PREF_TYPE_S1 1
#define PREF_TYPE_S128 2
#define PREF_FILENAME "/ui_prefs.txt"
#define PREF_MAXLINE (128+15)
#define PREF_DEFAULT_JOG_FEED_RATE 1500
// prh - if these are static, I think the compiler optimizes out
// the setting of jog_feed_rate
static int jog_feed_rate = PREF_DEFAULT_JOG_FEED_RATE;
static char macro_char[4][2];
static char macro_path[4][MAX_MACRO_PATH+1];
typedef struct pref_t {
const uint8_t type;
const char *name;
const void *ptr;
};
static const pref_t prefs[] = {
{PREF_TYPE_INT, "JOG_FEED_RATE", &jog_feed_rate, },
{PREF_TYPE_S1, "MACRO1_CHAR", macro_char[0], },
{PREF_TYPE_S1, "MACRO2_CHAR", macro_char[1], },
{PREF_TYPE_S1, "MACRO3_CHAR", macro_char[2], },
{PREF_TYPE_S1, "MACRO4_CHAR", macro_char[3], },
{PREF_TYPE_S128, "MACRO1_PATH", macro_path[0], },
{PREF_TYPE_S128, "MACRO2_PATH", macro_path[1], },
{PREF_TYPE_S128, "MACRO3_PATH", macro_path[2], },
{PREF_TYPE_S128, "MACRO4_PATH", macro_path[3], },
};
#define NUM_PREFS (sizeof(prefs) / sizeof(pref_t))
void parsePref(int line,char *buf)
{
char *val = buf;
while (*val && *val != '=') val++;
if (*val == '=')
{
*val++ = 0;
#if DEBUG_PREFS
g_debug("PREF[%s}=%s]",buf,val);
#endif
for (int i=0; i<NUM_PREFS; i++)
{
const pref_t *pref = &prefs[i];
if (!strcmp(buf,pref->name))
{
if (pref->type == PREF_TYPE_INT)
{
*((int *) pref->ptr) = atoi(val);
}
else
{
strcpy((char *)pref->ptr,val);
}
return;
}
}
g_debug("Error: Could not find pref[%d} named %s",line,buf);
}
g_debug("Error: Could not find '=' on prefs line[%d} '%s'",line,buf);
}
void readPrefs()
{
#if DEBUG_PREFS
g_debug("readPrefs()");
#endif
if (SPIFFS.exists(PREF_FILENAME))
{
File pfile = SPIFFS.open(PREF_FILENAME,FILE_READ);
if (pfile)
{
int len = 0;
int line = 0;
char buf[PREF_MAXLINE+1];
int c = pfile.read();
while (c != -1)
{
if (c == 13)
{
buf[len] = 0;
parsePref(line,buf);
len = 0;
line++;
}
else if (len < PREF_MAXLINE)
{
buf[len++] = c;
}
else
{
g_debug("Warning/Error: pref_line[%d] too long",line);
}
c = pfile.read();
}
pfile.close();
}
else
{
g_debug("Error: could not open %s for reading",PREF_FILENAME);
}
}
#if DEBUG_PREFS
else
{
g_debug("Warning: %s does not exist",PREF_FILENAME);
}
#endif
}
static void writePrefs()
{
#if DEBUG_PREFS
g_debug("writePrefs()");
#endif
File pfile = SPIFFS.open(PREF_FILENAME,FILE_WRITE);
if (pfile)
{
for (int i=0; i<NUM_PREFS; i++)
{
char buf[PREF_MAXLINE+1];
const pref_t *pref = &prefs[i];
if (pref->type == PREF_TYPE_INT)
{
sprintf(buf,"%s=%d\r",pref->name,*((int *)pref->ptr));
}
else
{
sprintf(buf,"%s=%s\r",pref->name,((char *)pref->ptr));
}
#if DEBUG_PREFS
g_debug("--->PREF %s",buf);
#endif
int len = strlen(buf);
if (pfile.write((const uint8_t *)buf,len) != len)
{
g_debug("Error: could not write '%s' to '%s'",buf,PREF_FILENAME);
pfile.close();
return;
}
}
pfile.close();
}
else
{
g_debug("Error: Could not open %s for writing",PREF_FILENAME);
}
}
int getIntPref(int id)
{
if (prefs[id].type != PREF_TYPE_INT)
{
g_debug("Error: illegal call to getIntPref(%d)",id);
return 0;
}
return *((volatile int *) prefs[id].ptr);
}
const char *getStrPref(int id)
{
if (prefs[id].type != PREF_TYPE_S1 &&
prefs[id].type != PREF_TYPE_S128)
{
g_debug("Error: illegal call to getStrPref(%d)",id);
return "";
}
return (const char *) prefs[id].ptr;
}
void setIntPref(int id, int value)
{
if (prefs[id].type != PREF_TYPE_INT)
{
g_debug("Error: illegal call to setIntPref(%d,%d)",id,value);
return;
}
*((int *) prefs[id].ptr) = value;
writePrefs();
}
void setStrPref(int id, const char *value)
{
if (prefs[id].type != PREF_TYPE_S1 &&
prefs[id].type != PREF_TYPE_S128)
{
g_debug("Error: illegal call to setStrPref(%d,%s)",id,value);
return;
}
int maxlen = prefs[id].type == PREF_TYPE_S1 ? 1 : 128;
int len = strlen(value);
if (len > maxlen)
{
g_debug("WARNING: setStrPref(%d,%s) len(%d) truncating to %d",id,value,len,maxlen);
len = maxlen;
}
char *p = (char *) prefs[id].ptr;
memcpy(p,value,len);
p[len] = 0;
writePrefs();
}