Skip to content

Commit f49e252

Browse files
committed
Pipe prototype.
1 parent 877afe9 commit f49e252

File tree

5 files changed

+452
-1
lines changed

5 files changed

+452
-1
lines changed

src/Commander-API.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,26 @@ void Commander::executeCommand( char *cmd ){
228228
// Pointer to the selected command data.
229229
API_t *commandData_ptr;
230230

231+
int32_t pipePos;
232+
231233
// Copy the command data to the internal buffer.
232234
// It is necessary because we have to modify the content
233235
// of it. If it is points to a const char array we will
234236
// get a bus-fault error without a buffer.
235237
strncpy( tempBuff, cmd, COMMANDER_MAX_COMMAND_SIZE );
236238

239+
// Remove whitespaces at the beginning.
240+
while( *tempBuff = ' ' ){
241+
tempBuff++;
242+
}
243+
244+
pipePos = hasChar( tempBuff, '|' );
245+
246+
if( pipePos > 0 ){
247+
248+
tempBuff[ pipePos ] = '\0';
249+
250+
}
237251

238252
// tempBuff is the address of the first character of the incoming command.
239253
// If we give arg variable the value stored in tempBuff means arg will point to
@@ -272,6 +286,13 @@ void Commander::executeCommand( char *cmd ){
272286

273287
}
274288

289+
else if( *arg == '|' ){
290+
291+
*arg = '\0';
292+
arg++;
293+
294+
}
295+
275296
// Try to find the command datata.
276297
commandData_ptr = (*this)[ tempBuff ];
277298

@@ -289,12 +310,37 @@ void Commander::executeCommand( char *cmd ){
289310

290311
}
291312

313+
// Example: random 0 180 | setServo
314+
292315
// If show_description flag is not set, than we have to execute the commands function.
293316
else{
294317

318+
if( pipePos > 0 ){
319+
320+
// TODO Switch response to pipe response.
321+
response = &pipeResponse;
322+
323+
}
324+
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+
}
331+
295332
// Execute commands function.
296333
(commandData_ptr -> func)( arg, response );
297334

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

300346
}
@@ -621,3 +667,23 @@ void Commander::helpFunction( bool description ){
621667
}
622668

623669
}
670+
671+
int32_t Commander::hasChar( char* str, char c ){
672+
673+
int32_t cntr = 0;
674+
675+
while( str[ cntr ] ){
676+
677+
if( str[ cntr ] == c ){
678+
679+
return cntr;
680+
681+
}
682+
683+
cntr++;
684+
685+
}
686+
687+
return -1;
688+
689+
}

src/Commander-API.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ SOFTWARE.
3535
#ifndef COMMANDER_API_SRC_COMMANDER_HPP_
3636
#define COMMANDER_API_SRC_COMMANDER_HPP_
3737

38-
#define COMMANDER_API_VERSION (const char*)"2.0.1"
38+
#define COMMANDER_API_VERSION (const char*)"2.1.0"
3939

4040
#include "stdint.h"
4141
#include "string.h"
@@ -339,6 +339,8 @@ class Commander{
339339
commandResponseWiFiClient WiFiClientDebugResponse;
340340
#endif
341341

342+
commandResponsePipe pipeResponse;
343+
342344
/// Pointer to response class. By default it
343345
/// points to the default debug response handler.
344346
commandResponse *dbgResponse = &defaultDebugResponse;
@@ -375,6 +377,10 @@ class Commander{
375377
/// the description data for all commands.
376378
void helpFunction( bool description = false );
377379

380+
int32_t hasChar( char* str, char c );
381+
382+
char pipeBuffer[COMMANDER_MAX_COMMAND_SIZE];
383+
378384
};
379385

380386

0 commit comments

Comments
 (0)