### Basic SerialConsole Setup and Usage Source: https://github.com/actuvon/serialconsole/blob/main/README.md Includes necessary headers, creates a SerialConsole object, initializes serial communication, adds a custom command, and sets up the main loop to listen for console input. ```cpp #include SerialConsole console; // Create a SerialConsole object void setup(){ Serial.begin(9600); /* ...some other setup stuff... */ // Add commands to the SerialConsole like this... console.AddCommand("setpin", cmd_setpin, "Turn a digital output pin on or off"); // This lets the user type in "setpin 13 1" in the Serial Monitor, and then the builtin LED will turn on! // The description at the end shows up if you type "help setpin". } void loop(){ console.Listen(); // This goes in your main loop to process your commands } // Your commands call functions, which are set up like this... void cmd_setpin(){ // Check if you have the right number of arguments if(console.ArgCount != 3) Serial.println("ERROR: Must specify a pin and a state to set it to."); // Arguments[0] is the command name ("setpin") int arg1 = String(console.Arguments[1]).toInt(); // Arguments[1] is the first argument (e.g., "13") int arg2 = String(console.Arguments[2]).toInt(); // Arguments[2] is the second argument (e.g., "1") if(arg1 > 0) pinMode(arg1, OUTPUT); else Serial.println("ERROR: The pin can not be negative."); if(arg2 == 0 || arg2 == 1) digitalWrite(arg1, arg2); else Serial.println("ERROR: Must set the pin to either 0 or 1."); } ``` -------------------------------- ### Built-in Help Command Example Source: https://context7.com/actuvon/serialconsole/llms.txt Demonstrates how SerialConsole automatically handles the 'help' command for listing registered commands and their descriptions. No explicit registration is needed for the basic help functionality. ```cpp #include SerialConsole console; void setup() { Serial.begin(9600); while (!Serial); console.AddCommand("ping", cmd_ping, "Check if the device is alive."); console.AddCommand("reset", cmd_reset, "Perform a software reset.\nWARNING: all state is lost."); console.AddCommand("silent", cmd_silent); // No help message registered } void loop() { console.Listen(); } void cmd_ping() { Serial.println("pong"); } void cmd_reset() { Serial.println("Resetting..."); /* asm volatile(\"jmp 0\"); */ } void cmd_silent() { Serial.println("(does something quietly)"); } /* >> help Type help for help on a specific command. Available commands: - ping - reset - silent >> help reset Perform a software reset. WARNING: all state is lost. >> help silent SerialConsole: No help message available for command: silent >> unknowncmd SerialConsole: Command "unknowncmd" not recognized. Type help for help on a specific command. Available commands: - ping - reset - silent */ ``` -------------------------------- ### Stream-Agnostic I/O with SoftwareSerial Source: https://context7.com/actuvon/serialconsole/llms.txt Configures SerialConsole to use a SoftwareSerial port for communication, allowing interaction over interfaces like Bluetooth modules. This example also shows how to run multiple independent console instances. ```cpp #include #include SoftwareSerial btSerial(10, 11); // RX, TX for HC-05 Bluetooth module SerialConsole btConsole( ([]() { SerialConsoleConfig cfg(btSerial); // Redirect all I/O to btSerial cfg.numCommands = 4; cfg.maxFullLineLength = 30; return cfg; })() ); // Optionally keep a second console on hardware Serial for local debug SerialConsole localConsole; void setup() { Serial.begin(9600); btSerial.begin(9600); btConsole.AddCommand("temp", cmd_temp, "Read temperature"); btConsole.AddCommand("led", cmd_led, "Toggle LED"); localConsole.AddCommand("debug", cmd_debug, "Print debug info"); } void loop() { btConsole.Listen(); // handles Bluetooth serial input localConsole.Listen(); // handles USB serial input independently } void cmd_temp() { btSerial.println("23.4 C"); } void cmd_debug() { Serial.println("Debug info here"); } bool ls = false; void cmd_led() { ls = !ls; digitalWrite(LED_BUILTIN, ls); } /* Bluetooth terminal: >> temp 23.4 C >> led ← toggles LED USB Serial Monitor: >> debug Debug info here */ ``` -------------------------------- ### Add Command to SerialConsole Source: https://github.com/actuvon/serialconsole/blob/main/README.md Demonstrates how to bind a custom command to the SerialConsole. The description string is shown when 'help ' is invoked. ```cpp console.AddCommand("LED", cmd_LED, "Toggle the builtin LED."); ``` -------------------------------- ### SerialConsole Default Constructor (Line Mode) Source: https://context7.com/actuvon/serialconsole/llms.txt Creates a console instance with default settings for the Arduino IDE Serial Monitor. Expects full lines terminated with \n or \r and echoes received commands. ```cpp #include SerialConsole console; // Default: Line Mode, Serial stream, 6 commands, 40 char max line void setup() { Serial.begin(9600); while (!Serial); // Register commands: trigger string, handler function, optional help message console.AddCommand("hello", cmd_hello, "Greet your Arduino!"); console.AddCommand("led", cmd_LED, "Toggle the built-in LED"); console.AddCommand("setpin", cmd_setpin, "Set a digital pin\nsetpin <0 or 1>"); } void loop() { console.Listen(); // Non-blocking poll — call every loop iteration } void cmd_hello() { Serial.println("Why, hello there!"); } bool ledState = false; void cmd_LED() { ledState = !ledState; digitalWrite(LED_BUILTIN, ledState); Serial.println("Toggling the LED"); } void cmd_setpin() { // Arguments[0] = command name, [1] = first arg, [2] = second arg if (console.ArgCount != 3) { Serial.println("ERROR: Must specify a pin and a state."); return; } int pin = String(console.Arguments[1]).toInt(); int state = String(console.Arguments[2]).toInt(); if (pin <= 0) { Serial.println("ERROR: Pin cannot be negative."); return; } if (state != 0 && state != 1) { Serial.println("ERROR: State must be 0 or 1."); return; } pinMode(pin, OUTPUT); digitalWrite(pin, state); } /* Serial Monitor session (baud 9600, "New Line" line ending): >> hello Why, hello there! >> setpin 13 1 <- turns pin 13 HIGH >> help setpin Set a digital pin setpin <0 or 1> >> foo SerialConsole: Command "foo" not recognized. Available commands: - hello - led - setpin */ ``` -------------------------------- ### SerialConsole(PuttyMode()) - PuTTY / Terminal Emulator Constructor Source: https://context7.com/actuvon/serialconsole/llms.txt Initializes the SerialConsole using the `PuttyMode()` helper, which configures the console for character-by-character terminals like PuTTY or VSCode Serial Monitor. This mode provides live backspace and per-character echo. ```APIDOC ## SerialConsole(PuttyMode()) — PuTTY / terminal emulator constructor ### Description `PuttyMode()` is a helper function that returns a `SerialConsoleConfig` pre-configured for character-by-character terminal emulators (PuTTY, VSCode Serial Monitor, screen, minicom). It enables per-character echo, live backspace support, and the `>>` ready prompt, while disabling full-line echo. ### Usage Example ```cpp #include // Single line to switch to PuTTY / VSCode terminal mode SerialConsole console(PuttyMode()); // PuttyMode on a different stream (e.g., Serial1): // SerialConsole console(PuttyMode(Serial1)); void setup() { Serial.begin(9600); while (!Serial); pinMode(LED_BUILTIN, OUTPUT); console.AddCommand("hello", cmd_hello, "Say hello"); console.AddCommand("led", cmd_LED, "Toggle built-in LED"); console.AddCommand("setpin", cmd_setpin, "setpin <0|1>"); } void loop() { console.Listen(); } void cmd_hello() { Serial.println("Why, hello there!"); } bool ledState = false; void cmd_LED() { ledState = !ledState; digitalWrite(LED_BUILTIN, ledState); Serial.println("Toggling the LED"); } void cmd_setpin() { if (console.ArgCount != 3) { Serial.println("ERROR: need pin and state"); return; } int pin = String(console.Arguments[1]).toInt(); int state = String(console.Arguments[2]).toInt(); if (pin > 0 && (state == 0 || state == 1)) { pinMode(pin, OUTPUT); digitalWrite(pin, state); } } ``` ### Session Example (PuTTY) ``` PuTTY session (characters appear as typed, backspace works): >> <- prompt shown when ready >> hello <- typed character-by-character with live echo Why, hello there! >> setpin 13 1 ``` ``` -------------------------------- ### Listen() - Poll and dispatch commands Source: https://context7.com/actuvon/serialconsole/llms.txt Call Listen() in your main loop to process incoming serial commands. It automatically handles command tokenization, dispatching, and built-in help. ```cpp #include SerialConsole console; // Sensor simulation float readTemperature() { return 23.4 + (millis() % 10) * 0.1; } void setup() { Serial.begin(9600); while (!Serial); console.AddCommand("temp", cmd_temp, "Read temperature sensor"); console.AddCommand("blink", cmd_blink, "Blink LED n times: blink "); console.AddCommand("echo", cmd_echo, "Echo back all arguments: echo "); } void loop() { console.Listen(); // Must be called every loop — non-blocking // Other non-blocking work can go here freely } void cmd_temp() { Serial.print("Temperature: "); Serial.print(readTemperature()); Serial.println(" C"); } void cmd_blink() { int count = (console.ArgCount >= 2) ? String(console.Arguments[1]).toInt() : 3; for (int i = 0; i < count; i++) { digitalWrite(LED_BUILTIN, HIGH); delay(200); digitalWrite(LED_BUILTIN, LOW); delay(200); } Serial.print("Blinked "); Serial.print(count); Serial.println(" times."); } void cmd_echo() { // Arguments[0] = "echo", subsequent entries are the user's words for (int i = 1; i < console.ArgCount; i++) { Serial.print(console.Arguments[i]); if (i < console.ArgCount - 1) Serial.print(" "); } Serial.println(); } /* >> temp Temperature: 23.7 C >> blink 5 Blinked 5 times. >> echo hello world hello world >> help Type help for help on a specific command. Available commands: - temp - blink - echo */ ``` -------------------------------- ### SerialConsole PuTTY / Terminal Emulator Constructor Source: https://context7.com/actuvon/serialconsole/llms.txt Configures the console for character-by-character terminal emulators like PuTTY or VSCode. Enables per-character echo, live backspace, and a ready prompt, while disabling full-line echo. ```cpp #include // Single line to switch to PuTTY / VSCode terminal mode SerialConsole console(PuttyMode()); // PuttyMode on a different stream (e.g., Serial1): // SerialConsole console(PuttyMode(Serial1)); void setup() { Serial.begin(9600); while (!Serial); pinMode(LED_BUILTIN, OUTPUT); console.AddCommand("hello", cmd_hello, "Say hello"); console.AddCommand("led", cmd_LED, "Toggle built-in LED"); console.AddCommand("setpin", cmd_setpin, "setpin <0|1>"); } void loop() { console.Listen(); } void cmd_hello() { Serial.println("Why, hello there!"); } bool ledState = false; void cmd_LED() { ledState = !ledState; digitalWrite(LED_BUILTIN, ledState); Serial.println("Toggling the LED"); } void cmd_setpin() { if (console.ArgCount != 3) { Serial.println("ERROR: need pin and state"); return; } int pin = String(console.Arguments[1]).toInt(); int state = String(console.Arguments[2]).toInt(); if (pin > 0 && (state == 0 || state == 1)) { pinMode(pin, OUTPUT); digitalWrite(pin, state); } } /* PuTTY session (characters appear as typed, backspace works): >> <- prompt shown when ready >> hello <- typed character-by-character with live echo Why, hello there! >> setpin 13 1 */ ``` -------------------------------- ### AddCommand() Source: https://context7.com/actuvon/serialconsole/llms.txt Registers a command with the serial console, associating a keyword with a handler function and an optional help message. ```APIDOC ## `AddCommand()` — Register a command Registers a trigger keyword, a zero-argument handler function, and an optional help string with the console. The console checks that the command slot limit (`numCommands`) has not been reached; if it has, it prints an error to the stream and ignores the registration. ```cpp #include SerialConsole console; void setup() { Serial.begin(9600); // Minimal registration — no help message console.AddCommand("reboot", cmd_reboot); // With a multi-line help message (shown via "help status") console.AddCommand("status", cmd_status, "Print system status.\nshows uptime, free RAM, and sensor readings."); // Attempting to add more commands than numCommands (default 6) prints an error: // "SerialConsole: ERROR: Could not add command because the SerialConsole is already full!" } void loop() { console.Listen(); } void cmd_reboot() { Serial.println("Rebooting..."); // platform-specific reset } void cmd_status() { Serial.print("Uptime (ms): "); Serial.println(millis()); Serial.print("Free RAM: "); // freeMemory() requires MemoryFree library on AVR Serial.println("(platform dependent)"); } /* >> help status Print system status. shows uptime, free RAM, and sensor readings. >> status Uptime (ms): 14302 Free RAM: (platform dependent) */ ``` ``` -------------------------------- ### SerialConsole(SerialConsoleConfig) Source: https://context7.com/actuvon/serialconsole/llms.txt Custom configuration constructor for SerialConsole. Allows fine-grained control over memory usage, parsing, timing, display, and stream selection. ```APIDOC ## `SerialConsole(SerialConsoleConfig)` — Custom configuration constructor `SerialConsoleConfig` exposes every tunable parameter of the console as plain struct fields. Construct one, modify only what you need, and pass it to the `SerialConsole` constructor. This is the primary mechanism for controlling RAM usage, changing delimiters and terminators, adjusting poll rate, and customizing the prompt string. ```cpp #include // Build a config using an immediately-invoked lambda for clean scoping SerialConsole console( ([]() { SerialConsoleConfig cfg; // --- Memory / capacity --- cfg.numCommands = 15; // max commands (default 6) cfg.maxFullLineLength = 60; // max chars per full command line incl. args (default 40) cfg.maxNumArgs = 8; // max arguments per command (default 5) // --- Parsing --- cfg.cmdTerminator1 = '|'; // end-of-command char #1 (default '\n') cfg.cmdTerminator2 = ';'; // end-of-command char #2 (default '\r') cfg.delimiter = '-'; // argument separator (default ' ') // --- Timing --- cfg.scanPeriod_ms = 100; // minimum ms between Listen() polls (default 250) // --- Display --- cfg.inputPrompter = "\n> "; // prompt string (default "\n>> ") cfg.echoFullCommand = true; // echo full line after terminator (default true) // cfg.echoIndividualChars = true; // per-char echo for terminal emulators // cfg.showPromptWhenReady = true; // show prompt when idle // --- Stream --- // cfg.IO_Stream = Serial1; // use any Arduino Stream (default Serial) return cfg; })() ); void setup() { Serial.begin(9600); while (!Serial); console.AddCommand("add", cmd_add, "Add two numbers: add--"); console.AddCommand("led", cmd_LED, "Toggle LED"); } void loop() { console.Listen(); } bool ledState = false; void cmd_LED() { ledState = !ledState; digitalWrite(LED_BUILTIN, ledState); } void cmd_add() { // With delimiter '-', "add-3.5-2.1" yields Arguments[1]="3.5", Arguments[2]="2.1" if (console.ArgCount != 3) { Serial.println("Usage: add--"); return; } float a = String(console.Arguments[1]).toFloat(); float b = String(console.Arguments[2]).toFloat(); Serial.print(a); Serial.print(" + "); Serial.print(b); Serial.print(" = "); Serial.println(a + b); } /* Serial Monitor (No Line Ending, custom terminator): type: add-3.5-2.1| output: 3.50 + 2.10 = 5.60 type: led; output: (LED toggles) */ ``` ``` -------------------------------- ### SerialConsole() - Default Constructor (Line Mode) Source: https://context7.com/actuvon/serialconsole/llms.txt Initializes the SerialConsole with default settings, optimized for the Arduino IDE Serial Monitor. It expects full lines terminated by newline characters and provides basic command handling. ```APIDOC ## SerialConsole() - Default constructor (Line Mode) ### Description Creates a console instance using all default configuration values, optimized for the Arduino IDE Serial Monitor. The console expects full lines terminated with `\n` or `\r`, echoes the received command back with a `>>` prefix, and allocates fixed buffers for up to 6 commands, 40-character lines, and 5 arguments. ### Usage Example ```cpp #include SerialConsole console; // Default: Line Mode, Serial stream, 6 commands, 40 char max line void setup() { Serial.begin(9600); while (!Serial); // Register commands: trigger string, handler function, optional help message console.AddCommand("hello", cmd_hello, "Greet your Arduino!"); console.AddCommand("led", cmd_LED, "Toggle the built-in LED"); console.AddCommand("setpin", cmd_setpin, "Set a digital pin\nsetpin <0 or 1>"); } void loop() { console.Listen(); // Non-blocking poll — call every loop iteration } void cmd_hello() { Serial.println("Why, hello there!"); } bool ledState = false; void cmd_LED() { ledState = !ledState; digitalWrite(LED_BUILTIN, ledState); Serial.println("Toggling the LED"); } void cmd_setpin() { // Arguments[0] = command name, [1] = first arg, [2] = second arg if (console.ArgCount != 3) { Serial.println("ERROR: Must specify a pin and a state."); return; } int pin = String(console.Arguments[1]).toInt(); int state = String(console.Arguments[2]).toInt(); if (pin <= 0) { Serial.println("ERROR: Pin cannot be negative."); return; } if (state != 0 && state != 1) { Serial.println("ERROR: State must be 0 or 1."); return; } pinMode(pin, OUTPUT); digitalWrite(pin, state); } ``` ### Session Example (Serial Monitor) ``` Serial Monitor session (baud 9600, "New Line" line ending): >> hello Why, hello there! >> setpin 13 1 <- turns pin 13 HIGH >> help setpin Set a digital pin setpin <0 or 1> >> foo SerialConsole: Command "foo" not recognized. Available commands: - hello - led - setpin ``` ``` -------------------------------- ### Configure SerialConsole with Custom Settings Source: https://context7.com/actuvon/serialconsole/llms.txt Use SerialConsoleConfig to tune parameters like command capacity, line length, terminators, delimiters, scan rate, prompt string, and echo behavior. Modify only the fields you need and pass the config to the SerialConsole constructor. ```cpp #include // Build a config using an immediately-invoked lambda for clean scoping SerialConsole console( ([]() { SerialConsoleConfig cfg; // --- Memory / capacity --- cfg.numCommands = 15; // max commands (default 6) cfg.maxFullLineLength = 60; // max chars per full command line incl. args (default 40) cfg.maxNumArgs = 8; // max arguments per command (default 5) // --- Parsing --- cfg.cmdTerminator1 = '|'; // end-of-command char #1 (default '\n') cfg.cmdTerminator2 = ';'; // end-of-command char #2 (default '\r') cfg.delimiter = '-'; // argument separator (default ' ') // --- Timing --- cfg.scanPeriod_ms = 100; // minimum ms between Listen() polls (default 250) // --- Display --- cfg.inputPrompter = "\n> "; // prompt string (default "\n>> ") cfg.echoFullCommand = true; // echo full line after terminator (default true) // cfg.echoIndividualChars = true; // per-char echo for terminal emulators // cfg.showPromptWhenReady = true; // show prompt when idle // --- Stream --- // cfg.IO_Stream = Serial1; // use any Arduino Stream (default Serial) return cfg; })() ); void setup() { Serial.begin(9600); while (!Serial); console.AddCommand("add", cmd_add, "Add two numbers: add--"); console.AddCommand("led", cmd_LED, "Toggle LED"); } void loop() { console.Listen(); } bool ledState = false; void cmd_LED() { ledState = !ledState; digitalWrite(LED_BUILTIN, ledState); } void cmd_add() { // With delimiter '-', "add-3.5-2.1" yields Arguments[1]="3.5", Arguments[2]="2.1" if (console.ArgCount != 3) { Serial.println("Usage: add--"); return; } float a = String(console.Arguments[1]).toFloat(); float b = String(console.Arguments[2]).toFloat(); Serial.print(a); Serial.print(" + "); Serial.print(b); Serial.print(" = "); Serial.println(a + b); } /* Serial Monitor (No Line Ending, custom terminator): type: add-3.5-2.1| output: 3.50 + 2.10 = 5.60 type: led; output: (LED toggles) */ ``` -------------------------------- ### Enable PuttyMode for SerialConsole Source: https://github.com/actuvon/serialconsole/blob/main/README.md Instantiate the SerialConsole object with PuttyMode() to enable character-by-character input processing suitable for real terminal emulators. ```cpp SerialConsole console(PuttyMode()); ``` -------------------------------- ### Register Commands with AddCommand Source: https://context7.com/actuvon/serialconsole/llms.txt Register commands with a trigger keyword, a handler function, and an optional help message. The console checks if the command slot limit (`numCommands`) is reached before registration. ```cpp #include SerialConsole console; void setup() { Serial.begin(9600); // Minimal registration — no help message console.AddCommand("reboot", cmd_reboot); // With a multi-line help message (shown via "help status") console.AddCommand("status", cmd_status, "Print system status.\nshows uptime, free RAM, and sensor readings."); // Attempting to add more commands than numCommands (default 6) prints an error: // "SerialConsole: ERROR: Could not add command because the SerialConsole is already full!" } void loop() { console.Listen(); } void cmd_reboot() { Serial.println("Rebooting..."); // platform-specific reset } void cmd_status() { Serial.print("Uptime (ms): "); Serial.println(millis()); Serial.print("Free RAM: "); // freeMemory() requires MemoryFree library on AVR Serial.println("(platform dependent)"); } /* >> help status Print system status. shows uptime, free RAM, and sensor readings. >> status Uptime (ms): 14302 Free RAM: (platform dependent) */ ``` -------------------------------- ### Arguments[] and ArgCount - Accessing parsed arguments Source: https://context7.com/actuvon/serialconsole/llms.txt Access command arguments via the `Arguments` array and count them using `ArgCount` after a command is dispatched. These are valid only during the command handler's execution. ```cpp #include SerialConsole console; void setup() { Serial.begin(9600); while (!Serial); console.AddCommand("color", cmd_color, "Set RGB LED: color (0-255 each)"); console.AddCommand("move", cmd_move, "Move servo: move "); } void loop() { console.Listen(); } // Simulated PWM pins const int R_PIN = 9, G_PIN = 10, B_PIN = 11; void cmd_color() { // ArgCount includes the command name, so "color 255 0 128" → ArgCount = 4 if (console.ArgCount != 4) { Serial.println("Usage: color "); return; } int r = constrain(String(console.Arguments[1]).toInt(), 0, 255); int g = constrain(String(console.Arguments[2]).toInt(), 0, 255); int b = constrain(String(console.Arguments[3]).toInt(), 0, 255); analogWrite(R_PIN, r); analogWrite(G_PIN, g); analogWrite(B_PIN, b); Serial.print("Color set to R:"); Serial.print(r); Serial.print(" G:"); Serial.print(g); Serial.print(" B:"); Serial.println(b); } void cmd_move() { if (console.ArgCount != 3) { Serial.println("Usage: move "); return; } int id = String(console.Arguments[1]).toInt(); int deg = constrain(String(console.Arguments[2]).toInt(), 0, 180); Serial.print("Servo "); Serial.print(id); Serial.print(" → "); Serial.print(deg); Serial.println("°"); // myServos[id].write(deg); } /* >> color 255 0 128 Color set to R:255 G:0 B:128 >> move 0 90 Servo 0 → 90° >> color 255 Usage: color */ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.