-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFairWindCommands.cpp
More file actions
584 lines (461 loc) · 14.6 KB
/
Copy pathFairWindCommands.cpp
File metadata and controls
584 lines (461 loc) · 14.6 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
* Copyright (C) 2014 University of Napoli Parthenope
* Department of Science and Technologies
* High Performance Scientific Computing Smart-Lab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: Raffaele Montella (raffaele.montella@uniparthenope.it)
*
* Notes: This software is part of the FairWind
* (Smart, Cloud-Enabled, Navigation System for Boats and Yachts)
* please visit http://fairwind.uniparthenope.it for more details.
* The main Android application is availabile here:
* https://play.google.com/store/apps/details?id=it.uniparthenope.fairwind
* Contact the author if you plan to use any part of FairWind on a real boat.
*/
#include "FairWindCommands.h"
#include "util.h"
/*
* The FairWind command handler constructor
*/
FairWindCommands::FairWindCommands(FairWindModel *model, NMEABase* nmeaIn) {
// The model
_model=model;
// The nmea input object
_nmeaIn=nmeaIn;
}
void FairWindCommands::manageDemoMode(NMEABase* nmeaOut) {
char sentenceOut[NMEA_MAX_MESSAGE_SIZE];
char *demoStatus[2]={"off","on"};
bool done=false;
// Check if
if (_nmeaIn->check(3,"on")) {
_model->demoOn();
done=true;
}
else if (_nmeaIn->check(3,"off")) {
_model->demoOff();
done=true;
}
else if (_nmeaIn->check(3,"status")) {
done=true;
}
if (done) {
// Prepare the output
sprintf(sentenceOut,"$FWACK,%s,demo,%s*",_model->getConfig()->id,demoStatus[_model->isDemo()]);
sprintf(sentenceOut,"%s%02X",sentenceOut,nmeaOut->calc_checksum(sentenceOut));
nmeaOut->set(sentenceOut);
}
}
void FairWindCommands::fillStatus(NMEABase* nmeaOut) {
char sentenceOut[NMEA_MAX_MESSAGE_SIZE];
bool done=false;
byte i[3],o[3];
int pos=0,idx=0;
for(int id=1;id<=20;id++) {
if (id>=1 && id<=8) idx=0;
else if (id>=9 && id<=16) idx=1;
else idx=2;
if (digitalRead(_model->getConfig()->inps[id-1])) SET_BIT(i[idx],pos); else CLEAR_BIT(i[idx],pos);
if (digitalReadOutputPin(_model->getConfig()->outs[id-1])) SET_BIT(o[idx],pos); else CLEAR_BIT(o[idx],pos);
pos++;
if (pos==8) pos=0;
}
sprintf(sentenceOut,"$FWSUP,%s,inp,%02X,%02X,%02X,out,%02X,%02X,%02X*",
_model->getConfig()->id,
i[0],i[1],i[2],o[0],o[1],o[2]
);
done=true;
if (done) {
sprintf(sentenceOut,"%s%02X",sentenceOut,nmeaOut->calc_checksum(sentenceOut));
nmeaOut->set(sentenceOut);
}
}
/*
* Process set configuration
*/
void FairWindCommands::setConfiguration(NMEABase* nmeaOut) {
char sentenceOut[NMEA_MAX_MESSAGE_SIZE];
// Save and reboot flag
bool save=false;
bool reboot=false;
// The config structure
Config config;
// Copy the config status
memcpy(&config,_model->getConfig(),sizeof(Config));
// Check if the target is "id"
if (_nmeaIn->check(3,"id")) {
// Set the label
strcpy(config.id,_nmeaIn->term(4));
// Prepare the output
sprintf(sentenceOut,"$FWACK,%s,id,%s*",
config.id,config.id
);
// Set the save and reboot flag
save=true;
} else
// Check if the target is "out"
if (_nmeaIn->check(3,"out")) {
// Get the out id
int outId=(int)(_nmeaIn->term_decimal(4));
// Check if the switch id is correct
if (outId>=1 && outId<=20) {
// Get the output pin
int outPin=(int)(_nmeaIn->term_decimal(5));
// Update the out table
config.outs[outId-1]=outPin;
// Make the setup effective since now
pinMode(outPin,OUTPUT);
//pinMode(inpPin,INPUT);
delay(10);
digitalWrite(outPin,LOW);
// Prepare the output
sprintf(sentenceOut,"$FWACK,%s,out,%d,%d,%d*",
config.id,outId,
config.outs[outId-1],
digitalReadOutputPin(config.outs[outId-1])
);
// Set the save and reboot flag
save=true;
}
} else
// Check if the target is "inp"
if (_nmeaIn->check(3,"inp")) {
// Get the inp id
int inpId=(int)(_nmeaIn->term_decimal(4));
// Check if the switch id is correct
if (inpId>=1 && inpId<=20) {
// Get the input pin
int inpPin=(int)(_nmeaIn->term_decimal(5));
// Update the out table
config.inps[inpId-1]=inpPin;
// Make the setup effective since now
pinMode(inpPin,INPUT);
delay(10);
// Prepare the output
sprintf(sentenceOut,"$FWACK,%s,inp,%d,%d,%d*",
config.id,inpId,
config.inps[inpId-1],
digitalRead(config.inps[inpId-1])
);
// Set the save and reboot flag
save=true;
}
} else
// Check if the target is nmea or seatalk
if (_nmeaIn->check(3,"nmea") || _nmeaIn->check(3,"seatalk")) {
// $FWCMD,,conf_set,nmea,0,host,115200*52
// The serial port number
int port=(int)(_nmeaIn->term_decimal(4));
// Check if the port number is correct
if (port>=0 || port<=4) {
// Set the label
strcpy(config.serialLabel[port],_nmeaIn->term(5));
// Check if is a nmea port
if (_nmeaIn->check(3,"nmea")) {
// set the baud rate
config.serialBaud[port]=(long)(_nmeaIn->term_decimal(6));
// Unset the seatalk port
config.seaTalk &= ~(1 << port);
// Set the save and reboot flag
reboot=save=true;
} else if (_nmeaIn->check(3,"seatalk")) {
// Check if the port nunber is correct
if (port==1 || port==2) {
// Set the label
strcpy(config.serialLabel[port],_nmeaIn->term(5));
// Set the baud rate at 4800
config.serialBaud[port]=4800l;
// Set the seatalk port
config.seaTalk |= 1 << port;
// Set the save and reboot flag
reboot=save=true;
}
}
}
}
// Check the save flag
if (save) {
// Copy the config status
memcpy(_model->getConfig(),&config,sizeof(Config));
// Save on the EEPROM
_model->saveConfig();
delay(250);
}
// Check the reboot flag
if (reboot) {
// Reboot
digitalWrite(6,LOW);
Serial.println("Reset");
}
// Check the save flag
if (save) {
// Create the ack string
sprintf(sentenceOut,"%s%02X",sentenceOut,nmeaOut->calc_checksum(sentenceOut));
nmeaOut->set(sentenceOut);
}
}
/*
* Process get configuration
*/
void FairWindCommands::fillConfiguration(NMEABase* nmeaOut) {
char sentenceOut[NMEA_MAX_MESSAGE_SIZE];
bool done=false;
// Initialize the ouput sentence
strcpy(sentenceOut,"");
// Check if the target is "ports"
if (_nmeaIn->check(3,"ports")) {
// Prepare the ACK string header
sprintf(sentenceOut,"$FWACK,%s,conf,ports",_model->getConfig()->id);
// For each port...
for (int i=0;i<5;i++) {
// Check the SeaTalk bit
if (CHECK_BIT(_model->getConfig()->seaTalk,i)) {
// Add the seatalk string
strcat(sentenceOut,",seatalk");
} else {
// Add the nmea string
strcat(sentenceOut,",nmea");
}
}
// Complete the sequence with the asterisk
strcat(sentenceOut,"*");
// Set the done flag
done=true;
} else
// Check if the target is nmea or seatalk
if (_nmeaIn->check(3,"nmea") || _nmeaIn->check(3,"seatalk")) {
// $FWCMD,,conf_get,nmea,0*41
// The serial port number
int port=(int)(_nmeaIn->term_decimal(4));
// Check if the port number is correct
if (port>=0 || port<=4) {
// Check if the target and the seatalk flag are consistent
if ((_nmeaIn->check(3,"nmea") && !_model->isSeaTalk(port)) || (_nmeaIn->check(2,"seatalk")) && _model->isSeaTalk(port)) {
// Prepare the ACK string
sprintf(sentenceOut,"$FWACK,%s,conf,%s,%d,%s,%lu*",
_model->getConfig()->id,
_nmeaIn->term(3),port,
_model->getConfig()->serialLabel[port],_model->getConfig()->serialBaud[port]);
// Set the done flag
done=true;
}
}
} else
// Check if the target is "out"
if (_nmeaIn->check(3,"out")) {
// Get the out id
int outId=(int)(_nmeaIn->term_decimal(4));
// Check if the id is correct
if (outId>=1 && outId<=20) {
// Prepare the ACK string
sprintf(sentenceOut,"$FWACK,%s,out,%d,%d*",
_model->getConfig()->id,outId,
_model->getConfig()->outs[outId-1]
);
// Set the done flag
done=true;
}
} else
// Check if the target is "inp"
if (_nmeaIn->check(3,"inp")) {
// Get the out id
int inpId=(int)(_nmeaIn->term_decimal(4));
// Check if the id is correct
if (inpId>=1 && inpId<=20) {
// Prepare the ACK string
sprintf(sentenceOut,"$FWACK,%s,inp,%d,%d*",
_model->getConfig()->id,inpId,
_model->getConfig()->inps[inpId-1]
);
// Set the done flag
done=true;
}
}
// Check the done flag
if (done) {
// Set the output string
sprintf(sentenceOut,"%s%02X",sentenceOut,nmeaOut->calc_checksum(sentenceOut));
nmeaOut->set(sentenceOut);
}
}
/*
* Process get configuration
*/
void FairWindCommands::fillWho(NMEABase* nmeaOut) {
char sentenceOut[NMEA_MAX_MESSAGE_SIZE];
bool done=false;
sprintf(sentenceOut,"$FWACK,%s*",_model->getConfig()->id);
done=true;
if (done) {
sprintf(sentenceOut,"%s%02X",sentenceOut,nmeaOut->calc_checksum(sentenceOut));
nmeaOut->set(sentenceOut);
}
}
void FairWindCommands::fillFirmwareVersion(NMEABase* nmeaOut) {
char sentenceOut[NMEA_MAX_MESSAGE_SIZE];
bool done=false;
sprintf(sentenceOut,"$FWACK,%s,ver,%s*",_model->getConfig()->id,FW_FIRMWARE_VERSION);
done=true;
if (done) {
sprintf(sentenceOut,"%s%02X",sentenceOut,nmeaOut->calc_checksum(sentenceOut));
nmeaOut->set(sentenceOut);
}
}
/*
* Set the output pin
*/
void FairWindCommands::setOut(NMEABase* nmeaOut) {
char sentenceOut[NMEA_MAX_MESSAGE_SIZE];
bool done=false;
// Get the output id
int outId=(int)(_nmeaIn->term_decimal(3));
// Check if the id is correct
if (outId>=1 && outId<=20) {
// Check if the value is positive
if ((int)(_nmeaIn->term_decimal(4))>0) {
// Set the status to HIGH
digitalWrite(_model->getConfig()->outs[outId-1],HIGH);
} else {
// Set the status to LOW
digitalWrite(_model->getConfig()->outs[outId-1],LOW);
}
// Wait... no too much rush!
delay(10);
// Prepare the sentence
sprintf(sentenceOut,"$FWACK,%s,out,%d,%d*",
_model->getConfig()->id,
outId,
digitalReadOutputPin(_model->getConfig()->outs[outId-1]));
// Set the done flag
done=true;
}
if (done) {
sprintf(sentenceOut,"%s%02X",sentenceOut,nmeaOut->calc_checksum(sentenceOut));
nmeaOut->set(sentenceOut);
}
}
/*
* Get the status of the output pin
*/
void FairWindCommands::fillOut(NMEABase* nmeaOut) {
// Get the output id
int outId=(int)(_nmeaIn->term_decimal(3));
fillOut(nmeaOut,outId);
}
void FairWindCommands::fillOut(NMEABase* nmeaOut, int outId) {
char sentenceOut[NMEA_MAX_MESSAGE_SIZE];
bool done=false;
// Check if the id is correct
if (outId>=1 && outId<=20) {
// Prepare the sentence
sprintf(sentenceOut,"$FWACK,%s,out,%d,%d*",
_model->getConfig()->id,
outId,
digitalReadOutputPin(_model->getConfig()->outs[outId-1]));
done=true;
}
if (done) {
sprintf(sentenceOut,"%s%02X",sentenceOut,nmeaOut->calc_checksum(sentenceOut));
nmeaOut->set(sentenceOut);
}
}
/*
* Get the status of the input pin
*/
void FairWindCommands::fillInp(NMEABase* nmeaOut) {
// Get the input id
int inpId=(int)(_nmeaIn->term_decimal(3));
fillInp(nmeaOut,inpId);
}
void FairWindCommands::fillInp(NMEABase* nmeaOut, int inpId) {
char sentenceOut[NMEA_MAX_MESSAGE_SIZE];
bool done=false;
// Check if the input id is correct
if (inpId>=1 && inpId<=20) {
// Prepare the sentence
sprintf(sentenceOut,"$FWACK,%s,inp,%d,%d*",
_model->getConfig()->id,
inpId,
digitalRead(_model->getConfig()->inps[inpId-1]));
// Set the done flag
done=true;
}
if (done) {
sprintf(sentenceOut,"%s%02X",sentenceOut,nmeaOut->calc_checksum(sentenceOut));
nmeaOut->set(sentenceOut);
}
}
/*
* Process a command for the board if any
*
*
* $FWWHO*41 Board ID
* $FWCMD,,ver*3A Firmware version
* $FWCMD,,conf_get,ports*30 Port configuration
* $FWCMD,,conf_get,nmea,0*41 Port configuration of nmea 0
* $FWCMD,,conf_set,nmea,0,host,115200*52 Set the serial 0 as nmea named host
* $FWCMD,,conf_set,out,1,4*25 Set the out 1 using the pin 4 as output
* $FWCMD,,conf_set,out,1,24*17 Set the out 1 usibf the pin 24 as output
* $FWCMD,,out_set,1,1*08 Set the out 1 to on
* $FWCMD,,out_get,1*01 Return the state of the out 1
* $FWCMD,,out_set,1,0*09 Set the out 1 to off
* $FWCMD,,conf_set,inp,1,12*0B Set the inp 1 uning the pin 12 as input
* $FWCMD,,inp_get,1*30 Return the state of the inp 1
* $FWCMD,,conf_get,out,1*55 Get the setup for the out 1
* $FWCMD,,conf_get,inp,1*55 Get the setup for the input 1
* $FWCMD,,conf_set,id,feder0*2F Set the board id as feder0
*
*/
bool FairWindCommands::process(NMEABase* nmeaOut) {
if (_nmeaIn->check(0,"FWWHO")) {
fillWho(nmeaOut);
return true;
}
// Check if the setting is for this board
if (_nmeaIn->check(0,"FWCMD") && (_nmeaIn->check(1,_model->getConfig()->id) || _nmeaIn->check(1,"")) ) {
// Manage the demo mode
if (_nmeaIn->check(2,"demo")) {
manageDemoMode(nmeaOut);
return true;
}
// Check if is a config custom sentence
else if (_nmeaIn->check(2,"conf_set")) {
setConfiguration(nmeaOut);
return true;
}
else if (_nmeaIn->check(2,"conf_get")) {
fillConfiguration(nmeaOut);
return true;
}
else if (_nmeaIn->check(2,"ver")) {
fillFirmwareVersion(nmeaOut);
return true;
}
else if (_nmeaIn->check(2,"out_set")) {
setOut(nmeaOut);
return true;
}
else if (_nmeaIn->check(2,"out_get")) {
fillOut(nmeaOut);
return true;
}
else if (_nmeaIn->check(2,"inp_get")) {
fillInp(nmeaOut);
return true;
}
}
// It is not a command
return false;
}