Open Lighting Architecture  0.9.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FtdiWidget.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  * This class is based on QLCFTDI class from
17  *
18  * Q Light Controller
19  * qlcftdi.h
20  *
21  * Copyright (C) Heikki Junnila
22  *
23  * Only standard CPP conversion was changed and function name changed
24  * to follow OLA coding standards.
25  *
26  * by Rui Barreiros
27  *
28  * Additional modifications to enable support for multiple outputs and
29  * additional devices ids did change the original structure.
30  *
31  * by E.S. Rosenberg a.k.a. Keeper of the Keys 5774/2014
32  */
33 
34 #ifndef PLUGINS_FTDIDMX_FTDIWIDGET_H_
35 #define PLUGINS_FTDIDMX_FTDIWIDGET_H_
36 
37 #include <ftdi.h>
38 #include <stdint.h>
39 #include <string.h>
40 
41 #include <string>
42 #include <vector>
43 
44 #include "ola/DmxBuffer.h"
45 
46 namespace ola {
47 namespace plugin {
48 namespace ftdidmx {
49 
54  public:
55  static const uint16_t FTDI_VID;
56  static const uint16_t FT232_PID;
57  static const uint16_t FT4232_PID;
58 
59  FtdiWidgetInfo(const std::string &name,
60  const std::string &serial,
61  unsigned int id,
62  const uint16_t vid = FTDI_VID,
63  const uint16_t pid = FT232_PID)
64  : m_name(name),
65  m_serial(serial),
66  m_id(id),
67  m_vid(vid),
68  m_pid(pid) {
69  }
70 
71  FtdiWidgetInfo(const FtdiWidgetInfo &info)
72  : m_name(info.Name()),
73  m_serial(info.Serial()),
74  m_id(info.Id()),
75  m_vid(info.Vid()),
76  m_pid(info.Pid()) {
77  }
78 
79  ~FtdiWidgetInfo() {}
80 
81  std::string Name() const { return m_name; }
82  std::string Serial() const { return m_serial; }
83 
84  unsigned int Id() const { return m_id; }
85  uint16_t Vid() const { return m_vid; }
86  uint16_t Pid() const { return m_pid; }
87 
88  std::string Description() const {
89  return m_name + " with serial number : " + m_serial + " ";
90  }
91 
92  FtdiWidgetInfo& operator=(const FtdiWidgetInfo &other) {
93  if (this != &other) {
94  m_name = other.Name();
95  m_serial = other.Serial();
96  m_id = other.Id();
97  m_vid = other.Vid();
98  m_pid = other.Pid();
99  }
100  return *this;
101  }
102 
103  private:
104  std::string m_name;
105  std::string m_serial;
106 
107  unsigned int m_id;
108  uint16_t m_vid;
109  uint16_t m_pid;
110 };
111 
115 class FtdiWidget {
116  public:
125  FtdiWidget(const std::string &serial,
126  const std::string &name,
127  uint32_t id = 0,
128  const uint16_t vid = FtdiWidgetInfo::FTDI_VID,
129  const uint16_t pid = FtdiWidgetInfo::FT232_PID);
130 
132  virtual ~FtdiWidget();
133 
135  std::string Serial() const { return m_serial; }
136 
138  std::string Name() const { return m_name; }
139 
140  uint16_t Vid() const { return m_vid; }
141  uint16_t Pid() const { return m_pid; }
142 
144  uint32_t Id() const { return m_id; }
145 
146  std::string Description() const {
147  return m_name + " with serial number : " + m_serial +" ";
148  }
149 
151  int GetInterfaceCount();
152 
157  static void Widgets(std::vector<FtdiWidgetInfo> *widgets);
158 
159  // From reading libftdi docs it seems they may reuse error codes, which is
160  // why I chose to name this const <lib>_<function>_<error>.
161  static const int libftdi_ftdi_usb_get_strings_get_serial_failed = -9;
162 
163  static bool m_missing_serial;
164 
165  private:
166  std::string m_serial;
167  std::string m_name;
168  uint32_t m_id;
169  const uint16_t m_vid;
170  const uint16_t m_pid;
171 };
172 
174  public:
175  FtdiInterface(const FtdiWidget * parent,
176  const ftdi_interface interface);
177 
178  virtual ~FtdiInterface();
179 
180  std::string Description() const {
181  return m_parent->Description();
182  }
183 
185  bool SetInterface();
186 
188  bool Open();
189 
191  bool Close();
192 
194  bool IsOpen() const;
195 
197  bool Reset();
198 
200  bool SetLineProperties();
201 
203  bool SetBaudRate(int speed = 250000);
204 
206  bool SetFlowControl();
207 
209  bool ClearRts();
210 
212  bool PurgeBuffers();
213 
215  bool SetBreak(bool on);
216 
218  bool Write(const ola::DmxBuffer &data);
219 
221  bool Read(unsigned char* buff, int size);
222 
224  bool SetupOutput();
225 
226  private:
227  const FtdiWidget * m_parent;
228  struct ftdi_context m_handle;
229  const ftdi_interface m_interface;
230 }; // FtdiInterface
231 } // namespace ftdidmx
232 } // namespace plugin
233 } // namespace ola
234 #endif // PLUGINS_FTDIDMX_FTDIWIDGET_H_