Skip to content

Commit 7b9e3a0

Browse files
committed
feat: Add type free pointer in SerialCommands to be used as context in commands callback
1 parent 502a891 commit 7b9e3a0

File tree

1 file changed

+97
-108
lines changed

1 file changed

+97
-108
lines changed

src/SerialCommands.h

Lines changed: 97 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -11,125 +11,114 @@ Repository : https://github.com/ppedro74/Arduino-SerialCommands
1111

1212
#include <Arduino.h>
1313

14-
typedef enum ternary
15-
{
16-
SERIAL_COMMANDS_SUCCESS = 0,
17-
SERIAL_COMMANDS_ERROR_NO_SERIAL,
18-
SERIAL_COMMANDS_ERROR_BUFFER_FULL
14+
typedef enum ternary {
15+
SERIAL_COMMANDS_SUCCESS = 0,
16+
SERIAL_COMMANDS_ERROR_NO_SERIAL,
17+
SERIAL_COMMANDS_ERROR_BUFFER_FULL
1918
} SERIAL_COMMANDS_ERRORS;
2019

2120
class SerialCommands;
2221

2322
typedef class SerialCommand SerialCommand;
24-
class SerialCommand
25-
{
23+
class SerialCommand {
2624
public:
27-
SerialCommand(const char* cmd, void(*func)(SerialCommands*), bool one_k=false)
28-
: command(cmd),
29-
function(func),
30-
next(NULL),
31-
one_key(one_k)
32-
{
33-
}
34-
35-
const char* command;
36-
void(*function)(SerialCommands*);
37-
SerialCommand* next;
38-
bool one_key;
25+
SerialCommand(const char *cmd, void (*func)(SerialCommands *),
26+
bool one_k = false)
27+
: command(cmd), function(func), next(NULL), one_key(one_k) {}
28+
29+
const char *command;
30+
void (*function)(SerialCommands *);
31+
SerialCommand *next;
32+
bool one_key;
3933
};
4034

41-
class SerialCommands
42-
{
35+
class SerialCommands {
4336
public:
44-
SerialCommands(Stream* serial, char* buffer, int16_t buffer_len, const char* term = "\r\n", const char* delim = " ") :
45-
serial_(serial),
46-
buffer_(buffer),
47-
buffer_len_(buffer!=NULL && buffer_len > 0 ? buffer_len - 1 : 0), //string termination char '\0'
48-
term_(term),
49-
delim_(delim),
50-
default_handler_(NULL),
51-
buffer_pos_(0),
52-
last_token_(NULL),
53-
term_pos_(0),
54-
commands_head_(NULL),
55-
commands_tail_(NULL),
56-
onek_cmds_head_(NULL),
57-
onek_cmds_tail_(NULL),
58-
commands_count_(0),
59-
onek_cmds_count_(0)
60-
{
61-
}
62-
63-
64-
/**
65-
* \brief Adds a command handler (Uses a linked list)
66-
* \param command
67-
*/
68-
void AddCommand(SerialCommand* command);
69-
70-
/**
71-
* \brief Checks the Serial port, reads the input buffer and calls a matching command handler.
72-
* \return SERIAL_COMMANDS_SUCCESS when successful or SERIAL_COMMANDS_ERROR_XXXX on error.
73-
*/
74-
SERIAL_COMMANDS_ERRORS ReadSerial();
75-
76-
/**
77-
* \brief Returns the source stream (Serial port)
78-
* \return
79-
*/
80-
Stream* GetSerial();
81-
82-
/**
83-
* \brief Attaches a Serial Port to this object
84-
* \param serial
85-
*/
86-
void AttachSerial(Stream* serial);
87-
88-
/**
89-
* \brief Detaches the serial port, if no serial port nothing will be done at ReadSerial
90-
*/
91-
void DetachSerial();
92-
93-
/**
94-
* \brief Sets a default handler can be used for a catch all or unrecognized commands
95-
* \param function
96-
*/
97-
void SetDefaultHandler(void(*function)(SerialCommands*, const char*));
98-
99-
/**
100-
* \brief Clears the buffer, and resets the indexes.
101-
*/
102-
void ClearBuffer();
103-
104-
/**
105-
* \brief Gets the next argument
106-
* \return returns NULL if no argument is available
107-
*/
108-
char* Next();
37+
SerialCommands(Stream *serial, char *buffer, int16_t buffer_len,
38+
const char *term = "\r\n", const char *delim = " ")
39+
: serial_(serial), buffer_(buffer),
40+
buffer_len_(buffer != NULL && buffer_len > 0
41+
? buffer_len - 1
42+
: 0), // string termination char '\0'
43+
term_(term), delim_(delim), default_handler_(NULL), buffer_pos_(0),
44+
last_token_(NULL), term_pos_(0), commands_head_(NULL),
45+
commands_tail_(NULL), onek_cmds_head_(NULL), onek_cmds_tail_(NULL),
46+
commands_count_(0), onek_cmds_count_(0) {}
47+
48+
/**
49+
* \brief Adds a command handler (Uses a linked list)
50+
* \param command
51+
*/
52+
void AddCommand(SerialCommand *command);
53+
54+
/**
55+
* \brief Checks the Serial port, reads the input buffer and calls a matching
56+
* command handler. \return SERIAL_COMMANDS_SUCCESS when successful or
57+
* SERIAL_COMMANDS_ERROR_XXXX on error.
58+
*/
59+
SERIAL_COMMANDS_ERRORS ReadSerial();
60+
61+
/**
62+
* \brief Returns the source stream (Serial port)
63+
* \return
64+
*/
65+
Stream *GetSerial();
66+
67+
/**
68+
* \brief Attaches a Serial Port to this object
69+
* \param serial
70+
*/
71+
void AttachSerial(Stream *serial);
72+
73+
/**
74+
* \brief Detaches the serial port, if no serial port nothing will be done at
75+
* ReadSerial
76+
*/
77+
void DetachSerial();
78+
79+
/**
80+
* \brief Sets a default handler can be used for a catch all or unrecognized
81+
* commands \param function
82+
*/
83+
void SetDefaultHandler(void (*function)(SerialCommands *, const char *));
84+
85+
/**
86+
* \brief Clears the buffer, and resets the indexes.
87+
*/
88+
void ClearBuffer();
89+
90+
/**
91+
* \brief Gets the next argument
92+
* \return returns NULL if no argument is available
93+
*/
94+
char *Next();
95+
96+
/** type-free pointer passed to commands callbacks */
97+
void *context;
10998

11099
private:
111-
Stream* serial_;
112-
char* buffer_;
113-
int16_t buffer_len_;
114-
const char* term_;
115-
const char* delim_;
116-
void(*default_handler_)(SerialCommands*, const char*);
117-
int16_t buffer_pos_;
118-
char* last_token_;
119-
int8_t term_pos_;
120-
SerialCommand* commands_head_;
121-
SerialCommand* commands_tail_;
122-
SerialCommand* onek_cmds_head_;
123-
SerialCommand* onek_cmds_tail_;
124-
uint8_t commands_count_;
125-
uint8_t onek_cmds_count_;
126-
127-
/**
128-
* \brief Tests for any one_key command and execute it if found
129-
* \return false if this was not a one_key command, true otherwise with
130-
* also clearing the buffer
131-
**/
132-
bool CheckOneKeyCmd();
100+
Stream *serial_;
101+
char *buffer_;
102+
int16_t buffer_len_;
103+
const char *term_;
104+
const char *delim_;
105+
void (*default_handler_)(SerialCommands *, const char *);
106+
int16_t buffer_pos_;
107+
char *last_token_;
108+
int8_t term_pos_;
109+
SerialCommand *commands_head_;
110+
SerialCommand *commands_tail_;
111+
SerialCommand *onek_cmds_head_;
112+
SerialCommand *onek_cmds_tail_;
113+
uint8_t commands_count_;
114+
uint8_t onek_cmds_count_;
115+
116+
/**
117+
* \brief Tests for any one_key command and execute it if found
118+
* \return false if this was not a one_key command, true otherwise with
119+
* also clearing the buffer
120+
**/
121+
bool CheckOneKeyCmd();
133122
};
134123

135124
#endif

0 commit comments

Comments
 (0)