Merge pull request #469 from redPanther/fadecandy

fadecandy fix

Former-commit-id: 4686a0ead7ba44c13dbc4ac1f0f876d9a4d165c3
This commit is contained in:
tvdzwan 2016-02-08 14:52:48 +01:00
commit 3280f7198d
4 changed files with 93 additions and 15 deletions

View File

@ -0,0 +1,79 @@
Fadecandy: Open Pixel Control Protocol
======================================
The Fadecandy Server (`fcserver`) operates as a bridge between LED controllers attached over USB, and visual effects that communicate via a TCP socket.
The primary protocol supported by `fcserver` is [Open Pixel Control](http://openpixelcontrol.org), a super simple way to send RGB values over a socket. We support the standard Open Pixel Control commands, as well as some Fadecandy extensions.
Socket
------
Open Pixel Control uses a TCP socket, by default on port 7890. For the best performance, remember to set TCP_NODELAY socket option.
Command Format
--------------
All OPC commands follow the same general format. All multi-byte values in Open Pixel Control are in network byte order, high byte followed by low byte.
Channel | Command | Length (N) | Data
---------- | --------- | ---------- | --------------------------
1 byte | 1 byte | 2 bytes | N bytes of message data
Set Pixel Colors
----------------
Video data arrives in a **Set Pixel Colors** command:
Byte | **Set Pixel Colors** command
------ | --------------------------------
0 | Channel Number
1 | Command (0x00)
2 - 3 | Data length
4 | Pixel #0, Red
5 | Pixel #0, Green
6 | Pixel #0, Blue
7 | Pixel #1, Red
8 | Pixel #1, Green
9 | Pixel #1, Blue
… | …
As soon as a complete Set Pixel Colors command is received, a new frame of video will be broadcast simultaneously to all attached Fadecandy devices.
Set Global Color Correction
---------------------------
The color correction data (from the 'color' configuration key) can also be changed at runtime, by sending a new blob of JSON text in a Fadecandy-specific command. Fadecandy's 16-bit System ID for Open Pixel Control's System Exclusive (0xFF) command is **0x0001**.
Byte | **Set Global Color Correction** command
------ | ------------------------------------------
0 | Channel Number (0x00, reserved)
1 | Command (0xFF, System Exclusive)
2 - 3 | Data length (JSON Length + 4)
4 - 5 | System ID (0x0001, Fadecandy)
6 - 7 | SysEx ID (0x0001, Set Global Color Correction)
8 - … | JSON Text
Set Firmware Configuration
--------------------------
The firmware supports some runtime configuration options. Any OPC client can send a new firmware configuration packet using this command. If the supplied data is shorter than the firmware's configuration buffer, only the provided bytes will be changed.
Byte | **Set Firmware Configuration** command
------ | ------------------------------------------
0 | Channel Number (0x00, reserved)
1 | Command (0xFF, System Exclusive)
2 - 3 | Data length (Configuration Length + 4)
4 - 5 | System ID (0x0001, Fadecandy)
6 - 7 | SysEx ID (0x0002, Set Firmware Configuration)
8 - … | Configuration Data
Current firmwares support the following configuration options:
Byte Offset | Bits | Description
----------- | ------ | ------------
0 | 7 … 4 | (reserved)
0 | 3 | Manual LED control bit
0 | 2 | 0 = LED shows USB activity, 1 = LED under manual control
0 | 1 | Disable keyframe interpolation
0 | 0 | Disable dithering
1 … 62 | 7 … 0 | (reserved)

View File

@ -246,9 +246,10 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
} }
else if (type == "fadecandy") else if (type == "fadecandy")
{ {
const std::string host = deviceConfig.get("output", "127.0.0.1").asString(); const std::string host = deviceConfig.get("output", "127.0.0.1").asString();
const uint16_t port = deviceConfig.get("port", 7890).asInt(); const uint16_t port = deviceConfig.get("port", 7890).asInt();
device = new LedDeviceFadeCandy(host,port); const uint16_t channel = deviceConfig.get("channel", 0).asInt();
device = new LedDeviceFadeCandy(host, port, channel);
} }
else if (type == "tpm2") else if (type == "tpm2")
{ {

View File

@ -1,16 +1,15 @@
#include "LedDeviceFadeCandy.h" #include "LedDeviceFadeCandy.h"
static const unsigned MAX_NUM_LEDS = 512; static const unsigned MAX_NUM_LEDS = 10000; // OPC can handle 21845 leds - in theory, fadecandy device should handle 10000 leds
static const unsigned OPC_BROADCAST = 0; // OPC broadcast channel static const unsigned OPC_SET_PIXELS = 0; // OPC command codes
static const unsigned OPC_SET_PIXELS = 0; // OPC command codes static const unsigned OPC_HEADER_SIZE = 4; // OPC header size
static const unsigned OPC_HEADER_SIZE = 4; // OPC header size
LedDeviceFadeCandy::LedDeviceFadeCandy(const std::string& host, const uint16_t port) : LedDeviceFadeCandy::LedDeviceFadeCandy(const std::string& host, const uint16_t port, const unsigned channel) :
_host(host), _port(port) _host(host), _port(port), _channel(channel)
{ {
_opc_data.resize( OPC_HEADER_SIZE ); _opc_data.resize( OPC_HEADER_SIZE );
_opc_data[0] = OPC_BROADCAST; _opc_data[0] = channel;
_opc_data[1] = OPC_SET_PIXELS; _opc_data[1] = OPC_SET_PIXELS;
_opc_data[2] = 0; _opc_data[2] = 0;
_opc_data[3] = 0; _opc_data[3] = 0;
@ -32,11 +31,9 @@ bool LedDeviceFadeCandy::isConnected()
bool LedDeviceFadeCandy::tryConnect() bool LedDeviceFadeCandy::tryConnect()
{ {
if ( _client.state() == QAbstractSocket::UnconnectedState ) { if ( _client.state() == QAbstractSocket::UnconnectedState ) {
qDebug("connecting to %s %i",_host.c_str(),_port);
_client.connectToHost( _host.c_str(), _port); _client.connectToHost( _host.c_str(), _port);
if ( _client.waitForConnected(1000) ) if ( _client.waitForConnected(1000) )
qDebug("connected"); qDebug("fadecandy/opc: connected to %s:%i on channel %i", _host.c_str(), _port, _channel);
} }
return isConnected(); return isConnected();
@ -51,7 +48,7 @@ int LedDeviceFadeCandy::write( const std::vector<ColorRgb> & ledValues )
if (nrLedValues > MAX_NUM_LEDS) if (nrLedValues > MAX_NUM_LEDS)
{ {
std::cerr << "Invalid attempt to write led values. Not more than " << MAX_NUM_LEDS << " leds are allowed." << std::endl; std::cerr << "fadecandy/opc: Invalid attempt to write led values. Not more than " << MAX_NUM_LEDS << " leds are allowed." << std::endl;
return -1; return -1;
} }

View File

@ -23,7 +23,7 @@ public:
/// @param host The ip address/host name of fadecandy/opc server /// @param host The ip address/host name of fadecandy/opc server
/// @param port The port to use (fadecandy default is 7890) /// @param port The port to use (fadecandy default is 7890)
/// ///
LedDeviceFadeCandy(const std::string& host, const uint16_t port); LedDeviceFadeCandy(const std::string& host, const uint16_t port, const unsigned channel);
/// ///
/// Destructor of the LedDevice; closes the tcp client /// Destructor of the LedDevice; closes the tcp client
@ -46,6 +46,7 @@ private:
QTcpSocket _client; QTcpSocket _client;
const std::string _host; const std::string _host;
const uint16_t _port; const uint16_t _port;
const unsigned _channel;
QByteArray _opc_data; QByteArray _opc_data;
/// try to establish connection to opc server, if not connected yet /// try to establish connection to opc server, if not connected yet