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,52 +22,47 @@ 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
- if (rec_cnt>=MAX_COMMAND_LENGTH) { // prevent buffer overrun if message is too long
36
- received_chars[0 ] = 0 ;
37
- rec_cnt=0 ;
38
- }
39
- }
25
+ run (*com_port, eol);
40
26
}
41
27
42
- void Commander::run (Stream& serial){
28
+ void Commander::run (Stream& serial, char eol ){
43
29
Stream* tmp = com_port; // save the serial instance
44
- // use the new serial instance to output if not available the one linked in constructor
45
- if (!tmp) com_port = &serial;
30
+ char eol_tmp = this ->eol ;
31
+ this ->eol = eol;
32
+ com_port = &serial;
46
33
47
34
// a string to hold incoming data
48
35
while (serial.available ()) {
49
36
// get the new byte:
50
- received_chars[rec_cnt] = (char )serial.read ();
37
+ int ch = serial.read ();
38
+ received_chars[rec_cnt++] = (char )ch;
51
39
// end of user input
52
- if (received_chars[rec_cnt++] == ' \n ' ) {
40
+ if (echo)
41
+ print ((char )ch);
42
+ if (isSentinel (ch)) {
53
43
// execute the user command
54
44
run (received_chars);
55
45
56
46
// reset the command buffer
57
47
received_chars[0 ] = 0 ;
58
48
rec_cnt=0 ;
59
49
}
50
+ if (rec_cnt>=MAX_COMMAND_LENGTH) { // prevent buffer overrun if message is too long
51
+ received_chars[0 ] = 0 ;
52
+ rec_cnt=0 ;
53
+ }
60
54
}
61
55
62
56
com_port = tmp; // reset the instance to the internal value
57
+ this ->eol = eol_tmp;
63
58
}
64
59
65
60
void Commander::run (char * user_input){
66
61
// execute the user command
67
62
char id = user_input[0 ];
63
+
64
+
65
+
68
66
switch (id){
69
67
case CMD_SCAN:
70
68
for (int i=0 ; i < call_count; i++){
@@ -75,7 +73,7 @@ void Commander::run(char* user_input){
75
73
}
76
74
break ;
77
75
case CMD_VERBOSE:
78
- if (user_input[1 ] != ' \n ' ) verbose = (VerboseMode)atoi (&user_input[1 ]);
76
+ if (! isSentinel ( user_input[1 ]) ) verbose = (VerboseMode)atoi (&user_input[1 ]);
79
77
printVerbose (F (" Verb:" ));
80
78
switch (verbose){
81
79
case VerboseMode::nothing:
@@ -88,7 +86,7 @@ void Commander::run(char* user_input){
88
86
}
89
87
break ;
90
88
case CMD_DECIMAL:
91
- if (user_input[1 ] != ' \n ' ) decimal_places = atoi (&user_input[1 ]);
89
+ if (! isSentinel ( user_input[1 ]) ) decimal_places = atoi (&user_input[1 ]);
92
90
printVerbose (F (" Decimal:" ));
93
91
println (decimal_places);
94
92
break ;
@@ -109,7 +107,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
109
107
char sub_cmd = user_command[1 ];
110
108
int value_index = (sub_cmd >= ' A' && sub_cmd <= ' Z' ) ? 2 : 1 ;
111
109
// check if get command
112
- bool GET = user_command[value_index] == ' \n ' ;
110
+ bool GET = isSentinel ( user_command[value_index]) ;
113
111
// parse command values
114
112
float value = atof (&user_command[value_index]);
115
113
@@ -358,7 +356,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
358
356
case SCMD_SET:
359
357
if (!GET) motor->monitor_variables = (uint8_t ) 0 ;
360
358
for (int i = 0 ; i < 7 ; i++){
361
- if (user_command[value_index+i] == ' \n ' ) break ;
359
+ if (isSentinel ( user_command[value_index+i]) ) break ;
362
360
if (!GET) motor->monitor_variables |= (user_command[value_index+i] - ' 0' ) << (6 -i);
363
361
print ( (user_command[value_index+i] - ' 0' ) );
364
362
}
@@ -378,7 +376,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
378
376
379
377
void Commander::pid (PIDController* pid, char * user_cmd){
380
378
char cmd = user_cmd[0 ];
381
- bool GET = user_cmd[1 ] == ' \n ' ;
379
+ bool GET = isSentinel ( user_cmd[1 ]) ;
382
380
float value = atof (&user_cmd[1 ]);
383
381
384
382
switch (cmd){
@@ -415,7 +413,7 @@ void Commander::pid(PIDController* pid, char* user_cmd){
415
413
416
414
void Commander::lpf (LowPassFilter* lpf, char * user_cmd){
417
415
char cmd = user_cmd[0 ];
418
- bool GET = user_cmd[1 ] == ' \n ' ;
416
+ bool GET = isSentinel ( user_cmd[1 ]) ;
419
417
float value = atof (&user_cmd[1 ]);
420
418
421
419
switch (cmd){
@@ -431,11 +429,26 @@ void Commander::lpf(LowPassFilter* lpf, char* user_cmd){
431
429
}
432
430
433
431
void Commander::scalar (float * value, char * user_cmd){
434
- bool GET = user_cmd[0 ] == ' \n ' ;
432
+ bool GET = isSentinel ( user_cmd[0 ]) ;
435
433
if (!GET) *value = atof (user_cmd);
436
434
println (*value);
437
435
}
438
436
437
+ bool Commander::isSentinel (char ch)
438
+ {
439
+ if (ch == eol)
440
+ return true ;
441
+ else if (ch == ' \r ' )
442
+ {
443
+ if (verbose == VerboseMode::user_friendly)
444
+ {
445
+ print (F (" Warning! \\ r detected but is not configured as end of line sentinel, which is configured as ascii code '" ));
446
+ print (int (eol));
447
+ print (" '\n " );
448
+ }
449
+ }
450
+ return false ;
451
+ }
439
452
440
453
void Commander::print (const int number){
441
454
if ( !com_port || verbose == VerboseMode::nothing ) return ;
0 commit comments