Skip to content

Commit 4c4ef97

Browse files
Update with new features
Updated to 1.2.0 with new features for file systems and a 'prefab' command structure for SD file navigation. also a few bug fixes and tweaks.
1 parent ffdabcb commit 4c4ef97

File tree

7 files changed

+479
-77
lines changed

7 files changed

+479
-77
lines changed

Commander.cpp

Lines changed: 105 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,46 @@ void Commander::begin(Stream *sPort, Stream *oPort, const commandList_t *command
5454
//==============================================================================================================
5555

5656
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+
}
5797
//returns true if any bytes left to read
5898
if(commandState.bit.isCommandPending){
5999
//there is a command still in the buffer, process it now
@@ -62,44 +102,39 @@ bool Commander::update(){
62102
if(ports.inPort) return (bool)ports.inPort->available(); //return true if any bytes left to read
63103
return 0;
64104
}
65-
//if( !commandState.bit.commanderStarted && ports.settings.bit.commandPromptEnabled) {
66-
//printCommandPrompt();
67-
//commandState.bit.commanderStarted = true;
68-
//}
105+
69106
commandState.bit.commandHandled = false;
70-
if(ports.settings.bit.commandParserEnabled && ports.inPort){
107+
if(ports.settings.bit.commandParserEnabled){
71108
while(ports.inPort->available()){
72-
//ports.inPort->println("Reading Data");
73109
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);
80111
if(processBuffer(inByte)) break; //break out of here - an end of line was found so unpack and handle the command
81112
}
82-
83-
84113
//copy any pending characters back from the alt ports.inPort
85114
if(ports.settings.bit.echoToAlt && ports.altPort) while(ports.altPort->available()) { ports.outPort->write(ports.altPort->read()); }
86115

87116
//If a newline was detected, try and handle the command
88117
if(commandState.bit.newLine == true) commandState.bit.commandHandled = handleCommand(); //returns true if data was unpacked
89-
90-
91118
}
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+
}
92129

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){
94133
//pass data between ports
95134
while(ports.altPort->available()) ports.outPort->write(ports.altPort->read());
96135
while(ports.inPort->available()) ports.altPort->write(ports.inPort->read());
97136
}
98-
99-
if(ports.inPort) return (bool)ports.inPort->available(); //return true if any bytes left to read
100-
return 0;
101137
}
102-
103138
//==============================================================================================================
104139
bool Commander::updatePending(){
105140
if(commandState.bit.isCommandPending){
@@ -479,8 +514,8 @@ bool Commander::handleCommand(){
479514
unknownCommand();
480515
returnVal = 0; //unknown command function
481516
}
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();
484519
if(returnVal == 1) unknownCommand();
485520
}
486521

@@ -546,6 +581,7 @@ bool Commander::processBuffer(int dataByte){
546581
if(commandState.bit.bufferFull) resetBuffer();//dump the buffer
547582
if(commandState.bit.newLine == true) {
548583
commandState.bit.bufferState = BUFFER_PACKET_RECEIVED;
584+
//if(bufferString.charAt(bytesWritten - 2) == '\r') bufferString.charAt(bytesWritten - 2) = '\n';
549585
return true; //return true because we have a newline
550586
//DEBUG.println("Newline Detected");
551587
} //unpack the data
@@ -566,7 +602,8 @@ void Commander::writeToBuffer(int dataByte){
566602
return;
567603
}
568604
//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;
570607
if(dataByte == endOfLine) commandState.bit.newLine = true;
571608
bytesWritten++;
572609
}
@@ -623,7 +660,7 @@ int Commander::matchCommand(){
623660
//Now check any default commands - these can be overridden if found in the users command list
624661
//First see if it starts with an int - if so then use the number function
625662
//Check if it is a number or minus sign
626-
if( isNumber(bufferString) ) return NUMERAL_COMMAND;
663+
if( isNumber(bufferString) ) return CUSTOM_COMMAND;
627664
for(uint16_t n = 0; n < INTERNAL_COMMAND_ITEMS; n++){
628665
//String intCmdLine =
629666
if(bufferString.startsWith( internalCommands[n] )){
@@ -702,11 +739,11 @@ int Commander::handleInternalCommand(uint16_t internalCommandIndex){
702739
}
703740
//==============================================================================================================
704741

705-
bool Commander::handleIndexCommand(){
742+
bool Commander::handleCustomCommand(){
706743
//if the function pointer is NULL then return
707744
//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);
710747
}
711748
//==============================================================================================================
712749
void Commander::unknownCommand(){
@@ -788,66 +825,80 @@ void Commander::printCommandList(){
788825
//Prints all the commands
789826
uint8_t n = 0;
790827
//int length1 = 0;
791-
print(commanderName);
792-
println(F(" User Commands:"));
793828
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);
794835
for(n = 0; n < commandListEntries; n++){
795836

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(' ');
801850
//length1 = commands[n].commandString.length();
802851
//for(int i = 0; i < (32-commandList[n].commandString.length()); i++){
803852
//add whitespace
804853
//uint8_t whiteSpaces = longestCommand - commandLengths[n];
805-
printWhiteSpace(longestCommand - commandLengths[n]);
854+
//print(getWhiteSpace(longestCommand - commandLengths[n]));
855+
//printWhiteSpace(longestCommand - commandLengths[n]);
806856
//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
807857
//}
808-
write('|');
809-
write(' ');
810-
println(commandList[n].manualString);
811-
812-
858+
println(cmdLine);
813859
}
814860
//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);
819865
//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);
823871
}
824872

825873

826-
println(F("Internal Commands:"));
874+
println(F(" Internal Commands:"));
827875
for(n = 0; n < INTERNAL_COMMAND_ITEMS; n++){
828-
write(' ');
876+
write('\t');
829877
if(n > 3){
830878
print(internalCommands[n]);
831879
println(F(" (on/off)"));
832880
}
833881
else println(internalCommands[n]);
834882
}
835-
print(F("Reload character: "));
883+
print(F(" Reload character: "));
836884
println(String(reloadCommandChar));
837885

838886

839-
print(F("Comment character: "));
887+
print(F(" Comment character: "));
840888
println(String(commentChar));
841889

842890
//return 0;
843891
}
844892

845893
//==============================================================================================================
846894

847-
void Commander::printWhiteSpace(uint8_t spaces){
895+
String Commander::getWhiteSpace(uint8_t spaces){
896+
String wSpace = " ";
848897
for(uint8_t n = 0; n < spaces; n++){
849-
write(' ');
898+
wSpace.concat(' ');
899+
//write(' ');
850900
}
901+
return wSpace;
851902
}
852903
//==============================================================================================================
853904

0 commit comments

Comments
 (0)