Skip to content

Commit 10b229a

Browse files
committed
Merge branch 'master' of github.com:buxtronix/arduino
2 parents f41f477 + e0da28a commit 10b229a

File tree

4 files changed

+151
-12
lines changed

4 files changed

+151
-12
lines changed

libraries/Cli/cli.cpp renamed to libraries/Cli/Cli.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* Cli library for Arduino */
22

33
#include "Arduino.h"
4-
#include "cli.h"
4+
#include "Cli.h"
55
#include <string.h>
66

77

@@ -25,7 +25,7 @@ void Cli::addChar(char c) {
2525
_parseAndRun();
2626
return;
2727
}
28-
if (c == 0x8) {
28+
if (c == 0x8 || c == 0x7F) {
2929
if (_cliBufferLength > 0) {
3030
_cliBuffer[--_cliBufferLength] = '\0';
3131
printfunc->print("\010 \010");
@@ -59,8 +59,8 @@ unsigned char len = 0;
5959
return len;
6060
}
6161

62-
char Cli::atoi(char *s) {
63-
char value = 0;
62+
int Cli::atoi(char *s) {
63+
int value = 0;
6464
int mult = 1;
6565
char *end = &s[_strlen(s)];
6666
while (end != s) {
@@ -105,7 +105,14 @@ char *ptr;
105105
return _argBuffer;
106106
}
107107

108-
void Cli::_help() {
108+
// Returns the nth arg on the command line parsed as an int (0 is the command).
109+
int Cli::getArgi(char n) {
110+
char *arg = getArg(n);
111+
return atoi(arg);
112+
}
113+
114+
// Displays help - all of the available commands.
115+
void Cli::help() {
109116
printfunc->println("Command help:");
110117
for (unsigned char i = 0 ; i < ncommands ; i++) {
111118
printfunc->write(' ');
@@ -126,7 +133,7 @@ unsigned char i;
126133
return;
127134
}
128135
if (_strlen(_cliBuffer) == 1 && _strncmp("?", _cliBuffer, 1)) {
129-
_help();
136+
help();
130137
return;
131138
}
132139

@@ -139,7 +146,7 @@ unsigned char i;
139146
Command c = commands[i];
140147
if (_strlen(_commandBuffer) == _strlen(c.name) &&
141148
_strncmp(_commandBuffer, c.name, _strlen(c.name))) {
142-
c.run();
149+
runFunc(c);
143150
_clear();
144151
printfunc->print(prompt);
145152
return;
@@ -150,3 +157,16 @@ unsigned char i;
150157
printfunc->println("Unknown command. '?' for help.");
151158
printfunc->print(prompt);
152159
}
160+
161+
// Runs the command, if the number of args matches.
162+
void Cli::runFunc(Command c) {
163+
if (c.args > 0 && getArg(c.args)[0] == '\0') {
164+
printfunc->print("Command '");
165+
printfunc->print(c.name);
166+
printfunc->print("' requires ");
167+
printfunc->print((int)c.args);
168+
printfunc->println(" arguments.");
169+
return;
170+
}
171+
c.run();
172+
}

libraries/Cli/cli.h renamed to libraries/Cli/Cli.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ struct Command {
2424
const char *name;
2525
// Help text associated with the command.
2626
const char *help;
27+
// Number of arguments required after the command.
28+
const char args;
2729
// The function to run when the command is run.
2830
CommandProc run;
2931
};
@@ -40,10 +42,14 @@ class Cli
4042
void addChar(char c);
4143
/* Used by the command's function to get the command argument at position n */
4244
char *getArg(char n);
45+
/* Similar to above, but parses the arg as an integer. */
46+
int getArgi(char n);
4347
/* Fetch a pointer to the start of the command's arguments. */
4448
char *getArg();
45-
/* Converts the given string to a signed char. */
46-
char atoi(char *);
49+
/* Converts the given string to a signed int. */
50+
int atoi(char *);
51+
// Display help.
52+
void help(void);
4753
/* The printable function */
4854
Print *printfunc;
4955
/* The valid commands in this cli. */
@@ -70,8 +76,8 @@ class Cli
7076
char _strncmp(const char *, const char *, char);
7177
// When enter is pressed, run the command.
7278
void _parseAndRun();
73-
// Display help.
74-
void _help(void);
79+
// Run the function that was found.
80+
void runFunc(Command);
7581
};
7682

7783
#endif
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Demonstrates use of the Cli library.
3+
*
4+
* The print function here is just the built in Serial module, so user i/o
5+
* is via this.
6+
*/
7+
#include <Cli.h>
8+
#include <stdio.h>
9+
10+
// Setup for being able to use "printf".
11+
static FILE uartout = {0};
12+
13+
// Initialise the Cli with the given prompt.
14+
const char *prompt = "arduino> ";
15+
Cli *cli;
16+
17+
/* This is where all of the commands are defined.
18+
* Each entry contains four parameters:
19+
* 1. The command that the user enters.
20+
* 2. The help string displayed to the user.
21+
* 3. Minimum number of arguments required.
22+
* 4. A pointer to the function called when the command is entered.
23+
*/
24+
Command commands[7] = {
25+
{"dl", "Sleep for given number of seconds", 1, &delay_func},
26+
{"ms", "Show Arduino millis() value.", 0, &millis_func},
27+
{"reset", "Reset the Arduino.", 0, &reset_func},
28+
{"dw", "Write a value to a digital pin. [pin] [0|1]", 2, &write_func},
29+
{"dr", "Read digital value from a pin.", 1, &read_func},
30+
{"aw", "Write a value to an analog pin. [pin] [0-255]", 2, &awrite_func},
31+
{"ar", "Read analogue value from a pin.", 1, &aread_func},
32+
};
33+
// Number of commands above.
34+
char ncommands = 7;
35+
36+
// putchar method for "printf" (http://playground.arduino.cc/Main/Printf)
37+
static int uart_putchar(char c, FILE *stream) {
38+
Serial.write(c);
39+
return 0;
40+
}
41+
42+
void setup() {
43+
// Setup for printf to work.
44+
fdev_setup_stream(&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
45+
stdout = &uartout;
46+
47+
// Initialise the serial port.
48+
Serial.begin(57600);
49+
// Initialise the Cli module with the commands defined above.
50+
cli = new Cli(prompt, &Serial);
51+
cli->commands = commands;
52+
cli->ncommands = ncommands;
53+
// Show some initial help.
54+
cli->help();
55+
}
56+
57+
void loop() {
58+
// Read characters from the serial port and pass them to the Cli module.
59+
if (Serial.available()) {
60+
cli->addChar(Serial.read());
61+
}
62+
}
63+
64+
// Handler for the "millis" command.
65+
void millis_func() {
66+
printf("Millis: %ld\r\n", millis());
67+
}
68+
69+
// Handler for the "reset" command.
70+
void(* resetFunc)(void) = 0;
71+
void reset_func() {
72+
Serial.println("Resetting...");
73+
Serial.flush();
74+
resetFunc();
75+
}
76+
77+
// Delay function.
78+
void delay_func() {
79+
int value = cli->getArgi(1);
80+
printf("Sleeping for %d millis...\r\n", value);
81+
delay(value);
82+
}
83+
84+
// Pin write function.
85+
void write_func() {
86+
char pin = cli->getArgi(1);
87+
char value = cli->getArgi(2);
88+
pinMode(pin, OUTPUT);
89+
digitalWrite(pin, value);
90+
printf("Wrote %d to pin %d\r\n", value, pin);
91+
}
92+
93+
// Pin read function.
94+
void read_func() {
95+
char pin = cli->getArgi(1);
96+
pinMode(pin, INPUT);
97+
printf("Value on pin %d: %d\r\n", pin, digitalRead(pin));
98+
}
99+
100+
// Analog write function.
101+
void awrite_func() {
102+
char pin = cli->getArgi(1);
103+
int value = cli->getArgi(2);
104+
pinMode(pin, OUTPUT);
105+
analogWrite(pin, value);
106+
printf("Write PWM %d to pin %d\r\n", value, pin);
107+
}
108+
109+
// Analog read function.
110+
void aread_func() {
111+
char pin = cli->getArgi(1);
112+
printf("Value on pin %d: %d\r\n", pin, analogRead(pin));
113+
}

libraries/Rotary/Rotary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
*/
6262

6363
#include "Arduino.h"
64-
#include "rotary.h"
64+
#include "Rotary.h"
6565

6666
/*
6767
* The below state table has, for each state (row), the new state

0 commit comments

Comments
 (0)