-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsettings.cpp
More file actions
433 lines (396 loc) · 12.3 KB
/
settings.cpp
File metadata and controls
433 lines (396 loc) · 12.3 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "settings.h"
#include <Arduino.h>
#include "icons.h"
#include "screen.h"
#include "menu.h"
#include <FlashStorage.h>
#include "joystick.h"
typedef struct {
boolean valid;
unsigned int transmitInterval;
join_type joinType;
spread_factor sf;
} Settings;
FlashStorage(flash_store, Settings);
Settings deviceSettings;
/**************************************************************************/
/*!
@brief Gets the current join type
@return The current join type
*/
/**************************************************************************/
join_type getJoinType() {
return deviceSettings.joinType;
}
/**************************************************************************/
/*!
@brief Gets the current spread factor
@return The current spread factor
*/
/**************************************************************************/
spread_factor getSpreadFactor() {
return deviceSettings.sf;
}
/**************************************************************************/
/*!
@brief Gets the current transmit interval
@return The current transmit interval
*/
/**************************************************************************/
unsigned int getTransmitInterval() {
return deviceSettings.transmitInterval;
}
/**************************************************************************/
/*!
@brief Sets the internal settings structure join type
@param The chosen join type
*/
/**************************************************************************/
void setJoinType(join_type chosenJoinType) {
deviceSettings.joinType = chosenJoinType;
}
/**************************************************************************/
/*!
@brief Sets the internal settings structure transmit interval
@param The chosen transmit interval
*/
/**************************************************************************/
void setSpreadFactor(spread_factor chosenSpreadFactor) {
deviceSettings.sf = chosenSpreadFactor;
}
/**************************************************************************/
/*!
@brief Sets the internal settings structure transmit interval
@param The chosen transmit interval
*/
/**************************************************************************/
void setTransmitInterval(unsigned int chosenTransmitInterval) {
deviceSettings.transmitInterval = chosenTransmitInterval;
}
/**************************************************************************/
/*!
@brief Loads the settings from internal flash. If not set, default populate
*/
/**************************************************************************/
void loadSettings(){
deviceSettings = flash_store.read();
// Not populated yet, so load default values
if(!deviceSettings.valid){
setJoinType(OTAA);
setSpreadFactor(SF_7);
setTransmitInterval(MANUAL_TRANSMIT);
}
}
/**************************************************************************/
/*!
@brief Saves the settings to internal flash
*/
/**************************************************************************/
void saveSettings(){
deviceSettings.valid = true;
flash_store.write(deviceSettings);
}
//used by both settings menu and exit/save
int settingsCursor = 1;
/**************************************************************************/
/*!
@brief Sets the screen up for settings exit.
*/
/**************************************************************************/
void initExitState() {
setLineInverted(0, false);
setLineInverted(1, true);
setLineInverted(2, true);
setLineInverted(3, true);
setLineInverted(4, true);
clearScreen();
drawText(38, 2, "Save As");
drawText(32, 3, "Default?");
drawText(28+80, 4, "No");
setLineInverted(4, false);
clearSpace(0,4, 80);
drawText(22, 4, "Yes");
settingsCursor = 0;
}
/**************************************************************************/
/*!
@brief Checks for joystick commands and updates screen accordingly
*/
/**************************************************************************/
void loopExitState() {
drawGPSIcon();
drawBatteryIcon();
switch(readJoystick()) {
case JOY_PRESSED:
if(!settingsCursor) //if YES
saveSettings();
setState(&menuState);
break;
case JOY_LEFT:
if(settingsCursor) { //if NO
setLineInverted(4, true);
clearSpace(80, 4, 160);
drawText(28+80, 4, "No");
setLineInverted(4, false);
clearSpace(0, 4, 80);
drawText(22, 4, "Yes");
settingsCursor = 0;
}
break;
case JOY_RIGHT:
if(!settingsCursor) { //if YES
setLineInverted(4, false);
clearSpace(80, 4, 160);
drawText(28+80, 4, "No");
setLineInverted(4, true);
clearSpace(0, 4, 80);
drawText(22, 4, "Yes");
settingsCursor = 1;
}
break;
}
}
state settingsExitState = {
&initExitState,
&loopExitState,
NULL
};
#define POINTER_OFFSET (4)
/**************************************************************************/
/*!
@brief Draws the join type value to the screen plus joystick widgets if necassary
*/
/**************************************************************************/
void showJoin() {
clearSpace(68,1,160);
switch(getJoinType()) {
case OTAA:
drawText(68+5+12+5,1,"OTAA");
break;
case ABP:
drawText(68+5+12+5+6,1,"ABP");
break;
}
if(settingsCursor == 1) {
drawText(68+5, 1, "<");
drawText(68+5+12+5+48+5, 1, ">");
}
}
/**************************************************************************/
/*!
@brief Draws the spread factor value to the screen plus joystick widgets if necassary
*/
/**************************************************************************/
void showSF() {
clearSpace(44,2,160);
drawText(44+8+12+8, 2, sfToText(getSpreadFactor()));
if(settingsCursor == 2) {
if(getSpreadFactor() > SF_7)
drawText(44+8, 2, "<");
if(getSpreadFactor() < SF_12)
drawText(44+8+12+8+60+8, 2, ">");
}
}
#define SECOND (1000)
#define MINUTE (60*SECOND)
const unsigned int intervals[10] = {MANUAL_TRANSMIT, SECOND, 2*SECOND, 5*SECOND, 10*SECOND, 30*SECOND, MINUTE, 2*MINUTE, 5*MINUTE, 10*MINUTE};
/**************************************************************************/
/*!
@brief For a given time interval, returns the next interval strictly greater. If the provided interval is equal to or larger
than the largest permitted, the maximum interval is returned.
@param The current time interval
@return The next larger interval or the maximum allowed interval
*/
/**************************************************************************/
unsigned int nextInterval(unsigned int curr) {
for(int i=0; i<9; i++)
if(intervals[i]>curr)
return intervals[i];
return intervals[9];
}
/**************************************************************************/
/*!
@brief For a given time interval, returns the next interval strictly smaller. If the provided interval is equal to or less than
than the smallest permitted, the minimum interval is returned.
@param The current time interval
@return The next smaller interval or the minimum allowed interval
*/
/**************************************************************************/
unsigned int prevInterval(unsigned int curr) {
for(int i=9; i>0; i--)
if(intervals[i]<curr)
return intervals[i];
return intervals[0];
}
/**************************************************************************/
/*!
@brief Finds the nearest valid interval that is not larger than the provided value
@param The current time interval
@return The nearest valid interval
*/
/**************************************************************************/
unsigned int nearestInterval(unsigned int curr) {
for(int i=9; i>0; i--)
if(intervals[i]<=curr)
return intervals[i];
return intervals[0];
}
/**************************************************************************/
/*!
@brief Ease of use function to convert valid time intervals to text. Returns NULL on invalid intervals
@param A valid time interval
@return A text representation or NULL
*/
/**************************************************************************/
char* intervalToText(unsigned int curr) {
switch(curr) {
case MANUAL_TRANSMIT: return "Man";
case SECOND: return "1s";
case 2*SECOND: return "2s";
case 5*SECOND: return "5s";
case 10*SECOND: return "10s";
case 30*SECOND: return "30s";
case MINUTE: return "1m";
case 2*MINUTE: return "2m";
case 5*MINUTE: return "5m";
case 10*MINUTE: return "10m";
default: return NULL;
}
}
/**************************************************************************/
/*!
@brief Draws the transmission interval value to the screen plus joystick widgets if necassary
*/
/**************************************************************************/
void showTxIvl() {
clearSpace(92,3,160);
unsigned int interval = nearestInterval(getTransmitInterval());
drawText(92+2+12+2, 3,intervalToText(interval));
if(settingsCursor == 3) {
if(interval>MANUAL_TRANSMIT)
drawText(92+2, 3, "<");
if(interval<10*MINUTE)
drawText(92+2+12+2+36+2, 3, ">");
}
}
/**************************************************************************/
/*!
@brief Sets up the screen for the Settings UI
*/
/**************************************************************************/
void initSettingsState() {
settingsCursor = 1;
setLineInverted(0, false);
setLineInverted(1, true);
setLineInverted(2, true);
setLineInverted(3, true);
setLineInverted(4, true);
clearScreen();
drawText(30, 0, "Settings");
drawText(20,1,"Join");
drawText(20,2,"SF");
drawText(20,3,"Tx IVL");
drawText(20,4,"Exit");
drawText(POINTER_OFFSET,settingsCursor,">");
showJoin();
showSF();
showTxIvl();
}
/**************************************************************************/
/*!
@brief Ease of use function to redraw the value portion of a particular line.
*/
/**************************************************************************/
void redrawSettings(int line) {
switch(line) {
case 1:
showJoin();
break;
case 2:
showSF();
break;
case 3:
showTxIvl();
break;
}
}
/**************************************************************************/
/*!
@brief Moves the curor to a new location
*/
/**************************************************************************/
void moveCursorSettings(int newLine) {
clearSpace(0,settingsCursor,20);
int oldLine = settingsCursor;
settingsCursor = newLine;
drawText(POINTER_OFFSET,settingsCursor,">");
redrawSettings(oldLine);
redrawSettings(newLine);
}
/**************************************************************************/
/*!
@brief Checks the joystick for actions and updates the screen and settings accordingly
*/
/**************************************************************************/
void settingsStateLoop() {
drawGPSIcon();
drawBatteryIcon();
joyState joy = readJoystick();
switch(joy) {
case JOY_PRESSED:
if(settingsCursor == 4) {
setState(&settingsExitState);
return;
}
break;
case JOY_UP:
if(settingsCursor>1)
moveCursorSettings(settingsCursor-1);
break;
case JOY_DOWN:
if(settingsCursor<4)
moveCursorSettings(settingsCursor+1);
break;
case JOY_LEFT:
switch(settingsCursor) {
case 1:
deviceSettings.joinType =
(deviceSettings.joinType == OTAA)
? ABP
: OTAA;
break;
case 2:
if(deviceSettings.sf>SF_7)
(*(int*)(&deviceSettings.sf))--;
break;
case 3:
deviceSettings.transmitInterval = prevInterval(deviceSettings.transmitInterval);
break;
}
redrawSettings(settingsCursor);
break;
case JOY_RIGHT:
switch(settingsCursor) {
case 1:
deviceSettings.joinType =
(deviceSettings.joinType == OTAA)
? ABP
: OTAA;
break;
case 2:
if(deviceSettings.sf<SF_12)
(*(int*)(&deviceSettings.sf))++;
break;
case 3:
deviceSettings.transmitInterval = nextInterval(deviceSettings.transmitInterval);
break;
}
redrawSettings(settingsCursor);
break;
}
}
state settingsState = {
&initSettingsState,
&settingsStateLoop,
NULL
};