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