Open Lighting Architecture  0.9.5
 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 std::string &path);
58 
59  static const uint8_t DEVICE_LABEL = 78;
60  static const uint8_t DMX_LABEL = 6;
61  static const uint8_t GET_PARAMS = 3;
62  static const uint8_t HARDWARE_VERSION_LABEL = 14;
63  static const uint8_t MANUFACTURER_LABEL = 77;
64  static const uint8_t SERIAL_LABEL = 10;
65 
66  private:
67  typedef enum {
68  PRE_SOM,
69  RECV_LABEL,
70  RECV_SIZE_LO,
71  RECV_SIZE_HI,
72  RECV_BODY,
73  RECV_EOM,
74  } receive_state;
75 
76  enum {MAX_DATA_SIZE = 600};
77 
78  typedef struct {
79  uint8_t som;
80  uint8_t label;
81  uint8_t len;
82  uint8_t len_hi;
83  } message_header;
84 
85  ola::io::ConnectedDescriptor *m_descriptor;
86  receive_state m_state;
87  unsigned int m_bytes_received;
88  message_header m_header;
89  uint8_t m_recv_buffer[MAX_DATA_SIZE];
90 
91  void ReceiveMessage();
92  virtual void HandleMessage(uint8_t label,
93  const uint8_t *data,
94  unsigned int length) = 0;
95 
96  static const uint8_t EOM = 0xe7;
97  static const uint8_t SOM = 0x7e;
98  static const unsigned int HEADER_SIZE;
99 };
100 
101 
107  public:
108  typedef ola::Callback3<void,
109  uint8_t,
110  const uint8_t*,
111  unsigned int> MessageCallback;
113  MessageCallback *callback)
114  : BaseUsbProWidget(descriptor),
115  m_callback(callback) {
116  }
117 
119  Stop();
120  }
121 
122  void Stop() {
123  if (m_callback)
124  delete m_callback;
125  }
126 
127  void SetHandler(MessageCallback *callback) {
128  if (m_callback)
129  delete m_callback;
130  m_callback = callback;
131  }
132 
133  private:
134  MessageCallback *m_callback;
135 
136  void HandleMessage(uint8_t label,
137  const uint8_t *data,
138  unsigned int length) {
139  m_callback->Run(label, data, length);
140  }
141 };
142 } // namespace usbpro
143 } // namespace plugin
144 } // namespace ola
145 #endif // PLUGINS_USBPRO_BASEUSBPROWIDGET_H_