### Full Example: ESP32/ESP8266 WiFi and Serial Command Interface Source: https://context7.com/gpb01/serialcmd/llms.txt This example demonstrates handling commands via HTTP GET requests and the hardware serial port simultaneously. It uses `ReadString()` with `fValidate` for safe pre-validation of commands. ```cpp #include // or for ESP8266 #include #include "credential.h" // defines NET_SSID and NET_PSW WiFiServer server(80); SerialCmd mySerCmd(Serial); bool isBlinking = false; uint32_t blinkTime = 0, blinkLast = 0; uint8_t blinkCnt = 0; void set_LEDON() { digitalWrite(LED_BUILTIN, HIGH); } void set_LEDOF() { digitalWrite(LED_BUILTIN, LOW); isBlinking = false; } void set_LEDBL() { char *p = mySerCmd.ReadNext(); if (!p) return; blinkTime = strtoul(p, NULL, 10); blinkLast = millis(); blinkCnt = 0; isBlinking = true; } void setup() { Serial.begin(115200); WiFi.begin(NET_SSID, NET_PSW); while (WiFi.status() != WL_CONNECTED) delay(500); Serial.println(WiFi.localIP()); server.begin(); mySerCmd.AddCmd("LEDON", SERIALCMD_FROMALL, set_LEDON); mySerCmd.AddCmd("LEDOF", SERIALCMD_FROMALL, set_LEDOF); mySerCmd.AddCmd("LEDBL", SERIALCMD_FROMALL, set_LEDBL); } void loop() { // --- Handle HTTP --- WiFiClient client = server.available(); if (client) { char buf[200] = {}, cmd[64] = {}; uint8_t idx = 0; uint32_t t = millis(); while (client.connected() && millis() - t < 2000) { if (!client.available()) continue; char c = client.read(); t = millis(); if (c == '\n') { char *s = strstr(buf, "GET /"), *e = strstr(buf, " HTTP"); if (s && e && (e - s - 5) < 63) { strlcpy(cmd, s + 5, e - s - 5 + 1); // Validate first, then execute if (mySerCmd.ReadString(cmd, true) == 1) mySerCmd.ReadString(cmd); else Serial.println("HTTP: unknown command"); } memset(buf, 0, sizeof buf); idx = 0; } else if (c != '\r') buf[idx++] = c; } client.println("HTTP/1.1 200 OK\r\nContent-type:text/html\r\nConnection: close\r\n"); client.stop(); } // --- Handle serial --- if (mySerCmd.ReadSer() == 0) mySerCmd.Print((char*)"ERROR: Unrecognized command\r\n"); // --- Blink logic --- if (isBlinking && millis() - blinkLast > blinkTime) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); blinkLast += blinkTime; if (++blinkCnt >= 10) mySerCmd.ReadString((char*)"LEDOF"); } yield(); } ``` -------------------------------- ### SerialCmd Constructor Examples Source: https://context7.com/gpb01/serialcmd/llms.txt Instantiate SerialCmd by passing a compatible Stream object. Customize the line terminator character and parameter separator string as needed. Examples show hardware serial, SoftwareSerial, and SD card file usage. ```cpp // Signature SerialCmd mySerCmd ( Serial ); ``` ```cpp // Hardware serial with LF terminator and semicolon separator SerialCmd mySerCmd ( Serial, SERIALCMD_LF, (char *) SERIALCMD_SEMICOL ); ``` ```cpp #include SoftwareSerial mySwSerial (10, 11); // RX, TX SerialCmd mySerCmd ( mySwSerial ); ``` ```cpp #include #include File dataFile; SerialCmd mySerCmd ( dataFile, SERIALCMD_LF ); // lines end with LF in the text file void setup() { SD.begin(10); dataFile = SD.open("CMDS.TXT", FILE_READ); // ... register commands, then read file in loop } ``` -------------------------------- ### ReadNext() - Retrieve Next Parameter Source: https://context7.com/gpb01/serialcmd/llms.txt Use ReadNext() inside a command callback to get the next parameter from the input line. Each call consumes parameters sequentially. Returns NULL if no more parameters are available. ```cpp // Signature char * ReadNext ( void ); ``` ```cpp // --- Example: command "MOVE,direction,steps" --- void cmd_Move() { char * dir = mySerCmd.ReadNext(); // first parameter char * steps = mySerCmd.ReadNext(); // second parameter char * extra = mySerCmd.ReadNext(); // third parameter (NULL if not supplied) if ( dir == NULL || steps == NULL ) { mySerCmd.Print( (char*) "ERROR: MOVE requires direction and steps\r\n" ); return; } uint32_t numSteps = strtoul( steps, NULL, 10 ); Serial.print("Moving "); Serial.print(dir); Serial.print(" by "); Serial.println(numSteps); // extra == NULL — gracefully ignored } // Register: "MOVE,FWD,200" → calls cmd_Move, dir="FWD", steps="200" // mySerCmd.AddCmd("MOVE", SERIALCMD_FROMALL, cmd_Move); ``` -------------------------------- ### Initialize SerialCmd with Hardware Serial Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Include the SerialCmd library and instantiate the class, passing the hardware serial object. Default terminator and separator characters are used. ```c++ #include SerialCmd mySerCmd( Serial ); ``` -------------------------------- ### Serial Command Demo Program Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md This C++ code demonstrates the SerialCmd library for receiving and processing commands over a serial port to control an onboard LED. It includes functions for turning the LED on, off, and making it blink, with error handling for missing parameters. ```cpp /* 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 #include #define LED_OFF LOW // adjust for your board #define LED_ON HIGH // adjust for your board #ifdef __AVR__ #pragma message "INFO: Compiling with AVR F() macro enabled." #endif bool isBlinking = false; // Indicates whether blinking is active or not uint8_t ledStatus = LED_OFF; // BUILTIN_LED status (OFF/ON) uint8_t blinkingCnt = 0; // Number of led status changes before turning off blinking uint32_t blinkingTime = 0; // Time of led status change uint32_t blinkingLast = 0; // Last millis() in which the status of the led was changed SerialCmd mySerCmd ( Serial ); // Initialize the SerialCmd constructor using the "Serial" port // ------------------- User functions -------------------- void sendOK ( void ) { #ifdef __AVR__ mySerCmd.Print ( F ( "OK \r\n" ) ); #else mySerCmd.Print ( ( char * ) "OK \r\n" ); #endif } // --------------- Functions for SerialCmd ? void set_LEDON ( void ) { isBlinking = false; ledStatus = LED_ON; digitalWrite ( LED_BUILTIN, LED_ON ); sendOK(); } void set_LEDOF ( void ) { isBlinking = false; ledStatus = LED_OFF; digitalWrite ( LED_BUILTIN, LED_OFF ); sendOK(); } void set_LEDBL ( void ) { char * sParam; // sParam = mySerCmd.ReadNext(); if ( sParam == NULL ) { #ifdef __AVR__ mySerCmd.Print ( F ( "ERROR: Missing blinking time \r\n" ) ); #else mySerCmd.Print ( ( char * ) "ERROR: Missing blinking time \r\n" ); #endif return; } blinkingCnt = 0; blinkingTime = strtoul ( sParam, NULL, 10 ); blinkingLast = millis(); isBlinking = true; sendOK(); } #ifdef __AVR__ void set_NLBL ( void ) { mySerCmd.AddCmd ( F ( "LEDBL" ) , SERIALCMD_FROMALL, NULL ); sendOK(); } void set_YLBL ( void ) { mySerCmd.AddCmd ( F ( "LEDBL" ) , SERIALCMD_FROMALL, set_LEDBL ); sendOK(); } #else void set_NLBL ( void ) { mySerCmd.AddCmd ( "LEDBL", SERIALCMD_FROMALL, NULL ); sendOK(); } void set_YLBL ( void ) { mySerCmd.AddCmd ( "LEDBL", SERIALCMD_FROMALL, set_LEDBL ); sendOK(); } #endif // ----------------------- setup() ? void setup() { delay ( 500 ); 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++ ) { // create a 3500 msec delay with blink for STM Nucleo boards delay ( 500 ); ledStatus = !ledStatus; digitalWrite ( LED_BUILTIN, ledStatus ); } #else while ( !Serial ) { delay ( 100 ); ledStatus = !ledStatus; digitalWrite ( LED_BUILTIN, ledStatus ); } #endif // #ifdef __AVR__ mySerCmd.AddCmd ( F ( "LEDON" ) , SERIALCMD_FROMALL, set_LEDON ); // BUILTIN LED ON mySerCmd.AddCmd ( F ( "LEDOF" ) , SERIALCMD_FROMALL, set_LEDOF ); // BUILTIN LED OFF mySerCmd.AddCmd ( F ( "LEDBL" ) , SERIALCMD_FROMALL, set_LEDBL ); // BUILTIN LED BLINK, period ms in parameter mySerCmd.AddCmd ( F ( "SETNB" ) , SERIALCMD_FROMALL, set_NLBL ); // DISABLE LEDBL command mySerCmd.AddCmd ( F ( "SETYB" ) , SERIALCMD_FROMALL, set_YLBL ); // REENABLE LEDBL command // mySerCmd.Print ( F ( "INFO: Program running on AVR ... \r\n" ) ); #else mySerCmd.AddCmd ( "LEDON", SERIALCMD_FROMALL, set_LEDON ); mySerCmd.AddCmd ( "LEDOF", SERIALCMD_FROMALL, set_LEDOF ); mySerCmd.AddCmd ( "LEDBL", SERIALCMD_FROMALL, set_LEDBL ); mySerCmd.AddCmd ( "SETNB", SERIALCMD_FROMALL, set_NLBL ); mySerCmd.AddCmd ( "SETYB", SERIALCMD_FROMALL, set_YLBL ); // ``` -------------------------------- ### Send Command from Application Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Simulates receiving a command from the serial line to be processed by the library. Can optionally validate without execution. ```cpp ret = mySerCmd.ReadString ( (char *) "LEDON" ); ``` -------------------------------- ### Serial Command Library Demo - Loop Function Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md This C++ code demonstrates the main loop of a program using the serial command library. It handles LED blinking based on time and processes incoming serial commands, including a 'LEDOF' command and unrecognized commands. ```cpp void loop() { int8_t ret; // if ( isBlinking && ( millis() - blinkingLast > blinkingTime ) ) { ledStatus = !ledStatus; digitalWrite ( LED_BUILTIN, ledStatus ); blinkingCnt++; blinkingLast += blinkingTime; } // if ( blinkingCnt >= 10 ) { blinkingCnt = 0; #ifdef __AVR__ ret = mySerCmd.ReadString ( F ( "LEDOF" ) ); #else ret = mySerCmd.ReadString ( ( char * ) "LEDOF" ); #endif if ( ret == false ) { // error processing command from string ... // ... insert here error handling. } } // ret = mySerCmd.ReadSer(); if ( ret == 0 ) { mySerCmd.Print ( ( char * ) "ERROR: Urecognized command. \r\n" ); #if ( SERIALCMD_PUBBUFFER == 1 ) mySerCmd.Print ( ( char * ) " line entered : " ); mySerCmd.Print ( mySerCmd.lastLine ); mySerCmd.Print ( ( char * ) "\r\n" ); #endif } } ``` -------------------------------- ### Initialize SerialCmd with Custom Terminator and Separator Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Instantiate the SerialCmd class with a specific serial port, a custom terminator character (e.g., SERIALCMD_LF), and a custom separator character (e.g., SERIALCMD_SEMICOL). ```c++ #include SerialCmd mySerCmd( Serial, SERIALCMD_LF, (char *) SERIALCMD_SEMICOL ); ``` -------------------------------- ### Read Next Command Parameter Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Retrieves the next parameter associated with a command. Returns NULL if no more parameters are available. ```cpp char * cPar; ... ... cPar = mySerCmd.ReadNext( ); ``` -------------------------------- ### AddCmd (const __FlashStringHelper *command, char allowedSource, void (*function)()) Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md AVR-specific overload for AddCmd. Adds a command to the recognized list and associates it with a function, storing the command string in PROGMEM to reduce SRAM usage. Supports redefining commands. The allowedSource parameter specifies where the command can originate from. Returns 1 if the command was added successfully, 0 otherwise. ```APIDOC ## AddCmd (AVR specific) ### Description Adds a command to the recognized list and defines which function should be called. This version is specific to AVR architecture and stores the command string in PROGMEM to reduce SRAM occupation. Supports redefining commands. ### Parameters - **command** (const __FlashStringHelper *) - The command string to register (stored in PROGMEM). - **allowedSource** (char) - Specifies the allowed sources for the command (e.g., SERIALCMD_FROMSTRING, SERIALCMD_FROMALL, SERIALCMD_FROMSERIAL). - **function** (void (*)()) - A pointer to the function to be called when the command is received. ### Returns - uint8_t - 1 if the command was added successfully, 0 otherwise. ### Example ```c ret = mySerCmd.AddCmd( F ( "LEDON" ), SERIALCMD_FROMALL, set_LedOn ); ``` ``` -------------------------------- ### Register Commands with AddCmd() Source: https://context7.com/gpb01/serialcmd/llms.txt Registers command strings, their allowed sources, and callback functions. Existing commands are updated. Returns 1 on success, 0 if the command table is full. Use `F()` macro for PROGMEM on AVR. ```cpp uint8_t AddCmd ( const char *command, char allowedSource, void (*function)() ); // AVR only: uint8_t AddCmd ( const __FlashStringHelper *command, char allowedSource, void (*function)() ); ``` ```cpp void cmd_LedOn () { digitalWrite(LED_BUILTIN, HIGH); } void cmd_LedOff () { digitalWrite(LED_BUILTIN, LOW); } void cmd_LedBlink () { char *p = mySerCmd.ReadNext(); // read the period parameter if (p == NULL) { mySerCmd.Print((char*)"ERROR: missing period\r\n"); return; } startBlink( strtoul(p, NULL, 10) ); // e.g. startBlink(500) for 500 ms half-period } ``` ```cpp void setup() { Serial.begin(9600); // Register commands valid from both serial and ReadString() mySerCmd.AddCmd("LEDON", SERIALCMD_FROMALL, cmd_LedOn); mySerCmd.AddCmd("LEDOF", SERIALCMD_FROMALL, cmd_LedOff); mySerCmd.AddCmd("LEDBL", SERIALCMD_FROMALL, cmd_LedBlink); // Register a command valid only from ReadString() (internal use) mySerCmd.AddCmd("RESET", SERIALCMD_FROMSTRING, cmd_Reset); // Register a command valid only from serial input mySerCmd.AddCmd("DIAG", SERIALCMD_FROMSERIAL, cmd_Diag); } ``` ```cpp // Disable LEDBL at runtime by setting its function to NULL void disableBlink() { mySerCmd.AddCmd("LEDBL", SERIALCMD_FROMALL, NULL); } // Re-enable LEDBL void enableBlink() { mySerCmd.AddCmd("LEDBL", SERIALCMD_FROMALL, cmd_LedBlink); } ``` ```cpp // AVR — store command string in PROGMEM with F() macro #ifdef __AVR__ void setupAVR() { mySerCmd.AddCmd( F("LEDON"), SERIALCMD_FROMALL, cmd_LedOn ); mySerCmd.AddCmd( F("LEDOF"), SERIALCMD_FROMALL, cmd_LedOff ); } #endif ``` -------------------------------- ### Print() Source: https://context7.com/gpb01/serialcmd/llms.txt Writes various data types to the stream the SerialCmd instance was initialized with. Supports multiple data types and AVR PROGMEM strings. ```APIDOC ## `Print()` — Write to the Bound Stream Overloaded method that writes any supported type to the same stream (serial port, file, etc.) that the SerialCmd instance was initialized with. Supported types: `String`, `char[]`, `char`, `unsigned char`, `int`, `unsigned int`, `long`, `unsigned long`, `float` (with optional decimal places), `double` (with optional decimal places). On AVR, a `const __FlashStringHelper*` overload accepts the `F()` macro to print strings from PROGMEM. ### Signatures (selection) ```cpp void Print ( String & ); void Print ( char[] ); void Print ( char ); void Print ( int ); void Print ( long ); void Print ( float, int numDec = 2 ); void Print ( double, int numDec = 2 ); // AVR only: void Print ( const __FlashStringHelper * ); ``` ### Examples ```cpp // Print a C string mySerCmd.Print( (char*) "OK\r\n" ); // Print an integer result int sensorVal = analogRead(A0); mySerCmd.Print( (char*) "ADC=" ); mySerCmd.Print( sensorVal ); // e.g. prints "512" mySerCmd.Print( (char*) "\r\n" ); // Print a float with 3 decimal places float voltage = sensorVal * (5.0 / 1023.0); mySerCmd.Print( (char*) "V=" ); mySerCmd.Print( voltage, 3 ); // e.g. prints "2.500" mySerCmd.Print( (char*) "\r\n" ); // Print an Arduino String object String msg = "Uptime: " + String(millis()) + " ms\r\n"; mySerCmd.Print( msg ); // AVR — print from PROGMEM to save SRAM #ifdef __AVR__ mySerCmd.Print( F("Device ready\r\n") ); #endif ``` ``` -------------------------------- ### Add Command with Flash String (AVR) Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Registers a command using a Flash String Helper for AVR architecture to reduce SRAM usage. Associates the command with a function. ```cpp ret = mySerCmd.AddCmd( F ( "LEDON" ), SERIALCMD_FROMALL, set_LedOn ); ``` -------------------------------- ### AddCmd() — Register or Reassign a Command Source: https://context7.com/gpb01/serialcmd/llms.txt Registers a command string, its allowed source, and a callback function pointer. If the command name already exists, it is updated. Returns 1 on success, 0 if the command table is full. An AVR-specific overload stores the string in PROGMEM to save SRAM. ```APIDOC ## `AddCmd()` — Register or Reassign a Command Registers a command string, its allowed source, and a callback function pointer. If the command name already exists in the table it is updated (source and function are replaced), enabling run-time enable/disable patterns. Returns `1` on success, `0` if the command table is full (`SERIALCMD_MAXCMDNUM` reached). On AVR, an `F()`-based overload stores the string in PROGMEM to save SRAM. ### Signatures ```cpp uint8_t AddCmd ( const char *command, char allowedSource, void (*function)() ); // AVR only: uint8_t AddCmd ( const __FlashStringHelper *command, char allowedSource, void (*function)() ); ``` ### Example: Register commands in setup(), disable/re-enable at runtime ```cpp void cmd_LedOn () { digitalWrite(LED_BUILTIN, HIGH); } void cmd_LedOff () { digitalWrite(LED_BUILTIN, LOW); } void cmd_LedBlink () { char *p = mySerCmd.ReadNext(); // read the period parameter if (p == NULL) { mySerCmd.Print((char*)"ERROR: missing period\r\n"); return; } startBlink( strtoul(p, NULL, 10) ); // e.g. startBlink(500) for 500 ms half-period } void setup() { Serial.begin(9600); // Register commands valid from both serial and ReadString() mySerCmd.AddCmd("LEDON", SERIALCMD_FROMALL, cmd_LedOn); mySerCmd.AddCmd("LEDOF", SERIALCMD_FROMALL, cmd_LedOff); mySerCmd.AddCmd("LEDBL", SERIALCMD_FROMALL, cmd_LedBlink); // Register a command valid only from ReadString() (internal use) mySerCmd.AddCmd("RESET", SERIALCMD_FROMSTRING, cmd_Reset); // Register a command valid only from serial input mySerCmd.AddCmd("DIAG", SERIALCMD_FROMSERIAL, cmd_Diag); } // Disable LEDBL at runtime by setting its function to NULL void disableBlink() { mySerCmd.AddCmd("LEDBL", SERIALCMD_FROMALL, NULL); } // Re-enable LEDBL void enableBlink() { mySerCmd.AddCmd("LEDBL", SERIALCMD_FROMALL, cmd_LedBlink); } // AVR — store command string in PROGMEM with F() macro #ifdef __AVR__ void setupAVR() { mySerCmd.AddCmd( F("LEDON"), SERIALCMD_FROMALL, cmd_LedOn ); mySerCmd.AddCmd( F("LEDOF"), SERIALCMD_FROMALL, cmd_LedOff ); } #endif ``` ``` -------------------------------- ### ReadNext() Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Retrieves the next parameter associated with a command. Returns a pointer to the parameter string, or NULL if no more parameters are available. Typically used within the function called by a registered command. ```APIDOC ## ReadNext ### Description Returns the address of the string that contains the next parameter. This is useful for retrieving arguments passed to a command. ### Returns - char* - A pointer to the next parameter string, or NULL if no more parameters exist. ### Example ```c char * cPar; ... ... cPar = mySerCmd.ReadNext( ); ``` ``` -------------------------------- ### Log Unrecognized Commands with lastLine Source: https://context7.com/gpb01/serialcmd/llms.txt Enable the `lastLine` buffer to store raw input for logging unrecognized commands. This requires setting `SERIALCMD_PUBBUFFER` to 1 in `SerialCmd.h`. ```cpp #define SERIALCMD_PUBBUFFER 1 // In your sketch: SerialCmd mySerCmd( Serial ); void loop() { int8_t ret = mySerCmd.ReadSer(); if ( ret == 0 ) { // Command not in the table — log the raw line mySerCmd.Print( (char*) "Unknown command: [" ); mySerCmd.Print( mySerCmd.lastLine ); // e.g. "HELLO,WORLD" mySerCmd.Print( (char*) "]\r\n" ); } } ``` -------------------------------- ### AddCmd (const char *command, char allowedSource, void (*function)()) Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Adds a command to the recognized list and associates it with a function. Supports redefining commands from version 1.1.3 onwards. The allowedSource parameter specifies where the command can originate from (e.g., SERIALCMD_FROMALL). Returns 1 if the command was added successfully, 0 otherwise. ```APIDOC ## AddCmd ### Description Adds a command to the list of recognized commands and defines which function should be called when the command is received. Supports redefining commands. ### Parameters - **command** (const char *) - The command string to register. - **allowedSource** (char) - Specifies the allowed sources for the command (e.g., SERIALCMD_FROMSTRING, SERIALCMD_FROMALL, SERIALCMD_FROMSERIAL). - **function** (void (*)()) - A pointer to the function to be called when the command is received. ### Returns - uint8_t - 1 if the command was added successfully, 0 otherwise. ### Example ```c ret = mySerCmd.AddCmd( "LEDON", SERIALCMD_FROMALL, set_LedOn ); ``` ``` -------------------------------- ### SerialCmd Configuration Constants Source: https://context7.com/gpb01/serialcmd/llms.txt Adjust these preprocessor directives in SerialCmd.h before compilation to tune SRAM usage and behavior. Constants control maximum command/buffer lengths, case sensitivity, and source validity. ```cpp #define SERIALCMD_FORCEUC 0 // 1 = force command identifier to uppercase (parameters unaffected) #define SERIALCMD_MAXCMDNUM 8 // maximum number of distinct commands that can be registered #define SERIALCMD_MAXCMDLNG 6 // maximum length (chars) of a command identifier, e.g. "LEDBL" #define SERIALCMD_MAXBUFFER 30 // maximum length (chars) of the full input line incl. parameters // Set to 1 to enable the public `lastLine` variable (full raw line before parsing) #define SERIALCMD_PUBBUFFER 0 // --- Source validity constants (used in AddCmd) --- #define SERIALCMD_FROMSTRING -1 // command valid only when triggered by ReadString() #define SERIALCMD_FROMALL 0 // command valid from both serial and ReadString() #define SERIALCMD_FROMSERIAL 1 // command valid only when triggered by ReadSer() // --- Terminator characters (used in constructor) --- #define SERIALCMD_CR 0x0D // Carriage Return (default) #define SERIALCMD_LF 0x0A // Line Feed #define SERIALCMD_NULL 0x00 // NULL byte // --- Parameter separator strings (used in constructor) --- #define SERIALCMD_COMMA "," #define SERIALCMD_SEMICOL ";" #define SERIALCMD_DOT "." #define SERIALCMD_SPACE " " // --- Version macros --- #define SERIALCMD_VER "1.1.5" #define SERIALCMD_VER_NUM 10105 #define SERIALCMD_VER_MAJ 1 #define SERIALCMD_VER_MIN 1 #define SERIALCMD_VER_REV 5 ``` -------------------------------- ### Print() Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Sends data to the associated serial port. Supports various data types including strings, characters, integers, and floating-point numbers. On AVR architecture, it supports using the F() macro for constant strings to save SRAM. ```APIDOC ## Print ### Description Sends data to the serial port. Can transmit various data types including strings, characters, integers, and floating-point numbers. Supports F() macro for constant strings on AVR MCUs. ### Parameters - Accepts various data types: String, char*, char, unsigned char, int, unsigned int, long, unsigned long, float, double. ### Example ```c mySerCmd.Print( (char *) "This is a message \r\n" ); // AVR specific example: mySerCmd.Print( F ( "This is a message \r\n" ) ); ``` ``` -------------------------------- ### Print() - Write to Bound Stream Source: https://context7.com/gpb01/serialcmd/llms.txt Write various data types to the stream SerialCmd was initialized with. Supports String, char arrays, numbers, floats, and doubles. On AVR, can print PROGMEM strings using F(). ```cpp // Signatures (selection) void Print ( String & ); void Print ( char[] ); void Print ( char ); void Print ( int ); void Print ( long ); void Print ( float, int numDec = 2 ); void Print ( double, int numDec = 2 ); // AVR only: void Print ( const __FlashStringHelper * ); ``` ```cpp // --- Examples --- // Print a C string mySerCmd.Print( (char*) "OK\r\n" ); ``` ```cpp // Print an integer result int sensorVal = analogRead(A0); mySerCmd.Print( (char*) "ADC=" ); mySerCmd.Print( sensorVal ); // e.g. prints "512" mySerCmd.Print( (char*) "\r\n" ); ``` ```cpp // Print a float with 3 decimal places float voltage = sensorVal * (5.0 / 1023.0); mySerCmd.Print( (char*) "V=" ); mySerCmd.Print( voltage, 3 ); // e.g. prints "2.500" mySerCmd.Print( (char*) "\r\n" ); ``` ```cpp // Print an Arduino String object String msg = "Uptime: " + String(millis()) + " ms\r\n"; mySerCmd.Print( msg ); ``` ```cpp // AVR — print from PROGMEM to save SRAM #ifdef __AVR__ mySerCmd.Print( F("Device ready\r\n") ); #endif ``` -------------------------------- ### Add Command with String Literal Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Registers a command with a string literal and associates it with a function. The command can be triggered from any source. ```cpp ret = mySerCmd.AddCmd( "LEDON", SERIALCMD_FROMALL, set_LedOn ); ``` -------------------------------- ### Define Command Function Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Defines the function that will be executed when a specific command is received. ```cpp void set_LedOn ( void ) { ... } ``` -------------------------------- ### ReadString(char *theCmd, uint8_t fValidate = false) Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Simulates receiving a command from the serial line. The provided string is processed as if it were sent via serial. The optional fValidate parameter, when true, checks for command existence without executing the associated function. Returns 1 if the command was recognized, 0 otherwise. ```APIDOC ## ReadString ### Description Sends a command to the application as if it had been received from the serial line. The optional fValidate parameter allows checking for command existence without execution. ### Parameters - **theCmd** (char *) - The command string to process, including any parameters. - **fValidate** (uint8_t) - Optional. If true, only validates the command without executing its associated function. Defaults to false. ### Returns - int8_t - 1 if the command was recognized, 0 otherwise. ### Example ```c ret = mySerCmd.ReadString ( (char *) "LEDON" ); ``` ``` -------------------------------- ### SerialCmd Customization Defines Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Customize SerialCmd behavior with these #define values. Options include forcing uppercase commands, specifying the data source, setting the terminator character, and defining the parameter separator. ```c++ #define SERIALCMD_FORCEUC 0 // If set to 1 force uppercase for serial command #define SERIALCMD_FROMSTRING -1 // Valid only as ReadString command #define SERIALCMD_FROMALL 0 // Always valid #define SERIALCMD_FROMSERIAL 1 // Valid only as ReadSer command #define SERIALCMD_CR 0x0D // Carriage Return (char) #define SERIALCMD_LF 0x0A // Line Feed (char) #define SERIALCMD_NULL 0x00 // NULL (char) #define SERIALCMD_COMMA "," // COMMA (C string) #define SERIALCMD_SEMICOL ";" // SEMI COLUMN (C string) #define SERIALCMD_DOT "." // DOT (C string) #define SERIALCMD_SPACE " " // SPACE (C string) ``` -------------------------------- ### ReadString() Source: https://context7.com/gpb01/serialcmd/llms.txt Processes a command string as if it arrived over the serial port, executing the associated callback. Can optionally validate commands without execution. ```APIDOC ## `ReadString()` — Inject a Command Programmatically Processes a command string as if it had arrived over the serial port, executing the associated callback. The optional `fValidate` flag (default `false`) performs only a lookup — returning `1` if the command exists — without calling the function, useful for validating HTTP or other external inputs before executing. Returns `1` if the command was recognized (and executed when `fValidate` is `false`), `0` otherwise. ### Signatures ```cpp // Standard signature int8_t ReadString ( char * theCmd, uint8_t fValidate = false ); // AVR only: int8_t ReadString ( const __FlashStringHelper * theCmd, uint8_t fValidate = false ); ``` ### Example Usage ```cpp // --- 1. Trigger a command from application logic --- void loop() { if ( blinkCount >= 10 ) { blinkCount = 0; int8_t ret = mySerCmd.ReadString( (char*) "LEDOF" ); if ( ret == 0 ) Serial.println("LEDOF not registered — check AddCmd"); } mySerCmd.ReadSer(); } // --- 2. Pass parameters the same way a serial user would --- int8_t ret = mySerCmd.ReadString( (char*) "LEDBL,500" ); // → calls cmd_LedBlink(); ReadNext() inside returns "500" // --- 3. Validate without executing (e.g. incoming HTTP GET command) --- // Extract command string from "GET /LEDON HTTP/1.1" char clientCmd[32] = "LEDON"; int8_t isKnown = mySerCmd.ReadString( clientCmd, true ); // fValidate = true if ( isKnown == 1 ) { mySerCmd.ReadString( clientCmd ); // now execute it } else { Serial.println("HTTP: command not recognized"); } // --- 4. AVR: command string in PROGMEM --- #ifdef __AVR__ mySerCmd.ReadString( F("LEDOF") ); #endif ``` ``` -------------------------------- ### Poll Serial Port with ReadSer() Source: https://context7.com/gpb01/serialcmd/llms.txt Call repeatedly in loop() to read serial data, buffer it, and dispatch callbacks. Returns 1 if command executed, 0 if line complete but no match, -1 if line incomplete. ```cpp int8_t ReadSer ( void ); ``` ```cpp void loop() { int8_t ret = mySerCmd.ReadSer(); if ( ret == 1 ) { // command recognized and callback was called — nothing extra needed } else if ( ret == 0 ) { // complete line received but command not in the table mySerCmd.Print( (char*) "ERROR: Unrecognized command\r\n" ); #if (SERIALCMD_PUBBUFFER == 1) // print the raw line that was not recognized (requires PUBBUFFER = 1) mySerCmd.Print( (char*) " received: " ); mySerCmd.Print( mySerCmd.lastLine ); mySerCmd.Print( (char*) "\r\n" ); #endif } // ret == -1 means still waiting for more data — no action needed } ``` ```cpp // SerialCmd bound to a File object; ReadSer() reads bytes from the file // exactly as it would from a serial port. void setup() { SD.begin(10); dataFile = SD.open("BLINK.TXT", FILE_READ); mySerCmd.AddCmd("LEDON", SERIALCMD_FROMALL, cmd_LedOn); mySerCmd.AddCmd("LEDOF", SERIALCMD_FROMALL, cmd_LedOff); mySerCmd.AddCmd("DELAY", SERIALCMD_FROMALL, cmd_Delay); while ( dataFile.available() ) { int8_t r = mySerCmd.ReadSer(); if ( r == 0 ) Serial.println("Unknown command in file"); } dataFile.close(); } ``` -------------------------------- ### ReadString Function for AVR Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Use this function on AVR architecture to simulate receiving a command from the serial line. The optional fValidate parameter can be used to check command existence without execution. ```cpp ret = mySerCmd.ReadString ( F ( "LEDON" ) ); ``` -------------------------------- ### Read and Process Serial Input Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Continuously reads and interprets commands from the serial port within the main loop. Returns status codes for command recognition. ```cpp void setup( ) { ... ... } void loop( ) { ret = mySerCmd.ReadSer( ); ... ... } ``` -------------------------------- ### ReadString() - Inject Command Programmatically Source: https://context7.com/gpb01/serialcmd/llms.txt Process a command string as if received over serial. The optional fValidate flag checks for command existence without execution. Useful for validating external inputs. Returns 1 if the command is recognized. ```cpp // Signatures int8_t ReadString ( char * theCmd, uint8_t fValidate = false ); // AVR only: int8_t ReadString ( const __FlashStringHelper * theCmd, uint8_t fValidate = false ); ``` ```cpp // --- 1. Trigger a command from application logic --- void loop() { if ( blinkCount >= 10 ) { blinkCount = 0; int8_t ret = mySerCmd.ReadString( (char*) "LEDOF" ); if ( ret == 0 ) Serial.println("LEDOF not registered — check AddCmd"); } mySerCmd.ReadSer(); } ``` ```cpp // --- 2. Pass parameters the same way a serial user would --- int8_t ret = mySerCmd.ReadString( (char*) "LEDBL,500" ); // → calls cmd_LedBlink(); ReadNext() inside returns "500" ``` ```cpp // --- 3. Validate without executing (e.g. incoming HTTP GET command) --- // Extract command string from "GET /LEDON HTTP/1.1" char clientCmd[32] = "LEDON"; int8_t isKnown = mySerCmd.ReadString( clientCmd, true ); // fValidate = true if ( isKnown == 1 ) { mySerCmd.ReadString( clientCmd ); // now execute it } else { Serial.println("HTTP: command not recognized"); } ``` ```cpp // --- 4. AVR: command string in PROGMEM --- #ifdef __AVR__ mySerCmd.ReadString( F("LEDOF") ); #endif ``` -------------------------------- ### Print Data to Serial Port Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Sends various data types (String, char*, numbers, floats) to the serial port. Supports F() macro for constant strings on AVR. ```cpp mySerCmd.Print( (char *) "This is a message \r\n" ); ``` ```cpp mySerCmd.Print( F ( "This is a message \r\n" ) ); ``` -------------------------------- ### ReadSer() — Poll Serial Port for Commands Source: https://context7.com/gpb01/serialcmd/llms.txt Must be called repeatedly inside `loop()`. Reads all available bytes from the stream, buffers them, and dispatches the registered callback when the terminator character is found. Returns 1 if a command was recognized and executed, 0 if the line was complete but no matching command was found, or -1 if no complete line has been received yet. ```APIDOC ## `ReadSer()` — Poll Serial Port for Commands Must be called repeatedly inside `loop()`. Reads all available bytes from the stream, buffers them, and dispatches the registered callback when the terminator character is found. Returns `1` if a command was recognized and executed, `0` if the line was complete but no matching command was found, or `-1` if no complete line has been received yet (terminator not yet seen). ### Signature ```cpp int8_t ReadSer ( void ); ``` ### Typical usage in loop() ```cpp void loop() { int8_t ret = mySerCmd.ReadSer(); if ( ret == 1 ) { // command recognized and callback was called — nothing extra needed } else if ( ret == 0 ) { // complete line received but command not in the table mySerCmd.Print( (char*) "ERROR: Unrecognized command\r\n" ); #if (SERIALCMD_PUBBUFFER == 1) // print the raw line that was not recognized (requires PUBBUFFER = 1) mySerCmd.Print( (char*) " received: " ); mySerCmd.Print( mySerCmd.lastLine ); mySerCmd.Print( (char*) "\r\n" ); #endif } // ret == -1 means still waiting for more data — no action needed } ``` ### Reading commands from an SD card file (stream-agnostic) *SerialCmd bound to a File object; ReadSer() reads bytes from the file exactly as it would from a serial port.* ```cpp void setup() { SD.begin(10); dataFile = SD.open("BLINK.TXT", FILE_READ); mySerCmd.AddCmd("LEDON", SERIALCMD_FROMALL, cmd_LedOn); mySerCmd.AddCmd("LEDOF", SERIALCMD_FROMALL, cmd_LedOff); mySerCmd.AddCmd("DELAY", SERIALCMD_FROMALL, cmd_Delay); while ( dataFile.available() ) { int8_t r = mySerCmd.ReadSer(); if ( r == 0 ) Serial.println("Unknown command in file"); } dataFile.close(); } ``` ``` -------------------------------- ### Memory Optimization Defines for SerialCmd Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Configure memory usage for the SerialCmd library by adjusting these #define values. They control the maximum number of commands, command length, buffer size, and public buffer usage. ```c++ #define SERIALCMD_MAXCMDNUM 8 // Max Number of Command #define SERIALCMD_MAXCMDLNG 6 // Max Command Length #define SERIALCMD_MAXBUFFER 30 // Max Buffer Length #define SERIALCMD_PUBBUFFER 0 // If set to 1 create a public double buffer ``` -------------------------------- ### ReadSer() Source: https://github.com/gpb01/serialcmd/blob/main/ReadMe.md Continuously called within the main loop to receive and interpret commands from the serial port. Returns -1 if the terminator character has not been encountered, 0 if the command is not recognized, and 1 if the command is recognized. ```APIDOC ## ReadSer ### Description Must be called continuously within the loop() function to receive and interpret commands from the serial port. It processes incoming serial data to identify commands. ### Returns - int8_t - -1: Terminator character not yet encountered. - int8_t - 0: Command not recognized. - int8_t - 1: Command recognized. ### Example ```c void loop( ) { ret = mySerCmd.ReadSer( ); ... ... } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.