Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UltraDMXProDevice.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  * UltraDMXProDevice.h
17  * A DMX King Ultra DMX Pro Device
18  * Copyright (C) 2011 Simon Newton
19  */
20 
21 #ifndef PLUGINS_USBPRO_ULTRADMXPRODEVICE_H_
22 #define PLUGINS_USBPRO_ULTRADMXPRODEVICE_H_
23 
24 #include <string>
25 #include <sstream>
26 #include "ola/DmxBuffer.h"
27 #include "olad/TokenBucket.h"
28 #include "olad/PluginAdaptor.h"
29 #include "olad/Port.h"
30 
31 #include "plugins/usbpro/UltraDMXProWidget.h"
32 #include "plugins/usbpro/UsbSerialDevice.h"
33 #include "plugins/usbpro/messages/UsbProConfigMessages.pb.h"
34 
35 namespace ola {
36 namespace plugin {
37 namespace usbpro {
38 
39 using google::protobuf::RpcController;
40 using ola::plugin::usbpro::Request;
41 
42 
43 /*
44  * An Ultra DMX Pro device
45  */
47  public:
48  UltraDMXProDevice(ola::PluginAdaptor *plugin_adaptor,
49  ola::AbstractPlugin *owner,
50  const string &name,
51  UltraDMXProWidget *widget,
52  uint16_t esta_id,
53  uint16_t device_id,
54  uint32_t serial,
55  unsigned int fps_limit);
56 
57  string DeviceId() const { return m_serial; }
58  // both output ports can be bound to the same universe
59  bool AllowMultiPortPatching() const { return true; }
60 
61  void Configure(RpcController *controller,
62  const string &request,
63  string *response,
64  google::protobuf::Closure *done);
65 
66  protected:
67  void PrePortStop();
68 
69  private:
70  void UpdateParams(bool status, const usb_pro_parameters &params);
71 
72  void HandleParametersRequest(RpcController *controller,
73  const Request *request,
74  string *response,
75  google::protobuf::Closure *done);
76 
77  void HandleParametersResponse(RpcController *controller,
78  string *response,
79  google::protobuf::Closure *done,
80  bool status,
81  const usb_pro_parameters &params);
82 
83  void HandleSerialRequest(RpcController *controller,
84  const Request *request,
85  string *response,
86  google::protobuf::Closure *done);
87 
88  UltraDMXProWidget *m_ultra_widget;
89  string m_serial;
90 
91  bool m_got_parameters;
92  uint8_t m_break_time;
93  uint8_t m_mab_time;
94  uint8_t m_rate;
95 };
96 
97 
98 /*
99  * The Input port
100  */
102  public:
104  UltraDMXProWidget *widget,
105  unsigned int id,
106  ola::PluginAdaptor *plugin_adaptor,
107  const string &serial)
108  : BasicInputPort(parent, id, plugin_adaptor),
109  m_serial(serial),
110  m_widget(widget) {}
111 
112  const DmxBuffer &ReadDMX() const {
113  return m_widget->FetchDMX();
114  }
115 
116  string Description() const {
117  std::stringstream str;
118  str << "Serial " << m_serial;
119  return str.str();
120  }
121 
122  private:
123  string m_serial;
124  UltraDMXProWidget *m_widget;
125 };
126 
127 
128 /*
129  * The output port, we have two of these per device.
130  */
132  public:
134  UltraDMXProWidget *widget,
135  unsigned int id,
136  string serial,
137  const TimeStamp *wake_time,
138  unsigned int max_burst,
139  unsigned int rate,
140  bool primary)
141  : BasicOutputPort(parent, id),
142  m_serial(serial),
143  m_widget(widget),
144  m_bucket(max_burst, rate, max_burst, *wake_time),
145  m_wake_time(wake_time),
146  m_primary(primary) {}
147 
148  bool WriteDMX(const DmxBuffer &buffer, uint8_t priority) {
149  if (m_bucket.GetToken(*m_wake_time))
150  if (m_primary)
151  return m_widget->SendDMX(buffer);
152  else
153  return m_widget->SendSecondaryDMX(buffer);
154  else
155  OLA_INFO << "Port rated limited, dropping frame";
156  return true;
157  (void) priority;
158  }
159 
160  string Description() const {
161  std::stringstream str;
162  str << "Serial " << m_serial << ", " <<
163  (m_primary ? "Port 1" : "Port 2");
164  return str.str();
165  }
166 
167  private:
168  string m_serial;
169  UltraDMXProWidget *m_widget;
170  TokenBucket m_bucket;
171  const TimeStamp *m_wake_time;
172  bool m_primary;
173 };
174 } // namespace usbpro
175 } // namespace plugin
176 } // namespace ola
177 #endif // PLUGINS_USBPRO_ULTRADMXPRODEVICE_H_