21
21
22
22
#include " Arduino_DebugUtils.h"
23
23
24
+ /* *****************************************************************************
25
+ INPUT BUFFER
26
+ ******************************************************************************/
27
+
28
+ #define COMMAND_BUFFER_SIZE 50 // Define a reasonable size for the input buffer
29
+ char commandBuffer[COMMAND_BUFFER_SIZE];
30
+
24
31
/* *****************************************************************************
25
32
CONSTANTS
26
33
******************************************************************************/
@@ -54,7 +61,7 @@ int Arduino_DebugUtils::getDebugLevel() const {
54
61
}
55
62
56
63
void Arduino_DebugUtils::setDebugOutputStream (Stream * stream) {
57
- _debug_output_stream = stream;
64
+ _debug_io_stream = stream;
58
65
}
59
66
60
67
void Arduino_DebugUtils::newlineOn () {
@@ -125,6 +132,65 @@ void Arduino_DebugUtils::print(int const debug_level, const __FlashStringHelper
125
132
va_end (args);
126
133
}
127
134
135
+ void Arduino_DebugUtils::processDebugUpdateLevelCommand ()
136
+ {
137
+ static size_t bufferIndex = 0 ; // Index to track buffer position
138
+
139
+ // Check if the stream is available and has data
140
+ if (_debug_io_stream && _debug_io_stream->available ()) {
141
+ // Read each character from the stream
142
+ char incomingChar = _debug_io_stream->read ();
143
+
144
+ // If it's a newline character, process the command
145
+ if (incomingChar == ' \n ' ) {
146
+ commandBuffer[bufferIndex] = ' \0 ' ; // Null-terminate the string
147
+
148
+ // Compare C-strings for each command
149
+ if (strcmp (commandBuffer, " V" ) == 0 || strcmp (commandBuffer, " VERBOSE" ) == 0 )
150
+ {
151
+ setDebugLevel (DBG_VERBOSE);
152
+ _debug_io_stream->println (" Debug level set to VERBOSE." );
153
+ }
154
+ else if (strcmp (commandBuffer, " D" ) == 0 || strcmp (commandBuffer, " DEBUG" ) == 0 )
155
+ {
156
+ setDebugLevel (DBG_INFO);
157
+ _debug_io_stream->println (" Debug level set to DEBUG." );
158
+ }
159
+ else if (strcmp (commandBuffer, " I" ) == 0 || strcmp (commandBuffer, " INFO" ) == 0 )
160
+ {
161
+ setDebugLevel (DBG_INFO);
162
+ _debug_io_stream->println (" Debug level set to INFO." );
163
+ }
164
+ else if (strcmp (commandBuffer, " W" ) == 0 || strcmp (commandBuffer, " WARNING" ) == 0 )
165
+ {
166
+ setDebugLevel (DBG_WARNING);
167
+ _debug_io_stream->println (" Debug level set to WARNING." );
168
+ }
169
+ else if (strcmp (commandBuffer, " E" ) == 0 || strcmp (commandBuffer, " ERROR" ) == 0 )
170
+ {
171
+ setDebugLevel (DBG_ERROR);
172
+ _debug_io_stream->println (" Debug level set to ERROR." );
173
+ }
174
+ else
175
+ {
176
+ _debug_io_stream->println (" Invalid command. Use V,D,I,W,E or VERBOSE, DEBUG, INFO, WARNING, or ERROR." );
177
+ }
178
+
179
+ // Clear the buffer for the next command
180
+ bufferIndex = 0 ;
181
+ commandBuffer[0 ] = ' \0 ' ;
182
+ }
183
+ else if (incomingChar != ' \r ' )
184
+ {
185
+ // Add the character to the buffer if it's not a carriage return
186
+ if (bufferIndex < COMMAND_BUFFER_SIZE - 1 )
187
+ {
188
+ commandBuffer[bufferIndex++] = incomingChar;
189
+ }
190
+ }
191
+ }
192
+ }
193
+
128
194
/* *****************************************************************************
129
195
PRIVATE MEMBER FUNCTIONS
130
196
******************************************************************************/
@@ -147,9 +213,9 @@ void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) {
147
213
va_end (args_copy);
148
214
149
215
if (_newline_on) {
150
- _debug_output_stream ->println (msg_buf);
216
+ _debug_io_stream ->println (msg_buf);
151
217
} else {
152
- _debug_output_stream ->print (msg_buf);
218
+ _debug_io_stream ->print (msg_buf);
153
219
}
154
220
155
221
#if __STDC_NO_VLA__ == 1
@@ -193,7 +259,7 @@ void Arduino_DebugUtils::printTimestamp()
193
259
snprintf (timestamp, sizeof (timestamp), " [ %lu ] " , millis ());
194
260
}
195
261
196
- _debug_output_stream ->print (timestamp);
262
+ _debug_io_stream ->print (timestamp);
197
263
}
198
264
199
265
void Arduino_DebugUtils::printDebugLabel (int const debug_level)
@@ -211,7 +277,7 @@ void Arduino_DebugUtils::printDebugLabel(int const debug_level)
211
277
if (!is_valid_debug_level)
212
278
return ;
213
279
214
- _debug_output_stream ->print (DEBUG_MODE_STRING[debug_level]);
280
+ _debug_io_stream ->print (DEBUG_MODE_STRING[debug_level]);
215
281
}
216
282
217
283
bool Arduino_DebugUtils::shouldPrint (int const debug_level) const
0 commit comments