@@ -54,6 +54,46 @@ void Commander::begin(Stream *sPort, Stream *oPort, const commandList_t *command
54
54
// ==============================================================================================================
55
55
56
56
bool Commander::update (){
57
+ if (!ports.inPort ) return 0 ; // don't bother if there is no stream attached
58
+ // Check if streamOn is true. If it is then all incoming chars get routed somewhere and no command processing happens (for sending files ...)
59
+ if (commandState.bit .streamOn ){
60
+
61
+ bufferString = " " ;// clear the buffer so we can fill it with any new chars
62
+ bytesWritten = 0 ;
63
+ commandState.bit .bufferFull = false ;
64
+
65
+ while (ports.inPort ->available ()){
66
+ int inByte = ports.inPort ->read ();
67
+ if (inByte == EOFChar){
68
+ println (" EOF Found, tidying up" );
69
+ commandState.bit .streamOn = false ;
70
+ // get rid of any newlines or CRs in the stream
71
+ while (ports.inPort ->peek () == ' \n ' || ports.inPort ->peek () == ' \r ' ) ports.inPort ->read ();
72
+ // call the handler again so it can clean up and close anything that needs closing
73
+ commandState.bit .commandHandled = handleCustomCommand ();
74
+ resetBuffer ();
75
+ printCommandPrompt ();
76
+ return (bool )ports.inPort ->available (); // return true if any bytes left to read
77
+ }
78
+ // write incoming data to the buffer
79
+ writeToBuffer (inByte);
80
+ // echo to ports if configured
81
+ echoPorts (inByte);
82
+ // call the handler if you fill the buffer, then return so everything is reset
83
+ if (bytesWritten == bufferSize-1 || !ports.inPort ->available ()) {
84
+
85
+ println (" Buffer ready, calling handler" );
86
+ commandState.bit .commandHandled = handleCustomCommand ();
87
+
88
+ println (" Clearing buffer" );
89
+ bufferString = " " ;// clear the buffer so we can fill it with any new chars
90
+ bytesWritten = 0 ;
91
+ resetBuffer ();
92
+ return (bool )ports.inPort ->available (); // return true if any bytes left to read
93
+ }
94
+ }
95
+ return (bool )ports.inPort ->available (); // return true if any bytes left to read
96
+ }
57
97
// returns true if any bytes left to read
58
98
if (commandState.bit .isCommandPending ){
59
99
// there is a command still in the buffer, process it now
@@ -62,44 +102,39 @@ bool Commander::update(){
62
102
if (ports.inPort ) return (bool )ports.inPort ->available (); // return true if any bytes left to read
63
103
return 0 ;
64
104
}
65
- // if( !commandState.bit.commanderStarted && ports.settings.bit.commandPromptEnabled) {
66
- // printCommandPrompt();
67
- // commandState.bit.commanderStarted = true;
68
- // }
105
+
69
106
commandState.bit .commandHandled = false ;
70
- if (ports.settings .bit .commandParserEnabled && ports. inPort ){
107
+ if (ports.settings .bit .commandParserEnabled ){
71
108
while (ports.inPort ->available ()){
72
- // ports.inPort->println("Reading Data");
73
109
int inByte = ports.inPort ->read ();
74
- if (ports.settings .bit .echoTerminal && !ports.settings .bit .locked ) ports.outPort ->write (inByte);
75
-
76
- if (ports.settings .bit .echoToAlt && ports.altPort && !ports.settings .bit .locked ) {
77
- ports.altPort ->write (inByte);
78
- }
79
-
110
+ echoPorts (inByte);
80
111
if (processBuffer (inByte)) break ; // break out of here - an end of line was found so unpack and handle the command
81
112
}
82
-
83
-
84
113
// copy any pending characters back from the alt ports.inPort
85
114
if (ports.settings .bit .echoToAlt && ports.altPort ) while (ports.altPort ->available ()) { ports.outPort ->write (ports.altPort ->read ()); }
86
115
87
116
// If a newline was detected, try and handle the command
88
117
if (commandState.bit .newLine == true ) commandState.bit .commandHandled = handleCommand (); // returns true if data was unpacked
89
-
90
-
91
118
}
119
+ else bridgePorts ();
120
+ if (ports.inPort ) return (bool )ports.inPort ->available (); // return true if any bytes left to read
121
+ return 0 ;
122
+ }
123
+
124
+ // Echo incoming to out and alt ports
125
+ void Commander::echoPorts (int portByte){
126
+ if (ports.settings .bit .echoTerminal && !ports.settings .bit .locked ) ports.outPort ->write (portByte);
127
+ if (ports.settings .bit .echoToAlt && ports.altPort && !ports.settings .bit .locked ) ports.altPort ->write (portByte);
128
+ }
92
129
93
- else if (!ports.settings .bit .commandParserEnabled && ports.settings .bit .echoToAlt && ports.inPort && ports.altPort ){
130
+ // copy data between ports
131
+ void Commander::bridgePorts (){
132
+ if (!ports.settings .bit .commandParserEnabled && ports.settings .bit .echoToAlt && ports.altPort ){
94
133
// pass data between ports
95
134
while (ports.altPort ->available ()) ports.outPort ->write (ports.altPort ->read ());
96
135
while (ports.inPort ->available ()) ports.altPort ->write (ports.inPort ->read ());
97
136
}
98
-
99
- if (ports.inPort ) return (bool )ports.inPort ->available (); // return true if any bytes left to read
100
- return 0 ;
101
137
}
102
-
103
138
// ==============================================================================================================
104
139
bool Commander::updatePending (){
105
140
if (commandState.bit .isCommandPending ){
@@ -479,8 +514,8 @@ bool Commander::handleCommand(){
479
514
unknownCommand ();
480
515
returnVal = 0 ; // unknown command function
481
516
}
482
- else if (commandVal == NUMERAL_COMMAND && ports.settings .bit .locked == false ){
483
- returnVal = handleIndexCommand ();
517
+ else if (commandVal == CUSTOM_COMMAND && ports.settings .bit .locked == false ){
518
+ returnVal = handleCustomCommand ();
484
519
if (returnVal == 1 ) unknownCommand ();
485
520
}
486
521
@@ -546,6 +581,7 @@ bool Commander::processBuffer(int dataByte){
546
581
if (commandState.bit .bufferFull ) resetBuffer ();// dump the buffer
547
582
if (commandState.bit .newLine == true ) {
548
583
commandState.bit .bufferState = BUFFER_PACKET_RECEIVED;
584
+ // if(bufferString.charAt(bytesWritten - 2) == '\r') bufferString.charAt(bytesWritten - 2) = '\n';
549
585
return true ; // return true because we have a newline
550
586
// DEBUG.println("Newline Detected");
551
587
} // unpack the data
@@ -566,7 +602,8 @@ void Commander::writeToBuffer(int dataByte){
566
602
return ;
567
603
}
568
604
// buf[bytesWritten] = dataByte;
569
- bufferString += (char )dataByte;
605
+ if (ports.settings .bit .stripCR && dataByte != ' \r ' ) bufferString += (char )dataByte; // ingore CR
606
+ else bufferString += (char )dataByte;
570
607
if (dataByte == endOfLine) commandState.bit .newLine = true ;
571
608
bytesWritten++;
572
609
}
@@ -623,7 +660,7 @@ int Commander::matchCommand(){
623
660
// Now check any default commands - these can be overridden if found in the users command list
624
661
// First see if it starts with an int - if so then use the number function
625
662
// Check if it is a number or minus sign
626
- if ( isNumber (bufferString) ) return NUMERAL_COMMAND ;
663
+ if ( isNumber (bufferString) ) return CUSTOM_COMMAND ;
627
664
for (uint16_t n = 0 ; n < INTERNAL_COMMAND_ITEMS; n++){
628
665
// String intCmdLine =
629
666
if (bufferString.startsWith ( internalCommands[n] )){
@@ -702,11 +739,11 @@ int Commander::handleInternalCommand(uint16_t internalCommandIndex){
702
739
}
703
740
// ==============================================================================================================
704
741
705
- bool Commander::handleIndexCommand (){
742
+ bool Commander::handleCustomCommand (){
706
743
// if the function pointer is NULL then return
707
744
// If not then call the function
708
- if (numeralHandler == NULL ) return 1 ;
709
- return numeralHandler (*this );
745
+ if (customHandler == NULL ) return 1 ;
746
+ return customHandler (*this );
710
747
}
711
748
// ==============================================================================================================
712
749
void Commander::unknownCommand (){
@@ -788,66 +825,80 @@ void Commander::printCommandList(){
788
825
// Prints all the commands
789
826
uint8_t n = 0 ;
790
827
// int length1 = 0;
791
- print (commanderName);
792
- println (F (" User Commands:" ));
793
828
String cmdLine = " " ;
829
+ cmdLine.concat (commanderName);
830
+ cmdLine.concat (F (" User Commands:" ));
831
+ // print(commanderName);
832
+ // println(F(" User Commands:"));
833
+ // String cmdLine = " ";
834
+ println (cmdLine);
794
835
for (n = 0 ; n < commandListEntries; n++){
795
836
796
- cmdLine += commandList[n].commandString ;
797
- cmdLine += ' ' ;
798
- write (' ' );
799
- print (commandList[n].commandString );
800
- write (' ' );
837
+ cmdLine = ' \t ' ;
838
+ cmdLine.concat (commandList[n].commandString );
839
+ // cmdLine += ' ';
840
+ cmdLine.concat (getWhiteSpace (longestCommand - commandLengths[n]));
841
+ cmdLine.concat (" | " );
842
+ cmdLine.concat (commandList[n].manualString );
843
+ // write('|');
844
+ // write(' ');
845
+ // println(commandList[n].manualString);
846
+ // cmdLine += '\t';
847
+ // write('\t');
848
+ // print(commandList[n].commandString);
849
+ // write(' ');
801
850
// length1 = commands[n].commandString.length();
802
851
// for(int i = 0; i < (32-commandList[n].commandString.length()); i++){
803
852
// add whitespace
804
853
// uint8_t whiteSpaces = longestCommand - commandLengths[n];
805
- printWhiteSpace (longestCommand - commandLengths[n]);
854
+ // print(getWhiteSpace(longestCommand - commandLengths[n]));
855
+ // printWhiteSpace(longestCommand - commandLengths[n]);
806
856
// for(int ws = 0; ws < whiteSpaces; ws++) write(' '); //Print blank spaces to format the line - CONVERT THIS TO A FUNCTION BECAUSE IT IS USED SEVERAL TIMES
807
857
// }
808
- write (' |' );
809
- write (' ' );
810
- println (commandList[n].manualString );
811
-
812
-
858
+ println (cmdLine);
813
859
}
814
860
// NOW print any alt commands altCommandListPrintEnable
815
- if (numeralHandler != NULL ){
816
- print ( " * " ) ;
817
-
818
- printWhiteSpace (longestCommand-1 );
861
+ if (customHandler != NULL ){
862
+ cmdLine = " \t * " ;
863
+ cmdLine. concat ( getWhiteSpace (longestCommand- 1 ));
864
+ // printWhiteSpace(longestCommand-1);
819
865
// for(int ws = 0; ws < longestCommand-1; ws++) write(' ');
820
- write (' |' );
821
- write (' ' );
822
- println (F (" Number Command" ));
866
+ cmdLine.concat (' |' );
867
+ cmdLine.concat (' ' );
868
+
869
+ cmdLine.concat ( F (" Custom Command" ));
870
+ println (cmdLine);
823
871
}
824
872
825
873
826
- println (F (" Internal Commands:" ));
874
+ println (F (" Internal Commands:" ));
827
875
for (n = 0 ; n < INTERNAL_COMMAND_ITEMS; n++){
828
- write (' ' );
876
+ write (' \t ' );
829
877
if (n > 3 ){
830
878
print (internalCommands[n]);
831
879
println (F (" (on/off)" ));
832
880
}
833
881
else println (internalCommands[n]);
834
882
}
835
- print (F (" Reload character: " ));
883
+ print (F (" Reload character: " ));
836
884
println (String (reloadCommandChar));
837
885
838
886
839
- print (F (" Comment character: " ));
887
+ print (F (" Comment character: " ));
840
888
println (String (commentChar));
841
889
842
890
// return 0;
843
891
}
844
892
845
893
// ==============================================================================================================
846
894
847
- void Commander::printWhiteSpace (uint8_t spaces){
895
+ String Commander::getWhiteSpace (uint8_t spaces){
896
+ String wSpace = " " ;
848
897
for (uint8_t n = 0 ; n < spaces; n++){
849
- write (' ' );
898
+ wSpace.concat (' ' );
899
+ // write(' ');
850
900
}
901
+ return wSpace;
851
902
}
852
903
// ==============================================================================================================
853
904
0 commit comments