Vrekrer SCPI parser  0.5
A simple SCPI parser for small Arduino projects.
Vrekrer_scpi_parser.h
Go to the documentation of this file.
1 
6 #ifndef VREKRER_SCPI_PARSER_H_
7 #define VREKRER_SCPI_PARSER_H_
8 
9 
11 #ifndef SCPI_ARRAY_SYZE
12  #define SCPI_ARRAY_SYZE 6
13 #endif
14 
16 #ifndef SCPI_MAX_TOKENS
17  #define SCPI_MAX_TOKENS 15
18 #endif
19 
21 #ifndef SCPI_MAX_COMMANDS
22  #define SCPI_MAX_COMMANDS 20
23 #endif
24 
26 #ifndef SCPI_BUFFER_LENGTH
27  #define SCPI_BUFFER_LENGTH 64
28 #endif
29 
31 #ifndef SCPI_TIMEOUT
32  #define SCPI_TIMEOUT 10
33 #endif
34 
36 #ifndef SCPI_HASH_TYPE
37  #define SCPI_HASH_TYPE uint8_t
38 #endif
39 
40 #include "Arduino.h"
41 
56  public:
57  char* operator[](const byte index); //Add indexing capability
58  void Append(char* value); //Append new string (LIFO stack Push)
59  char* Pop(); //LIFO stack Pop
60  char* First(); //Returns the first element of the array
61  char* Last(); //Returns the last element of the array
62  uint8_t Size(); //Array size
63  protected:
64  uint8_t size_ = 0; //Internal array size
65  char* values_[SCPI_ARRAY_SYZE]; //Storage of the strings
66 };
67 
73  public:
74  //Dummy constructor.
75  SCPI_Commands();
76  //Constructor that extracts and tokenize a command from a message
77  SCPI_Commands(char* message);
80 };
81 
87  public:
88  //Dummy constructor.
90  //Constructor that extracts and splits parameters from a message
91  SCPI_Parameters(char *message);
94 };
95 
98 
101 
103 typedef void (*SCPI_caller_t)(SCPI_Commands, SCPI_Parameters, Stream&);
104 
108 class SCPI_Parser {
109  public:
110  //Constructor
111  SCPI_Parser();
112  //Change the TreeBase for the next RegisterCommand calls
113  void SetCommandTreeBase(char* tree_base);
114  //SetCommandTreeBase version with RAM string support
115  void SetCommandTreeBase(const char* tree_base);
116  //SetCommandTreeBase version with Flash strings (F() macro) support
117  void SetCommandTreeBase(const __FlashStringHelper* tree_base);
118  //Registers a new valid command and associate a procedure to it
119  void RegisterCommand(char* command, SCPI_caller_t caller);
120  //RegisterCommand version with RAM string support.
121  void RegisterCommand(const char* command, SCPI_caller_t caller);
122  //RegisterCommand version with Flash strings (F() macro) support
123  void RegisterCommand(const __FlashStringHelper* command, SCPI_caller_t caller);
124  //Set the function to be used by the error handler.
125  void SetErrorHandler(SCPI_caller_t caller);
127  enum class ErrorCode{
129  NoError,
133  Timeout,
136  };
139  //Process a message and execute it a valid command is found
140  void Execute(char* message, Stream& interface);
141  //Gets a message from a Stream interface and execute it
142  void ProcessInput(Stream &interface, const char* term_chars);
143  //Gets a message from a Stream interface
144  char* GetMessage(Stream& interface, const char* term_chars);
145  //Prints registered tokens and command hashes to the serial interface
146  void PrintDebugInfo();
147 
148  protected:
149  //Add a token to the tokens' storage
150  void AddToken_(char* token);
151  //Get a hash from a command
153  //Number of stored tokens
154  uint8_t tokens_size_ = 0;
155  //Storage for tokens
156  char *tokens_[SCPI_MAX_TOKENS];
157  //Number of registered commands
158  uint8_t codes_size_ = 0;
159  //Registered commands' hash storage
160  SCPI_HASH_TYPE valid_codes_[SCPI_MAX_COMMANDS];
161  //Pointers to the functions to be called when a valid command is received
162  SCPI_caller_t callers_[SCPI_MAX_COMMANDS+1];
163  //Branch's hash used when calculating unique codes (0 for root)
164  SCPI_HASH_TYPE tree_code_ = 0;
165  //Message buffer.
166  char msg_buffer_[SCPI_BUFFER_LENGTH];
167  //Length of the readed message
168  uint8_t message_length_ = 0;
169  //Varible used for checking timeout errors
170  unsigned long time_checker_;
171 };
172 
173 #endif //VREKRER_SCPI_PARSER_H_
#define SCPI_HASH_TYPE
Integer size used for hashes.
Definition: Vrekrer_scpi_parser.h:37
SCPI_Commands SCPI_C
Alias of SCPI_Commands.
Definition: Vrekrer_scpi_parser.h:97
#define SCPI_MAX_TOKENS
Max number of valid tokens.
Definition: Vrekrer_scpi_parser.h:17
void(* SCPI_caller_t)(SCPI_Commands, SCPI_Parameters, Stream &)
Void template used with SCPI_Parser::RegisterCommand.
Definition: Vrekrer_scpi_parser.h:103
#define SCPI_ARRAY_SYZE
Max branch size of the command tree and max number of parameters.
Definition: Vrekrer_scpi_parser.h:12
#define SCPI_BUFFER_LENGTH
Length of the message buffer.
Definition: Vrekrer_scpi_parser.h:27
#define SCPI_MAX_COMMANDS
Max number of registered commands.
Definition: Vrekrer_scpi_parser.h:22
SCPI_Parameters SCPI_P
Alias of SCPI_Parameters.
Definition: Vrekrer_scpi_parser.h:100
String array class used to store the tokens of a command.
Definition: Vrekrer_scpi_parser.h:72
SCPI_Commands()
Dummy constructor.
Definition: Vrekrer_scpi_parser.cpp:55
char * not_processed_message
Not processed part of the message after the constructor is called.
Definition: Vrekrer_scpi_parser.h:79
String array class used to store the parameters found after a command.
Definition: Vrekrer_scpi_parser.h:86
char * not_processed_message
Not processed part of the message after the constructor is called.
Definition: Vrekrer_scpi_parser.h:93
SCPI_Parameters()
Dummy constructor.
Definition: Vrekrer_scpi_parser.cpp:90
Main class of the Vrekrer_SCPI_Parser library.
Definition: Vrekrer_scpi_parser.h:108
ErrorCode last_error
Variable that holds the last error code.
Definition: Vrekrer_scpi_parser.h:138
SCPI_HASH_TYPE GetCommandCode_(SCPI_Commands &commands)
Get a hash from a valid command.
Definition: Vrekrer_scpi_parser.cpp:167
void SetErrorHandler(SCPI_caller_t caller)
Set the function to be used by the error handler.
Definition: Vrekrer_scpi_parser.cpp:316
void RegisterCommand(char *command, SCPI_caller_t caller)
Registers a new valid command and associate a procedure to it.
Definition: Vrekrer_scpi_parser.cpp:277
SCPI_Parser()
SCPI_Parser constructor.
Definition: Vrekrer_scpi_parser.cpp:134
ErrorCode
SCPI Error codes.
Definition: Vrekrer_scpi_parser.h:127
@ BufferOverflow
Message buffer overflow.
@ Timeout
Timeout before receiving the termination chars.
@ UnknownCommand
Unknown command received.
void AddToken_(char *token)
Add a token to the tokens' storage.
Definition: Vrekrer_scpi_parser.cpp:139
void SetCommandTreeBase(char *tree_base)
Change the TreeBase for the next RegisterCommand calls.
Definition: Vrekrer_scpi_parser.cpp:237
void ProcessInput(Stream &interface, const char *term_chars)
Gets a message from a Stream interface and execute it.
Definition: Vrekrer_scpi_parser.cpp:353
char * GetMessage(Stream &interface, const char *term_chars)
Gets a message from a Stream interface.
Definition: Vrekrer_scpi_parser.cpp:373
void PrintDebugInfo()
Prints registered tokens and command hashes to the serial interface.
Definition: Vrekrer_scpi_parser.cpp:416
void Execute(char *message, Stream &interface)
Process a message and execute it a valid command is found.
Definition: Vrekrer_scpi_parser.cpp:332
Variable size string array class.
Definition: Vrekrer_scpi_parser.h:55
char * Last()
Returns the last element of the array.
Definition: Vrekrer_scpi_parser.cpp:38
uint8_t Size()
Array size.
Definition: Vrekrer_scpi_parser.cpp:47
void Append(char *value)
Append new string (LIFO stack Push).
Definition: Vrekrer_scpi_parser.cpp:11
char * Pop()
LIFO stack Pop.
Definition: Vrekrer_scpi_parser.cpp:19
char * First()
Returns the first element of the array.
Definition: Vrekrer_scpi_parser.cpp:29
char * operator[](const byte index)
Add indexing capability.
Definition: Vrekrer_scpi_parser.cpp:6