|
@@ -4,6 +4,11 @@
|
4
|
4
|
#include <sysexits.h>
|
5
|
5
|
#include <climits>
|
6
|
6
|
#include <getopt.h>
|
|
7
|
+#include <unistd.h>
|
|
8
|
+#include <pifacedigital.h>
|
|
9
|
+
|
|
10
|
+int MainClass::current_device_ = -1;
|
|
11
|
+uint8_t MainClass::pins_ = 0;
|
7
|
12
|
|
8
|
13
|
MainClass::MainClass(int argc, char* argv[])
|
9
|
14
|
: argc_(argc)
|
|
@@ -71,23 +76,40 @@ bool MainClass::build_actions_()
|
71
|
76
|
return bad_value_();
|
72
|
77
|
actions_.push_back([arg]() -> bool
|
73
|
78
|
{
|
74
|
|
- return true;
|
|
79
|
+ if (get_current_device() != -1)
|
|
80
|
+ pifacedigital_close(get_current_device());
|
|
81
|
+ return pifacedigital_open(set_current_device(arg)) != -1;
|
75
|
82
|
});
|
76
|
83
|
}
|
77
|
84
|
else if (opt == 'u')
|
78
|
85
|
{
|
79
|
86
|
if (arg > 7)
|
80
|
87
|
return bad_value_();
|
|
88
|
+ actions_.push_back([arg]() -> bool
|
|
89
|
+ {
|
|
90
|
+ pifacedigital_write_reg(up_pin(arg), OUTPUT, get_current_device());
|
|
91
|
+ return true;
|
|
92
|
+ });
|
81
|
93
|
}
|
82
|
94
|
else if (opt == 'd')
|
83
|
95
|
{
|
84
|
96
|
if (arg > 7)
|
85
|
97
|
return bad_value_();
|
|
98
|
+ actions_.push_back([arg]() -> bool
|
|
99
|
+ {
|
|
100
|
+ pifacedigital_write_reg(down_pin(arg), OUTPUT, get_current_device());
|
|
101
|
+ return true;
|
|
102
|
+ });
|
86
|
103
|
}
|
87
|
104
|
else if (opt == 's')
|
88
|
105
|
{
|
89
|
106
|
if (arg > UINT_MAX)
|
90
|
107
|
return bad_value_();
|
|
108
|
+ actions_.push_back([arg]() -> bool
|
|
109
|
+ {
|
|
110
|
+ usleep(arg * 1000);
|
|
111
|
+ return true;
|
|
112
|
+ });
|
91
|
113
|
}
|
92
|
114
|
else
|
93
|
115
|
return false;
|
|
@@ -104,3 +126,28 @@ int MainClass::execute()
|
104
|
126
|
return 1;
|
105
|
127
|
return 0;
|
106
|
128
|
}
|
|
129
|
+
|
|
130
|
+int MainClass::get_current_device()
|
|
131
|
+{
|
|
132
|
+ return current_device_;
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+int MainClass::set_current_device(int c)
|
|
136
|
+{
|
|
137
|
+ return (current_device_ = c);
|
|
138
|
+}
|
|
139
|
+
|
|
140
|
+uint8_t MainClass::get_pins()
|
|
141
|
+{
|
|
142
|
+ return pins_;
|
|
143
|
+}
|
|
144
|
+
|
|
145
|
+uint8_t MainClass::up_pin(uint8_t pin)
|
|
146
|
+{
|
|
147
|
+ return (pins_ |= (1 << pin));
|
|
148
|
+}
|
|
149
|
+
|
|
150
|
+uint8_t MainClass::down_pin(uint8_t pin)
|
|
151
|
+{
|
|
152
|
+ return (pins_ &= ~(1 << pin));
|
|
153
|
+}
|