Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GPIODriver.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  * GPIODriver.h
17  * Uses data in a DMXBuffer to drive GPIO pins.
18  * Copyright (C) 2014 Simon Newton
19  */
20 
21 #ifndef PLUGINS_GPIO_GPIODRIVER_H_
22 #define PLUGINS_GPIO_GPIODRIVER_H_
23 
24 #include <stdint.h>
25 #include <ola/DmxBuffer.h>
26 #include <ola/base/Macro.h>
27 #include <ola/thread/Thread.h>
28 
29 #include <vector>
30 
31 namespace ola {
32 namespace plugin {
33 namespace gpio {
34 
39  public:
43  struct Options {
44  public:
45  Options(): start_address(1), turn_on(128), turn_off(127) {}
46 
50  std::vector<uint8_t> gpio_pins;
51 
55  uint16_t start_address;
56 
60  uint8_t turn_on;
61 
65  uint8_t turn_off;
66  };
67 
72  explicit GPIODriver(const Options &options);
73 
77  ~GPIODriver();
78 
83  bool Init();
84 
89  std::vector<uint8_t> PinList() const { return m_options.gpio_pins; }
90 
96  bool SendDmx(const DmxBuffer &dmx);
97 
98  void *Run();
99 
100  private:
101  enum GPIOState {
102  ON,
103  OFF,
104  UNDEFINED,
105  };
106 
107  struct GPIOPin {
108  int fd;
109  GPIOState state;
110  bool last_value;
111  };
112 
113  typedef std::vector<GPIOPin> GPIOPins;
114 
115  const Options m_options;
116  GPIOPins m_gpio_pins;
117 
118  DmxBuffer m_buffer;
119  bool m_term; // GUARDED_BY(m_mutex);
120  bool m_dmx_changed; // GUARDED_BY(m_mutex);
121  ola::thread::Mutex m_mutex;
123 
124  bool SetupGPIO();
125  bool UpdateGPIOPins(const DmxBuffer &dmx);
126  void CloseGPIOFDs();
127 
128  static const char GPIO_BASE_DIR[];
129 
130  DISALLOW_COPY_AND_ASSIGN(GPIODriver);
131 };
132 } // namespace gpio
133 } // namespace plugin
134 } // namespace ola
135 #endif // PLUGINS_GPIO_GPIODRIVER_H_