Open Lighting Architecture  0.9.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BaseRobeWidget.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  * BaseRobeWidget.h
17  * Read and Write to a USB Widget that implements the Robe frame format.
18  * Copyright (C) 2011 Simon Newton
19  */
20 
21 #ifndef PLUGINS_USBPRO_BASEROBEWIDGET_H_
22 #define PLUGINS_USBPRO_BASEROBEWIDGET_H_
23 
24 #include <stdint.h>
25 #include "ola/Callback.h"
26 #include "ola/DmxBuffer.h"
27 #include "ola/io/Descriptor.h"
28 #include "plugins/usbpro/SerialWidgetInterface.h"
29 
30 namespace ola {
31 namespace plugin {
32 namespace usbpro {
33 
34 
35 /*
36  * A widget that implements the Robe frame format.
37  */
39  public:
40  explicit BaseRobeWidget(ola::io::ConnectedDescriptor *descriptor);
41  virtual ~BaseRobeWidget();
42 
43  ola::io::ConnectedDescriptor *GetDescriptor() const {
44  return m_descriptor;
45  }
46 
47  bool SendMessage(uint8_t label,
48  const uint8_t *data,
49  unsigned int length) const;
50 
51  static const uint8_t CHANNEL_A_OUT = 0x06;
52  static const uint8_t INFO_REQUEST = 0x14;
53  static const uint8_t INFO_RESPONSE = 0x15;
54  static const uint8_t RDM_DISCOVERY = 0x12;
55  static const uint8_t RDM_DISCOVERY_RESPONSE = 0x13;
56  static const uint8_t RDM_REQUEST = 0x10;
57  static const uint8_t RDM_RESPONSE = 0x11;
58  static const uint8_t UID_REQUEST = 0x24;
59  static const uint8_t UID_RESPONSE = 0x25;
60  static const uint8_t DMX_IN_REQUEST = 0x04;
61  static const uint8_t DMX_IN_RESPONSE = 0x05;
62 
63  private:
64  typedef enum {
65  PRE_SOM,
66  RECV_PACKET_TYPE,
67  RECV_SIZE_LO,
68  RECV_SIZE_HI,
69  RECV_HEADER_CRC,
70  RECV_BODY,
71  RECV_CRC,
72  } receive_state;
73 
74  enum {MAX_DATA_SIZE = 522};
75 
76  typedef struct {
77  uint8_t som;
78  uint8_t packet_type;
79  uint8_t len;
80  uint8_t len_hi;
81  uint8_t header_crc;
82  } message_header;
83 
84  ola::io::ConnectedDescriptor *m_descriptor;
85  receive_state m_state;
86  unsigned int m_bytes_received, m_data_size;
87  uint8_t m_crc;
88  message_header m_header;
89  uint8_t m_recv_buffer[MAX_DATA_SIZE];
90 
91  void DescriptorReady();
92  void ReceiveMessage();
93  virtual void HandleMessage(uint8_t label,
94  const uint8_t *data,
95  unsigned int length) = 0;
96 
97  static const uint8_t SOM = 0xa5;
98  static const unsigned int HEADER_SIZE;
99 };
100 
101 
102 /*
103  * DispatchingRobeWidget
104  */
106  public:
107  typedef ola::Callback3<void,
108  uint8_t,
109  const uint8_t*,
110  unsigned int> MessageCallback;
112  MessageCallback *callback = NULL)
113  : BaseRobeWidget(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_BASEROBEWIDGET_H_