├── Cmd.cpp ├── Cmd.h ├── README.md ├── examples ├── cmd_line_ex1_hello │ └── cmd_line_ex1_hello.ino ├── cmd_line_ex2_args │ └── cmd_line_ex2_args.ino ├── cmd_line_ex3_led_blink │ └── cmd_line_ex3_led_blink.ino ├── cmd_line_ex4_pwm │ └── cmd_line_ex4_pwm.ino └── cmd_line_ex5_multi_functions │ └── cmd_line_ex5_multi_functions.ino └── keywords.txt /Cmd.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************* 2 | Copyright (C) 2009 FreakLabs 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions 7 | are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 2. Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | 3. Neither the name of the the copyright holder nor the names of its contributors 15 | may be used to endorse or promote products derived from this software 16 | without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 | SUCH DAMAGE. 29 | 30 | Originally written by Christopher Wang aka Akiba. 31 | Please post support questions to the FreakLabs forum. 32 | 33 | *******************************************************************/ 34 | /*! 35 | \file Cmd.c 36 | 37 | This implements a simple command line interface for the Arduino so that 38 | its possible to execute individual functions within the sketch. 39 | */ 40 | /**************************************************************************/ 41 | #include 42 | #if ARDUINO >= 100 43 | #include 44 | #else 45 | #include 46 | #endif 47 | #include "HardwareSerial.h" 48 | #include "Cmd.h" 49 | 50 | // command line message buffer and pointer 51 | static uint8_t msg[MAX_MSG_SIZE]; 52 | static uint8_t *msg_ptr; 53 | 54 | // linked list for command table 55 | static cmd_t *cmd_tbl_list, *cmd_tbl; 56 | 57 | // text strings for command prompt (stored in flash) 58 | const char cmd_banner[] PROGMEM = "*************** CMD *******************"; 59 | const char cmd_prompt[] PROGMEM = "CMD >> "; 60 | const char cmd_unrecog[] PROGMEM = "CMD: Command not recognized."; 61 | 62 | static Stream* stream; 63 | 64 | /**************************************************************************/ 65 | /*! 66 | Generate the main command prompt 67 | */ 68 | /**************************************************************************/ 69 | void cmd_display() 70 | { 71 | char buf[50]; 72 | 73 | stream->println(); 74 | 75 | strcpy_P(buf, cmd_banner); 76 | stream->println(buf); 77 | 78 | strcpy_P(buf, cmd_prompt); 79 | stream->print(buf); 80 | } 81 | 82 | /**************************************************************************/ 83 | /*! 84 | Parse the command line. This function tokenizes the command input, then 85 | searches for the command table entry associated with the commmand. Once found, 86 | it will jump to the corresponding function. 87 | */ 88 | /**************************************************************************/ 89 | void cmd_parse(char *cmd) 90 | { 91 | uint8_t argc, i = 0; 92 | char *argv[30]; 93 | char buf[50]; 94 | cmd_t *cmd_entry; 95 | 96 | fflush(stdout); 97 | 98 | // parse the command line statement and break it up into space-delimited 99 | // strings. the array of strings will be saved in the argv array. 100 | argv[i] = strtok(cmd, " "); 101 | do 102 | { 103 | argv[++i] = strtok(NULL, " "); 104 | } while ((i < 30) && (argv[i] != NULL)); 105 | 106 | // save off the number of arguments for the particular command. 107 | argc = i; 108 | 109 | // parse the command table for valid command. used argv[0] which is the 110 | // actual command name typed in at the prompt 111 | for (cmd_entry = cmd_tbl; cmd_entry != NULL; cmd_entry = cmd_entry->next) 112 | { 113 | if (!strcmp(argv[0], cmd_entry->cmd)) 114 | { 115 | cmd_entry->func(argc, argv); 116 | cmd_display(); 117 | return; 118 | } 119 | } 120 | 121 | // command not recognized. print message and re-generate prompt. 122 | strcpy_P(buf, cmd_unrecog); 123 | stream->println(buf); 124 | 125 | cmd_display(); 126 | } 127 | 128 | /**************************************************************************/ 129 | /*! 130 | This function processes the individual characters typed into the command 131 | prompt. It saves them off into the message buffer unless its a "backspace" 132 | or "enter" key. 133 | */ 134 | /**************************************************************************/ 135 | void cmd_handler() 136 | { 137 | char c = stream->read(); 138 | 139 | switch (c) 140 | { 141 | case '\r': 142 | // terminate the msg and reset the msg ptr. then send 143 | // it to the handler for processing. 144 | *msg_ptr = '\0'; 145 | stream->print("\r\n"); 146 | cmd_parse((char *)msg); 147 | msg_ptr = msg; 148 | break; 149 | 150 | case '\n': 151 | // ignore newline characters. they usually come in pairs 152 | // with the \r characters we use for newline detection. 153 | break; 154 | 155 | case '\b': 156 | // backspace 157 | stream->print(c); 158 | if (msg_ptr > msg) 159 | { 160 | msg_ptr--; 161 | } 162 | break; 163 | 164 | default: 165 | // normal character entered. add it to the buffer 166 | stream->print(c); 167 | *msg_ptr++ = c; 168 | break; 169 | } 170 | } 171 | 172 | /**************************************************************************/ 173 | /*! 174 | This function should be set inside the main loop. It needs to be called 175 | constantly to check if there is any available input at the command prompt. 176 | */ 177 | /**************************************************************************/ 178 | void cmdPoll() 179 | { 180 | while (stream->available()) 181 | { 182 | cmd_handler(); 183 | } 184 | } 185 | 186 | /**************************************************************************/ 187 | /*! 188 | Initialize the command line interface. This sets the terminal speed and 189 | and initializes things. 190 | */ 191 | /**************************************************************************/ 192 | void cmdInit(Stream *str) 193 | { 194 | stream = str; 195 | // init the msg ptr 196 | msg_ptr = msg; 197 | 198 | // init the command table 199 | cmd_tbl_list = NULL; 200 | 201 | } 202 | 203 | /**************************************************************************/ 204 | /*! 205 | Add a command to the command table. The commands should be added in 206 | at the setup() portion of the sketch. 207 | */ 208 | /**************************************************************************/ 209 | void cmdAdd(const char *name, void (*func)(int argc, char **argv)) 210 | { 211 | // alloc memory for command struct 212 | cmd_tbl = (cmd_t *)malloc(sizeof(cmd_t)); 213 | 214 | // alloc memory for command name 215 | char *cmd_name = (char *)malloc(strlen(name)+1); 216 | 217 | // copy command name 218 | strcpy(cmd_name, name); 219 | 220 | // terminate the command name 221 | cmd_name[strlen(name)] = '\0'; 222 | 223 | // fill out structure 224 | cmd_tbl->cmd = cmd_name; 225 | cmd_tbl->func = func; 226 | cmd_tbl->next = cmd_tbl_list; 227 | cmd_tbl_list = cmd_tbl; 228 | } 229 | 230 | /**************************************************************************/ 231 | /*! 232 | Get a pointer to the stream used by the interpreter. This allows 233 | commands to use the same communication channel as the interpreter 234 | without tracking it in the main program. 235 | */ 236 | /**************************************************************************/ 237 | Stream* cmdGetStream(void) 238 | { 239 | return stream; 240 | } 241 | 242 | /**************************************************************************/ 243 | /*! 244 | Convert a string to a number. The base must be specified, ie: "32" is a 245 | different value in base 10 (decimal) and base 16 (hexadecimal). 246 | */ 247 | /**************************************************************************/ 248 | uint32_t cmdStr2Num(char *str, uint8_t base) 249 | { 250 | return strtol(str, NULL, base); 251 | } 252 | -------------------------------------------------------------------------------- /Cmd.h: -------------------------------------------------------------------------------- 1 | /******************************************************************* 2 | Copyright (C) 2009 FreakLabs 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions 7 | are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 2. Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | 3. Neither the name of the the copyright holder nor the names of its contributors 15 | may be used to endorse or promote products derived from this software 16 | without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 | SUCH DAMAGE. 29 | 30 | Originally written by Christopher Wang aka Akiba. 31 | Please post support questions to the FreakLabs forum. 32 | 33 | *******************************************************************/ 34 | /*! 35 | \file 36 | \ingroup 37 | 38 | 39 | */ 40 | /**************************************************************************/ 41 | #ifndef CMD_H 42 | #define CMD_H 43 | 44 | #define MAX_MSG_SIZE 60 45 | #include 46 | #include 47 | 48 | // command line structure 49 | typedef struct _cmd_t 50 | { 51 | char *cmd; 52 | void (*func)(int argc, char **argv); 53 | struct _cmd_t *next; 54 | } cmd_t; 55 | 56 | void cmdInit(Stream *); 57 | void cmdPoll(); 58 | void cmdAdd(const char *name, void (*func)(int argc, char **argv)); 59 | Stream* cmdGetStream(void); 60 | uint32_t cmdStr2Num(char *str, uint8_t base); 61 | 62 | #endif //CMD_H 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | == CmdArduino 2 | 3 | 4 | CmdArduino is a small library to parse commands from the Serial port 5 | or anything else which implements the Stream API. It is based on the 6 | original by Akiba at Freaklabs.org 7 | 8 | To use it create your command functions, then wire them up in your setup function. 9 | All command functions must take `int arg_cnt, char ** args` as it's parameters 10 | and return `void`. 11 | Here's an example from a simple robot: 12 | 13 | ``` 14 | void left(int arg_cnt, char **args) { 15 | LeftMotor->run(FORWARD); 16 | delay(200); 17 | LeftMotor->run(RELEASE); 18 | } 19 | 20 | void right(int arg_cnt, char **args) { 21 | RightMotor->run(FORWARD); 22 | delay(200); 23 | RightMotor->run(RELEASE); 24 | } 25 | 26 | void setup() { 27 | Serial.begin(9600); 28 | cmdInit(&Serial); 29 | cmdAdd('left',left); 30 | cmdAdd('right',right); 31 | } 32 | ``` 33 | 34 | Call `cmdInit` with a pointer to the stream you are using. Call `cmdAdd` 35 | to attach a string command to the command function. Now you can type `left` 36 | or `right` into your serial port to command the robot. The serial port 37 | will be parsed using newline, `\n`, as the delimiter. The command name should 38 | be the first item and any arguments after that. For example, to tell the robot 39 | to turn left for three seconds, type in: 40 | 41 | ``` 42 | left 3000\n 43 | ``` 44 | 45 | Then the left function becomes 46 | 47 | ``` 48 | void left(int arg_cnt, char **args) { 49 | int time = 200; 50 | if(arg_cnt > 0) { 51 | time = parseNum(args[0]); 52 | } 53 | LeftMotor->run(FORWARD); 54 | delay(time); 55 | LeftMotor->run(RELEASE); 56 | } 57 | ``` 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | == original readme 66 | 67 | The Arduino Command Line Interface, aka CmdArduino, is a simple shell that can 68 | be run on an Arduino. It's nothing fancy and its main purpose is to allow users 69 | to easily call their functions on a running Arduino via a simple serial 70 | terminal. It also allows users to pass in arguments from the command line into 71 | the functions they wrote so they can easily toggle pins, set blinking speed, 72 | set pwm duty cycles, or whatever else might need command line user input. Using 73 | it is fairly simple and just requires unzipping the files into the 74 | "Arduino/libraries" sub-directory in the Arduino program folder. 75 | -------------------------------------------------------------------------------- /examples/cmd_line_ex1_hello/cmd_line_ex1_hello.ino: -------------------------------------------------------------------------------- 1 | /***************************************************** 2 | This is the demo sketch for the command line interface 3 | by FreakLabs. It's a simple command line interface 4 | where you can define your own commands and pass arguments 5 | to them. 6 | *****************************************************/ 7 | #include 8 | 9 | void setup() 10 | { 11 | // init the command line and set it for a speed of 57600 12 | Serial.begin(57600); 13 | cmdInit(&Serial); 14 | 15 | // add the commands to the command table. These functions must 16 | // already exist in the sketch. See the functions below. 17 | // The functions need to have the format: 18 | // 19 | // void func_name(int arg_cnt, char **args) 20 | // 21 | // arg_cnt is the number of arguments typed into the command line 22 | // args is a list of argument strings that were typed into the command line 23 | cmdAdd("hello", hello); 24 | } 25 | 26 | void loop() 27 | { 28 | cmdPoll(); 29 | } 30 | 31 | // Print "hello world" when called from the command line. 32 | // Usage: At the command line, type: 33 | // hello 34 | // 35 | // That's it. 36 | void hello(int arg_cnt, char **args) 37 | { 38 | cmdGetStream()->println("Hello world."); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /examples/cmd_line_ex2_args/cmd_line_ex2_args.ino: -------------------------------------------------------------------------------- 1 | /***************************************************** 2 | This is the demo sketch for the command line interface 3 | by FreakLabs. It's a simple command line interface 4 | where you can define your own commands and pass arguments 5 | to them. 6 | *****************************************************/ 7 | #include 8 | 9 | void setup() 10 | { 11 | // init the command line and set it for a speed of 57600 12 | Serial.begin(57600); 13 | cmdInit(&Serial); 14 | 15 | // add the commands to the command table. These functions must 16 | // already exist in the sketch. See the functions below. 17 | // The functions need to have the format: 18 | // 19 | // void func_name(int arg_cnt, char **args) 20 | // 21 | // arg_cnt is the number of arguments typed into the command line 22 | // args is a list of argument strings that were typed into the command line 23 | cmdAdd("args", arg_display); 24 | } 25 | 26 | void loop() 27 | { 28 | cmdPoll(); 29 | } 30 | 31 | // Example to show what the argument count and arguments look like. The 32 | // arg_cnt is the number of arguments typed in by the user. "char **args" is 33 | // a bit nasty looking, but its a list of the arguments typed in as ASCII strings. 34 | // In C, char *something means an array of characters, aka a string. So 35 | // char **something is an array of an array of characters, or a string array. 36 | // 37 | // Usage: At the command line, type 38 | // args hello world i love you 3 4 5 yay 39 | // 40 | // The output should look like this: 41 | // Arg 0: args 42 | // Arg 1: hello 43 | // Arg 2: world 44 | // Arg 3: i 45 | // Arg 4: love 46 | // Arg 5: you 47 | // Arg 6: 3 48 | // Arg 7: 4 49 | // Arg 8: 5 50 | // Arg 9: yay 51 | void arg_display(int arg_cnt, char **args) 52 | { 53 | Stream *s = cmdGetStream(); 54 | 55 | for (int i=0; iprint("Arg "); 58 | s->print(i); 59 | s->print(": "); 60 | s->println(args[i]); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /examples/cmd_line_ex3_led_blink/cmd_line_ex3_led_blink.ino: -------------------------------------------------------------------------------- 1 | /***************************************************** 2 | This is the demo sketch for the command line interface 3 | by FreakLabs. It's a simple command line interface 4 | where you can define your own commands and pass arguments 5 | to them. 6 | *****************************************************/ 7 | #include 8 | 9 | int led_pin = 13; 10 | bool led_blink_enb = false; 11 | int led_blink_delay_time = 1000; 12 | 13 | void setup() 14 | { 15 | // set the led pin as an output. its part of the demo. 16 | pinMode(led_pin, OUTPUT); 17 | 18 | // init the command line and set it for a speed of 57600 19 | Serial.begin(57600); 20 | cmdInit(&Serial); 21 | 22 | // add the commands to the command table. These functions must 23 | // already exist in the sketch. See the functions below. 24 | // The functions need to have the format: 25 | // 26 | // void func_name(int arg_cnt, char **args) 27 | // 28 | // arg_cnt is the number of arguments typed into the command line 29 | // args is a list of argument strings that were typed into the command line 30 | cmdAdd("blink", led_blink); 31 | } 32 | 33 | void loop() 34 | { 35 | cmdPoll(); 36 | 37 | // This is where the blinking happens. The led_blink function 38 | // only controls the delay time and whether to enable the blinking 39 | // action or not. 40 | // One thing to be careful of is having delays in the loop() function. 41 | // This will slow down the response time of the command line since 42 | // the loop() function needs to get past the delays before it can 43 | // check for any commands at the command line. 44 | if (led_blink_enb) 45 | { 46 | digitalWrite(led_pin, HIGH); // set the LED on 47 | delay(led_blink_delay_time); // wait for a second 48 | digitalWrite(led_pin, LOW); // set the LED off 49 | delay(led_blink_delay_time); 50 | } 51 | } 52 | 53 | // Blink the LED. This is an example of using command line arguments 54 | // to call a function in a sketch. 55 | // If a numeric arg is specified, then use that to set the 56 | // delay time. If called with no arguments, then turn the LED off. 57 | // 58 | // Usage: At the command line, to blink the LED, type: 59 | // blink 100 60 | // 61 | // This blinks the LED with a 100 msec on/off time. 62 | // 63 | // Usage: At the command line, to turn off the LED, type: 64 | // blink 65 | // 66 | // Calling the function with no arguments will turn off the LED 67 | // 68 | // Also, you'll notice that the function "cmdStr2Num" is needed. Since 69 | // the numeric arg is stored as an ASCII string, it needs to be converted 70 | // to an integer. When you call cmdStr2Num, you need to specify two arguments: 71 | // 1) the numeric string to be converted 72 | // 2) the numeric base that will be used to convert it,ie: 10 = decimal, 16 = hex 73 | void led_blink(int arg_cnt, char **args) 74 | { 75 | if (arg_cnt > 1) 76 | { 77 | led_blink_delay_time = cmdStr2Num(args[1], 10); 78 | led_blink_enb = true; 79 | } 80 | else 81 | { 82 | led_blink_enb = false; 83 | } 84 | } 85 | 86 | 87 | -------------------------------------------------------------------------------- /examples/cmd_line_ex4_pwm/cmd_line_ex4_pwm.ino: -------------------------------------------------------------------------------- 1 | /***************************************************** 2 | This is the demo sketch for the command line interface 3 | by FreakLabs. It's a simple command line interface 4 | where you can define your own commands and pass arguments 5 | to them. 6 | *****************************************************/ 7 | #include 8 | 9 | int pwm_pin = 10; 10 | 11 | void setup() 12 | { 13 | // set the PWM pin as an output. its part of the demo. 14 | pinMode(pwm_pin, OUTPUT); 15 | 16 | // init the command line and set it for a speed of 57600 17 | Serial.begin(57600); 18 | cmdInit(&Serial); 19 | 20 | // add the commands to the command table. These functions must 21 | // already exist in the sketch. See the functions below. 22 | // The functions need to have the format: 23 | // 24 | // void func_name(int arg_cnt, char **args) 25 | // 26 | // arg_cnt is the number of arguments typed into the command line 27 | // args is a list of argument strings that were typed into the command line 28 | cmdAdd("pwm", led_pwm); 29 | } 30 | 31 | void loop() 32 | { 33 | cmdPoll(); 34 | } 35 | 36 | // This is another example to demonstrate how command line arguments can 37 | // be used when calling your sketch function from the command line. 38 | // 39 | // This function sets the brightness of an LED on pin 10 via PWM from the 40 | // command line. If no args are present,then the LED will be turned off. 41 | // 42 | // Usage: At the command line, to set the brightness, type: 43 | // pwm 100 44 | // 45 | // This will set the LED brightness to a level of 100 (on a range of 0-255) 46 | // 47 | // Usage: At the command line, to turn off the LED, type: 48 | // pwm 49 | // 50 | // This will turn off the LED. 51 | // 52 | // Similar to ex3, when you use numeric arguments, you'll need to use the 53 | // cmdStr2Num function to convert the numeric string into a usable integer. 54 | // See ex3 or the tutorial for more information on this function. 55 | void led_pwm(int arg_cnt, char **args) 56 | { 57 | int pwm_val; 58 | 59 | if (arg_cnt > 1) 60 | { 61 | // if args are present, then use the first arg as the brightness level 62 | pwm_val = cmdStr2Num(args[1], 10); 63 | analogWrite(pwm_pin, pwm_val); 64 | } 65 | else 66 | { 67 | // if no args, turn off the LED 68 | analogWrite(pwm_pin, 0); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /examples/cmd_line_ex5_multi_functions/cmd_line_ex5_multi_functions.ino: -------------------------------------------------------------------------------- 1 | /***************************************************** 2 | This is the demo sketch for the command line interface 3 | by FreakLabs. It's a simple command line interface 4 | where you can define your own commands and pass arguments 5 | to them. 6 | *****************************************************/ 7 | #include 8 | 9 | int led_pin = 13; 10 | bool led_blink_enb = false; 11 | int led_blink_delay_time = 1000; 12 | int pwm_pin = 10; 13 | 14 | void setup() 15 | { 16 | // set the led pin as an output. its part of the demo. 17 | pinMode(led_pin, OUTPUT); 18 | pinMode(pwm_pin, OUTPUT); 19 | 20 | // init the command line and set it for a speed of 57600 21 | Serial.begin(57600); 22 | cmdInit(&Serial); 23 | 24 | // add the commands to the command table. These functions must 25 | // already exist in the sketch. See the functions below. 26 | // The functions need to have the format: 27 | // 28 | // void func_name(int arg_cnt, char **args) 29 | // 30 | // arg_cnt is the number of arguments typed into the command line 31 | // args is a list of argument strings that were typed into the command line 32 | cmdAdd("hello", hello); 33 | cmdAdd("args", arg_display); 34 | cmdAdd("blink", led_blink); 35 | cmdAdd("pwm", led_pwm); 36 | 37 | } 38 | 39 | void loop() 40 | { 41 | cmdPoll(); 42 | 43 | if (led_blink_enb) 44 | { 45 | digitalWrite(led_pin, HIGH); // set the LED on 46 | delay(led_blink_delay_time); // wait for a second 47 | digitalWrite(led_pin, LOW); // set the LED off 48 | delay(led_blink_delay_time); 49 | } 50 | } 51 | 52 | // Print "hello world" when called from the command line. 53 | // 54 | // Usage: 55 | // hello 56 | void hello(int arg_cnt, char **args) 57 | { 58 | cmdGetStream()->println("Hello world."); 59 | } 60 | 61 | // Display the contents of the args string array. 62 | // 63 | // Usage: 64 | // args 12 34 56 hello gothic baby 65 | // 66 | // Will display the contents of the args array as a list of strings 67 | // Output: 68 | // Arg 0: args 69 | // Arg 1: 12 70 | // Arg 2: 34 71 | // Arg 3: 56 72 | // Arg 4: hello 73 | // Arg 5: gothic 74 | // Arg 6: baby 75 | void arg_display(int arg_cnt, char **args) 76 | { 77 | Stream *s = cmdGetStream(); 78 | for (int i=0; iprint("Arg "); 81 | s->print(i); 82 | s->print(": "); 83 | s->println(args[i]); 84 | } 85 | } 86 | 87 | // Blink the LED with the delay time specified in the args. 88 | // 89 | // Usage: 90 | // blink 100 91 | // 92 | // Blinks the LED with 100 msec on/off time 93 | // 94 | // Usage: 95 | // blink 96 | // 97 | // Turns off the LED. 98 | void led_blink(int arg_cnt, char **args) 99 | { 100 | if (arg_cnt > 1) 101 | { 102 | led_blink_delay_time = cmdStr2Num(args[1], 10); 103 | led_blink_enb = true; 104 | } 105 | else 106 | { 107 | led_blink_enb = false; 108 | } 109 | } 110 | 111 | // This function sets the brightness of an LED on pin 10 via PWM from the 112 | // command line. If no args are present,then the LED will be turned off. 113 | // 114 | // Usage: 115 | // pwm 100 116 | // 117 | // Sets the LED brightness to a value of 100 on a scale from 0 to 255 118 | // 119 | // Usage: 120 | // pwm 121 | // 122 | // Turns off the LED 123 | void led_pwm(int arg_cnt, char **args) 124 | { 125 | int pwm_val; 126 | 127 | if (arg_cnt > 1) 128 | { 129 | // if args are present, then use the first arg as the brightness level 130 | pwm_val = cmdStr2Num(args[1], 10); 131 | analogWrite(pwm_pin, pwm_val); 132 | } 133 | else 134 | { 135 | // if no args, turn off the LED 136 | analogWrite(pwm_pin, 0); 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For CmdArduino 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ####################################### 10 | # Methods and Functions (KEYWORD2) 11 | ####################################### 12 | cmdInit KEYWORD2 13 | cmdPoll KEYWORD2 14 | cmdAdd KEYWORD2 15 | cmdStr2Num KEYWORD2 16 | 17 | ####################################### 18 | # Constants (LITERAL1) 19 | ####################################### 20 | 21 | --------------------------------------------------------------------------------