Skip to content

Commit

Permalink
Release 1.1.0
Browse files Browse the repository at this point in the history
- eliminated the error message in case the command is not recognized (can create problems)
- int8_t ReadSer () - the method now returns an error code documented in ReadMe
- void Print (float, int numDec = 2) - added Print for floats with default of two decimals
- void Print (double, int numDec = 2) - added Print for doubles with default of two decimals
- the SerialCmd_Buffer (private) has now, at each cycle, completely cleaned at 0x00
  • Loading branch information
gpb01 committed Feb 22, 2022
1 parent e149968 commit 21cfef9
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 47 deletions.
59 changes: 40 additions & 19 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SerialCmd Library v1.0.8
# SerialCmd Library v1.1.0
© 2022 Guglielmo Braguglia

---
Expand Down Expand Up @@ -96,9 +96,9 @@ SerialCmd mySerCmd( Serial, SERIALCMD_LF, (char *) SERIALCMD_SEMICOL );

### Library methods

##### AddCmd ( const char *command, char allowedSource, void ( *function ) () )
##### uint8_t AddCmd ( const char *command, char allowedSource, void ( *function ) () )

Add a "command" to the list of recognized commands and define which function should be called. Parameter "allowedSource" can be one of those defined in .h (*SERIALCMD_FROMSTRING, SERIALCMD_FROMALL, SERIALCMD_FROMSERIAL*). Return and uint8_t to indicate whether the command was added (*true value*) or not (*false value*).
Add a "command" to the list of recognized commands and define which function should be called. Parameter "allowedSource" can be one of those defined in .h (*SERIALCMD_FROMSTRING, SERIALCMD_FROMALL, SERIALCMD_FROMSERIAL*). Returns and uint8_t to indicate whether the command was added (*true/1 value*) or not (*false/0 value*).

Example:

Expand All @@ -118,9 +118,9 @@ void set_LedOn ( void ) {

---

##### AddCmd( const __FlashStringHelper *command, char allowedSource, void ( *function )() );
##### uint8_t AddCmd( const __FlashStringHelper *command, char allowedSource, void ( *function )() );

Valid only on **AVR** architecture, add a "command" to the list of recognized commands and define which function should be called. Parameter "allowedSource" can be one of those defined in .h (*SERIALCMD_FROMSTRING, SERIALCMD_FROMALL, SERIALCMD_FROMSERIAL*). Return and uint8_t to indicate whether the command was added (*true value*) or not (*false value*).
Valid only on **AVR** architecture, add a "command" to the list of recognized commands and define which function should be called. Parameter "allowedSource" can be one of those defined in .h (*SERIALCMD_FROMSTRING, SERIALCMD_FROMALL, SERIALCMD_FROMSERIAL*). Returns and uint8_t to indicate whether the command was added (*true/1 value*) or not (*false/0 value*).

Example:

Expand All @@ -141,9 +141,9 @@ void set_LedOn ( void ) {
---


##### ReadNext( )
##### char* ReadNext( )

Return the address of the string that contains the next parameter, if there is no next parameter, it contains the value NULL. It is normally used within the function called by the "command" to retrieve any parameters.
Returns the address of the string that contains the next parameter, if there is no next parameter, it contains the value NULL. It is normally used within the function called by the "command" to retrieve any parameters.

Example:

Expand All @@ -156,9 +156,9 @@ cPar = mySerCmd.ReadNext( );

---

##### Print( )
##### void Print( )

It allows to send a String (*class String*), a character string (*char\**), a signed/unsigned character (*char, unsigned char*), a signed/unsigned integer (*int, unsigned int*) or a signed/unsigned long (*long, unsigned long*) to the serial port (*hardware or software*) associated with the SerialCmd.
It allows to send a String (*class String*), a character string (*char\**), a signed/unsigned character (*char, unsigned char*), a signed/unsigned integer (*int, unsigned int*), a signed/unsigned long (*long, unsigned long*) or a float/double (*float, double*), to the serial port (*hardware or software*) associated with the SerialCmd.

On AVR architecture it also allows the use of the macro F (), with constant strings, to reduce the SRAM occupation.

Expand All @@ -176,9 +176,9 @@ mySerCmd.Print( F ( "This is a message \r\n" ) );

---

##### ReadSer( )
##### int8_t ReadSer( )

It **must** be called **continuously** inside the loop () to receive and interpret the commands received from the serial port (*hardware or software*)
It **must** be called **continuously** inside the loop () to receive and interpret the commands received from the serial port (*hardware or software*). Returns an int8_t that can take the following values: **-1**: terminator character not yet encountered; **0**: command **not** recognized; **1**: command recognized.

Example:

Expand All @@ -189,7 +189,7 @@ void setup( ) {
}
void loop( ) {
mySerCmd.ReadSer( );
ret = mySerCmd.ReadSer( );
...
...
}
Expand All @@ -198,9 +198,9 @@ void loop( ) {

---

##### ReadString ( char * theCmd )
##### int8_t ReadString ( char * theCmd )

It is used to send a command from the application as if it had been received from the serial line. The content of the string must be the same as it would have been sent through the serial port (*including parameters*). Return and uint8_t to indicate whether the command was recognized (*true value*) or not (*false value*).
It is used to send a command from the application as if it had been received from the serial line. The content of the string must be the same as it would have been sent through the serial port (*including parameters*). Returns and int8_t to indicate whether the command was recognized (*true/1 value*) or not (*false/0 value*).

Example:

Expand All @@ -210,9 +210,9 @@ ret = mySerCmd.ReadString ( (char *) "LEDON" );

---

##### ReadString ( const __FlashStringHelper * theCmd )
##### int8_t ReadString ( const __FlashStringHelper * theCmd )

Valid only on **AVR** architecture, it is used to send a command from the application as if it had been received from the serial line. The content of the string must be the same as it would have been sent through the serial port (*including parameters*). Return and uint8_t to indicate whether the command was recognized (*true value*) or not (*false value*).
Valid only on **AVR** architecture, it is used to send a command from the application as if it had been received from the serial line. The content of the string must be the same as it would have been sent through the serial port (*including parameters*). Returns and int8_t to indicate whether the command was recognized (*true/1 value*) or not (*false/0 value*).

Example:

Expand All @@ -229,6 +229,25 @@ ret = mySerCmd.ReadString ( F ( "LEDON" ) );
The following example uses the "**Serial**" serial port to manage three commands: "LEDON" which turns on the LED on the board, "LEDOF" which turns off the LED on the board and the command "LEDBL,*time*" which makes the LED blinking with half-period equal to the "*time*" parameter (*in milliseconds*). The number of flashes is counted and when a certain number is reached, the LED, by means of a command from the "**buffer**" (*therefore from the application program*), is switched off.

```
/*
Demo_SerialCmd - A simple program to demostrate the use of SerialCmd
library to show the capability to receive commands via serial port.
Copyright (C) 2013 - 2022 Guglielmo Braguglia
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
*/
#include <stdlib.h>
#include <SerialCmd.h>
Expand Down Expand Up @@ -289,6 +308,7 @@ void setup() {
pinMode ( LED_BUILTIN, OUTPUT );
digitalWrite ( LED_BUILTIN, ledStatus );
Serial.begin ( 9600 );
while ( !Serial ) delay ( 500 );
//
#ifdef ARDUINO_ARCH_STM32
for ( uint8_t i = 0; i < 7; i++ ) {
Expand Down Expand Up @@ -321,7 +341,7 @@ void setup() {
}
void loop() {
uint8_t ret;
int8_t ret;
//
if ( isBlinking && ( millis() - blinkingLast > blinkingTime ) ) {
ledStatus = !ledStatus;
Expand All @@ -344,9 +364,10 @@ void loop() {
}
}
//
mySerCmd.ReadSer();
ret = mySerCmd.ReadSer();
if ( ret == 0 )
mySerCmd.Print ( ( char * ) "ERROR: Urecognized command. \r\n" );
}
```

---
Binary file modified ReadMe.pdf
Binary file not shown.
11 changes: 7 additions & 4 deletions examples/Demo_SerialCmd/Demo_SerialCmd.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
*/
*/
#include <stdlib.h>
#include <SerialCmd.h>

Expand Down Expand Up @@ -77,6 +77,7 @@ void setup() {
pinMode ( LED_BUILTIN, OUTPUT );
digitalWrite ( LED_BUILTIN, ledStatus );
Serial.begin ( 9600 );
while ( !Serial ) delay ( 500 );
//
#ifdef ARDUINO_ARCH_STM32
for ( uint8_t i = 0; i < 7; i++ ) {
Expand Down Expand Up @@ -109,7 +110,7 @@ void setup() {
}

void loop() {
uint8_t ret;
int8_t ret;
//
if ( isBlinking && ( millis() - blinkingLast > blinkingTime ) ) {
ledStatus = !ledStatus;
Expand All @@ -132,5 +133,7 @@ void loop() {
}
}
//
mySerCmd.ReadSer();
ret = mySerCmd.ReadSer();
if ( ret == 0 )
mySerCmd.Print ( ( char * ) "ERROR: Urecognized command. \r\n" );
}
7 changes: 5 additions & 2 deletions examples/ESP_SerialCmd/ESP_SerialCmd.ino
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ void setup() {
// ------------------------------------------------------------------

void loop() {
char* retVal;
char* retVal;
int8_t erc;
//
retVal = checkWiFiCommand();
if ( retVal != NULL ) {
Expand All @@ -204,7 +205,9 @@ void loop() {
mySerCmd.ReadString ( ( char * ) "LEDOF" );
}
//
mySerCmd.ReadSer();
erc = mySerCmd.ReadSer();
if ( erc == 0 )
mySerCmd.Print ( ( char * ) "ERROR: Urecognized command. \r\n" );
//
yield();
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SerialCmd
version=1.0.9
version=1.1.0
author=Guglielmo Braguglia <[email protected]>
maintainer=Guglielmo Braguglia <[email protected]>
sentence=Just another library to tokenize and parse commands.
Expand Down
42 changes: 26 additions & 16 deletions src/SerialCmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
*/

void SerialCmd::ClearBuffer() {
SerialCmd_Buffer[0] = 0x00;
memset (SerialCmd_Buffer, 0x00, SERIALCMD_MAXBUFFER + 1);
SerialCmd_BufferIdx = 0;
}

Expand All @@ -68,13 +68,13 @@ void SerialCmd::ConvertUC() {

void SerialCmd::ReadStringCommon () {
SerialCmd_Command = strtok_r ( SerialCmd_Buffer, SerialCmd_Sep, &SerialCmd_Last );
SerialCmd_Found = false;
SerialCmd_Found = 0;
if ( SerialCmd_Command != NULL ) {
for ( SerialCmd_Idx = 0; SerialCmd_Idx < SerialCmd_CmdCount; SerialCmd_Idx++ ) {
if ( strncmp ( SerialCmd_Command, SerialCmd_CmdList[SerialCmd_Idx].command, SERIALCMD_MAXCMDLNG ) == 0 ) {
if ( SerialCmd_CmdList[SerialCmd_Idx].allowedSource <= 0 ) {
( *SerialCmd_CmdList[SerialCmd_Idx].function ) ();
SerialCmd_Found = true;
SerialCmd_Found = 1;
break;
}
}
Expand Down Expand Up @@ -121,51 +121,51 @@ uint8_t SerialCmd::AddCmd ( const __FlashStringHelper *command, char allowedSour
}
#endif

void SerialCmd::ReadSer() {
int8_t SerialCmd::ReadSer() {
SerialCmd_Found = -1;
while ( theSerial->available() > 0 ) {
SerialCmd_InChar = theSerial->read();
if ( SerialCmd_InChar == SerialCmd_Term ) {
SerialCmd_Command = strtok_r ( SerialCmd_Buffer, SerialCmd_Sep, &SerialCmd_Last );
SerialCmd_Found = false;
SerialCmd_Found = 0;
if ( SerialCmd_Command != NULL ) {
if ( SERIALCMD_FORCEUC ) ConvertUC();
for ( SerialCmd_Idx = 0; SerialCmd_Idx < SerialCmd_CmdCount; SerialCmd_Idx++ ) {
if ( strncmp ( SerialCmd_Command, SerialCmd_CmdList[SerialCmd_Idx].command, SERIALCMD_MAXCMDLNG ) == 0 ) {
if ( SerialCmd_CmdList[SerialCmd_Idx].allowedSource >= 0 ) {
( *SerialCmd_CmdList[SerialCmd_Idx].function ) ();
SerialCmd_Found = true;
SerialCmd_Found = 1;
break;
}
}
}
}
ClearBuffer();
if ( !SerialCmd_Found ) {
// Command NOT found
Print ( ( char* ) "ERROR: Command not found.\r\n" );
}
} else {
if ( SerialCmd_BufferIdx < SERIALCMD_MAXBUFFER ) {
SerialCmd_Buffer[SerialCmd_BufferIdx++] = SerialCmd_InChar;
SerialCmd_Buffer[SerialCmd_BufferIdx] = 0x00;
if ( (SerialCmd_InChar != SERIALCMD_CR) && (SerialCmd_InChar != SERIALCMD_LF) ) {
SerialCmd_Buffer[SerialCmd_BufferIdx++] = SerialCmd_InChar;
SerialCmd_Buffer[SerialCmd_BufferIdx] = 0x00;
}
} else {
ClearBuffer();
}
}
}
return SerialCmd_Found;
}

uint8_t SerialCmd::ReadString ( char * theCmd ) {
if ( strlen ( theCmd ) >= SERIALCMD_MAXBUFFER ) return false;
int8_t SerialCmd::ReadString ( char * theCmd ) {
if ( strlen ( theCmd ) >= SERIALCMD_MAXBUFFER ) return 0;
//
strcpy ( SerialCmd_Buffer, theCmd );
ReadStringCommon();
return SerialCmd_Found;
}

#ifdef __AVR__
uint8_t SerialCmd::ReadString ( const __FlashStringHelper * theCmd ) {
if ( strlen_P ( ( const char* ) theCmd ) >= SERIALCMD_MAXBUFFER ) return false;
int8_t SerialCmd::ReadString ( const __FlashStringHelper * theCmd ) {
if ( strlen_P ( ( const char* ) theCmd ) >= SERIALCMD_MAXBUFFER ) return 0;
//
strcpy_P ( SerialCmd_Buffer, ( const char* ) theCmd );
ReadStringCommon();
Expand Down Expand Up @@ -222,3 +222,13 @@ void SerialCmd::Print ( unsigned long theULong ) {
if ( ( theSerial ) )
theSerial->print ( theULong );
}

void SerialCmd::Print ( float theFloat, int numDec ) {
if ( ( theSerial ) )
theSerial->print ( theFloat, numDec );
}

void SerialCmd::Print ( double theDouble, int numDec ) {
if ( ( theSerial ) )
theSerial->print ( theDouble, numDec );
}
12 changes: 7 additions & 5 deletions src/SerialCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ class SerialCmd {
public:

SerialCmd ( Stream &mySerial, char TermCh = SERIALCMD_CR, char * SepCh = ( char * ) SERIALCMD_COMMA ); // Constructor
void ReadSer ( void );
int8_t ReadSer ( void );
uint8_t AddCmd ( const char *, char, void ( * ) () );
char * ReadNext ( void );
uint8_t ReadString ( char * );
char * ReadNext ( void );
int8_t ReadString ( char * );
void Print ( String & );
void Print ( char[] );
void Print ( char );
Expand All @@ -104,9 +104,11 @@ class SerialCmd {
void Print ( unsigned int );
void Print ( long );
void Print ( unsigned long );
void Print (float, int numDec = 2 );
void Print (double, int numDec = 2 );
#ifdef __AVR__
uint8_t AddCmd ( const __FlashStringHelper *, char, void ( * ) () );
uint8_t ReadString ( const __FlashStringHelper * );
int8_t ReadString ( const __FlashStringHelper * );
void Print ( const __FlashStringHelper * );
#endif

Expand All @@ -132,7 +134,7 @@ class SerialCmd {

uint8_t SerialCmd_Idx; // General index for FOR loops
uint8_t SerialCmd_BufferIdx; // Serial buffer Index
uint8_t SerialCmd_Found; // Valid command found
int8_t SerialCmd_Found; // Valid command found

Stream* theSerial; // Serial stream in use

Expand Down

0 comments on commit 21cfef9

Please sign in to comment.