-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrsdioctl.cpp
More file actions
312 lines (290 loc) · 9.41 KB
/
Copy pathrsdioctl.cpp
File metadata and controls
312 lines (290 loc) · 9.41 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <rsdio.h>
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
static bool stringToState(std::string str)
{
std::transform(str.begin(), str.end(), str.begin(), toupper);
if (str == "LOW" || str == "FALSE" || str == "0")
return false;
else if (str == "HIGH" || str == "TRUE" || str == "1")
return true;
throw std::invalid_argument("Invalid pin state");
}
static const char* stateToString(bool state)
{
if (!state)
return "low";
else
return "high";
}
static rs::OutputMode stringToMode(std::string str)
{
std::transform(str.begin(), str.end(), str.begin(), toupper);
if (str == "SOURCE" || str == "0")
return rs::OutputMode::Source;
else if (str == "SINK" || str == "1")
return rs::OutputMode::Sink;
throw std::invalid_argument("Invalid output mode");
}
static const char* modeToString(rs::OutputMode mode)
{
if (mode == rs::OutputMode::Sink)
return "Sink";
else if (mode == rs::OutputMode::Source)
return "Source";
return "Error";
}
static rs::PinDirection stringToDirection(std::string str)
{
std::transform(str.begin(), str.end(), str.begin(), toupper);
if (str == "OUTPUT" || str == "0")
return rs::PinDirection::Output;
else if (str == "INPUT" || str == "1")
return rs::PinDirection::Input;
throw std::invalid_argument("Invalid pin direction");
}
static const char* directionToString(rs::PinDirection dir)
{
return dir == rs::PinDirection::Input ? "input" : "output";
}
static void showUsage()
{
std::cout
<< "Usage: rsdioctl FILE COMMAND [OPTIONS...]\n"
<< "\n"
<< "Commands:\n"
<< "s, state\t\toutput the state of a pin\n"
<< "\t\t\trequires -p to be defined\n"
<< "\n"
<< "s=STATE, state=STATE\tsets the state of a pin\n"
<< "\t\t\tStates:\n"
<< "\t\t\t0, LOW\n"
<< "\t\t\t1, HIGH\n"
<< "\t\t\trequires DIO and PIN to be defined\n"
<< "\n"
<< "d=DIRECTION, d=DIRECTION\tsets the direction of a pin\n"
<< "\t\t\tDirections:\n"
<< "\t\t\t0, INPUT\n"
<< "\t\t\t1, OUTPUT\n"
<< "\t\t\trequires DIO and PIN to be defined\n"
<< "\t\t\tpin must support the mode\n"
<< "\n"
<< "m, mode\t\tOutput the current output mode of a specific dio port\n"
<< "m=MODE, mode=MODE\tsets the output mode of a specific dio port\n"
<< "\t\t\tModes:\n"
<< "\t\t\t0, SOURCE\n"
<< "\t\t\t1, SINK\n"
<< "\n"
<< "Options:\n"
<< "-p NUM, --pin NUM \tthe pin number to be used by COMMAND\n"
<< "\n"
<< "-d NUM, --dio NUM \tthe dio number to be used by COMMAND\n"
<< "\t\t\tdefaults to 1 if not supplied\n"
<< "\n"
<< "-h, --human-readable \toutput data in a human readable format\n"
<< "\n"
<< "--help \t\t\tdisplay this help text and exit\n"
<< "--version \t\tdisplay library version information\n"
<< "--debug \t\tprint debug information\n";
}
int main(int argc, char* argv[])
{
// Create a list of args without optional switches
// Allows for switches to be position independent
bool human = false;
bool debug = false;
int dio = 1, pin = -1;
std::vector<std::string> argList;
std::vector<std::string> ignoredArgs;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--help") {
showUsage();
return 0;
}
else if (arg == "--version") {
std::cout << rs::rsDioVersion() << std::endl;
return 0;
}
else if (arg == "-p" || arg == "--pin") {
if (i < argc - 1) {
try {
pin = std::stoi(std::string(argv[++i]));
}
catch (...) {
std::cerr << "Invalid pin number" << std::endl;
showUsage();
return 1;
}
}
else {
std::cerr << "Missing pin number" << std::endl;
showUsage();
return 1;
}
}
else if (arg == "-d" || arg == "--dio") {
if (i < argc - 1) {
try {
dio = std::stoi(std::string(argv[++i]));
}
catch (...) {
std::cerr << "Invalid dio number" << std::endl;
showUsage();
return 1;
}
}
else {
std::cerr << "Missing dio number" << std::endl;
showUsage();
return 1;
}
}
else if (arg == "-h" || arg == "--human-readable")
human = true;
else if (arg == "--debug")
debug = true;
else if (arg.find("-") == 0)
ignoredArgs.emplace_back(arg);
else
argList.emplace_back(arg);
}
if (argList.size() < 2) {
showUsage();
return 1;
}
if (size_t s = ignoredArgs.size()) {
std::cerr << "Ignoring unknown argument";
if (s == 1)
std::cerr << ": ";
else if (s > 1)
std::cerr << "s: ";
for (size_t i = 0; i < ignoredArgs.size(); ++i) {
const std::string& arg = ignoredArgs[i];
std::cerr << arg;
if (i < ignoredArgs.size() - 1) std::cerr << ", ";
}
std::cerr << std::endl;
}
std::shared_ptr<rs::RsDio> rsdio(
rs::createRsDio(), std::mem_fn(&rs::RsDio::destroy)
);
if (!rsdio) {
std::cerr << "Failed to create instance of RsDio" << std::endl;
return 1;
}
rsdio->setXmlFile(argList[0].data(), debug);
if (rsdio->getLastError()) {
std::cerr << rsdio->getLastErrorString() << std::endl;
return 1;
}
std::string val;
std::string& cmd = argList[1];
// If the command contains "=" we need to break it up into command / value
// pair.
size_t index = cmd.find("=");
if (index != cmd.npos) {
val = cmd.substr(index + 1);
cmd = cmd.substr(0, cmd.size() - val.size());
}
std::string errorString;
bool userError = false;
try {
if (cmd == "s=" || cmd == "state=") {
if (pin < 0) {
errorString = "Missing required option: pin";
userError = true;
}
else {
bool state = stringToState(val);
rsdio->digitalWrite(dio, pin, state);
if (rsdio->getLastError())
errorString = rsdio->getLastErrorString();
}
}
else if (cmd == "s" || cmd == "state") {
if (pin < 0) {
errorString = "Missing required option: pin";
userError = true;
}
else {
bool state = rsdio->digitalRead(dio, pin);
if (rsdio->getLastError())
errorString = rsdio->getLastErrorString();
else if (human)
printf("%s\n", stateToString(state));
else
printf("%i", state);
}
}
else if (cmd == "d=" || cmd == "direction=") {
if (pin < 0) {
errorString = "Missing required option: pin";
userError = true;
}
else {
rs::PinDirection dir = stringToDirection(val);
rsdio->setPinDirection(dio, pin, dir);
if (rsdio->getLastError())
errorString = rsdio->getLastErrorString();
}
}
else if (cmd == "d" || cmd == "direction") {
if (pin < 0) {
errorString = "Missing required option: pin";
userError = true;
}
else {
rs::PinDirection dir = rsdio->getPinDirection(dio, pin);
if (rsdio->getLastError())
errorString = rsdio->getLastErrorString();
else if (human)
printf("%s\n", directionToString(dir));
else
printf("%i", dir);
}
}
else if (cmd == "m=" || cmd == "mode=") {
rs::OutputMode mode = stringToMode(val);
rsdio->setOutputMode(dio, mode);
if (rsdio->getLastError())
errorString = rsdio->getLastErrorString();
}
else if (cmd == "m" || cmd == "mode") {
rs::OutputMode mode = rsdio->getOutputMode(dio);
if (rsdio->getLastError())
errorString = rsdio->getLastErrorString();
else if (human)
printf("%s\n", modeToString(mode));
else if (mode == rs::OutputMode::Source)
printf("0");
else if (mode == rs::OutputMode::Sink)
printf("1");
}
else {
errorString = "Invalid command";
userError = true;
}
}
catch (std::invalid_argument& ex) {
errorString = ex.what();
userError = true;
}
catch (std::exception& ex) {
errorString = ex.what();
}
catch (...) {
errorString = "Unknown error";
}
if (!errorString.empty()) {
std::cerr << errorString << std::endl;
if (userError) showUsage();
return 1;
}
return 0;
}