Open Lighting Architecture  0.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SunliteOutputPort.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  * SunliteOutputPort.h
17  * The output port for a Sunlite USBDMX2 device.
18  * Copyright (C) 2010 Simon Newton
19  *
20  * It takes around 11ms to complete the transfer to the device so we use a
21  * a separate thread for the writes. The time to acquire the lock, copy the
22  * buffer & release is 1-2 uS.
23  */
24 
25 #ifndef PLUGINS_USBDMX_SUNLITEOUTPUTPORT_H_
26 #define PLUGINS_USBDMX_SUNLITEOUTPUTPORT_H_
27 
28 #include <libusb.h>
29 #include <pthread.h>
30 #include <string>
31 #include "ola/DmxBuffer.h"
32 #include "ola/thread/Thread.h"
33 #include "olad/Port.h"
34 
35 namespace ola {
36 namespace plugin {
37 namespace usbdmx {
38 
39 class SunliteDevice;
40 
42  public:
44  unsigned int id,
45  libusb_device *usb_device);
47 
48  bool Start();
49  void *Run();
50 
51  bool WriteDMX(const DmxBuffer &buffer, uint8_t priority);
52  std::string Description() const { return ""; }
53 
54  private:
55  enum {SUNLITE_PACKET_SIZE = 0x340};
56 
57  static const unsigned int CHUNKS_PER_PACKET = 26;
58  static const unsigned int CHANNELS_PER_CHUNK = 20;
59  static const unsigned int CHUNK_SIZE = 32;
60  static const uint8_t ENDPOINT = 1;
61  static const unsigned int TIMEOUT = 50; // 50ms is ok
62 
63  bool m_term;
64  bool m_new_data;
65  uint8_t m_packet[SUNLITE_PACKET_SIZE];
66  libusb_device *m_usb_device;
67  libusb_device_handle *m_usb_handle;
68  DmxBuffer m_buffer;
69  ola::thread::Mutex m_data_mutex;
70  ola::thread::Mutex m_term_mutex;
71 
72  void InitPacket();
73  bool SendDMX(const DmxBuffer &buffer);
74 };
75 } // namespace usbdmx
76 } // namespace plugin
77 } // namespace ola
78 #endif // PLUGINS_USBDMX_SUNLITEOUTPUTPORT_H_