Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BaseUsbProWidget.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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * BaseUsbProWidget.h
17  * Read and Write to a USB Serial Widget that uses the Enttec Usb Pro frame
18  * layout.
19  * Copyright (C) 2010 Simon Newton
20  */
21 
22 #ifndef PLUGINS_USBPRO_BASEUSBPROWIDGET_H_
23 #define PLUGINS_USBPRO_BASEUSBPROWIDGET_H_
24 
25 #include <stdint.h>
26 #include <string>
27 #include "ola/Callback.h"
28 #include "ola/DmxBuffer.h"
29 #include "ola/io/Descriptor.h"
30 #include "plugins/usbpro/SerialWidgetInterface.h"
31 
32 namespace ola {
33 namespace plugin {
34 namespace usbpro {
35 
36 
37 /*
38  * A widget that implements the Usb Pro frame format.
39  */
41  public:
42  explicit BaseUsbProWidget(ola::io::ConnectedDescriptor *descriptor);
43  virtual ~BaseUsbProWidget();
44 
45  ola::io::ConnectedDescriptor *GetDescriptor() const {
46  return m_descriptor;
47  }
48  void DescriptorReady();
49 
50  // we locate the SendDMX in the base class since so many widgets share it.
51  virtual bool SendDMX(const DmxBuffer &buffer);
52 
53  bool SendMessage(uint8_t label,
54  const uint8_t *data,
55  unsigned int length) const;
56 
57  static ola::io::ConnectedDescriptor *OpenDevice(const string &path);
58 
59  static const uint8_t DMX_LABEL = 6;
60  static const uint8_t SERIAL_LABEL = 10;
61  static const uint8_t MANUFACTURER_LABEL = 77;
62  static const uint8_t DEVICE_LABEL = 78;
63  static const uint8_t HARDWARE_VERSION_LABEL = 14;
64 
65  private:
66  typedef enum {
67  PRE_SOM,
68  RECV_LABEL,
69  RECV_SIZE_LO,
70  RECV_SIZE_HI,
71  RECV_BODY,
72  RECV_EOM,
73  } receive_state;
74 
75  enum {MAX_DATA_SIZE = 600};
76 
77  typedef struct {
78  uint8_t som;
79  uint8_t label;
80  uint8_t len;
81  uint8_t len_hi;
82  } message_header;
83 
84  ola::io::ConnectedDescriptor *m_descriptor;
85  receive_state m_state;
86  unsigned int m_bytes_received;
87  message_header m_header;
88  uint8_t m_recv_buffer[MAX_DATA_SIZE];
89 
90  void ReceiveMessage();
91  virtual void HandleMessage(uint8_t label,
92  const uint8_t *data,
93  unsigned int length) = 0;
94 
95  static const uint8_t EOM = 0xe7;
96  static const uint8_t SOM = 0x7e;
97  static const unsigned int HEADER_SIZE;
98 };
99 
100 
106  public:
107  typedef ola::Callback3<void,
108  uint8_t,
109  const uint8_t*,
110  unsigned int> MessageCallback;
112  MessageCallback *callback)
113  : BaseUsbProWidget(descriptor),
114  m_callback(callback) {
115  }
116 
118  Stop();
119  }
120 
121  void Stop() {
122  if (m_callback)
123  delete m_callback;
124  }
125 
126  void SetHandler(MessageCallback *callback) {
127  if (m_callback)
128  delete m_callback;
129  m_callback = callback;
130  }
131 
132  private:
133  MessageCallback *m_callback;
134 
135  void HandleMessage(uint8_t label,
136  const uint8_t *data,
137  unsigned int length) {
138  m_callback->Run(label, data, length);
139  }
140 };
141 } // namespace usbpro
142 } // namespace plugin
143 } // namespace ola
144 #endif // PLUGINS_USBPRO_BASEUSBPROWIDGET_H_