1
1
#include " Commander.h"
2
2
3
3
4
- Commander::Commander (Stream& serial){
4
+ Commander::Commander (Stream& serial, char eol, bool echo ){
5
5
com_port = &serial;
6
+ this ->eol = eol;
7
+ this ->echo = echo;
6
8
}
7
- Commander::Commander (){
8
- // do nothing
9
+ Commander::Commander (char eol, bool echo){
10
+ this ->eol = eol;
11
+ this ->echo = echo;
9
12
}
10
13
11
14
@@ -19,33 +22,24 @@ void Commander::add(char id, CommandCallback onCommand, char* label ){
19
22
20
23
void Commander::run (){
21
24
if (!com_port) return ;
22
- // a string to hold incoming data
23
- while (com_port->available ()) {
24
- // get the new byte:
25
- received_chars[rec_cnt] = (char )com_port->read ();
26
- // end of user input
27
- if (received_chars[rec_cnt++] == ' \n ' ) {
28
- // execute the user command
29
- run (received_chars);
30
-
31
- // reset the command buffer
32
- received_chars[0 ] = 0 ;
33
- rec_cnt=0 ;
34
- }
35
- }
25
+ run (*com_port, eol);
36
26
}
37
27
38
- void Commander::run (Stream& serial){
28
+ void Commander::run (Stream& serial, char eol ){
39
29
Stream* tmp = com_port; // save the serial instance
40
- // use the new serial instance to output if not available the one linked in constructor
41
- if (!tmp) com_port = &serial;
30
+ char eol_tmp = this ->eol ;
31
+ this ->eol = eol;
32
+ com_port = &serial;
42
33
43
34
// a string to hold incoming data
44
35
while (serial.available ()) {
45
36
// get the new byte:
46
- received_chars[rec_cnt] = (char )serial.read ();
37
+ int ch = serial.read ();
38
+ received_chars[rec_cnt++] = (char )ch;
47
39
// end of user input
48
- if (received_chars[rec_cnt++] == ' \n ' ) {
40
+ if (echo)
41
+ print ((char )ch);
42
+ if (isSentinel (ch)) {
49
43
// execute the user command
50
44
run (received_chars);
51
45
@@ -56,11 +50,15 @@ void Commander::run(Stream& serial){
56
50
}
57
51
58
52
com_port = tmp; // reset the instance to the internal value
53
+ this ->eol = eol_tmp;
59
54
}
60
55
61
56
void Commander::run (char * user_input){
62
57
// execute the user command
63
58
char id = user_input[0 ];
59
+
60
+
61
+
64
62
switch (id){
65
63
case CMD_SCAN:
66
64
for (int i=0 ; i < call_count; i++){
@@ -71,7 +69,7 @@ void Commander::run(char* user_input){
71
69
}
72
70
break ;
73
71
case CMD_VERBOSE:
74
- if (user_input[1 ] != ' \n ' ) verbose = (VerboseMode)atoi (&user_input[1 ]);
72
+ if (! isSentinel ( user_input[1 ]) ) verbose = (VerboseMode)atoi (&user_input[1 ]);
75
73
printVerbose (F (" Verb:" ));
76
74
switch (verbose){
77
75
case VerboseMode::nothing:
@@ -84,7 +82,7 @@ void Commander::run(char* user_input){
84
82
}
85
83
break ;
86
84
case CMD_DECIMAL:
87
- if (user_input[1 ] != ' \n ' ) decimal_places = atoi (&user_input[1 ]);
85
+ if (! isSentinel ( user_input[1 ]) ) decimal_places = atoi (&user_input[1 ]);
88
86
printVerbose (F (" Decimal:" ));
89
87
println (decimal_places);
90
88
break ;
@@ -105,7 +103,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
105
103
char sub_cmd = user_command[1 ];
106
104
int value_index = (sub_cmd >= ' A' && sub_cmd <= ' Z' ) ? 2 : 1 ;
107
105
// check if get command
108
- bool GET = user_command[value_index] == ' \n ' ;
106
+ bool GET = isSentinel ( user_command[value_index]) ;
109
107
// parse command values
110
108
float value = atof (&user_command[value_index]);
111
109
@@ -354,7 +352,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
354
352
case SCMD_SET:
355
353
if (!GET) motor->monitor_variables = (uint8_t ) 0 ;
356
354
for (int i = 0 ; i < 7 ; i++){
357
- if (user_command[value_index+i] == ' \n ' ) break ;
355
+ if (isSentinel ( user_command[value_index+i]) ) break ;
358
356
if (!GET) motor->monitor_variables |= (user_command[value_index+i] - ' 0' ) << (6 -i);
359
357
print ( (user_command[value_index+i] - ' 0' ) );
360
358
}
@@ -374,7 +372,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
374
372
375
373
void Commander::pid (PIDController* pid, char * user_cmd){
376
374
char cmd = user_cmd[0 ];
377
- bool GET = user_cmd[1 ] == ' \n ' ;
375
+ bool GET = isSentinel ( user_cmd[1 ]) ;
378
376
float value = atof (&user_cmd[1 ]);
379
377
380
378
switch (cmd){
@@ -411,7 +409,7 @@ void Commander::pid(PIDController* pid, char* user_cmd){
411
409
412
410
void Commander::lpf (LowPassFilter* lpf, char * user_cmd){
413
411
char cmd = user_cmd[0 ];
414
- bool GET = user_cmd[1 ] == ' \n ' ;
412
+ bool GET = isSentinel ( user_cmd[1 ]) ;
415
413
float value = atof (&user_cmd[1 ]);
416
414
417
415
switch (cmd){
@@ -427,11 +425,26 @@ void Commander::lpf(LowPassFilter* lpf, char* user_cmd){
427
425
}
428
426
429
427
void Commander::scalar (float * value, char * user_cmd){
430
- bool GET = user_cmd[0 ] == ' \n ' ;
428
+ bool GET = isSentinel ( user_cmd[0 ]) ;
431
429
if (!GET) *value = atof (user_cmd);
432
430
println (*value);
433
431
}
434
432
433
+ bool Commander::isSentinel (char ch)
434
+ {
435
+ if (ch == eol)
436
+ return true ;
437
+ else if (ch == ' \r ' )
438
+ {
439
+ if (verbose == VerboseMode::user_friendly)
440
+ {
441
+ print (F (" Warning! \\ r detected but is not configured as end of line sentinel, which is configured as ascii code '" ));
442
+ print (int (eol));
443
+ print (" '\n " );
444
+ }
445
+ }
446
+ return false ;
447
+ }
435
448
436
449
void Commander::print (const int number){
437
450
if ( !com_port || verbose == VerboseMode::nothing ) return ;
0 commit comments