-
Notifications
You must be signed in to change notification settings - Fork 1
/
pageCommands.pde
414 lines (376 loc) · 11.4 KB
/
pageCommands.pde
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
// Created 2011 By Colin G http://www.diydrones.com/profile/ColinG
#define COMMANDFIELDWIDTH 18
#define CMDVALCOUNT 6
#if SAVEMEM == 0
PROGMEM const prog_char confirmReset_Index[] =
//01234567890123456789
" This will reset\n"
" the mission\n"
" Press OK to\n"
" Confirm";
PROGMEM const prog_char confirmRequest_Params[] =
//01234567890123456789
" This will request\n"
" the parameters\n"
" Press OK to\n"
" Confirm";
PROGMEM const prog_char confirmStop_Stream[] =
//01234567890123456789
" This will stop\n"
" the data stream\n"
" Press OK to\n"
" Confirm";
PROGMEM const prog_char confirmStart_Stream[] =
//01234567890123456789
" This will restart\n"
" the data stream\n"
" Press OK to\n"
" Confirm";
PROGMEM const prog_char confirmSet_GCSHome[] =
//01234567890123456789
"This will initialise\n"
" the GCS's Home\n"
"position. Ensure the\n"
"UAV is next to GCS";
PROGMEM const prog_char confirmReset_Home[] =
//01234567890123456789
"This will initialise\n"
"the Aircraft's Home\n"
"position. Ensure it\n"
"is **on the GROUND**";
#endif
// struct msg_command_upload {
// uint8_t action;
// uint16_t itemNumber;
// uint16_t listLength;
// uint8_t commandID;
// uint8_t p1;
// int32_t p2;
// int32_t p3;
// int32_t p4;
//////////////////////////////////////////////////////////////////////////////
// Commands page
////////////////////////////////////////////////////////////////////////////////
PageCommands::PageCommands(const prog_char *textCommands)
{
/// Copy the header and types to internal storage
_textCommands = textCommands;
}
void
PageCommands::_enter(uint8_t fromPage)
{
lcd.clear();
_render();
}
void
PageCommands::_update(void)
{
// don't waste time redrawing if we have no changes to announce
if (!_updated)
return;
// don't redraw too often
if ((millis() - _lastRedraw) < STATUS_UPDATE_INTERVAL)
return;
// redraw the page
_render();
_updated = false;
_lastRedraw = millis();
}
void
PageCommands::_render(void)
{
uint8_t c;
uint8_t i,j,k;
uint8_t row;
const prog_char *str;
str = _textCommands;
lcd.clear();
row = 0;
i=0;
for(j=0;j<_stateFirstVal+4;j++) { //Need to go from zero to read through the string
if (j>=_stateFirstVal)
lcd.setCursor(1,j-_stateFirstVal);
k=0;
for (;;) {
c = pgm_read_byte_near(_textCommands + i++);
if (0 == c)
break;
if ('\n' == c)
break;
else {
if (j>=_stateFirstVal && j<CMDVALCOUNT && k++<COMMANDFIELDWIDTH)
lcd.write(c);
}
}
}
// redraw the "choosing" marker
_paintMarker();
}
/// Draw the "choosing" marker
void
PageCommands::_paintMarker(void)
{
if (_state > 0 && _state < 100) {
lcd.setCursor(0,_state-1 - _stateFirstVal);
lcd.write('>');
}
}
/// remove the "choosing" marker
void
PageCommands::_clearMarker(void)
{
if (_state > 0 && _state < 200) {
lcd.setCursor(0,_state-1 - _stateFirstVal);
lcd.write(' ');
}
}
void
PageCommands::_commandConfirm(void)
{
#if SAVEMEM == 0
switch (_state-201) {
case 0: // Reset waypoint to zero
_commandConfirmMessage(confirmReset_Index);
break;
case 1: // Request all the parameters
_commandConfirmMessage(confirmRequest_Params);
break;
case 2: // Stop data stream
_commandConfirmMessage(confirmStop_Stream);
break;
case 3: // Restart data stream
_commandConfirmMessage(confirmStart_Stream);
break;
case 4: // Set GCS Home
_commandConfirmMessage(confirmSet_GCSHome);
break;
case 5: // Set UAV Home
_commandConfirmMessage(confirmReset_Home);
break;
}
#else
_commandConfirmMessage(confirmMessage);
#endif
}
void
PageCommands::_commandConfirmMessage(const prog_char *str)
{
uint8_t c;
uint8_t row;
lcd.clear();
row = 0;
for (;;) {
c = pgm_read_byte_near(str++);
if (0 == c)
break;
if ('\n' == c) {
lcd.setCursor(0, ++row);
continue;
}
// emit
lcd.write(c);
}
}
void
PageCommands::_commandSend(void)
{
uint8_t i;
mavlink_message_t msg;
// struct BinComm::msg_command_upload command;
switch (_state - 201)
{
case 0: // Reset waypoint index
// mavlink_msg_waypoint_request_list_pack(0xFF, 0xFA, &msg, 1, 1);
// Restart the mission by setting the current waypoint to waypoint 1
mavlink_msg_waypoint_set_current_pack(0xFF, 0xFA, &msg, 1, 1, 1);
comm.send(&msg);
break;
case 1: // Request parameters
mavlink_msg_param_request_list_pack(0xFF, 0xFA, &msg, 1, 1);
// int8_t param_id[15];// = {'H','D','N','G','2','R','L','L','_','D'};
// strcpy(param_id,"HDNG2RLL_D");
// param_id[0] = 'H';
// param_id[1] = 'D';
// param_id[2] = 'N';
// param_id[3] = 'G';
// param_id[4] = '2';
// param_id[5] = 'R';
// param_id[6] = 'L';
// param_id[7] = 'L';
// param_id[8] = '_';
// param_id[9] = 'D';
// param_id[10] = ' ';
// param_id[11] = ' ';
// param_id[12] = ' ';
// param_id[13] = ' ';
// param_id[14] = ' ';
// mavlink_msg_param_request_read_pack(0xFF, 0xFA, &msg, 1, 1, param_id, 111);
comm.send(&msg);
break;
case 2: // Stop Stream
mavlink_msg_request_data_stream_pack(0xFF, 0xFA, &msg, 1, 1, MAV_DATA_STREAM_ALL, 0, 0);
comm.send(&msg);
break;
case 3: // Start Stream
comm.request();
break;
case 4: // GCS Home
gcsLat = mavGPS.lat;
gcsLon = mavGPS.lon;
gcsAlt = mavGPS.alt;
break;
case 5: // UAV Home
// mavlink_msg_command_pack(0xFF, 0xFA, &msg, 1, 1, MAV_CMD_DO_SET_HOME, 0, 1, 0, 0, 0);
// uint8_t system_id, uint8_t component_id, mavlink_message_t* msg, uint8_t target_system, uint8_t target_component, uint16_t seq, uint8_t frame, uint8_t command, uint8_t current, uint8_t autocontinue, float param1, float param2, float param3, float param4, float x, float y, float z)
if (mavWptCount.count != 0) {
mavlink_msg_waypoint_count_pack(0xFF, 0xFA, &msg, 1, 1, mavWptCount.count);
comm.send(&msg);
// mavlink_msg_waypoint_pack(0xFF, 0xFA, &msg, 1, 1, 0, MAV_FRAME_GLOBAL, MAV_CMD_DO_SET_HOME, 0, 0, 1, 0, 0, 0, 0, 0, 0);
mavlink_msg_waypoint_pack(0xFF, 0xFA, &msg, 1, 1, 0, MAV_FRAME_GLOBAL, MAV_CMD_NAV_WAYPOINT, 1, 1, 0, 0, 0, 0, mavGPS.lat, mavGPS.lon, mavGPS.alt);
comm.send(&msg);
delay(50);
mavlink_msg_waypoint_request_pack(0xFF, 0xFA, &msg, 1, 1, 0);
comm.send(&msg);
}
break;
}
/* --- OLD BINCOMM CODE FOR REFERENCE ONLY --- */
// case BINCOMM: // Reset waypoint to zero
// command.action = 1; // Execute immediately
// command.itemNumber = 0; // Not required
// command.listLength = 0; ///@bug <--- EEK!! This is bad
// command.commandID = 0x31; // The command ID for RESET_INDEX
// command.p1 = 0; // Simply set these to zero, unused
// command.p2 = 0;
// command.p3 = 0;
// command.p4 = 0;
// comm.send_msg_command_upload(command.action, command.itemNumber, command.listLength, command.commandID, command.p1, command.p2, command.p3, command.p4);
//comm.send_msg_command_upload(1, 0, 0, 0x31, 0, 0, 0, 0);
//
// break;
// case BINCOMM: // Initialise home to current location
// Upon completing this, the plane will wiggle its servo and send a ready to fly message
// No IMU calibration is done here, so plane orientation is not important
// Plane altitude is, on the other hand, important as this is one of the variables set
// Do not do this whilst flying!
// command.action = 1; // Execute immediately
// command.itemNumber = 0; // Not required
// command.listLength = 0; ///@bug <--- EEK!! This is bad
// command.commandID = 0x44; // The command ID for CMD_RESET_HOME
// command.p1 = 0; // Simply set these to zero, unused
// command.p2 = 0;
// command.p3 = 0;
// command.p4 = 0;
// comm.send_msg_command_upload(command.action, command.itemNumber, command.listLength, command.commandID, command.p1, command.p2, command.p3, command.p4);
//comm.send_msg_command_upload(1, 0, 0, 0x44, 0, 0, 0, 0);
// break;
// case 3: // Fly the square
// // Send the messages in reverse, when the first waypoint is given we start the mission
// for (i=0; i<6; i++)
// {
// //memcpy(&command, &mission1[i], sizeof(command));
// command = mission1++;
// Serial.print("Sending, with action=");
// Serial.print(command.action, DEC);
// Serial.print(", Item number = ");
// Serial.print(command.itemNumber, DEC);
// Serial.print(", p1=");
// Serial.println(command.p1, DEC);
// comm.send_msg_command_upload(command.action, command.itemNumber, command.listLength, command.commandID, command.p1, command.p2, command.p3, command.p4);
// }
// // Now execute the mission... This really really could do with having the previous messages ack'd before sending
// memcpy(&command, &mission1[0], sizeof(command));
// comm.send_msg_command_upload(1, command.itemNumber, command.listLength, command.commandID, command.p1, command.p2, command.p3, command.p4);
}
void
PageCommands::_handleEvent(Page::event eventCode)
{
_clearMarker();
switch (eventCode) {
case UP:
// Navigation
if (_state == 0) {
if (_stateFirstVal > 0) {
_stateFirstVal --;
_updated = true;
}
}
// Picking
else if (_state > 1 && _state < 100) {
if (_state == (_stateFirstVal + 1)) {
_stateFirstVal--;
_updated = true;
}
_state--;
}
// Confirming
else if (_state > 200)
return;
break;
case DOWN:
// Navigation
if (_state == 0) {
if (_stateFirstVal < CMDVALCOUNT-4) {
_stateFirstVal++;
_updated = true;
}
}
// Picking
else if (_state > 0 && _state < CMDVALCOUNT) {
if (_state == (_stateFirstVal + 4)) {
_stateFirstVal++;
_updated = true;
}
_state++;
}
// Confirming
else if (_state > 200)
return;
break;
case OK:
// Enter picking mode
if (_state == 0) {
_state = _stateFirstVal + 1;
}
// Choose command
else if(_state < 100) {
// Update the state
_state += 200;
_commandConfirm();
return;
}
// Confirming
else if(_state > 200) {
// Save the value
_commandSend();
_state = 0;
_updated = true;
return; // Leave before we draw the marker again
}
break;
case LEFT:
// Navigation
if (_state == 0)
_leave(LEFT);
else // Selection, ignore
return;
break;
case RIGHT:
// Navigation
if (_state == 0)
_leave(RIGHT);
else // Selection, ignore
return;
break;
case CANCEL:
if (_state == 0) {
_leave(CANCEL);
return; // avoid drawing the cursor
}
else {
_state = 0;
_updated = true;
}
}
_paintMarker();
}