diff --git a/Cmd.cpp b/Cmd.cpp index b4b14a5..05a9c5e 100755 --- a/Cmd.cpp +++ b/Cmd.cpp @@ -148,6 +148,11 @@ void cmd_handler() msg_ptr = msg; break; + case '\n': + // ignore newline characters. they usually come in pairs + // with the \r characters we use for newline detection. + break; + case '\b': // backspace stream->print(c); @@ -202,7 +207,7 @@ void cmdInit(Stream *str) at the setup() portion of the sketch. */ /**************************************************************************/ -void cmdAdd(char *name, void (*func)(int argc, char **argv)) +void cmdAdd(const char *name, void (*func)(int argc, char **argv)) { // alloc memory for command struct cmd_tbl = (cmd_t *)malloc(sizeof(cmd_t)); @@ -223,6 +228,18 @@ void cmdAdd(char *name, void (*func)(int argc, char **argv)) cmd_tbl_list = cmd_tbl; } +/**************************************************************************/ +/*! + Get a pointer to the stream used by the interpreter. This allows + commands to use the same communication channel as the interpreter + without tracking it in the main program. +*/ +/**************************************************************************/ +Stream* cmdGetStream(void) +{ + return stream; +} + /**************************************************************************/ /*! Convert a string to a number. The base must be specified, ie: "32" is a diff --git a/Cmd.h b/Cmd.h index d1f9b8c..d8818fb 100755 --- a/Cmd.h +++ b/Cmd.h @@ -54,7 +54,8 @@ typedef struct _cmd_t void cmdInit(Stream *); void cmdPoll(); -void cmdAdd(char *name, void (*func)(int argc, char **argv)); +void cmdAdd(const char *name, void (*func)(int argc, char **argv)); +Stream* cmdGetStream(void); uint32_t cmdStr2Num(char *str, uint8_t base); #endif //CMD_H diff --git a/examples/cmd_line_ex1_hello/cmd_line_ex1_hello.pde b/examples/cmd_line_ex1_hello/cmd_line_ex1_hello.ino old mode 100755 new mode 100644 similarity index 91% rename from examples/cmd_line_ex1_hello/cmd_line_ex1_hello.pde rename to examples/cmd_line_ex1_hello/cmd_line_ex1_hello.ino index abae6b5..1f8c95e --- a/examples/cmd_line_ex1_hello/cmd_line_ex1_hello.pde +++ b/examples/cmd_line_ex1_hello/cmd_line_ex1_hello.ino @@ -9,7 +9,8 @@ to them. void setup() { // init the command line and set it for a speed of 57600 - cmdInit(57600); + Serial.begin(57600); + cmdInit(&Serial); // add the commands to the command table. These functions must // already exist in the sketch. See the functions below. @@ -34,5 +35,6 @@ void loop() // That's it. void hello(int arg_cnt, char **args) { - Serial.println("Hello world."); -} + cmdGetStream()->println("Hello world."); +} + diff --git a/examples/cmd_line_ex2_args/cmd_line_ex2_args.pde b/examples/cmd_line_ex2_args/cmd_line_ex2_args.ino old mode 100755 new mode 100644 similarity index 90% rename from examples/cmd_line_ex2_args/cmd_line_ex2_args.pde rename to examples/cmd_line_ex2_args/cmd_line_ex2_args.ino index d837348..f405b91 --- a/examples/cmd_line_ex2_args/cmd_line_ex2_args.pde +++ b/examples/cmd_line_ex2_args/cmd_line_ex2_args.ino @@ -9,7 +9,8 @@ to them. void setup() { // init the command line and set it for a speed of 57600 - cmdInit(57600); + Serial.begin(57600); + cmdInit(&Serial); // add the commands to the command table. These functions must // already exist in the sketch. See the functions below. @@ -49,11 +50,13 @@ void loop() // Arg 9: yay void arg_display(int arg_cnt, char **args) { + Stream *s = cmdGetStream(); + for (int i=0; iprint("Arg "); + s->print(i); + s->print(": "); + s->println(args[i]); } -} +} diff --git a/examples/cmd_line_ex3_led_blink/cmd_line_ex3_led_blink.pde b/examples/cmd_line_ex3_led_blink/cmd_line_ex3_led_blink.ino old mode 100755 new mode 100644 similarity index 98% rename from examples/cmd_line_ex3_led_blink/cmd_line_ex3_led_blink.pde rename to examples/cmd_line_ex3_led_blink/cmd_line_ex3_led_blink.ino index 38ec070..b341869 --- a/examples/cmd_line_ex3_led_blink/cmd_line_ex3_led_blink.pde +++ b/examples/cmd_line_ex3_led_blink/cmd_line_ex3_led_blink.ino @@ -16,7 +16,8 @@ void setup() pinMode(led_pin, OUTPUT); // init the command line and set it for a speed of 57600 - cmdInit(57600); + Serial.begin(57600); + cmdInit(&Serial); // add the commands to the command table. These functions must // already exist in the sketch. See the functions below. @@ -82,4 +83,4 @@ void led_blink(int arg_cnt, char **args) } } - + diff --git a/examples/cmd_line_ex4_pwm/cmd_line_ex4_pwm.pde b/examples/cmd_line_ex4_pwm/cmd_line_ex4_pwm.ino old mode 100755 new mode 100644 similarity index 95% rename from examples/cmd_line_ex4_pwm/cmd_line_ex4_pwm.pde rename to examples/cmd_line_ex4_pwm/cmd_line_ex4_pwm.ino index 1b09cb8..b988dd9 --- a/examples/cmd_line_ex4_pwm/cmd_line_ex4_pwm.pde +++ b/examples/cmd_line_ex4_pwm/cmd_line_ex4_pwm.ino @@ -10,11 +10,12 @@ int pwm_pin = 10; void setup() { - // set the led pin as an output. its part of the demo. + // set the PWM pin as an output. its part of the demo. pinMode(pwm_pin, OUTPUT); // init the command line and set it for a speed of 57600 - cmdInit(57600); + Serial.begin(57600); + cmdInit(&Serial); // add the commands to the command table. These functions must // already exist in the sketch. See the functions below. @@ -66,4 +67,4 @@ void led_pwm(int arg_cnt, char **args) // if no args, turn off the LED analogWrite(pwm_pin, 0); } -} +} diff --git a/examples/cmd_line_ex5_multi_functions/cmd_line_ex5_multi_functions.pde b/examples/cmd_line_ex5_multi_functions/cmd_line_ex5_multi_functions.ino old mode 100755 new mode 100644 similarity index 93% rename from examples/cmd_line_ex5_multi_functions/cmd_line_ex5_multi_functions.pde rename to examples/cmd_line_ex5_multi_functions/cmd_line_ex5_multi_functions.ino index 9dedf17..4eedd2a --- a/examples/cmd_line_ex5_multi_functions/cmd_line_ex5_multi_functions.pde +++ b/examples/cmd_line_ex5_multi_functions/cmd_line_ex5_multi_functions.ino @@ -18,7 +18,8 @@ void setup() pinMode(pwm_pin, OUTPUT); // init the command line and set it for a speed of 57600 - cmdInit(57600); + Serial.begin(57600); + cmdInit(&Serial); // add the commands to the command table. These functions must // already exist in the sketch. See the functions below. @@ -54,7 +55,7 @@ void loop() // hello void hello(int arg_cnt, char **args) { - Serial.println("Hello world."); + cmdGetStream()->println("Hello world."); } // Display the contents of the args string array. @@ -73,12 +74,13 @@ void hello(int arg_cnt, char **args) // Arg 6: baby void arg_display(int arg_cnt, char **args) { + Stream *s = cmdGetStream(); for (int i=0; iprint("Arg "); + s->print(i); + s->print(": "); + s->println(args[i]); } } @@ -133,4 +135,4 @@ void led_pwm(int arg_cnt, char **args) // if no args, turn off the LED analogWrite(pwm_pin, 0); } -} +}