Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RobeWidget.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  * RobeWidget.h
17  * Read and Write to a USB Widget.
18  * Copyright (C) 2011 Simon Newton
19  */
20 
21 #ifndef PLUGINS_USBPRO_ROBEWIDGET_H_
22 #define PLUGINS_USBPRO_ROBEWIDGET_H_
23 
24 #include <stdint.h>
25 #include <memory>
26 #include "ola/Callback.h"
27 #include "ola/DmxBuffer.h"
28 #include "ola/io/Descriptor.h"
29 #include "ola/rdm/DiscoveryAgent.h"
31 #include "ola/rdm/RDMCommand.h"
33 #include "ola/rdm/UID.h"
34 #include "ola/rdm/UIDSet.h"
35 #include "ola/thread/SchedulingExecutorInterface.h"
36 #include "plugins/usbpro/BaseRobeWidget.h"
37 
38 class RobeWidgetTest;
39 
40 
41 namespace ola {
42 namespace plugin {
43 namespace usbpro {
44 
45 using std::auto_ptr;
46 
47 /*
48  * A Robe USB Widget implementation.
49  */
53  public:
54  explicit RobeWidgetImpl(ola::io::ConnectedDescriptor *descriptor,
55  const ola::rdm::UID &uid);
56  ~RobeWidgetImpl() {}
57 
58  void Stop();
59 
60  bool SendDMX(const DmxBuffer &buffer);
61 
62  void SendRDMRequest(const ola::rdm::RDMRequest *request,
63  ola::rdm::RDMCallback *on_complete);
66 
67  // incoming DMX methods
68  bool ChangeToReceiveMode();
69  // ownership of the callback is transferred
70  void SetDmxCallback(Callback0<void> *callback);
71  const DmxBuffer &FetchDMX() {
72  return m_buffer;
73  }
74 
75  // The following are the implementation of DiscoveryTargetInterface
76  void MuteDevice(const ola::rdm::UID &target,
77  MuteDeviceCallback *mute_complete);
78  void UnMuteAll(UnMuteDeviceCallback *unmute_complete);
79  void Branch(const ola::rdm::UID &lower,
80  const ola::rdm::UID &upper,
81  BranchCallback *callback);
82 
83  static const int DMX_FRAME_DATA_SIZE;
84 
85  private:
86  ola::rdm::RDMCallback *m_rdm_request_callback;
87  MuteDeviceCallback *m_mute_callback;
88  UnMuteDeviceCallback *m_unmute_callback;
89  BranchCallback *m_branch_callback;
90  ola::rdm::DiscoveryAgent m_discovery_agent;
91  auto_ptr<Callback0<void> > m_dmx_callback;
92  DmxBuffer m_buffer;
93  const ola::rdm::RDMRequest *m_pending_request;
94  const ola::rdm::UID m_uid;
95  uint8_t m_transaction_number;
96 
97  void HandleMessage(uint8_t label,
98  const uint8_t *data,
99  unsigned int length);
100  void HandleRDMResponse(const uint8_t *data,
101  unsigned int length);
102  void HandleDiscoveryResponse(const uint8_t *data,
103  unsigned int length);
104  void DiscoveryComplete(ola::rdm::RDMDiscoveryCallback *callback,
105  bool status,
106  const ola::rdm::UIDSet &uids);
107  void HandleDmxFrame(const uint8_t *data, unsigned int length);
108  bool PackAndSendRDMRequest(uint8_t label,
109  const ola::rdm::RDMRequest *request);
110  static const unsigned int RDM_PADDING_BYTES = 4;
111 };
112 
113 
114 /*
115  * A Robe Widget. This mostly just wraps the implementation.
116  */
119  public:
121  const ola::rdm::UID &uid,
122  unsigned int queue_size = 20);
123  ~RobeWidget();
124 
125  void Stop() { m_impl->Stop(); }
126 
127  ola::io::ConnectedDescriptor *GetDescriptor() const {
128  return m_impl->GetDescriptor();
129  }
130 
131  bool SendDMX(const DmxBuffer &buffer) {
132  return m_impl->SendDMX(buffer);
133  }
134 
136  ola::rdm::RDMCallback *on_complete) {
137  m_controller->SendRDMRequest(request, on_complete);
138  }
139 
141  m_impl->RunFullDiscovery(callback);
142  }
143 
144  void RunIncrementalDiscovery(ola::rdm::RDMDiscoveryCallback *callback) {
145  m_impl->RunIncrementalDiscovery(callback);
146  }
147 
148  bool ChangeToReceiveMode() {
149  return m_impl->ChangeToReceiveMode();
150  }
151 
152  void SetDmxCallback(Callback0<void> *callback) {
153  m_impl->SetDmxCallback(callback);
154  }
155 
156  const DmxBuffer& FetchDMX() {
157  return m_impl->FetchDMX();
158  }
159 
160  // the tests access the implementation directly.
161  friend class ::RobeWidgetTest;
162 
163  private:
164  // we need to control the order of construction & destruction here so these
165  // are pointers.
166  RobeWidgetImpl *m_impl;
168 };
169 } // namespace usbpro
170 } // namespace plugin
171 } // namespace ola
172 #endif // PLUGINS_USBPRO_ROBEWIDGET_H_