Browse Source

pifacedigital lib function use

tags/v1.0
Robin Thoni 10 years ago
parent
commit
5ecceb6f06
2 changed files with 58 additions and 1 deletions
  1. 48
    1
      src/mainclass.cpp
  2. 10
    0
      src/mainclass.hh

+ 48
- 1
src/mainclass.cpp View File

4
 #include <sysexits.h>
4
 #include <sysexits.h>
5
 #include <climits>
5
 #include <climits>
6
 #include <getopt.h>
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
 MainClass::MainClass(int argc, char* argv[])
13
 MainClass::MainClass(int argc, char* argv[])
9
   : argc_(argc)
14
   : argc_(argc)
71
         return bad_value_();
76
         return bad_value_();
72
       actions_.push_back([arg]() -> bool
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
     else if (opt == 'u')
84
     else if (opt == 'u')
78
     {
85
     {
79
       if (arg > 7)
86
       if (arg > 7)
80
         return bad_value_();
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
     else if (opt == 'd')
94
     else if (opt == 'd')
83
     {
95
     {
84
       if (arg > 7)
96
       if (arg > 7)
85
         return bad_value_();
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
     else if (opt == 's')
104
     else if (opt == 's')
88
     {
105
     {
89
       if (arg > UINT_MAX)
106
       if (arg > UINT_MAX)
90
         return bad_value_();
107
         return bad_value_();
108
+      actions_.push_back([arg]() -> bool
109
+          {
110
+          usleep(arg * 1000);
111
+          return true;
112
+          });
91
     }
113
     }
92
     else
114
     else
93
       return false;
115
       return false;
104
       return 1;
126
       return 1;
105
   return 0;
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 View File

10
     int usage();
10
     int usage();
11
     int execute();
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
   private:
20
   private:
14
     bool build_actions_();
21
     bool build_actions_();
15
     bool bad_value_();
22
     bool bad_value_();
16
     int argc_;
23
     int argc_;
17
     char** argv_;
24
     char** argv_;
18
     std::vector<std::function<bool()>> actions_;
25
     std::vector<std::function<bool()>> actions_;
26
+
27
+    static int current_device_;
28
+    static uint8_t pins_;
19
 };
29
 };
20
 #endif /* !MAINCLASS_HH */
30
 #endif /* !MAINCLASS_HH */

Loading…
Cancel
Save