浏览代码

pifacedigital lib function use

tags/v1.0
Robin Thoni 10 年前
父节点
当前提交
5ecceb6f06
共有 2 个文件被更改,包括 58 次插入1 次删除
  1. 48
    1
      src/mainclass.cpp
  2. 10
    0
      src/mainclass.hh

+ 48
- 1
src/mainclass.cpp 查看文件

@@ -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
+}

+ 10
- 0
src/mainclass.hh 查看文件

@@ -10,11 +10,21 @@ class MainClass
10 10
     int usage();
11 11
     int execute();
12 12
 
13
+    static int get_current_device();
14
+    static int set_current_device(int c);
15
+
16
+    static uint8_t get_pins();
17
+    static uint8_t up_pin(uint8_t pin);
18
+    static uint8_t down_pin(uint8_t pin);
19
+
13 20
   private:
14 21
     bool build_actions_();
15 22
     bool bad_value_();
16 23
     int argc_;
17 24
     char** argv_;
18 25
     std::vector<std::function<bool()>> actions_;
26
+
27
+    static int current_device_;
28
+    static uint8_t pins_;
19 29
 };
20 30
 #endif /* !MAINCLASS_HH */

正在加载...
取消
保存