Newer
Older
/*
* Fadecandy driver for the Enttec DMX USB Pro.
*
* Copyright (c) 2013 Micah Elizabeth Scott
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "enttecdmxdevice.h"
#include <sstream>
#include <iostream>
EnttecDMXDevice::Transfer::Transfer(EnttecDMXDevice *device, void *buffer, int length)
: transfer(libusb_alloc_transfer(0)),
device(device)
{
libusb_fill_bulk_transfer(transfer, device->mHandle,
OUT_ENDPOINT, (uint8_t*) buffer, length, EnttecDMXDevice::completeTransfer, this, 2000);
}
EnttecDMXDevice::Transfer::~Transfer()
{
libusb_free_transfer(transfer);
}
EnttecDMXDevice::EnttecDMXDevice(libusb_device *device, bool verbose)
: USBDevice(device, verbose),
mFoundEnttecStrings(false),
mConfigMap(0)
{
mSerial[0] = '\0';
// Initialize a minimal valid DMX packet
memset(&mChannelBuffer, 0, sizeof mChannelBuffer);
mChannelBuffer.start = START_OF_MESSAGE;
mChannelBuffer.label = SEND_DMX_PACKET;
mChannelBuffer.data[0] = START_CODE;
setChannel(1, 0);
}
EnttecDMXDevice::~EnttecDMXDevice()
{
/*
* If we have pending transfers, cancel them and jettison them
* from the EnttecDMXDevice. The Transfer objects themselves will be freed
* once libusb completes them.
*/
for (std::set<Transfer*>::iterator i = mPending.begin(), e = mPending.end(); i != e; ++i) {
Transfer *fct = *i;
libusb_cancel_transfer(fct->transfer);
fct->device = 0;
}
}
bool EnttecDMXDevice::probe(libusb_device *device)
{
/*
* Prior to opening the device, all we can do is look for an FT245 device.
* We'll take a closer look in probeAfterOpening(), once we can see the
* string descriptors.
*/
libusb_device_descriptor dd;
if (libusb_get_device_descriptor(device, &dd) < 0) {
// Can't access descriptor?
return false;
}
// FTDI FT245
return dd.idVendor == 0x0403 && dd.idProduct == 0x6001;
}
int EnttecDMXDevice::open()
{
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
libusb_device_descriptor dd;
int r = libusb_get_device_descriptor(mDevice, &dd);
if (r < 0) {
return r;
}
r = libusb_open(mDevice, &mHandle);
if (r < 0) {
return r;
}
/*
* Match the manufacturer and product strings! This is the least intrusive way to
* determine that the attached device is in fact an Enttec DMX USB Pro, since it doesn't
* have a unique vendor/product ID.
*/
if (dd.iManufacturer && dd.iProduct && dd.iSerialNumber) {
char manufacturer[256];
char product[256];
r = libusb_get_string_descriptor_ascii(mHandle, dd.iManufacturer, (uint8_t*)manufacturer, sizeof manufacturer);
if (r < 0) {
return r;
}
r = libusb_get_string_descriptor_ascii(mHandle, dd.iProduct, (uint8_t*)product, sizeof product);
if (r < 0) {
return r;
}
mFoundEnttecStrings = !strcmp(manufacturer, "ENTTEC") && !strcmp(product, "DMX USB PRO");
}
/*
* Only go further if we have in fact found evidence that this is the right device.
*/
if (mFoundEnttecStrings) {
// Only relevant on linux; try to detach the FTDI driver.
libusb_detach_kernel_driver(mHandle, 0);
r = libusb_claim_interface(mHandle, 0);
if (r < 0) {
return r;
}
r = libusb_get_string_descriptor_ascii(mHandle, dd.iSerialNumber, (uint8_t*)mSerial, sizeof mSerial);
if (r < 0) {
return r;
}
}
return 0;
}
bool EnttecDMXDevice::probeAfterOpening()
{
// By default, any device is supported by the time we get to opening it.
return mFoundEnttecStrings;
}
bool EnttecDMXDevice::matchConfiguration(const Value &config)
{
if (matchConfigurationWithTypeAndSerial(config, "enttec", mSerial)) {
mConfigMap = findConfigMap(config);
return true;
}
return false;
}
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
std::string EnttecDMXDevice::getName()
{
std::ostringstream s;
s << "Enttec DMX USB Pro";
if (mSerial[0]) {
s << " (Serial# " << mSerial << ")";
}
return s.str();
}
void EnttecDMXDevice::setChannel(unsigned n, uint8_t value)
{
if (n >= 1 && n <= 512) {
unsigned len = std::max<unsigned>(mChannelBuffer.length, n + 1);
mChannelBuffer.length = len;
mChannelBuffer.data[n] = value;
mChannelBuffer.data[len] = END_OF_MESSAGE;
}
}
void EnttecDMXDevice::submitTransfer(Transfer *fct)
{
/*
* Submit a new USB transfer. The Transfer object is guaranteed to be freed eventually.
* On error, it's freed right away.
*/
int r = libusb_submit_transfer(fct->transfer);
if (r < 0) {
if (mVerbose && r != LIBUSB_ERROR_PIPE) {
std::clog << "Error submitting USB transfer: " << libusb_strerror(libusb_error(r)) << "\n";
}
delete fct;
} else {
mPending.insert(fct);
}
}
void EnttecDMXDevice::completeTransfer(struct libusb_transfer *transfer)
{
/*
* Transfer complete. The EnttecDMXDevice may or may not still exist; if the device was unplugged,
* fct->device will be set to 0 by ~EnttecDMXDevice().
*/
EnttecDMXDevice::Transfer *fct = static_cast<EnttecDMXDevice::Transfer*>(transfer->user_data);
EnttecDMXDevice *self = fct->device;
if (self) {
self->mPending.erase(fct);
}
delete fct;
}
void EnttecDMXDevice::writeDMXPacket()
{
/*
* Asynchronously write an FTDI packet containing an Enttec packet containing
* our set of DMX channels.
*
* XXX: We should probably throttle this so that we don't send DMX messages
* faster than the Enttec device can keep up!
*/
submitTransfer(new Transfer(this, &mChannelBuffer, mChannelBuffer.length + 5));
}
void EnttecDMXDevice::writeMessage(const OPCSink::Message &msg)
{
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Dispatch an incoming OPC command
*/
switch (msg.command) {
case OPCSink::SetPixelColors:
opcSetPixelColors(msg);
writeDMXPacket();
return;
case OPCSink::SystemExclusive:
// No relevant SysEx for this device
return;
}
if (mVerbose) {
std::clog << "Unsupported OPC command: " << unsigned(msg.command) << "\n";
}
}
void EnttecDMXDevice::opcSetPixelColors(const OPCSink::Message &msg)
{
/*
* Parse through our device's mapping, and store any relevant portions of 'msg'
* in the framebuffer.
*/
if (!mConfigMap) {
// No mapping defined yet. This device is inactive.
return;
}
const Value &map = *mConfigMap;
for (unsigned i = 0, e = map.Size(); i != e; i++) {
opcMapPixelColors(msg, map[i]);
}
void EnttecDMXDevice::opcMapPixelColors(const OPCSink::Message &msg, const Value &inst)
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Parse one JSON mapping instruction, and copy any relevant parts of 'msg'
* into our framebuffer. This looks for any mapping instructions that we
* recognize:
*
* [ OPC Channel, OPC Pixel, Pixel Color, DMX Channel ]
*/
unsigned msgPixelCount = msg.length() / 3;
if (inst.IsArray() && inst.Size() == 4) {
// Map a range from an OPC channel to our framebuffer
const Value &vChannel = inst[0u];
const Value &vPixelIndex = inst[1];
const Value &vPixelColor = inst[2];
const Value &vDMXChannel = inst[3];
if (vChannel.IsUint() && vPixelIndex.IsUint() && vPixelColor.IsString() && vDMXChannel.IsUint()) {
unsigned channel = vChannel.GetUint();
unsigned pixelIndex = vPixelIndex.GetUint();
const char *pixelColor = vPixelColor.GetString();
unsigned dmxChannel = vDMXChannel.GetUint();
if (channel != msg.channel || pixelIndex >= msgPixelCount) {
return;
}
const uint8_t *pixel = msg.data + (pixelIndex * 3);
switch (pixelColor[0]) {
case 'r':
setChannel(dmxChannel, pixel[0]);
break;
case 'g':
setChannel(dmxChannel, pixel[1]);
break;
case 'b':
setChannel(dmxChannel, pixel[2]);
break;
case 'l':
setChannel(dmxChannel, (unsigned(pixel[0]) + unsigned(pixel[1]) + unsigned(pixel[2])) / 3);
break;
}
return;
}
}
// Still haven't found a match?
if (mVerbose) {
std::clog << "Unsupported JSON mapping instruction\n";
}