Skip to content

Commit b6d82ad

Browse files
committed
Stream Class implemented.
1 parent b78b6ba commit b6d82ad

File tree

5 files changed

+60
-1931
lines changed

5 files changed

+60
-1931
lines changed

src/Commander-API.cpp

Lines changed: 31 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ void Commander::attachTreeFunction( API_t *API_tree_p, uint32_t API_tree_size_p
4242
API_tree = API_tree_p;
4343
API_tree_size = API_tree_size_p;
4444

45-
dbgResponse -> printf( (const char*)"API tree attached with %d commands.\r\n", API_tree_size );
45+
dbgResponse -> print( (const char*)"API tree attached with " );
46+
dbgResponse -> print( API_tree_size );
47+
dbgResponse -> println( " commands." );
4648

4749
}
4850

@@ -55,10 +57,10 @@ void Commander::init(){
5557
// Temporary variable, used to flip elements.
5658
API_t temp;
5759

58-
dbgResponse -> printf( (const char*)"Commander init start\r\n" );
60+
dbgResponse -> println( (const char*)"Commander init start" );
5961

6062
// Make the tree ordered by alphabet.
61-
dbgResponse -> printf( (const char*)"\tCreating alphabetical order... " );
63+
dbgResponse -> print( (const char*)"\tCreating alphabetical order... " );
6264
for( i = 0; i < API_tree_size; i++ ){
6365

6466
for( j = i + 1; j < API_tree_size; j++ ){
@@ -82,16 +84,16 @@ void Commander::init(){
8284
API_tree[ i ].place = i;
8385

8486
}
85-
dbgResponse -> printf( (const char*)"[ OK ]\r\n" );
87+
dbgResponse -> println( (const char*)"[ OK ]" );
8688

8789
// Optimize the tree to make it balanced.
8890
// It is necessary to speed up the command
8991
// search phase.
90-
dbgResponse -> printf( (const char*)"\tCreate balanced binary structure... " );
92+
dbgResponse -> print( (const char*)"\tCreate balanced binary structure... " );
9193
optimize_api_tree();
92-
dbgResponse -> printf( (const char*)"[ OK ]\r\n" );
94+
dbgResponse -> println( (const char*)"[ OK ]" );
9395

94-
dbgResponse -> printf( (const char*)"Commander init finished!\r\n" );
96+
dbgResponse -> println( (const char*)"Commander init finished!" );
9597

9698
}
9799

@@ -236,19 +238,8 @@ void Commander::executeCommand( char *cmd ){
236238
// get a bus-fault error without a buffer.
237239
strncpy( tempBuff, cmd, COMMANDER_MAX_COMMAND_SIZE );
238240

239-
// Remove whitespaces at the beginning.
240-
while( *tempBuff == ' ' ){
241-
tempBuff++;
242-
}
243-
244241
pipePos = hasChar( tempBuff, '|' );
245242

246-
if( pipePos > 0 ){
247-
248-
tempBuff[ pipePos ] = '\0';
249-
250-
}
251-
252243
// tempBuff is the address of the first character of the incoming command.
253244
// If we give arg variable the value stored in tempBuff means arg will point to
254245
// the first character of the command as well.
@@ -305,42 +296,27 @@ void Commander::executeCommand( char *cmd ){
305296
if( show_description ){
306297

307298
// Print the description text to the output channel.
308-
response -> printf( "%s: %s\r\n", commandData_ptr -> name, commandData_ptr -> desc );
299+
response -> print( commandData_ptr -> name );
300+
response -> print( ": " );
301+
response -> println( commandData_ptr -> desc );
309302

310303

311304
}
312305

313-
// Example: random 0 180 | setServo
314-
315306
// If show_description flag is not set, than we have to execute the commands function.
316307
else{
317308

318309
if( pipePos > 0 ){
319310

320-
// TODO Switch response to pipe response.
321-
response = &pipeResponse;
311+
// TODO Switch response to internal buffer.
322312

323313
}
324314

325-
// TODO if the internal buffer has data, the arg has to be replaced with that.
326-
if( pipeResponse.available() ){
327-
328-
arg = pipeResponse.getData();
329-
330-
}
315+
// TODO if the internal buffer has data, the arg has to be replaced to that.
331316

332317
// Execute commands function.
333318
(commandData_ptr -> func)( arg, response );
334319

335-
pipeResponse.flush();
336-
337-
// TODO recursive function call.
338-
if( pipePos > 0 ){
339-
340-
executeCommand( cmd + pipePos );
341-
342-
}
343-
344320
}
345321

346322
}
@@ -368,7 +344,9 @@ void Commander::executeCommand( char *cmd ){
368344

369345
// If we went through the whole tree and we did not found the command in it,
370346
// we have to notice the user abut the problem. Maybe a Type-O
371-
response -> printf( (const char*)"Command \'%s\' not found!!!\r\n", tempBuff );
347+
response -> print( (const char*)"Command \'" );
348+
response -> print( tempBuff );
349+
response -> println( "\' not found!" );
372350

373351
}
374352

@@ -394,174 +372,33 @@ void Commander::execute( const char *cmd ){
394372

395373
}
396374

397-
#ifdef COMMANDER_USE_SERIAL_RESPONSE
398-
399-
void Commander::execute( char *cmd, Serial *resp ){
400-
401-
// Serial execute handler, so the Serial response will be chosen.
402-
response = &serialResponse;
403-
404-
// Select the right Serial object in the response class.
405-
serialResponse.select( resp );
406-
407-
// Execute the command.
408-
executeCommand( cmd );
409-
410-
}
411-
412-
void Commander::execute( const char *cmd, Serial *resp ){
413-
414-
// Serial execute handler, so the Serial response will be chosen.
415-
response = &serialResponse;
416-
417-
// Select the right Serial object in the response class.
418-
serialResponse.select( resp );
419-
420-
// Execute the command.
421-
executeCommand( (char*)cmd );
422-
423-
}
424-
425-
void Commander::attachDebugChannel( Serial *resp ){
426-
427-
dbgResponse = &serialDebugResponse;
428-
429-
// Select the right Serial object in the response class.
430-
serialDebugResponse.select( resp );
375+
void Commander::execute( char *cmd, Stream *resp ){
431376

432-
// Enable debug messages.
433-
debugEnabled = true;
434-
435-
}
436-
437-
#endif
438-
439-
#ifdef COMMANDER_USE_ARDUINO_SERIAL_RESPONSE
440-
441-
void Commander::execute( char *cmd, HardwareSerial *resp ){
442-
443-
// Arduino Serial execute handler, so the Arduino Serial response will be chosen.
444-
response = &arduinoSerialResponse;
445-
446-
// Select the right HardwareSerial object in the response class.
447-
arduinoSerialResponse.select( resp );
377+
response = resp;
448378

449379
// Execute the command.
450380
executeCommand( cmd );
451381

452382
}
453383

454-
void Commander::execute( const char *cmd, HardwareSerial *resp ){
384+
void Commander::execute( const char *cmd, Stream *resp ){
455385

456-
// Arduino Serial execute handler, so the Arduino Serial response will be chosen.
457-
response = &arduinoSerialResponse;
458-
459-
// Select the right HardwareSerial object in the response class.
460-
arduinoSerialResponse.select( resp );
386+
response = resp;
461387

462388
// Execute the command.
463389
executeCommand( (char*)cmd );
464390

465391
}
466392

467-
void Commander::attachDebugChannel( HardwareSerial *resp ){
468-
469-
dbgResponse = &arduinoSerialDebugResponse;
393+
void Commander::attachDebugChannel( Stream *resp ){
470394

471-
// Select the right HardwareSerial object in the response class.
472-
arduinoSerialDebugResponse.select( resp );
395+
dbgResponse = resp;
473396

474397
// Enable debug messages.
475398
debugEnabled = true;
476399

477400
}
478401

479-
#endif
480-
481-
#ifdef COMMANDER_USE_ARDUINO_32U4_SERIAL_RESPONSE
482-
483-
void Commander::execute( char *cmd, Serial_ *resp ){
484-
485-
// Arduino Serial execute handler, so the Arduino Serial response will be chosen.
486-
response = &arduino32U4SerialResponse;
487-
488-
// Select the right HardwareSerial object in the response class.
489-
arduino32U4SerialResponse.select( resp );
490-
491-
// Execute the command.
492-
executeCommand( cmd );
493-
494-
}
495-
496-
void Commander::execute( const char *cmd, Serial_ *resp ){
497-
498-
// Arduino Serial execute handler, so the Arduino Serial response will be chosen.
499-
response = &arduino32U4SerialResponse;
500-
501-
// Select the right HardwareSerial object in the response class.
502-
arduino32U4SerialResponse.select( resp );
503-
504-
// Execute the command.
505-
executeCommand( (char*)cmd );
506-
507-
}
508-
509-
void Commander::attachDebugChannel( Serial_ *resp ){
510-
511-
dbgResponse = &arduino32U4SerialResponse;
512-
513-
// Select the right HardwareSerial object in the response class.
514-
arduino32U4SerialResponse.select( resp );
515-
516-
// Enable debug messages.
517-
debugEnabled = true;
518-
519-
}
520-
521-
#endif
522-
523-
#ifdef COMMANDER_USE_WIFI_CLIENT_RESPONSE
524-
525-
void Commander::execute( char *cmd, WiFiClient *resp ){
526-
527-
// Arduino Serial execute handler, so the Arduino Serial response will be chosen.
528-
response = &WiFiClientResponse;
529-
530-
// Select the right HardwareSerial object in the response class.
531-
WiFiClientResponse.select( resp );
532-
533-
// Execute the command.
534-
executeCommand( cmd );
535-
536-
}
537-
538-
void Commander::execute( const char *cmd, WiFiClient *resp ){
539-
540-
// Arduino Serial execute handler, so the Arduino Serial response will be chosen.
541-
response = &WiFiClientResponse;
542-
543-
// Select the right HardwareSerial object in the response class.
544-
WiFiClientResponse.select( resp );
545-
546-
// Execute the command.
547-
executeCommand( (char*)cmd );
548-
549-
}
550-
551-
void Commander::attachDebugChannel( WiFiClient *resp ){
552-
553-
dbgResponse = &WiFiClientDebugResponse;
554-
555-
// Select the right HardwareSerial object in the response class.
556-
WiFiClientDebugResponse.select( resp );
557-
558-
// Enable debug messages.
559-
debugEnabled = true;
560-
561-
}
562-
563-
#endif
564-
565402
void Commander::enableDebug(){
566403

567404
debugEnabled = true;
@@ -638,7 +475,7 @@ void Commander::helpFunction( bool description ){
638475
uint32_t i;
639476
uint32_t j;
640477

641-
response -> printf( (const char*)"---- Available commands ----\r\n" );
478+
response -> println( (const char*)"---- Available commands ----" );
642479
for( i = 0; i < API_tree_size; i++ ){
643480

644481
for( j = 0; j < API_tree_size; j++ ){
@@ -647,14 +484,18 @@ void Commander::helpFunction( bool description ){
647484

648485
if( description ){
649486

650-
response -> printf( (const char*)"%s:\r\n", API_tree[ j ].name );
651-
response -> printf( (const char*)"\t%s\r\n\r\n", API_tree[ j ].desc );
487+
response -> print( API_tree[ j ].name );
488+
response -> println( ':' );
489+
response -> print( '\t' );
490+
response -> print( API_tree[ j ].desc );
491+
response -> println();
492+
response -> println();
652493

653494
}
654495

655496
else{
656497

657-
response -> printf( (const char*)"%s\r\n", API_tree[ j ].name );
498+
response -> println( API_tree[ j ].name );
658499

659500
}
660501

0 commit comments

Comments
 (0)