Interprets Commands received char by char.
CMD_Interpreter.h@2:5934744ac614, 2019-10-31 (annotated)
- Committer:
- sepp_nepp
- Date:
- Thu Oct 31 10:47:57 2019 +0000
- Revision:
- 2:5934744ac614
- Parent:
- 0:b4341838304c
Debugging done.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sepp_nepp | 0:b4341838304c | 1 | |
sepp_nepp | 0:b4341838304c | 2 | #ifndef CMD_Interpreter_H |
sepp_nepp | 0:b4341838304c | 3 | #define CMD_Interpreter_H |
sepp_nepp | 0:b4341838304c | 4 | |
sepp_nepp | 0:b4341838304c | 5 | #include "mbed.h" |
sepp_nepp | 0:b4341838304c | 6 | #include <string> |
sepp_nepp | 0:b4341838304c | 7 | |
sepp_nepp | 0:b4341838304c | 8 | const int BuffLen = 512; |
sepp_nepp | 0:b4341838304c | 9 | |
sepp_nepp | 0:b4341838304c | 10 | // define commands to be handled! |
sepp_nepp | 0:b4341838304c | 11 | typedef void (*caseHandler)(int param); |
sepp_nepp | 0:b4341838304c | 12 | |
sepp_nepp | 0:b4341838304c | 13 | typedef struct EXE_CMD_TYPE{ |
sepp_nepp | 0:b4341838304c | 14 | char cmd; // THe single command character |
sepp_nepp | 0:b4341838304c | 15 | char Npar; // The number of parameters needed by the command, only for help text. All command Hanlders must have a Parameter |
sepp_nepp | 0:b4341838304c | 16 | caseHandler Handler; // Command Handlers |
sepp_nepp | 0:b4341838304c | 17 | string help; // Help Text for that command |
sepp_nepp | 0:b4341838304c | 18 | }EXE_CMD_TYPE; |
sepp_nepp | 0:b4341838304c | 19 | |
sepp_nepp | 0:b4341838304c | 20 | // Define States of the interpreter |
sepp_nepp | 0:b4341838304c | 21 | enum InterpretState { isStartNew, isLineFilling, isOverflow, isWaitNewLine}; |
sepp_nepp | 0:b4341838304c | 22 | |
sepp_nepp | 0:b4341838304c | 23 | typedef struct RD_CMD_TYPE { |
sepp_nepp | 0:b4341838304c | 24 | char Command; // Holds the next read command character |
sepp_nepp | 0:b4341838304c | 25 | int Parameter ; // Holds one int Parameter |
sepp_nepp | 0:b4341838304c | 26 | int NumParam ; // Indicates how many parameters where found |
sepp_nepp | 0:b4341838304c | 27 | } RD_CMD_TYPE; |
sepp_nepp | 0:b4341838304c | 28 | |
sepp_nepp | 0:b4341838304c | 29 | // Uses a Ring buffer to buffer incomiing characters |
sepp_nepp | 0:b4341838304c | 30 | // Uses a parsing function to extract lines containing a command each |
sepp_nepp | 0:b4341838304c | 31 | class Interpreter{ |
sepp_nepp | 0:b4341838304c | 32 | public: |
sepp_nepp | 0:b4341838304c | 33 | // Interpreter Class Creation |
sepp_nepp | 0:b4341838304c | 34 | Interpreter( void ); |
sepp_nepp | 0:b4341838304c | 35 | void Reinit( void ); |
sepp_nepp | 0:b4341838304c | 36 | void FillCommands(int aNumCommands, const EXE_CMD_TYPE *ACommands ); |
sepp_nepp | 0:b4341838304c | 37 | public: // the writing mechanics |
sepp_nepp | 0:b4341838304c | 38 | void AddChar ( char aChar ); // Barebone function, assumes that checks havee been performed by writeBuf! |
sepp_nepp | 0:b4341838304c | 39 | void writeBuf( char c ) ; // High level method to add a Char to the buffer, |
sepp_nepp | 0:b4341838304c | 40 | char RingBuf[BuffLen]; |
sepp_nepp | 0:b4341838304c | 41 | InterpretState MyState;// Indicates if buffer overflow, line ended etc. |
sepp_nepp | 0:b4341838304c | 42 | int WriteIndex ; // points to next index to write to |
sepp_nepp | 0:b4341838304c | 43 | int BufAvail ; // indicates how much of the buffer is still available |
sepp_nepp | 0:b4341838304c | 44 | int LinesComplete; // Indicates how many complete lines are available |
sepp_nepp | 0:b4341838304c | 45 | bool ProcessPresentCommands( void ); |
sepp_nepp | 0:b4341838304c | 46 | RD_CMD_TYPE ParseCommand( void ); |
sepp_nepp | 0:b4341838304c | 47 | bool executeCommand(RD_CMD_TYPE cmd); |
sepp_nepp | 2:5934744ac614 | 48 | bool InvalidCommand; |
sepp_nepp | 0:b4341838304c | 49 | void PrtCmdHelp ( Serial *aSerial ); // Display list of Commands |
sepp_nepp | 0:b4341838304c | 50 | // void GetLastMessage( string LastMsg); // not possible to implement at the moment |
sepp_nepp | 0:b4341838304c | 51 | public: // the reading mechanics |
sepp_nepp | 0:b4341838304c | 52 | int ScanIndex; // points to next index to read from to |
sepp_nepp | 0:b4341838304c | 53 | char actC ; // holds the actual character |
sepp_nepp | 0:b4341838304c | 54 | private: |
sepp_nepp | 0:b4341838304c | 55 | char GetAChar ( void ); |
sepp_nepp | 0:b4341838304c | 56 | void SkipBlanks( void ); |
sepp_nepp | 0:b4341838304c | 57 | int ReadAnInt ( void ); |
sepp_nepp | 0:b4341838304c | 58 | char actPP ( void ); |
sepp_nepp | 0:b4341838304c | 59 | EXE_CMD_TYPE* AllCommands; // Defines an array of all commands to be filled by the user. |
sepp_nepp | 0:b4341838304c | 60 | int NumCommands; // Holds the actual number of commands that are stored in the array |
sepp_nepp | 0:b4341838304c | 61 | /* |
sepp_nepp | 0:b4341838304c | 62 | char buff[100]; // Intermediate buffer to hold messages for datalogging |
sepp_nepp | 0:b4341838304c | 63 | string LastMessage; |
sepp_nepp | 0:b4341838304c | 64 | void DataLog( char *DebugMessage ); |
sepp_nepp | 0:b4341838304c | 65 | void CmdInval( int param ); // Feedback that the command is invalid |
sepp_nepp | 0:b4341838304c | 66 | */ |
sepp_nepp | 0:b4341838304c | 67 | }; // class Interpreter |
sepp_nepp | 0:b4341838304c | 68 | |
sepp_nepp | 0:b4341838304c | 69 | #endif // CMD_Interpreter_H |