Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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  * FtdiWidget.h
17  * This class is based on QLCFTDI class from
18  *
19  * Q Light Controller
20  * qlcftdi.h
21  *
22  * Copyright (C) Heikki Junnila
23  *
24  * Only standard CPP conversion was changed and function name changed
25  * to follow OLA coding standards.
26  *
27  * by Rui Barreiros
28  *
29  * Additional modifications to enable support for multiple outputs and
30  * additional devices ids did change the original structure.
31  *
32  * by E.S. Rosenberg a.k.a. Keeper of the Keys 5774/2014
33  */
34 
35 #ifndef PLUGINS_FTDIDMX_FTDIWIDGET_H_
36 #define PLUGINS_FTDIDMX_FTDIWIDGET_H_
37 
38 #include <ftdi.h>
39 #include <stdint.h>
40 #include <string.h>
41 
42 #include <string>
43 #include <vector>
44 
45 #include "ola/DmxBuffer.h"
46 
47 namespace ola {
48 namespace plugin {
49 namespace ftdidmx {
50 
55  public:
56  static const uint16_t FTDI_VID;
57  static const uint16_t FT232_PID;
58  static const uint16_t FT4232_PID;
59 
60  FtdiWidgetInfo(const std::string &name,
61  const std::string &serial,
62  unsigned int id,
63  const uint16_t vid = FTDI_VID,
64  const uint16_t pid = FT232_PID)
65  : m_name(name),
66  m_serial(serial),
67  m_id(id),
68  m_vid(vid),
69  m_pid(pid) {
70  }
71 
72  FtdiWidgetInfo(const FtdiWidgetInfo &info)
73  : m_name(info.Name()),
74  m_serial(info.Serial()),
75  m_id(info.Id()),
76  m_vid(info.Vid()),
77  m_pid(info.Pid()) {
78  }
79 
80  ~FtdiWidgetInfo() {}
81 
82  std::string Name() const { return m_name; }
83  std::string Serial() const { return m_serial; }
84 
85  unsigned int Id() const { return m_id; }
86  uint16_t Vid() const { return m_vid; }
87  uint16_t Pid() const { return m_pid; }
88 
89  std::string Description() const {
90  return m_name + " with serial number : " + m_serial + " ";
91  }
92 
93  FtdiWidgetInfo& operator=(const FtdiWidgetInfo &other) {
94  if (this != &other) {
95  m_name = other.Name();
96  m_serial = other.Serial();
97  m_id = other.Id();
98  m_vid = other.Vid();
99  m_pid = other.Pid();
100  }
101  return *this;
102  }
103 
104  private:
105  std::string m_name;
106  std::string m_serial;
107 
108  unsigned int m_id;
109  uint16_t m_vid;
110  uint16_t m_pid;
111 };
112 
116 class FtdiWidget {
117  public:
126  FtdiWidget(const std::string &serial,
127  const std::string &name,
128  uint32_t id = 0,
129  const uint16_t vid = FtdiWidgetInfo::FTDI_VID,
130  const uint16_t pid = FtdiWidgetInfo::FT232_PID);
131 
133  virtual ~FtdiWidget();
134 
136  std::string Serial() const { return m_serial; }
137 
139  std::string Name() const { return m_name; }
140 
141  uint16_t Vid() const { return m_vid; }
142  uint16_t Pid() const { return m_pid; }
143 
145  uint32_t Id() const { return m_id; }
146 
147  std::string Description() const {
148  return m_name + " with serial number : " + m_serial +" ";
149  }
150 
152  int GetInterfaceCount();
153 
158  static void Widgets(std::vector<FtdiWidgetInfo> *widgets);
159 
160  // From reading libftdi docs it seems they may reuse error codes, which is
161  // why I chose to name this const <lib>_<function>_<error>.
162  static const int libftdi_ftdi_usb_get_strings_get_serial_failed = -9;
163 
164  static bool m_missing_serial;
165 
166  private:
167  std::string m_serial;
168  std::string m_name;
169  uint32_t m_id;
170  const uint16_t m_vid;
171  const uint16_t m_pid;
172 };
173 
175  public:
176  FtdiInterface(const FtdiWidget * parent,
177  const ftdi_interface interface);
178 
179  virtual ~FtdiInterface();
180 
181  std::string Description() const {
182  return m_parent->Description();
183  }
184 
186  bool SetInterface();
187 
189  bool Open();
190 
192  bool Close();
193 
195  bool IsOpen() const;
196 
198  bool Reset();
199 
201  bool SetLineProperties();
202 
204  bool SetBaudRate(int speed = 250000);
205 
207  bool SetFlowControl();
208 
210  bool ClearRts();
211 
213  bool PurgeBuffers();
214 
216  bool SetBreak(bool on);
217 
219  bool Write(const ola::DmxBuffer &data);
220 
222  bool Read(unsigned char* buff, int size);
223 
225  bool SetupOutput();
226 
227  private:
228  const FtdiWidget * m_parent;
229  struct ftdi_context m_handle;
230  const ftdi_interface m_interface;
231 }; // FtdiInterface
232 } // namespace ftdidmx
233 } // namespace plugin
234 } // namespace ola
235 #endif // PLUGINS_FTDIDMX_FTDIWIDGET_H_
uint32_t Id() const
Get the widget's FTD2XX ID number.
Definition: FtdiWidget.h:145
bool SetLineProperties()
Setup communications line for 8N2 traffic.
Definition: FtdiWidget.cpp:277
bool SetBreak(bool on)
Toggle communications line BREAK condition on/off.
Definition: FtdiWidget.cpp:327
FtdiWidget(const std::string &serial, const std::string &name, uint32_t id=0, const uint16_t vid=FtdiWidgetInfo::FTDI_VID, const uint16_t pid=FtdiWidgetInfo::FT232_PID)
Construct a new FtdiWidget instance for one widget.
Definition: FtdiWidget.cpp:60
bool Open()
Open the widget.
Definition: FtdiWidget.cpp:226
bool Close()
Close the widget.
Definition: FtdiWidget.cpp:253
An FTDI widget.
Definition: FtdiWidget.h:116
static void Widgets(std::vector< FtdiWidgetInfo > *widgets)
Build a list of available ftdi widgets.
Definition: FtdiWidget.cpp:98
bool SetFlowControl()
Disable flow control.
Definition: FtdiWidget.cpp:297
std::string Name() const
Get the widget's USB name.
Definition: FtdiWidget.h:139
Used to hold a single universe of DMX data.
Definition: DmxBuffer.h:49
bool SetBaudRate(int speed=250000)
Set 250kbps baud rate.
Definition: FtdiWidget.cpp:287
virtual ~FtdiWidget()
Destructor.
Definition: FtdiWidget.cpp:71
A class used to hold a single universe of DMX data.
bool ClearRts()
Clear the RTS bit.
Definition: FtdiWidget.cpp:307
bool PurgeBuffers()
Purge TX & RX buffers.
Definition: FtdiWidget.cpp:317
std::string Serial() const
Get the widget's USB serial number.
Definition: FtdiWidget.h:136
bool SetupOutput()
Setup device for DMX Output.
Definition: FtdiWidget.cpp:365
bool Write(const ola::DmxBuffer &data)
Write data to a previously-opened line.
Definition: FtdiWidget.cpp:338
int GetInterfaceCount()
Get Widget available interface count.
Definition: FtdiWidget.cpp:81
bool SetInterface()
Set interface on the widget.
Definition: FtdiWidget.cpp:215
This class holds information about an attached FTDI chip.
Definition: FtdiWidget.h:54
bool Reset()
Reset the communications line.
Definition: FtdiWidget.cpp:267
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
bool IsOpen() const
Check if the widget is open.
Definition: FtdiWidget.cpp:263
bool Read(unsigned char *buff, int size)
Read data from a previously-opened line.
Definition: FtdiWidget.cpp:354
Definition: FtdiWidget.h:174