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 (user_input[1 ] != eol ) 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 (user_input[1 ] != eol ) 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
@@ -306,7 +304,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
306
304
case SCMD_SET:
307
305
if (!GET) motor->monitor_variables = (uint8_t ) 0 ;
308
306
for (int i = 0 ; i < 7 ; i++){
309
- if (user_command[value_index+i] == ' \n ' ) break ;
307
+ if (isSentinel ( user_command[value_index+i]) ) break ;
310
308
if (!GET) motor->monitor_variables |= (user_command[value_index+i] - ' 0' ) << (6 -i);
311
309
print ( (user_command[value_index+i] - ' 0' ) );
312
310
}
@@ -326,7 +324,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
326
324
327
325
void Commander::pid (PIDController* pid, char * user_cmd){
328
326
char cmd = user_cmd[0 ];
329
- bool GET = user_cmd[1 ] == ' \n ' ;
327
+ bool GET = isSentinel ( user_cmd[1 ]) ;
330
328
float value = atof (&user_cmd[1 ]);
331
329
332
330
switch (cmd){
@@ -363,7 +361,7 @@ void Commander::pid(PIDController* pid, char* user_cmd){
363
361
364
362
void Commander::lpf (LowPassFilter* lpf, char * user_cmd){
365
363
char cmd = user_cmd[0 ];
366
- bool GET = user_cmd[1 ] == ' \n ' ;
364
+ bool GET = isSentinel ( user_cmd[1 ]) ;
367
365
float value = atof (&user_cmd[1 ]);
368
366
369
367
switch (cmd){
@@ -379,11 +377,26 @@ void Commander::lpf(LowPassFilter* lpf, char* user_cmd){
379
377
}
380
378
381
379
void Commander::scalar (float * value, char * user_cmd){
382
- bool GET = user_cmd[0 ] == ' \n ' ;
380
+ bool GET = isSentinel ( user_cmd[0 ]) ;
383
381
if (!GET) *value = atof (user_cmd);
384
382
println (*value);
385
383
}
386
384
385
+ bool Commander::isSentinel (char ch)
386
+ {
387
+ if (ch == eol)
388
+ return true ;
389
+ else if (ch == ' \r ' )
390
+ {
391
+ if (verbose == VerboseMode::user_friendly)
392
+ {
393
+ print (F (" Warning! \\ r detected but is not configured as end of line sentinel, which is configured as ascii code '" ));
394
+ print (int (eol));
395
+ print (" '\n " );
396
+ }
397
+ }
398
+ return false ;
399
+ }
387
400
388
401
void Commander::print (const int number){
389
402
if ( !com_port || verbose == VerboseMode::nothing ) return ;
0 commit comments