### Arduino Command Function Definition and Setup Source: https://github.com/joshmarinacci/cmdarduino/blob/master/README.md Defines example command functions ('left', 'right') and the setup function to initialize the serial port, the CmdArduino library, and register the command functions. Command functions must accept 'int arg_cnt, char ** args' and return 'void'. ```C++ void left(int arg_cnt, char **args) { LeftMotor->run(FORWARD); delay(200); LeftMotor->run(RELEASE); } void right(int arg_cnt, char **args) { RightMotor->run(FORWARD); delay(200); RightMotor->run(RELEASE); } void setup() { Serial.begin(9600); cmdInit(&Serial); cmdAdd('left',left); cmdAdd('right',right); } ``` -------------------------------- ### Complete Multi-Command Example with CmdArduino (C++) Source: https://context7.com/joshmarinacci/cmdarduino/llms.txt A comprehensive example showcasing how to register multiple commands ('hello', 'args', 'blink', 'pwm') within a single Arduino sketch using the CmdArduino library. It demonstrates handling different functionalities like printing messages, displaying arguments, controlling LED blinking, and setting PWM output based on received string commands. ```cpp #include int led_pin = 13; bool led_blink_enb = false; int led_blink_delay_time = 1000; int pwm_pin = 10; void setup() { pinMode(led_pin, OUTPUT); pinMode(pwm_pin, OUTPUT); Serial.begin(57600); cmdInit(&Serial); // Register all available commands cmdAdd("hello", hello); cmdAdd("args", arg_display); cmdAdd("blink", led_blink); cmdAdd("pwm", led_pwm); } void loop() { cmdPoll(); if (led_blink_enb) { digitalWrite(led_pin, HIGH); delay(led_blink_delay_time); digitalWrite(led_pin, LOW); delay(led_blink_delay_time); } } void hello(int arg_cnt, char **args) { cmdGetStream()->println("Hello world."); } void arg_display(int arg_cnt, char **args) { Stream *s = cmdGetStream(); for (int i = 0; i < arg_cnt; i++) { s->print("Arg "); s->print(i); s->print(": "); s->println(args[i]); } } void led_blink(int arg_cnt, char **args) { if (arg_cnt > 1) { led_blink_delay_time = cmdStr2Num(args[1], 10); led_blink_enb = true; } else { led_blink_enb = false; } } void led_pwm(int arg_cnt, char **args) { int pwm_val; if (arg_cnt > 1) { pwm_val = cmdStr2Num(args[1], 10); analogWrite(pwm_pin, pwm_val); } else { analogWrite(pwm_pin, 0); } } // Available Commands: // hello - Prints "Hello world." // args a b c - Displays all arguments // blink 100 - Blinks LED with 100ms delay // blink - Stops LED blinking // pwm 200 - Sets PWM to 200 (0-255 range) // pwm - Turns off PWM output ``` -------------------------------- ### cmdInit - Initialize Command Line Interface Source: https://context7.com/joshmarinacci/cmdarduino/llms.txt Initializes the CmdArduino library with a specified Stream object. This function must be called in the setup() function before any other CmdArduino functions. ```APIDOC ## cmdInit ### Description Initializes the command-line interface with a pointer to a Stream object. This must be called in your `setup()` function before adding commands or polling for input. The Stream can be Serial, SoftwareSerial, or any other Stream-compatible interface. ### Method `cmdInit(Stream *stream)` ### Parameters #### Path Parameters - **stream** (Stream*) - Required - A pointer to the Stream object (e.g., &Serial) to be used for communication. ### Request Example ```cpp #include void setup() { Serial.begin(57600); cmdInit(&Serial); } void loop() { cmdPoll(); } ``` ### Response This function does not return a value. Initialization is confirmed by successful execution without errors. ``` -------------------------------- ### Initialize CmdArduino Interface Source: https://context7.com/joshmarinacci/cmdarduino/llms.txt Initializes the command-line interface with a specified Stream object (e.g., Serial). This function must be called in the setup() function before adding commands or polling for input. It enables interactive control of Arduino projects via a serial monitor. ```cpp #include void setup() { // Initialize serial communication at 57600 baud Serial.begin(57600); // Initialize command line interface with Serial stream cmdInit(&Serial); // Now ready to add commands and poll for input } void loop() { cmdPoll(); } ``` -------------------------------- ### Arduino Command Function with Argument Parsing Source: https://github.com/joshmarinacci/cmdarduino/blob/master/README.md An example of an Arduino command function ('left') that parses an argument passed from the serial port. It uses 'parseNum' to convert the first argument (if provided) into an integer to control the delay time. ```C++ void left(int arg_cnt, char **args) { int time = 200; if(arg_cnt > 0) { time = parseNum(args[0]); } LeftMotor->run(FORWARD); delay(time); LeftMotor->run(RELEASE); } ``` -------------------------------- ### cmdGetStream - Get Current Stream Source: https://context7.com/joshmarinacci/cmdarduino/llms.txt Returns a pointer to the Stream object that was configured during initialization. ```APIDOC ## cmdGetStream ### Description Returns a pointer to the Stream object configured during initialization. This allows command handler functions to send output back through the same communication channel without needing to track the stream globally. ### Method `Stream* cmdGetStream()` ### Parameters This function takes no parameters. ### Request Example ```cpp #include void arg_display(int arg_cnt, char **args) { // Get the stream pointer for output Stream *s = cmdGetStream(); // Display all arguments passed to the command for (int i = 0; i < arg_cnt; i++) { s->print("Arg "); s->print(i); s->print(": "); s->println(args[i]); } } void setup() { Serial.begin(57600); cmdInit(&Serial); cmdAdd("args", arg_display); } void loop() { cmdPoll(); } ``` ### Response #### Success Response (200) - **Stream*** (Stream*) - A pointer to the currently configured Stream object. ``` -------------------------------- ### Convert String Argument to Number (C++) Source: https://context7.com/joshmarinacci/cmdarduino/llms.txt Demonstrates the use of cmdStr2Num to convert string arguments to unsigned 32-bit integers in specified bases (decimal and hexadecimal). This is crucial for processing numeric command inputs received as strings. It shows examples for setting PWM values and device addresses. ```cpp #include int pwm_pin = 10; void led_pwm(int arg_cnt, char **args) { int pwm_val; if (arg_cnt > 1) { // Convert string argument to decimal integer (base 10) pwm_val = cmdStr2Num(args[1], 10); analogWrite(pwm_pin, pwm_val); cmdGetStream()->print("PWM set to: "); cmdGetStream()->println(pwm_val); } else { // No argument - turn off LED analogWrite(pwm_pin, 0); cmdGetStream()->println("PWM turned off"); } } void set_address(int arg_cnt, char **args) { if (arg_cnt > 1) { // Convert hexadecimal string to integer (base 16) uint32_t address = cmdStr2Num(args[1], 16); cmdGetStream()->print("Address set to: 0x"); cmdGetStream()->println(address, HEX); } } void setup() { pinMode(pwm_pin, OUTPUT); Serial.begin(57600); cmdInit(&Serial); cmdAdd("pwm", led_pwm); cmdAdd("addr", set_address); } void loop() { cmdPoll(); } // Serial Input: pwm 128 // Output: PWM set to: 128 // Result: LED brightness set to 50% // Serial Input: pwm 255 // Output: PWM set to: 255 // Result: LED at full brightness // Serial Input: pwm // Output: PWM turned off // Serial Input: addr FF // Output: Address set to: 0xFF ``` -------------------------------- ### Get CmdArduino Stream Object Source: https://context7.com/joshmarinacci/cmdarduino/llms.txt Retrieves a pointer to the Stream object that was configured during CmdArduino initialization. This allows command handler functions to send output back through the same communication channel, simplifying output management without global stream tracking. ```cpp #include void arg_display(int arg_cnt, char **args) { // Get the stream pointer for output Stream *s = cmdGetStream(); // Display all arguments passed to the command for (int i = 0; i < arg_cnt; i++) { s->print("Arg "); s->print(i); s->print(": "); s->println(args[i]); } } void setup() { Serial.begin(57600); cmdInit(&Serial); cmdAdd("args", arg_display); } void loop() { cmdPoll(); } // Serial Input: args hello world i love you 3 4 5 yay // Output: // Arg 0: args // Arg 1: hello // Arg 2: world // Arg 3: i // Arg 4: love // Arg 5: you // Arg 6: 3 // Arg 7: 4 // Arg 8: 5 // Arg 9: yay ``` -------------------------------- ### Register Commands with CmdArduino Source: https://context7.com/joshmarinacci/cmdarduino/llms.txt Registers command strings and their associated handler functions. Handler functions must adhere to the `void func(int arg_cnt, char **args)` signature. This allows for custom control of Arduino functionalities by typing commands into a serial monitor. ```cpp #include // Command handler function - must use this signature void hello(int arg_cnt, char **args) { cmdGetStream()->println("Hello world."); } void left(int arg_cnt, char **args) { LeftMotor->run(FORWARD); delay(200); LeftMotor->run(RELEASE); } void right(int arg_cnt, char **args) { RightMotor->run(FORWARD); delay(200); RightMotor->run(RELEASE); } void setup() { Serial.begin(57600); cmdInit(&Serial); // Register multiple commands cmdAdd("hello", hello); // Type "hello" to trigger cmdAdd("left", left); // Type "left" to trigger cmdAdd("right", right); // Type "right" to trigger } void loop() { cmdPoll(); } // Serial Input: hello // Output: Hello world. ``` -------------------------------- ### cmdAdd - Register a Command Source: https://context7.com/joshmarinacci/cmdarduino/llms.txt Registers a command string with its corresponding handler function. The handler function is executed when the command is received. ```APIDOC ## cmdAdd ### Description Registers a command string and its associated handler function to the command table. The handler function must have the signature `void func(int arg_cnt, char **args)` where `arg_cnt` is the total number of arguments (including the command name) and `args` is an array of string arguments. ### Method `cmdAdd(const char *name, void (*handler)(int, char **))` ### Parameters #### Path Parameters - **name** (const char*) - Required - The string name of the command to register. - **handler** (void (*)(int, char **)) - Required - A pointer to the function that will handle the command. This function must accept an integer for the argument count and a char pointer array for the arguments. ### Request Example ```cpp #include void hello(int arg_cnt, char **args) { cmdGetStream()->println("Hello world."); } void setup() { Serial.begin(57600); cmdInit(&Serial); cmdAdd("hello", hello); } void loop() { cmdPoll(); } ``` ### Response This function does not return a value. Commands are added to the internal table upon successful registration. ``` -------------------------------- ### cmdPoll - Process Incoming Commands Source: https://context7.com/joshmarinacci/cmdarduino/llms.txt Checks for and processes incoming characters from the configured stream. This function should be called repeatedly in the main loop. ```APIDOC ## cmdPoll ### Description Checks for available input on the configured stream and processes any received characters. This function must be called repeatedly in the `loop()` function to handle incoming commands. Commands are parsed when a newline character (`\n` or `\r`) is received. ### Method `cmdPoll()` ### Parameters This function takes no parameters. ### Request Example ```cpp #include void setup() { Serial.begin(57600); cmdInit(&Serial); } void loop() { // Poll for commands - must be called in every loop iteration cmdPoll(); } ``` ### Response This function does not return a value. It processes input asynchronously and triggers registered command handlers when a complete command is received. ``` -------------------------------- ### Poll for CmdArduino Input Source: https://context7.com/joshmarinacci/cmdarduino/llms.txt Continuously checks for incoming data on the configured serial stream and processes any received commands. This function is essential and must be called within the Arduino `loop()` function to enable real-time command handling. Commands are parsed upon receiving a newline character. ```cpp #include int led_pin = 13; bool led_blink_enb = false; int led_blink_delay_time = 1000; void led_blink(int arg_cnt, char **args) { if (arg_cnt > 1) { led_blink_delay_time = cmdStr2Num(args[1], 10); led_blink_enb = true; } else { led_blink_enb = false; } } void setup() { pinMode(led_pin, OUTPUT); Serial.begin(57600); cmdInit(&Serial); cmdAdd("blink", led_blink); } void loop() { // Poll for commands - must be called in every loop iteration cmdPoll(); // Application logic runs alongside command processing if (led_blink_enb) { digitalWrite(led_pin, HIGH); delay(led_blink_delay_time); digitalWrite(led_pin, LOW); delay(led_blink_delay_time); } } // Serial Input: blink 100 // Result: LED blinks with 100ms on/off time // Serial Input: blink // Result: LED stops blinking ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.