-
Notifications
You must be signed in to change notification settings - Fork 3
/
sweep.cpp
270 lines (247 loc) · 7.14 KB
/
sweep.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
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
#include "sweep.h"
#include <Arduino.h>
#include "icons.h"
#include "screen.h"
#include "settings.h"
#include "joystick.h"
#include "menu.h"
#define DELAY_BETWEEN_TESTS_MS (4000)
#define NO_CHANNEL_DELAY_SECONDS (8)
bool sfSuccess[6];
void initSweepResults();
void initSweepError();
void spinBackMenu();
state sweepResultsState = {
&initSweepResults,
&spinBackMenu,
NULL
};
state sweepErrorState = {
&initSweepError,
&spinBackMenu,
NULL
};
/**************************************************************************/
/*!
@brief Sets up the screen for sweep mode
*/
/**************************************************************************/
void initSweep() {
if(!loraJoinIfNeeded()) {
setState(&menuState);
return;
}
setLineInverted(0, true);
setLineInverted(1, false);
setLineInverted(2, true);
setLineInverted(3, false);
setLineInverted(4, true);
clearScreen();
drawText(32, 1, "Press To");
drawText(50, 2, "Sweep");
}
/**************************************************************************/
/*!
@brief Sends a single lorawan transmission. Updates the screen with the results.
@param sf The spread factor to test at.
@result The overal result of the transmission.
*/
/**************************************************************************/
transmit_responce testSF(spread_factor sf) {
int errorAttempt = 0;
transmit_result res;
char buffer[20];
drawSmallIcon(25, 0, 24, 24, transmitting);
clearLine(4);
while(1) {
switch(loraTransmit(false, sf, res)) {
case TEST_SUCCESS:
clearLine(2);
drawText(5, 2, sfToText(sf));
snprintf(buffer, 20, "%ddBM", getTransmitPower());
drawText(97, 2, buffer);
clearSpace(25, 0, 25);
clearSpace(50, 1, 160);
snprintf(buffer, 20, "%3.1fMHz", res.freq);
drawText(64, 1, buffer);
snprintf(buffer, 20, "%ddBM", res.noise);
drawText(5, 4, buffer);
snprintf(buffer, 20, "GW %d", res.gateways);
drawText(95, 4, buffer);
return TEST_SUCCESS;
case TEST_FAIL:
clearLine(2);
drawText(5, 2, sfToText(sf));
snprintf(buffer, 20, "%ddBM", getTransmitPower());
drawText(97, 2, buffer);
clearSpace(25, 0, 25);
drawText(5, 4, "Fail");
return TEST_FAIL;
case TEST_ERROR:
clearLine(2);
drawText(5, 2, sfToText(sf));
snprintf(buffer, 20, "%ddBM", getTransmitPower());
drawText(97, 2, buffer);
clearSpace(25, 0, 25);
for(int i=0; i<NO_CHANNEL_DELAY_SECONDS; i++) {
if(readJoystick()==JOY_PRESSED)
return TEST_ERROR;
if((errorAttempt++)&1)
drawText(8, 4, "Cancel Test?");
else
drawText(2, 4, "No Free Chan.");
delay(1000);
clearLine(4);
}
if(readJoystick()==JOY_PRESSED)
return TEST_ERROR;
}
}
}
/**************************************************************************/
/*!
@brief Checks for a joystick press then begins a sweep test through all spread factors. Also updates the gps + battery icon.
*/
/**************************************************************************/
void spinSweep() {
drawGPSIcon();
drawBatteryIcon();
if(readJoystick() == JOY_PRESSED) {
transmit_responce tr;
clearLine(1);
drawText(5, 1, "TX");
clearLine(2);
clearLine(3);
drawText(5, 3, "RX Qual.");
if( (tr = testSF(SF_7)) == TEST_ERROR) {
setState(&sweepErrorState);
return;
}
sfSuccess[0] = tr==TEST_SUCCESS;
drawGPSIcon();
drawBatteryIcon();
delay(DELAY_BETWEEN_TESTS_MS);
if( (tr = testSF(SF_8)) == TEST_ERROR) {
setState(&sweepErrorState);
return;
}
sfSuccess[1] = tr==TEST_SUCCESS;
drawGPSIcon();
drawBatteryIcon();
delay(DELAY_BETWEEN_TESTS_MS);
if( (tr = testSF(SF_9)) == TEST_ERROR) {
setState(&sweepErrorState);
return;
}
sfSuccess[2] = tr==TEST_SUCCESS;
drawGPSIcon();
drawBatteryIcon();
delay(DELAY_BETWEEN_TESTS_MS);
if( (tr = testSF(SF_10)) == TEST_ERROR) {
setState(&sweepErrorState);
return;
}
sfSuccess[3] = tr==TEST_SUCCESS;
drawGPSIcon();
drawBatteryIcon();
delay(DELAY_BETWEEN_TESTS_MS);
if( (tr = testSF(SF_11)) == TEST_ERROR) {
setState(&sweepErrorState);
return;
}
sfSuccess[4] = tr==TEST_SUCCESS;
drawGPSIcon();
drawBatteryIcon();
delay(DELAY_BETWEEN_TESTS_MS);
if( (tr = testSF(SF_12)) == TEST_ERROR) {
setState(&sweepErrorState);
return;
}
sfSuccess[5] = tr==TEST_SUCCESS;
drawGPSIcon();
drawBatteryIcon();
delay(DELAY_BETWEEN_TESTS_MS);
setState(&sweepResultsState);
}
}
/**************************************************************************/
/*!
@brief Ease-of-use function to convert array indicies to spread factor text.
@param index the array index
@return The corresponding text
*/
/**************************************************************************/
char* sfIndexToText(int index) {
switch(index) {
case 0: return "SF 7";
case 1: return "SF 8";
case 2: return "SF 9";
case 3: return "SF10";
case 4: return "SF11";
case 5: return "SF12";
default: return "Unknown";
}
}
/**************************************************************************/
/*!
@brief Sets up the screen to display the test results
*/
/**************************************************************************/
void initSweepResults() {
setLineInverted(0, true);
setLineInverted(1, false);
setLineInverted(2, false);
setLineInverted(3, false);
setLineInverted(4, false);
clearScreen();
for(int i=0; i<3; i++) {
setLineInverted(2, sfSuccess[i]);
clearSpace(i*53, 2, 54);
drawText(i*53+2, 2, sfIndexToText(i));
setLineInverted(3, sfSuccess[i+3]);
clearSpace(i*53, 3, 54);
drawText(i*53+2, 3, sfIndexToText(i+3));
}
drawText(2, 1, "SF Avail.");
drawText(2, 4, "<Back");
drawText(99, 4, "Menu>");
}
/**************************************************************************/
/*!
@brief Sets up the screen to display an error if the sweep test errors.
*/
/**************************************************************************/
void initSweepError() {
setLineInverted(0, true);
setLineInverted(1, false);
setLineInverted(2, false);
setLineInverted(3, false);
setLineInverted(4, false);
clearScreen();
drawText(50, 0, "Error");
//TODO: use lines 1-3 to display some sort of error message. Was it a hardware fault or ran out of duty cycle, etc.
drawText(2, 4, "<Back");
drawText(99, 4, "Menu>");
}
/**************************************************************************/
/*!
@brief A simple spin that monitors for joystick input. Transitions to initial sweep state, or back to main menu.
*/
/**************************************************************************/
void spinBackMenu() {
drawGPSIcon();
drawBatteryIcon();
switch(readJoystick()) {
case JOY_LEFT:
setState(&sweepState);
return;
case JOY_RIGHT:
setState(&menuState);
return;
}
}
state sweepState = {
&initSweep,
&spinSweep,
NULL
};