Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -34,5 +35,6 @@ void loop()
// That's it.
void hello(int arg_cnt, char **args)
{
Serial.println("Hello world.");
}
cmdGetStream()->println("Hello world.");
}

Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -49,11 +50,13 @@ void loop()
// Arg 9: yay
void arg_display(int arg_cnt, char **args)
{
Stream *s = cmdGetStream();

for (int i=0; i<arg_cnt; i++)
{
Serial.print("Arg ");
Serial.print(i);
Serial.print(": ");
Serial.println(args[i]);
s->print("Arg ");
s->print(i);
s->print(": ");
s->println(args[i]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -82,4 +83,4 @@ void led_blink(int arg_cnt, char **args)
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -66,4 +67,4 @@ void led_pwm(int arg_cnt, char **args)
// if no args, turn off the LED
analogWrite(pwm_pin, 0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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; i<arg_cnt; i++)
{
Serial.print("Arg ");
Serial.print(i);
Serial.print(": ");
Serial.println(args[i]);
s->print("Arg ");
s->print(i);
s->print(": ");
s->println(args[i]);
}
}

Expand Down Expand Up @@ -133,4 +135,4 @@ void led_pwm(int arg_cnt, char **args)
// if no args, turn off the LED
analogWrite(pwm_pin, 0);
}
}
}