This repository was archived by the owner on Jun 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoCommands.cpp
More file actions
178 lines (159 loc) · 4.28 KB
/
ArduinoCommands.cpp
File metadata and controls
178 lines (159 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// ArduinoCommands.cpp
// support for controlling Arduino pins through a serial command line interface
//
//
////////////////////////////////////////////////////////////////////////////////
#include "Arduino.h"
#include "CommandParser.hpp"
#include "Streaming.h"
// see Arduino.h and pins_arduino.h for useful declarations.
////////////////////////////////////////////////////////////////////////////////
int failMsg(const char * command, int failId, const char * message = 0)
{
Serial.print(command);
if (message)
{
Serial.print(": ");
Serial.println(message);
}
Serial.println();
return failId;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// int <--> symbolic translations
//
struct lookupVals
{
char * name;
int val;
};
////////////////////////////////////////////////////////////////////////////////
int lookup(const char * aName, const lookupVals entries[])
{
int i = 0;
for (; entries[i].name; i++)
{
if (strncasecmp(aName, entries[i].name, 20) == 0) {
//return entries[i].val;
break;
}
}
return entries[i].val;
}
////////////////////////////////////////////////////////////////////////////////
const char * reverseLookup(int aVal, const lookupVals entries[])
{
int i = 0;
for (; entries[i].name; i++)
{
if (aVal == entries[i].val) {
break;
}
}
return entries[i].val;
}
////////////////////////////////////////////////////////////////////////////////
const lookupVals PROGMEM modes[] = {
//{"input", INPUT},
{"output", OUTPUT},
{"pullup", INPUT_PULLUP},
{NULL, INPUT} // default
};
int setPinMode(int argc, char **argv)
{
if (argc == 3)
{
auto pin = atoi(argv[1]);
auto mode = lookup(argv[2], modes);
pinMode(pin, mode);
return EXIT_SUCCESS;
}
return failMsg(argv[0], -1, "bad arg count");
}
////////////////////////////////////////////////////////////////////////////////
int analogRead(int argc, char **argv)
{
if (argc == 2)
{
auto pin = atoi(argv[1]);
if (pin < 0 || pin > NUM_ANALOG_INPUTS)
{
return failMsg(argv[0], -1, "bad analog pin");
}
auto val = analogRead(pin);
Serial.println(val);
return EXIT_SUCCESS;
}
return failMsg(argv[0], -1, "bad arg count");
}
////////////////////////////////////////////////////////////////////////////////
int analogWrite(int argc, char **argv)
{
if (argc == 3)
{
auto pin = atoi(argv[1]);
if (digitalPinHasPWM(pin))
{
return failMsg(argv[0], -1, "bad analog output pin");
}
auto val = atoi(argv[2]);
auto cval = constrain(val, 0, 255);
if (val != cval)
{
return failMsg(argv[0], -1, "analog output out of range");
}
analogWrite(pin, val);
return EXIT_SUCCESS;
}
return failMsg(argv[0], -1, "bad arg count");
}
////////////////////////////////////////////////////////////////////////////////
const lookupVals PROGMEM digLevels[] = {
{"high", HIGH},
{NULL, LOW} // default
};
////////////////////////////////////////////////////////////////////////////////
int digitalWrite(int argc, char **argv)
{
if (argc == 3)
{
auto pin = atoi(argv[1]);
if (pin < 0 || pin > NUM_DIGITAL_PINS)
{
return failMsg(argv[0], -1, "bad digital pin");
}
auto level = lookup(argv[2], digLevels);
digitalWrite(pin, level);
return EXIT_SUCCESS;
}
return failMsg(argv[0], -1, "bad arg count");
}
////////////////////////////////////////////////////////////////////////////////
int digitalRead(int argc, char **argv)
{
if (argc == 2)
{
auto pin = atoi(argv[1]);
if (pin < 0 || pin > NUM_DIGITAL_PINS)
{
return failMsg(argv[0], -1, "bad digital pin");
}
auto val = digitalRead(pin);
Serial << val << F(" ");
auto valName = reverseLookup(val, digLevels);
Serial << valName << endl;
return EXIT_SUCCESS;
}
return failMsg(argv[0], -1, "bad arg count");
}
////////////////////////////////////////////////////////////////////////////////
int addArduinoCommands(CommandParser * shell)
{
shell->add("setpinmode", setPinMode);
shell->add("digitalwrite", digitalWrite);
shell->add("digitalread", digitalRead);
shell->add("analogread", analogRead);
shell->add("analogwrite", analogWrite);
return EXIT_SUCCESS;
}