Skip to content

Commit 3cce8c7

Browse files
committed
Debug functionality.
1 parent db4027c commit 3cce8c7

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

src/commander.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void Commander::attachTreeFunction( API_t *API_tree_p, uint32_t API_tree_size_p
4141
API_tree = API_tree_p;
4242
API_tree_size = API_tree_size_p;
4343

44+
dbgResponse -> printf( (const char*)"API tree attached with %d commands.\r\n", API_tree_size );
45+
4446
}
4547

4648
void Commander::init(){
@@ -52,7 +54,10 @@ void Commander::init(){
5254
// Temporary variable, used to flip elements.
5355
API_t temp;
5456

57+
dbgResponse -> printf( (const char*)"Commander init start\r\n" );
58+
5559
// Make the tree ordered by alphabet.
60+
dbgResponse -> printf( (const char*)"\tCreating alphabetical order... " );
5661
for( i = 0; i < API_tree_size; i++ ){
5762

5863
for( j = i + 1; j < API_tree_size; j++ ){
@@ -76,11 +81,16 @@ void Commander::init(){
7681
API_tree[ i ].place = i;
7782

7883
}
84+
dbgResponse -> printf( (const char*)"[ OK ]\r\n" );
7985

8086
// Optimize the tree to make it balanced.
8187
// It is necessary to speed up the command
8288
// search phase.
89+
dbgResponse -> printf( (const char*)"\tCreate balanced binary structure... " );
8390
optimize_api_tree();
91+
dbgResponse -> printf( (const char*)"[ OK ]\r\n" );
92+
93+
dbgResponse -> printf( (const char*)"Commander init finished!\r\n" );
8494

8595
}
8696

@@ -365,6 +375,18 @@ void Commander::execute( const char *cmd, Serial *resp ){
365375

366376
}
367377

378+
void Commander::attachDebugChannel( Serial *resp ){
379+
380+
dbgResponse = &serialDebugResponse;
381+
382+
// Select the right Serial object in the response class.
383+
serialDebugResponse.select( resp );
384+
385+
// Enable debug messages.
386+
debugEnabled = true;
387+
388+
}
389+
368390
#endif
369391

370392
#ifdef COMMANDER_USE_ARDUINO_SERIAL_RESPONSE
@@ -395,6 +417,18 @@ void Commander::execute( const char *cmd, HardwareSerial *resp ){
395417

396418
}
397419

420+
void Commander::attachDebugChannel( HardwareSerial *resp ){
421+
422+
dbgResponse = &arduinoSerialDebugResponse;
423+
424+
// Select the right HardwareSerial object in the response class.
425+
arduinoSerialDebugResponse.select( resp );
426+
427+
// Enable debug messages.
428+
debugEnabled = true;
429+
430+
}
431+
398432
#endif
399433

400434
#ifdef COMMANDER_USE_WIFI_CLIENT_RESPONSE
@@ -425,8 +459,32 @@ void Commander::execute( const char *cmd, WiFiClient *resp ){
425459

426460
}
427461

462+
void Commander::attachDebugChannel( WiFiClient *resp ){
463+
464+
dbgResponse = &WiFiClientDebugResponse;
465+
466+
// Select the right HardwareSerial object in the response class.
467+
WiFiClientDebugResponse.select( resp );
468+
469+
// Enable debug messages.
470+
debugEnabled = true;
471+
472+
}
473+
428474
#endif
429475

476+
void Commander::enableDebug(){
477+
478+
debugEnabled = true;
479+
480+
}
481+
482+
void Commander::disableDebug(){
483+
484+
debugEnabled = false;
485+
486+
}
487+
430488
Commander::API_t* Commander::operator [] ( int i ){
431489

432490
// Detect wrong addressing.

src/commander.hpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ class Commander{
146146
/// be visible.
147147
void execute( const char *cmd );
148148

149-
150149
#ifdef COMMANDER_USE_SERIAL_RESPONSE
151150
/// Execution function for Serial response.
152151
///
@@ -165,6 +164,13 @@ class Commander{
165164
/// will be passed to the selected Serial
166165
/// object.
167166
void execute( const char *cmd, Serial *resp );
167+
168+
/// Debug channel for Serial.
169+
///
170+
/// This function attaches a Serial channel
171+
/// for debug messages. It also enables
172+
/// the debug functionality.
173+
void attachDebugChannel( Serial *resp );
168174
#endif
169175

170176
#ifdef COMMANDER_USE_ARDUINO_SERIAL_RESPONSE
@@ -185,6 +191,13 @@ class Commander{
185191
/// will be passed to the selected Serial
186192
/// object.
187193
void execute( const char *cmd, HardwareSerial *resp );
194+
195+
/// Debug channel for Arduino Serial.
196+
///
197+
/// This function attaches a HardwareSerial channel
198+
/// for debug messages. It also enables
199+
/// the debug functionality.
200+
void attachDebugChannel( HardwareSerial *resp );
188201
#endif
189202

190203
#ifdef COMMANDER_USE_WIFI_CLIENT_RESPONSE
@@ -195,6 +208,7 @@ class Commander{
195208
/// the messages from the command handler
196209
/// will be passed to the selected Serial
197210
/// object.
211+
198212
void execute( char *cmd, WiFiClient *resp );
199213
/// Execution function for WiFi Client response.
200214
///
@@ -204,8 +218,21 @@ class Commander{
204218
/// will be passed to the selected Serial
205219
/// object.
206220
void execute( const char *cmd, WiFiClient *resp );
221+
222+
/// Debug channel for WiFiClient.
223+
///
224+
/// This function attaches a WiFiClient channel
225+
/// for debug messages. It also enables
226+
/// the debug functionality.
227+
void attachDebugChannel( WiFiClient *resp );
207228
#endif
208229

230+
/// Enables debug messages.
231+
void enableDebug();
232+
233+
/// Disables debug messages.
234+
void disableDebug();
235+
209236
private:
210237

211238
/// Starting address of the API-tree.
@@ -245,6 +272,31 @@ class Commander{
245272
/// points to the default response handler.
246273
commandResponse *response = &defaultResponse;
247274

275+
/// Flag to enable or disable debug messages.
276+
bool debugEnabled = false;
277+
278+
/// Default response handler for debug messages.
279+
commandResponse defaultDebugResponse;
280+
281+
#ifdef COMMANDER_USE_SERIAL_RESPONSE
282+
/// Serial response handler class.
283+
commandResponseSerial serialDebugResponse;
284+
#endif
285+
286+
#ifdef COMMANDER_USE_ARDUINO_SERIAL_RESPONSE
287+
/// Serial response handler class.
288+
commandResponseArduinoSerial arduinoSerialDebugResponse;
289+
#endif
290+
291+
#ifdef COMMANDER_USE_WIFI_CLIENT_RESPONSE
292+
/// WiFi Client response handler class.
293+
commandResponseWiFiClient WiFiClientDebugResponse;
294+
#endif
295+
296+
/// Pointer to response class. By default it
297+
/// points to the default debug response handler.
298+
commandResponse *dbgResponse = &defaultDebugResponse;
299+
248300
/// Find an API element in the tree by alphabetical place.
249301
uint16_t find_api_index_by_place( uint16_t place );
250302

0 commit comments

Comments
 (0)