Tarek Lule / CMD_Interpreter
Committer:
sepp_nepp
Date:
Fri Sep 06 11:09:23 2019 +0000
Revision:
1:f21d726a2b37
Parent:
0:b4341838304c
Release for the world

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sepp_nepp 0:b4341838304c 1 #include "CMD_Interpreter.h"
sepp_nepp 0:b4341838304c 2
sepp_nepp 0:b4341838304c 3 //************************************
sepp_nepp 0:b4341838304c 4 // Interpreter Class Creation
sepp_nepp 0:b4341838304c 5 //************************************
sepp_nepp 0:b4341838304c 6 // Strategy: the interpreter accumulates characters in its input buffer
sepp_nepp 0:b4341838304c 7 // it flags the presence of complete lines
sepp_nepp 0:b4341838304c 8 // it also flags Overflow of the buffer in which case all subsequent characters
sepp_nepp 0:b4341838304c 9 // are lost until the queue is emptied, and a CR-LF is received
sepp_nepp 0:b4341838304c 10 // Defined States of the interpreter:
sepp_nepp 0:b4341838304c 11 // MyState = isStartNew, isLineFilling, isOverflow, isWaitNewLine};
sepp_nepp 0:b4341838304c 12
sepp_nepp 0:b4341838304c 13 // #define DEBUG(...) { snprintf(buff, sizeof(buff), __VA_ARGS__); DataLog( buff ); }
sepp_nepp 0:b4341838304c 14
sepp_nepp 0:b4341838304c 15 Interpreter::Interpreter( void )
sepp_nepp 0:b4341838304c 16 { Reinit( );
sepp_nepp 0:b4341838304c 17 MyState = isStartNew;
sepp_nepp 0:b4341838304c 18 AllCommands = NULL;
sepp_nepp 0:b4341838304c 19 NumCommands = 0;
sepp_nepp 0:b4341838304c 20 }
sepp_nepp 0:b4341838304c 21
sepp_nepp 0:b4341838304c 22 void Interpreter::FillCommands( int aNumCommands, const EXE_CMD_TYPE *ACommands )
sepp_nepp 0:b4341838304c 23 { int cnt;
sepp_nepp 0:b4341838304c 24 AllCommands = new EXE_CMD_TYPE[ aNumCommands ];
sepp_nepp 0:b4341838304c 25 for( cnt=0; cnt<aNumCommands; cnt++)
sepp_nepp 0:b4341838304c 26 { AllCommands[cnt] = ACommands[cnt]; } ;
sepp_nepp 0:b4341838304c 27 NumCommands = aNumCommands;
sepp_nepp 0:b4341838304c 28 }
sepp_nepp 0:b4341838304c 29
sepp_nepp 0:b4341838304c 30 void Interpreter::Reinit( void ) {
sepp_nepp 0:b4341838304c 31 MyState = isWaitNewLine; // indicates no buffer overflow
sepp_nepp 0:b4341838304c 32 WriteIndex = 0; // points to next index to write to
sepp_nepp 0:b4341838304c 33 LinesComplete = 0; // indicates that no complete line is currently available
sepp_nepp 0:b4341838304c 34 // Start Scanning at the start
sepp_nepp 0:b4341838304c 35 ScanIndex = 0;
sepp_nepp 0:b4341838304c 36 BufAvail = BuffLen;// the full buffer size is available
sepp_nepp 0:b4341838304c 37 }
sepp_nepp 0:b4341838304c 38
sepp_nepp 0:b4341838304c 39 // Barebone function, assumes that checks havee been performed by writeBuf!
sepp_nepp 0:b4341838304c 40 void Interpreter::AddChar( char aChar ) {
sepp_nepp 0:b4341838304c 41 if (aChar == 8) // is it a backspace code, then remove one character:
sepp_nepp 0:b4341838304c 42 { if (BufAvail<BuffLen) { // there's actually a character to be removed
sepp_nepp 0:b4341838304c 43 BufAvail++; // Buffer regrows
sepp_nepp 0:b4341838304c 44 if (WriteIndex == 0) {WriteIndex=BuffLen-1;}
sepp_nepp 0:b4341838304c 45 else {WriteIndex--;} // recoil the write index
sepp_nepp 0:b4341838304c 46 }
sepp_nepp 0:b4341838304c 47 }
sepp_nepp 0:b4341838304c 48 else {
sepp_nepp 0:b4341838304c 49 if (WriteIndex == BuffLen-1) {WriteIndex=0;}
sepp_nepp 0:b4341838304c 50 else {WriteIndex++;}
sepp_nepp 0:b4341838304c 51 RingBuf[WriteIndex]=aChar; // all right, buffer it!
sepp_nepp 0:b4341838304c 52 BufAvail--; // Buffer is shrinking
sepp_nepp 0:b4341838304c 53 if (BufAvail==0) { MyState = isOverflow; }
sepp_nepp 0:b4341838304c 54 }
sepp_nepp 0:b4341838304c 55 }
sepp_nepp 0:b4341838304c 56
sepp_nepp 0:b4341838304c 57 // High level method to add a Char to the buffer,
sepp_nepp 0:b4341838304c 58 // Separates at Chars 10, 13, 0; replaced by a single 0 char
sepp_nepp 0:b4341838304c 59 // Blocking write when buffer has overflowed
sepp_nepp 0:b4341838304c 60 void Interpreter::writeBuf(char aChar) {
sepp_nepp 0:b4341838304c 61 bool LineEnds = aChar==10 || aChar==13 || aChar==0;
sepp_nepp 0:b4341838304c 62 switch (MyState) {
sepp_nepp 0:b4341838304c 63 case isOverflow: break;
sepp_nepp 0:b4341838304c 64 case isStartNew: // ready for the next line to start
sepp_nepp 0:b4341838304c 65 // avoid that consecutive CR LF are counted as multiple lines
sepp_nepp 0:b4341838304c 66 if (!LineEnds) {AddChar(aChar ); MyState = isLineFilling; }
sepp_nepp 0:b4341838304c 67 break;
sepp_nepp 0:b4341838304c 68 case isLineFilling:// New line has started filling
sepp_nepp 0:b4341838304c 69 if (!LineEnds) {AddChar(aChar);}
sepp_nepp 0:b4341838304c 70 else
sepp_nepp 0:b4341838304c 71 {MyState = isStartNew; // ready for the next line
sepp_nepp 0:b4341838304c 72 // Between consecutive commands, the endstring=NULL character is inserted
sepp_nepp 0:b4341838304c 73 // this is to indicate that line was already counted as completed
sepp_nepp 0:b4341838304c 74 AddChar(0 ); // append a line end char, will detect bufferFull!
sepp_nepp 0:b4341838304c 75 LinesComplete++; } // count completed lines
sepp_nepp 0:b4341838304c 76 break;
sepp_nepp 0:b4341838304c 77 case isWaitNewLine: // waiting for a new line end to arrive after an overflow
sepp_nepp 0:b4341838304c 78 if (LineEnds) { MyState = isStartNew; }
sepp_nepp 0:b4341838304c 79 break;
sepp_nepp 0:b4341838304c 80 default: MyState = isOverflow; // goes into error state, should never happen though
sepp_nepp 0:b4341838304c 81 }
sepp_nepp 0:b4341838304c 82 } // writeBuf
sepp_nepp 0:b4341838304c 83
sepp_nepp 0:b4341838304c 84 // Barebone function, that performs the checks to be performed!
sepp_nepp 0:b4341838304c 85 // Passes back the actC, and reads already the nextChar into actC
sepp_nepp 0:b4341838304c 86 char Interpreter::GetAChar( void ) {
sepp_nepp 0:b4341838304c 87 char oldC = actC;
sepp_nepp 0:b4341838304c 88 if (BufAvail==BuffLen) { actC = 0; LinesComplete=0; } // buffer is empty, no more lines!!
sepp_nepp 0:b4341838304c 89 else // something is in the buffer
sepp_nepp 0:b4341838304c 90 { if (ScanIndex == BuffLen) {ScanIndex=0;}
sepp_nepp 0:b4341838304c 91 else {ScanIndex++;}
sepp_nepp 0:b4341838304c 92 actC=RingBuf[ScanIndex]; // all right, get it
sepp_nepp 0:b4341838304c 93 if (actC==0) {LinesComplete--; } // now there is one line less in the storage
sepp_nepp 0:b4341838304c 94 BufAvail++; // Buffer is increasing
sepp_nepp 0:b4341838304c 95 } // something is in the buffer
sepp_nepp 0:b4341838304c 96 return oldC;
sepp_nepp 0:b4341838304c 97 }
sepp_nepp 0:b4341838304c 98
sepp_nepp 0:b4341838304c 99 // skip true blank, but also Tab characters
sepp_nepp 0:b4341838304c 100 void Interpreter::SkipBlanks( void ) {
sepp_nepp 0:b4341838304c 101 while(BufAvail<BuffLen && (actC==' ' || actC==9|| actC==0)) { GetAChar(); }
sepp_nepp 0:b4341838304c 102 } // SkipBlanks
sepp_nepp 0:b4341838304c 103
sepp_nepp 0:b4341838304c 104 int Interpreter::ReadAnInt( void ) {
sepp_nepp 0:b4341838304c 105 bool Negative = false;
sepp_nepp 0:b4341838304c 106 int Result = 0;
sepp_nepp 0:b4341838304c 107 if (actC=='-') {Negative = true; GetAChar(); }
sepp_nepp 0:b4341838304c 108 else if (actC=='+') {Negative = false; GetAChar(); }
sepp_nepp 0:b4341838304c 109 while(BufAvail<BuffLen && actC>='0' && actC<='9')
sepp_nepp 0:b4341838304c 110 { Result = Result * 10 + (GetAChar()-'0') ; }
sepp_nepp 0:b4341838304c 111 return Negative? -Result : Result;
sepp_nepp 0:b4341838304c 112 } // ReadAnInt
sepp_nepp 0:b4341838304c 113
sepp_nepp 0:b4341838304c 114
sepp_nepp 0:b4341838304c 115 RD_CMD_TYPE Interpreter::ParseCommand( void ) {
sepp_nepp 0:b4341838304c 116 RD_CMD_TYPE cmd; // locally built command
sepp_nepp 0:b4341838304c 117 actC=RingBuf[ScanIndex]; // initialiye the actC variable, as the first char to use
sepp_nepp 0:b4341838304c 118 SkipBlanks();
sepp_nepp 0:b4341838304c 119 // Next Character is the command
sepp_nepp 0:b4341838304c 120 cmd.Command = GetAChar();
sepp_nepp 0:b4341838304c 121 // Next Blanks are to be omitted, but are not even mandatory
sepp_nepp 0:b4341838304c 122 SkipBlanks();
sepp_nepp 0:b4341838304c 123 if ((actC>='0' && actC<='9') || actC=='-' || actC=='+' )
sepp_nepp 0:b4341838304c 124 { cmd.Parameter= ReadAnInt(); cmd.NumParam = 1; }
sepp_nepp 0:b4341838304c 125 else { cmd.Parameter= 0; cmd.NumParam = 0; }
sepp_nepp 0:b4341838304c 126 SkipBlanks( ); // There should be at least a trailing NUL character to be removed
sepp_nepp 0:b4341838304c 127 return cmd; // return the built command
sepp_nepp 0:b4341838304c 128 }
sepp_nepp 0:b4341838304c 129
sepp_nepp 0:b4341838304c 130 bool Interpreter::executeCommand(RD_CMD_TYPE cmd) {
sepp_nepp 0:b4341838304c 131 int CmdNr;
sepp_nepp 0:b4341838304c 132 bool Found;
sepp_nepp 0:b4341838304c 133 CmdNr = 0;
sepp_nepp 0:b4341838304c 134 Found = false;
sepp_nepp 0:b4341838304c 135 // DEBUG("Com '%c'=%d %d NPar=%d \n\r",cmd.Command, cmd.Command, cmd.Parameter, cmd.NumParam);
sepp_nepp 0:b4341838304c 136 // While not found go through all the commands, linear; they are not sorted by alphabet.
sepp_nepp 0:b4341838304c 137 while(CmdNr<NumCommands && !Found)
sepp_nepp 0:b4341838304c 138 { // DEBUG("NR:%d \n\r", CmdNr);
sepp_nepp 0:b4341838304c 139 if (AllCommands[CmdNr].cmd == cmd.Command)
sepp_nepp 0:b4341838304c 140 { // the command character matches
sepp_nepp 0:b4341838304c 141 if (cmd.NumParam == AllCommands[CmdNr].Npar)
sepp_nepp 0:b4341838304c 142 { // DEBUG("Execute: %s \n\r", AllCommands[CmdNr].help);
sepp_nepp 0:b4341838304c 143 AllCommands[CmdNr].Handler( cmd.Parameter ); // then call the handler
sepp_nepp 0:b4341838304c 144 }
sepp_nepp 0:b4341838304c 145 // else { DataLog("Param Mismatch!\n\r"); } // Inform about Parameter error
sepp_nepp 0:b4341838304c 146 Found = true; // and mark as found
sepp_nepp 0:b4341838304c 147 }
sepp_nepp 0:b4341838304c 148 else { CmdNr++; } // go on serarching
sepp_nepp 0:b4341838304c 149 } // while
sepp_nepp 0:b4341838304c 150 // if ( !Found ) { CmdInval(cmd.Command) ; } // call invalid command function
sepp_nepp 0:b4341838304c 151 return Found;
sepp_nepp 0:b4341838304c 152 }
sepp_nepp 0:b4341838304c 153
sepp_nepp 0:b4341838304c 154 bool Interpreter::ProcessPresentCommands( void )
sepp_nepp 0:b4341838304c 155 { RD_CMD_TYPE Readcmd;
sepp_nepp 0:b4341838304c 156 if (MyState==isOverflow) { Reinit(); }
sepp_nepp 0:b4341838304c 157 if(LinesComplete>0)
sepp_nepp 0:b4341838304c 158 { // DEBUG("X: Scan=%d ", Interp.ScanIndex);
sepp_nepp 0:b4341838304c 159 Readcmd = ParseCommand();
sepp_nepp 0:b4341838304c 160 executeCommand( Readcmd );
sepp_nepp 0:b4341838304c 161 //DEBUG("state=%d; wix=%d; rix=%d; buf=%s ", Interp.MyState, Interp.WriteIndex, Interp.ScanIndex, &Interp.RingBuf[Interp.ScanIndex]);
sepp_nepp 0:b4341838304c 162 return true;
sepp_nepp 0:b4341838304c 163 }
sepp_nepp 0:b4341838304c 164 else { return false; }
sepp_nepp 0:b4341838304c 165 }
sepp_nepp 0:b4341838304c 166
sepp_nepp 0:b4341838304c 167 char *ParamHelp[] = {" ", "<param>", "<par><par>"};
sepp_nepp 0:b4341838304c 168
sepp_nepp 0:b4341838304c 169 void Interpreter::PrtCmdHelp (Serial *aSerial ) // Display list of Commands
sepp_nepp 0:b4341838304c 170 { EXE_CMD_TYPE *com;
sepp_nepp 0:b4341838304c 171 int CmdNr;
sepp_nepp 0:b4341838304c 172 char *parStr;
sepp_nepp 0:b4341838304c 173 // DataLog("List of commands: \n\r");
sepp_nepp 0:b4341838304c 174 // Go Through all commands in the table
sepp_nepp 0:b4341838304c 175 for (CmdNr = 0; CmdNr<NumCommands; CmdNr++ )
sepp_nepp 0:b4341838304c 176 { com = &AllCommands[CmdNr]; // get a pointer to the command
sepp_nepp 0:b4341838304c 177 parStr = ParamHelp[com->Npar]; // Get the string that represents the number of parameters
sepp_nepp 0:b4341838304c 178 aSerial->printf( " %c %s => %s \n\r", com->cmd, parStr, com->help.c_str() ); // output the full command help
sepp_nepp 0:b4341838304c 179 }
sepp_nepp 0:b4341838304c 180 }
sepp_nepp 0:b4341838304c 181
sepp_nepp 0:b4341838304c 182 /*
sepp_nepp 0:b4341838304c 183 void Interpreter::CmdInval( int param ) // Feedback that the command is invalid
sepp_nepp 0:b4341838304c 184 { DEBUG( " Invalid command %d = '%c'; use: 'h' for help. \n\r", param, char(param) ) ;
sepp_nepp 0:b4341838304c 185 }
sepp_nepp 0:b4341838304c 186
sepp_nepp 0:b4341838304c 187 string Interpreter::GetLastMessage( void )
sepp_nepp 0:b4341838304c 188 { string LastMsg = LastMessage;
sepp_nepp 0:b4341838304c 189 LastMessage = ""; // Empty the old Message
sepp_nepp 0:b4341838304c 190 return LastMsg;
sepp_nepp 0:b4341838304c 191 }
sepp_nepp 0:b4341838304c 192
sepp_nepp 0:b4341838304c 193 void Interpreter::DataLog( char *DebugMessage )
sepp_nepp 0:b4341838304c 194 { if (LastMessage=="" ) { LastMessage = DebugMessage; }
sepp_nepp 0:b4341838304c 195 else { LastMessage = LastMessage + "\n\r" + DebugMessage; }
sepp_nepp 0:b4341838304c 196 };
sepp_nepp 0:b4341838304c 197 */