Skip to content

Commit

Permalink
Merge 849dcb5 into 8bc7854
Browse files Browse the repository at this point in the history
  • Loading branch information
peternewman committed Nov 28, 2014
2 parents 8bc7854 + 849dcb5 commit 0feb8c2
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
x/y/2014 ola-0.9.4
Features:
* Added support for the img Stage Line DMX-1USB - #398
*

API:
Expand Down
3 changes: 3 additions & 0 deletions debian/ola.udev
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ SUBSYSTEM=="usb|usb_device", ACTION=="add", ATTRS{idVendor}=="10cf", ATTRS{idPro

# udev rules for the Eurolite
SUBSYSTEM=="usb|usb_device", ACTION=="add", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="fa63", GROUP="plugdev" MODE="660"

# udev rules for the img Stage Line DMX device
SUBSYSTEM=="usb|usb_device", ACTION=="add", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="ff86", GROUP="plugdev"
47 changes: 47 additions & 0 deletions plugins/usbdmx/ImgStageLineDevice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* ImgStageLineDevice.cpp
* The img Stage Line DMX-1USB device
* Copyright (C) 2014 Peter Newman
*/

#include <string.h>
#include <sys/time.h>

#include "ola/Logging.h"
#include "plugins/usbdmx/ImgStageLineDevice.h"
#include "plugins/usbdmx/ImgStageLineOutputPort.h"

namespace ola {
namespace plugin {
namespace usbdmx {

/*
* Start this device.
*/
bool ImgStageLineDevice::StartHook() {
ImgStageLineOutputPort *output_port =
new ImgStageLineOutputPort(this, 0, m_usb_device);
if (!output_port->Start()) {
delete output_port;
return false;
}
AddPort(output_port);
return true;
}
} // namespace usbdmx
} // namespace plugin
} // namespace ola
50 changes: 50 additions & 0 deletions plugins/usbdmx/ImgStageLineDevice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* ImgStageLineDevice.h
* Interface for the ImgStageLine device
* Copyright (C) 2014 Peter Newman
*/

#ifndef PLUGINS_USBDMX_IMGSTAGELINEDEVICE_H_
#define PLUGINS_USBDMX_IMGSTAGELINEDEVICE_H_

#include <libusb.h>
#include <string>
#include "plugins/usbdmx/UsbDevice.h"

namespace ola {
namespace plugin {
namespace usbdmx {

/*
* An ImgStageLine device
*/
class ImgStageLineDevice: public UsbDevice {
public:
ImgStageLineDevice(ola::AbstractPlugin *owner,
libusb_device *usb_device)
: UsbDevice(owner, "ImgStageLine USB Device", usb_device) {
}

std::string DeviceId() const { return "dmx-1usb"; }

protected:
bool StartHook();
};
} // namespace usbdmx
} // namespace plugin
} // namespace ola
#endif // PLUGINS_USBDMX_IMGSTAGELINEDEVICE_H_
230 changes: 230 additions & 0 deletions plugins/usbdmx/ImgStageLineOutputPort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* ImgStageLineOutputPort.cpp
* Thread for the img Stage Line DMX-1USB Output Port
* Copyright (C) 2014 Peter Newman
*
* See the comments in ImgStageLineOutputPort.h
*/

#include <string.h>
#include <sys/types.h>
#include <algorithm>

#include "ola/Logging.h"
#include "plugins/usbdmx/ImgStageLineOutputPort.h"
#include "plugins/usbdmx/ImgStageLineDevice.h"


namespace ola {
namespace plugin {
namespace usbdmx {


/*
* Create a new ImgStageLineOutputPort object
*/
ImgStageLineOutputPort::ImgStageLineOutputPort(ImgStageLineDevice *parent,
unsigned int id,
libusb_device *usb_device)
: BasicOutputPort(parent, id),
m_term(false),
m_new_data(false),
m_usb_device(usb_device),
m_usb_handle(NULL),
m_packet_count(0) {
}


/*
* Cleanup
*/
ImgStageLineOutputPort::~ImgStageLineOutputPort() {
{
ola::thread::MutexLocker locker(&m_term_mutex);
m_term = true;
}
OLA_INFO << "In total sent " << m_packet_count << " packets";
Join();
}


/*
* Start this thread
*/
bool ImgStageLineOutputPort::Start() {
libusb_device_handle *usb_handle;

if (libusb_open(m_usb_device, &usb_handle)) {
OLA_WARN << "Failed to open img USB device";
return false;
}

int ret_code;
int config_value;
ret_code = libusb_get_configuration(usb_handle, &config_value);
if (ret_code) {
OLA_WARN << "img Stage Line get config failed, with libusb error code " <<
ret_code;
} else {
OLA_INFO << "img Stage Line original config is " << config_value;
}

ret_code = libusb_set_configuration(usb_handle, 1);
if (ret_code) {
OLA_WARN << "img Stage Line set config failed, with libusb error code " <<
ret_code;
}

ret_code = libusb_get_configuration(usb_handle, &config_value);
if (ret_code) {
OLA_WARN << "img Stage Line get config failed, with libusb error code " <<
ret_code;
} else {
OLA_INFO << "img Stage Line new config is " << config_value;
}

if (libusb_claim_interface(usb_handle, 0)) {
OLA_WARN << "Failed to claim img USB device";
libusb_close(usb_handle);
return false;
}

m_usb_handle = usb_handle;
bool ret = ola::thread::Thread::Start();
if (!ret) {
OLA_WARN << "pthread create failed";
libusb_release_interface(m_usb_handle, 0);
libusb_close(usb_handle);
return false;
}
return true;
}


/*
* Run this thread
*/
void *ImgStageLineOutputPort::Run() {
DmxBuffer buffer;
bool new_data;

if (!m_usb_handle)
return NULL;

while (true) {
{
ola::thread::MutexLocker locker(&m_term_mutex);
if (m_term)
break;
}

{
ola::thread::MutexLocker locker(&m_data_mutex);
buffer.Set(m_buffer);
new_data = m_new_data;
m_new_data = false;
}

if (new_data) {
if (!SendDMX(buffer)) {
OLA_WARN << "Send failed, stopping thread...";
break;
}
} else {
// sleep for a bit
usleep(40000);
}
}
libusb_release_interface(m_usb_handle, 0);
libusb_close(m_usb_handle);
return NULL;
}


/*
* Store the data in the shared buffer
*/
bool ImgStageLineOutputPort::WriteDMX(const DmxBuffer &buffer,
uint8_t priority) {
ola::thread::MutexLocker locker(&m_data_mutex);
m_buffer.Set(buffer);
m_new_data = true;
return true;
(void) priority;
}


/*
* Send DMX to the widget
*/
bool ImgStageLineOutputPort::SendDMX(const DmxBuffer &buffer) {
bool success = false;

m_packet_count++;
OLA_INFO << "About to send packet " << m_packet_count;

for (unsigned int i = 0;
i < DMX_MAX_TRANSMIT_CHANNELS;
i = i + CHANNELS_PER_PACKET) {
// zero everything
memset(m_packet, 0, IMGSTAGELINE_PACKET_SIZE);

if (i == 0) {
m_packet[0] = CHANNEL_HEADER_LOW;
} else if (i == CHANNELS_PER_PACKET) {
m_packet[0] = CHANNEL_HEADER_HIGH;
} else {
OLA_FATAL << "Unknown channel value " << i << ", couldn't find channel "
"header value";
}
OLA_INFO << "Sending sub packet " << i << " with header value "
<< static_cast<int>(m_packet[0]);

// Copy the data if there is some, otherwise we'll just send a packet of
// zeros for the channel values
if ((buffer.Size() - i) > 0) {
unsigned int channels = std::min((unsigned int) CHANNELS_PER_PACKET,
(buffer.Size() - i));

buffer.GetRange(i, &m_packet[1], &channels);
}

int transferred;
int r = libusb_bulk_transfer(
m_usb_handle,
ENDPOINT,
(unsigned char*) m_packet,
IMGSTAGELINE_PACKET_SIZE,
&transferred,
TIMEOUT);
if (transferred != IMGSTAGELINE_PACKET_SIZE) {
// not sure if this is fatal or not
OLA_WARN << "img driver failed to transfer all data, transferred "
<< transferred << " bytes, expected to send "
<< IMGSTAGELINE_PACKET_SIZE;
}
success = (r == 0);
if (!success) {
OLA_FATAL << "Failed with error " << r;
return success;
}
}
return success;
}
} // namespace usbdmx
} // namespace plugin
} // namespace ola

0 comments on commit 0feb8c2

Please sign in to comment.