Leddevice refactoring the next part (#253)

* add general switchOff

* refactoring of leddevices regarding ledcount and switchoff

* remove obsolete includes
This commit is contained in:
redPanther 2016-09-23 08:49:22 +02:00 committed by GitHub
parent 5aeec2e1e5
commit d6a34edfb2
71 changed files with 274 additions and 1073 deletions

View File

@ -1,9 +1,13 @@
#pragma once #pragma once
#include <QObject>
#include <QString>
// STL incldues // STL incldues
#include <vector> #include <vector>
#include <QObject> #include <string>
#include <map> #include <map>
#include <algorithm>
// Utility includes // Utility includes
#include <utils/ColorRgb.h> #include <utils/ColorRgb.h>
@ -32,17 +36,10 @@ public:
/// ///
virtual ~LedDevice() {} virtual ~LedDevice() {}
///
/// Writes the RGB-Color values to the leds.
///
/// @param[in] ledValues The RGB-color per led
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb>& ledValues) = 0;
/// Switch the leds off /// Switch the leds off
virtual int switchOff() = 0; virtual int switchOff();
virtual int setLedValues(const std::vector<ColorRgb>& ledValues);
/// ///
/// Opens and configures the output device /// Opens and configures the output device
@ -58,6 +55,15 @@ public:
static Json::Value getLedDeviceSchemas(); static Json::Value getLedDeviceSchemas();
protected: protected:
///
/// Writes the RGB-Color values to the leds.
///
/// @param[in] ledValues The RGB-color per led
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb>& ledValues) = 0;
/// The common Logger instance for all LedDevices /// The common Logger instance for all LedDevices
Logger * _log; Logger * _log;

View File

@ -1012,9 +1012,9 @@ void Hyperion::update()
// Write the data to the device // Write the data to the device
if (_deviceSmooth->enabled()) if (_deviceSmooth->enabled())
_deviceSmooth->write(_ledBuffer); _deviceSmooth->setLedValues(_ledBuffer);
else else
_device->write(_ledBuffer); _device->setLedValues(_ledBuffer);
// Start the timeout-timer // Start the timeout-timer
if (priorityInfo.timeoutTime_ms == -1) if (priorityInfo.timeoutTime_ms == -1)

View File

@ -112,7 +112,7 @@ void LinearColorSmoothing::queueColors(const std::vector<ColorRgb> & ledColors)
{ {
// No output delay => immediate write // No output delay => immediate write
if ( _writeToLedsEnable ) if ( _writeToLedsEnable )
_ledDevice->write(ledColors); _ledDevice->setLedValues(ledColors);
} }
else else
{ {
@ -125,7 +125,7 @@ void LinearColorSmoothing::queueColors(const std::vector<ColorRgb> & ledColors)
{ {
if ( _outputQueue.size() > _outputDelay || !_writeToLedsEnable ) if ( _outputQueue.size() > _outputDelay || !_writeToLedsEnable )
{ {
_ledDevice->write(_outputQueue.front()); _ledDevice->setLedValues(_outputQueue.front());
_outputQueue.pop_front(); _outputQueue.pop_front();
} }
} }

View File

@ -67,3 +67,18 @@ Json::Value LedDevice::getLedDeviceSchemas()
return result; return result;
} }
int LedDevice::setLedValues(const std::vector<ColorRgb>& ledValues)
{
_ledCount = ledValues.size();
return write(ledValues);
}
int LedDevice::switchOff()
{
return write(std::vector<ColorRgb>(_ledCount, ColorRgb::BLACK ));
}

View File

@ -1,21 +1,9 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceAPA102.h" #include "LedDeviceAPA102.h"
LedDeviceAPA102::LedDeviceAPA102(const Json::Value &deviceConfig) LedDeviceAPA102::LedDeviceAPA102(const Json::Value &deviceConfig)
: ProviderSpi(deviceConfig) : ProviderSpi(deviceConfig)
{ {
_latchTime_ns = 500000; _latchTime_ns = 500000; // fixed latchtime
} }
LedDevice* LedDeviceAPA102::construct(const Json::Value &deviceConfig) LedDevice* LedDeviceAPA102::construct(const Json::Value &deviceConfig)
@ -25,7 +13,6 @@ LedDevice* LedDeviceAPA102::construct(const Json::Value &deviceConfig)
int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues) int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
{ {
_ledCount = ledValues.size();
const unsigned int startFrameSize = 4; const unsigned int startFrameSize = 4;
const unsigned int endFrameSize = std::max<unsigned int>(((_ledCount + 15) / 16), 4); const unsigned int endFrameSize = std::max<unsigned int>(((_ledCount + 15) / 16), 4);
const unsigned int APAbufferSize = (_ledCount * 4) + startFrameSize + endFrameSize; const unsigned int APAbufferSize = (_ledCount * 4) + startFrameSize + endFrameSize;
@ -48,8 +35,3 @@ int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
return writeBytes(_ledBuffer.size(), _ledBuffer.data()); return writeBytes(_ledBuffer.size(), _ledBuffer.data());
} }
int LedDeviceAPA102::switchOff()
{
return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0,0,0}));
}

View File

@ -1,11 +1,8 @@
#pragma once #pragma once
// STL includes
#include <string>
// hyperion incluse // hyperion incluse
#include "ProviderSpi.h" #include "ProviderSpi.h"
#include <json/json.h>
/// ///
/// Implementation of the LedDevice interface for writing to APA102 led device. /// Implementation of the LedDevice interface for writing to APA102 led device.
@ -23,7 +20,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -31,7 +28,4 @@ public:
/// @return Zero on succes else negative /// @return Zero on succes else negative
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
}; };

View File

@ -1,16 +1,4 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceAdalight.h" #include "LedDeviceAdalight.h"
#include <leddevice/LedDevice.h>
LedDeviceAdalight::LedDeviceAdalight(const Json::Value &deviceConfig) LedDeviceAdalight::LedDeviceAdalight(const Json::Value &deviceConfig)
: ProviderRs232(deviceConfig) : ProviderRs232(deviceConfig)
@ -60,16 +48,6 @@ int LedDeviceAdalight::write(const std::vector<ColorRgb> & ledValues)
return writeBytes(_ledBuffer.size(), _ledBuffer.data()); return writeBytes(_ledBuffer.size(), _ledBuffer.data());
} }
int LedDeviceAdalight::switchOff()
{
// restart the timer
_timer.start();
// write data
memset(6 + _ledBuffer.data(), 0, _ledBuffer.size()-6);
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}
void LedDeviceAdalight::rewriteLeds() void LedDeviceAdalight::rewriteLeds()
{ {
writeBytes(_ledBuffer.size(), _ledBuffer.data()); writeBytes(_ledBuffer.size(), _ledBuffer.data());

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// Qt includes // Qt includes
#include <QTimer> #include <QTimer>
@ -27,6 +24,11 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private slots:
/// Write the last data to the leds again
void rewriteLeds();
protected:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -35,14 +37,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> & ledValues); virtual int write(const std::vector<ColorRgb> & ledValues);
/// Switch the leds off
virtual int switchOff();
private slots:
/// Write the last data to the leds again
void rewriteLeds();
protected:
/// Timer object which makes sure that led data is written at a minimum rate /// Timer object which makes sure that led data is written at a minimum rate
/// The Adalight device will switch off when it does not receive data at least /// The Adalight device will switch off when it does not receive data at least
/// every 15 seconds /// every 15 seconds

View File

@ -1,14 +1,3 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceAdalightApa102.h" #include "LedDeviceAdalightApa102.h"
LedDeviceAdalightApa102::LedDeviceAdalightApa102(const Json::Value &deviceConfig) LedDeviceAdalightApa102::LedDeviceAdalightApa102(const Json::Value &deviceConfig)
@ -27,30 +16,24 @@ LedDevice* LedDeviceAdalightApa102::construct(const Json::Value &deviceConfig)
// 2 - in order to accomodate point 1 above, number of leds sent to adalight is increased by 1/3rd // 2 - in order to accomodate point 1 above, number of leds sent to adalight is increased by 1/3rd
int LedDeviceAdalightApa102::write(const std::vector<ColorRgb> & ledValues) int LedDeviceAdalightApa102::write(const std::vector<ColorRgb> & ledValues)
{ {
_ledCount = ledValues.size();
const unsigned int startFrameSize = 4; const unsigned int startFrameSize = 4;
const unsigned int endFrameSize = std::max<unsigned int>(((_ledCount + 15) / 16), 4); const unsigned int endFrameSize = std::max<unsigned int>(((_ledCount + 15) / 16), 4);
const unsigned int mLedCount = (_ledCount * 4) + startFrameSize + endFrameSize; const unsigned int mLedCount = (_ledCount * 4) + startFrameSize + endFrameSize;
if(_ledBuffer.size() != mLedCount+6){ if(_ledBuffer.size() != mLedCount+6)
{
_ledBuffer.resize(mLedCount+6, 0x00); _ledBuffer.resize(mLedCount+6, 0x00);
_ledBuffer[0] = 'A'; _ledBuffer[0] = 'A';
_ledBuffer[1] = 'd'; _ledBuffer[1] = 'd';
_ledBuffer[2] = 'a'; _ledBuffer[2] = 'a';
_ledBuffer[3] = (((unsigned int)(ledValues.size())) >> 8) & 0xFF; // LED count high byte _ledBuffer[3] = (((unsigned int)(_ledCount)) >> 8) & 0xFF; // LED count high byte
_ledBuffer[4] = ((unsigned int)(ledValues.size())) & 0xFF; // LED count low byte _ledBuffer[4] = ((unsigned int)(_ledCount)) & 0xFF; // LED count low byte
_ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum _ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
Debug( _log, "Adalight header for %d leds: %c%c%c 0x%02x 0x%02x 0x%02x", Debug( _log, "Adalight header for %d leds: %c%c%c 0x%02x 0x%02x 0x%02x", _ledCount,
ledValues.size(), _ledBuffer[0], _ledBuffer[1], _ledBuffer[2], _ledBuffer[3], _ledBuffer[4], _ledBuffer[5] );
_ledBuffer[0],
_ledBuffer[1],
_ledBuffer[2],
_ledBuffer[3],
_ledBuffer[4],
_ledBuffer[5]
);
} }
for (signed iLed=1; iLed<=_ledCount; iLed++) { for (signed iLed=1; iLed<=_ledCount; iLed++)
{
const ColorRgb& rgb = ledValues[iLed-1]; const ColorRgb& rgb = ledValues[iLed-1];
_ledBuffer[iLed*4+6] = 0xFF; _ledBuffer[iLed*4+6] = 0xFF;
_ledBuffer[iLed*4+1+6] = rgb.red; _ledBuffer[iLed*4+1+6] = rgb.red;
@ -65,19 +48,3 @@ int LedDeviceAdalightApa102::write(const std::vector<ColorRgb> & ledValues)
return writeBytes(_ledBuffer.size(), _ledBuffer.data()); return writeBytes(_ledBuffer.size(), _ledBuffer.data());
} }
int LedDeviceAdalightApa102::switchOff()
{
for (signed iLed=1; iLed<=_ledCount; iLed++) {
_ledBuffer[iLed*4+6] = 0xFF;
_ledBuffer[iLed*4+1+6] = 0x00;
_ledBuffer[iLed*4+2+6] = 0x00;
_ledBuffer[iLed*4+3+6] = 0x00;
}
// restart the timer
_timer.start();
// write data
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// hyperion include // hyperion include
#include "LedDeviceAdalight.h" #include "LedDeviceAdalight.h"
@ -25,6 +22,7 @@ public:
/// create leddevice when type in config is set to this type /// create leddevice when type in config is set to this type
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
protected:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -32,8 +30,5 @@ public:
/// @return Zero on succes else negative /// @return Zero on succes else negative
/// ///
virtual int write(const std::vector<ColorRgb> & ledValues); virtual int write(const std::vector<ColorRgb> & ledValues);
/// Switch the leds off
virtual int switchOff();
}; };

View File

@ -21,19 +21,14 @@ int LedDeviceAtmo::write(const std::vector<ColorRgb> &ledValues)
{ {
// The protocol is shomehow limited. we always need to send exactly 5 channels + header // The protocol is shomehow limited. we always need to send exactly 5 channels + header
// (19 bytes) for the hardware to recognize the data // (19 bytes) for the hardware to recognize the data
if (ledValues.size() != 5) if (_ledCount != 5)
{ {
Error( _log, "%d channels configured. This should always be 5!", ledValues.size()); Error( _log, "%d channels configured. This should always be 5!", _ledCount);
return 0; return 0;
} }
// write data // write data
memcpy(4 + _ledBuffer.data(), ledValues.data(), ledValues.size() * sizeof(ColorRgb)); memcpy(4 + _ledBuffer.data(), ledValues.data(), _ledCount * sizeof(ColorRgb));
return writeBytes(_ledBuffer.size(), _ledBuffer.data()); return writeBytes(_ledBuffer.size(), _ledBuffer.data());
} }
int LedDeviceAtmo::switchOff()
{
memset(4 + _ledBuffer.data(), 0, _ledBuffer.size() - 4);
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// hyperion incluse // hyperion incluse
#include "ProviderRs232.h" #include "ProviderRs232.h"
@ -22,6 +19,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -29,7 +27,4 @@ public:
/// @return Zero on succes else negative /// @return Zero on succes else negative
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
}; };

View File

@ -167,30 +167,11 @@ void LedDeviceAtmoOrb::sendCommand(const QByteArray &bytes)
_udpSocket->writeDatagram(datagram.data(), datagram.size(), _groupAddress, _multiCastGroupPort); _udpSocket->writeDatagram(datagram.data(), datagram.size(), _groupAddress, _multiCastGroupPort);
} }
int LedDeviceAtmoOrb::switchOff() { int LedDeviceAtmoOrb::switchOff()
for (unsigned int i = 0; i < _orbIds.size(); i++)
{ {
QByteArray bytes; for (auto orbId : _orbIds)
bytes.resize(5 + _numLeds * 3); {
bytes.fill('\0'); setColor(orbId, ColorRgb::BLACK, 1);
// Command identifier: C0FFEE
bytes[0] = 0xC0;
bytes[1] = 0xFF;
bytes[2] = 0xEE;
// Command type
bytes[3] = 1;
// Orb ID
bytes[4] = _orbIds[i];
// RED / GREEN / BLUE
bytes[5] = 0;
bytes[6] = 0;
bytes[7] = 0;
sendCommand(bytes);
} }
return 0; return 0;
} }

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// Qt includes // Qt includes
#include <QObject> #include <QObject>
#include <QString> #include <QString>
@ -65,6 +62,9 @@ public:
/// ///
virtual ~LedDeviceAtmoOrb(); virtual ~LedDeviceAtmoOrb();
virtual int switchOff();
private:
/// ///
/// Sends the given led-color values to the Orbs /// Sends the given led-color values to the Orbs
/// ///
@ -73,9 +73,6 @@ public:
/// ///
virtual int write(const std::vector <ColorRgb> &ledValues); virtual int write(const std::vector <ColorRgb> &ledValues);
virtual int switchOff();
private:
/// QNetworkAccessManager object for sending requests. /// QNetworkAccessManager object for sending requests.
QNetworkAccessManager *_manager; QNetworkAccessManager *_manager;

View File

@ -85,11 +85,10 @@ bool LedDeviceFadeCandy::tryConnect()
int LedDeviceFadeCandy::write( const std::vector<ColorRgb> & ledValues ) int LedDeviceFadeCandy::write( const std::vector<ColorRgb> & ledValues )
{ {
ssize_t nrLedValues = ledValues.size(); ssize_t led_data_size = _ledCount * 3; // 3 color bytes
ssize_t led_data_size = nrLedValues * 3; // 3 color bytes
ssize_t opc_data_size = led_data_size + OPC_HEADER_SIZE; ssize_t opc_data_size = led_data_size + OPC_HEADER_SIZE;
if (nrLedValues > MAX_NUM_LEDS) if (_ledCount > MAX_NUM_LEDS)
{ {
Error(_log, "fadecandy/opc: Invalid attempt to write led values. Not more than %d leds are allowed.", MAX_NUM_LEDS); Error(_log, "fadecandy/opc: Invalid attempt to write led values. Not more than %d leds are allowed.", MAX_NUM_LEDS);
return -1; return -1;
@ -122,15 +121,6 @@ int LedDeviceFadeCandy::transferData()
return -2; return -2;
} }
int LedDeviceFadeCandy::switchOff()
{
for ( int idx=OPC_HEADER_SIZE; idx < _opc_data.size(); idx++ )
_opc_data[idx] = 0;
return ( transferData()<0 ? -1 : 0 );
}
int LedDeviceFadeCandy::sendSysEx(uint8_t systemId, uint8_t commandId, QByteArray msg) int LedDeviceFadeCandy::sendSysEx(uint8_t systemId, uint8_t commandId, QByteArray msg)
{ {
if ( isConnected() ) if ( isConnected() )

View File

@ -1,8 +1,6 @@
#pragma once #pragma once
// STL/Qt includes // STL/Qt includes
#include <fstream>
#include <QObject>
#include <QTcpSocket> #include <QTcpSocket>
// Leddevice includes // Leddevice includes
@ -65,9 +63,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> & ledValues); virtual int write(const std::vector<ColorRgb> & ledValues);
/// Switch the leds off
virtual int switchOff();
private: private:
QTcpSocket _client; QTcpSocket _client;
std::string _host; std::string _host;

View File

@ -1,5 +1,3 @@
// Local-Hyperion includes
#include "LedDeviceFile.h" #include "LedDeviceFile.h"
LedDeviceFile::LedDeviceFile(const Json::Value &deviceConfig) LedDeviceFile::LedDeviceFile(const Json::Value &deviceConfig)
@ -41,8 +39,3 @@ int LedDeviceFile::write(const std::vector<ColorRgb> & ledValues)
return 0; return 0;
} }
int LedDeviceFile::switchOff()
{
return 0;
}

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
// STL includes0 // STL includes
#include <fstream> #include <fstream>
// Leddevice includes // Leddevice includes
@ -35,6 +35,7 @@ public:
/// @return true if success /// @return true if success
virtual bool setConfig(const Json::Value &deviceConfig); virtual bool setConfig(const Json::Value &deviceConfig);
protected:
/// ///
/// Writes the given led-color values to the output stream /// Writes the given led-color values to the output stream
/// ///
@ -44,10 +45,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> & ledValues); virtual int write(const std::vector<ColorRgb> & ledValues);
/// Switch the leds off
virtual int switchOff();
private:
/// The outputstream /// The outputstream
std::ofstream _ofs; std::ofstream _ofs;
}; };

View File

@ -137,8 +137,6 @@ int LedDeviceHyperionUsbasp::testAndOpen(libusb_device * device)
int LedDeviceHyperionUsbasp::write(const std::vector<ColorRgb> &ledValues) int LedDeviceHyperionUsbasp::write(const std::vector<ColorRgb> &ledValues)
{ {
_ledCount = ledValues.size();
int nbytes = libusb_control_transfer( int nbytes = libusb_control_transfer(
_deviceHandle, // device handle _deviceHandle, // device handle
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT, // request type LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT, // request type
@ -159,12 +157,6 @@ int LedDeviceHyperionUsbasp::write(const std::vector<ColorRgb> &ledValues)
return 0; return 0;
} }
int LedDeviceHyperionUsbasp::switchOff()
{
std::vector<ColorRgb> ledValues(_ledCount, ColorRgb::BLACK);
return write(ledValues);
}
libusb_device_handle * LedDeviceHyperionUsbasp::openDevice(libusb_device *device) libusb_device_handle * LedDeviceHyperionUsbasp::openDevice(libusb_device *device)
{ {
Logger * log = Logger::getInstance("LedDevice"); Logger * log = Logger::getInstance("LedDevice");

View File

@ -52,6 +52,7 @@ public:
/// ///
int open(); int open();
protected:
/// ///
/// Writes the RGB-Color values to the leds. /// Writes the RGB-Color values to the leds.
/// ///
@ -61,14 +62,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb>& ledValues); virtual int write(const std::vector<ColorRgb>& ledValues);
///
/// Switch the leds off
///
/// @return Zero on success else negative
///
virtual int switchOff();
private:
/// ///
/// Test if the device is a Hyperion Usbasp device /// Test if the device is a Hyperion Usbasp device
/// ///
@ -80,7 +73,6 @@ private:
static std::string getString(libusb_device * device, int stringDescriptorIndex); static std::string getString(libusb_device * device, int stringDescriptorIndex);
private:
/// command to write the leds /// command to write the leds
uint8_t _writeLedsCommand; uint8_t _writeLedsCommand;

View File

@ -39,8 +39,8 @@ LedDeviceLightpackHidapi::LedDeviceLightpackHidapi()
, _serialNumber("") , _serialNumber("")
, _firmwareVersion({-1,-1}) , _firmwareVersion({-1,-1})
, _bitsPerChannel(-1) , _bitsPerChannel(-1)
, _hwLedCount(-1)
{ {
_ledCount = -1;
} }
LedDeviceLightpackHidapi::~LedDeviceLightpackHidapi() LedDeviceLightpackHidapi::~LedDeviceLightpackHidapi()
@ -165,11 +165,11 @@ int LedDeviceLightpackHidapi::testAndOpen(hid_device_info *device, const std::st
// determine the number of leds // determine the number of leds
if (_firmwareVersion.majorVersion == 4) if (_firmwareVersion.majorVersion == 4)
{ {
_ledCount = 8; _hwLedCount = 8;
} }
else else
{ {
_ledCount = 10; _hwLedCount = 10;
} }
// determine the bits per channel // determine the bits per channel
@ -184,7 +184,7 @@ int LedDeviceLightpackHidapi::testAndOpen(hid_device_info *device, const std::st
} }
// set the led buffer size (repport id + command + 6 bytes per led) // set the led buffer size (repport id + command + 6 bytes per led)
_ledBuffer = std::vector<uint8_t>(2 + _ledCount * 6, 0); _ledBuffer = std::vector<uint8_t>(2 + _hwLedCount * 6, 0);
_ledBuffer[0] = 0x0; // report id _ledBuffer[0] = 0x0; // report id
_ledBuffer[1] = CMD_UPDATE_LEDS; _ledBuffer[1] = CMD_UPDATE_LEDS;
@ -202,16 +202,15 @@ int LedDeviceLightpackHidapi::testAndOpen(hid_device_info *device, const std::st
return -1; return -1;
} }
int LedDeviceLightpackHidapi::write(const std::vector<ColorRgb> &ledValues) int LedDeviceLightpack::write(const std::vector<ColorRgb> &ledValues)
{ {
return write(ledValues.data(), ledValues.size()); return write(ledValues.data(), _ledCount);
} }
int LedDeviceLightpackHidapi::write(const ColorRgb * ledValues, int size) int LedDeviceLightpack::write(const ColorRgb * ledValues, int size)
{ {
int count = std::min(_ledCount, size); int count = std::min(_hwLedCount,size);
for (int i=0; i<count; i++)
for (int i = 0; i < count ; ++i)
{ {
const ColorRgb & color = ledValues[i]; const ColorRgb & color = ledValues[i];

View File

@ -34,15 +34,6 @@ public:
/// ///
int open(const std::string & serialNumber = ""); int open(const std::string & serialNumber = "");
///
/// Writes the RGB-Color values to the leds.
///
/// @param[in] ledValues The RGB-color per led
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb>& ledValues);
/// ///
/// Writes the RGB-Color values to the leds. /// Writes the RGB-Color values to the leds.
/// ///
@ -67,6 +58,15 @@ public:
int getLedCount() const; int getLedCount() const;
private: private:
///
/// Writes the RGB-Color values to the leds.
///
/// @param[in] ledValues The RGB-color per led
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb>& ledValues);
/// ///
/// Test if the device is a (or the) lightpack we are looking for /// Test if the device is a (or the) lightpack we are looking for
/// ///
@ -86,7 +86,6 @@ private:
int minorVersion; int minorVersion;
}; };
private:
/// libusb device handle /// libusb device handle
hid_device * _deviceHandle; hid_device * _deviceHandle;
@ -97,11 +96,8 @@ private:
Version _firmwareVersion; Version _firmwareVersion;
/// the number of leds of the device /// the number of leds of the device
int _ledCount; int _hwLedCount;
/// the number of bits per channel /// the number of bits per channel
int _bitsPerChannel; int _bitsPerChannel;
/// buffer for led data
std::vector<uint8_t> _ledBuffer;
}; };

View File

@ -41,12 +41,12 @@ LedDeviceLightpack::LedDeviceLightpack(const std::string & serialNumber)
, _serialNumber(serialNumber) , _serialNumber(serialNumber)
, _firmwareVersion({-1,-1}) , _firmwareVersion({-1,-1})
, _bitsPerChannel(-1) , _bitsPerChannel(-1)
, _hwLedCount(-1)
{ {
_ledCount = -1;
} }
LedDeviceLightpack::LedDeviceLightpack(const Json::Value &deviceConfig) LedDeviceLightpack::LedDeviceLightpack(const Json::Value &deviceConfig)
: LedDeviceLightpack() : LedDevice()
{ {
setConfig(deviceConfig); setConfig(deviceConfig);
} }
@ -207,11 +207,11 @@ int LedDeviceLightpack::testAndOpen(libusb_device * device, const std::string &
// determine the number of leds // determine the number of leds
if (_firmwareVersion.majorVersion == 4) if (_firmwareVersion.majorVersion == 4)
{ {
_ledCount = 8; _hwLedCount = 8;
} }
else else
{ {
_ledCount = 10; _hwLedCount = 10;
} }
// determine the bits per channel // determine the bits per channel
@ -226,7 +226,7 @@ int LedDeviceLightpack::testAndOpen(libusb_device * device, const std::string &
} }
// set the led buffer size (command + 6 bytes per led) // set the led buffer size (command + 6 bytes per led)
_ledBuffer = std::vector<uint8_t>(1 + _ledCount * 6, 0); _ledBuffer = std::vector<uint8_t>(1 + _hwLedCount * 6, 0);
_ledBuffer[0] = CMD_UPDATE_LEDS; _ledBuffer[0] = CMD_UPDATE_LEDS;
// return success // return success
@ -251,7 +251,7 @@ int LedDeviceLightpack::write(const std::vector<ColorRgb> &ledValues)
int LedDeviceLightpack::write(const ColorRgb * ledValues, int size) int LedDeviceLightpack::write(const ColorRgb * ledValues, int size)
{ {
int count = std::min(_ledCount, size); int count = std::min(_hwLedCount, _ledCount);
for (int i = 0; i < count ; ++i) for (int i = 0; i < count ; ++i)
{ {

View File

@ -1,9 +1,7 @@
#pragma once #pragma once
// stl includes // stl includes
#include <vector>
#include <cstdint> #include <cstdint>
#include <string>
// libusb include // libusb include
#include <libusb.h> #include <libusb.h>
@ -52,15 +50,6 @@ public:
/// ///
int open(); int open();
///
/// Writes the RGB-Color values to the leds.
///
/// @param[in] ledValues The RGB-color per led
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb>& ledValues);
/// ///
/// Writes the RGB-Color values to the leds. /// Writes the RGB-Color values to the leds.
/// ///
@ -85,6 +74,15 @@ public:
int getLedCount() const; int getLedCount() const;
private: private:
///
/// Writes the RGB-Color values to the leds.
///
/// @param[in] ledValues The RGB-color per led
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb>& ledValues);
/// ///
/// Test if the device is a (or the) lightpack we are looking for /// Test if the device is a (or the) lightpack we are looking for
/// ///
@ -107,7 +105,6 @@ private:
static libusb_device_handle * openDevice(libusb_device * device); static libusb_device_handle * openDevice(libusb_device * device);
static std::string getString(libusb_device * device, int stringDescriptorIndex); static std::string getString(libusb_device * device, int stringDescriptorIndex);
private:
/// libusb context /// libusb context
libusb_context * _libusbContext; libusb_context * _libusbContext;
@ -128,4 +125,7 @@ private:
/// the number of bits per channel /// the number of bits per channel
int _bitsPerChannel; int _bitsPerChannel;
/// count of real hardware leds
int _hwLedCount;
}; };

View File

@ -1,13 +1,3 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceLpd6803.h" #include "LedDeviceLpd6803.h"
LedDeviceLpd6803::LedDeviceLpd6803(const Json::Value &deviceConfig) LedDeviceLpd6803::LedDeviceLpd6803(const Json::Value &deviceConfig)
@ -22,7 +12,7 @@ LedDevice* LedDeviceLpd6803::construct(const Json::Value &deviceConfig)
int LedDeviceLpd6803::write(const std::vector<ColorRgb> &ledValues) int LedDeviceLpd6803::write(const std::vector<ColorRgb> &ledValues)
{ {
unsigned messageLength = 4 + 2*ledValues.size() + ledValues.size()/8 + 1; unsigned messageLength = 4 + 2*_ledCount + _ledCount/8 + 1;
// Reconfigure if the current connfiguration does not match the required configuration // Reconfigure if the current connfiguration does not match the required configuration
if (messageLength != _ledBuffer.size()) if (messageLength != _ledBuffer.size())
{ {
@ -31,7 +21,7 @@ int LedDeviceLpd6803::write(const std::vector<ColorRgb> &ledValues)
} }
// Copy the colors from the ColorRgb vector to the Ldp6803 data vector // Copy the colors from the ColorRgb vector to the Ldp6803 data vector
for (unsigned iLed=0; iLed<ledValues.size(); ++iLed) for (unsigned iLed=0; iLed<(unsigned)_ledCount; ++iLed)
{ {
const ColorRgb& rgb = ledValues[iLed]; const ColorRgb& rgb = ledValues[iLed];
@ -40,14 +30,5 @@ int LedDeviceLpd6803::write(const std::vector<ColorRgb> &ledValues)
} }
// Write the data // Write the data
if (writeBytes(_ledBuffer.size(), _ledBuffer.data()) < 0) return (writeBytes(_ledBuffer.size(), _ledBuffer.data()) < 0) ? -1 : 0;
{
return -1;
}
return 0;
}
int LedDeviceLpd6803::switchOff()
{
return write(std::vector<ColorRgb>(_ledBuffer.size(), ColorRgb{0,0,0}));
} }

View File

@ -27,6 +27,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -34,7 +35,4 @@ public:
/// @return Zero on succes else negative /// @return Zero on succes else negative
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
}; };

View File

@ -1,13 +1,3 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceLpd8806.h" #include "LedDeviceLpd8806.h"
LedDeviceLpd8806::LedDeviceLpd8806(const Json::Value &deviceConfig) LedDeviceLpd8806::LedDeviceLpd8806(const Json::Value &deviceConfig)
@ -22,19 +12,20 @@ LedDevice* LedDeviceLpd8806::construct(const Json::Value &deviceConfig)
int LedDeviceLpd8806::write(const std::vector<ColorRgb> &ledValues) int LedDeviceLpd8806::write(const std::vector<ColorRgb> &ledValues)
{ {
const unsigned clearSize = ledValues.size()/32+1; const unsigned clearSize = _ledCount/32+1;
unsigned messageLength = 3*_ledCount + clearSize;
// Reconfigure if the current connfiguration does not match the required configuration // Reconfigure if the current connfiguration does not match the required configuration
if (3*ledValues.size() + clearSize != _ledBuffer.size()) if (messageLength != _ledBuffer.size())
{ {
// Initialise the buffer // Initialise the buffer
_ledBuffer.resize(3*ledValues.size() + clearSize, 0x00); _ledBuffer.resize(messageLength, 0x00);
// Perform an initial reset to start accepting data on the first led // Perform an initial reset to start accepting data on the first led
writeBytes(clearSize, _ledBuffer.data()); writeBytes(clearSize, _ledBuffer.data());
} }
// Copy the colors from the ColorRgb vector to the Ldp8806 data vector // Copy the colors from the ColorRgb vector to the Ldp8806 data vector
for (unsigned iLed=0; iLed<ledValues.size(); ++iLed) for (unsigned iLed=0; iLed<(unsigned)_ledCount; ++iLed)
{ {
const ColorRgb& rgb = ledValues[iLed]; const ColorRgb& rgb = ledValues[iLed];
@ -44,14 +35,5 @@ int LedDeviceLpd8806::write(const std::vector<ColorRgb> &ledValues)
} }
// Write the data // Write the data
if (writeBytes(_ledBuffer.size(), _ledBuffer.data()) < 0) return (writeBytes(_ledBuffer.size(), _ledBuffer.data()) < 0) ? -1 : 0;
{
return -1;
}
return 0;
}
int LedDeviceLpd8806::switchOff()
{
return write(std::vector<ColorRgb>(_ledBuffer.size(), ColorRgb{0,0,0}));
} }

View File

@ -88,6 +88,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -95,7 +96,4 @@ public:
/// @return Zero on succes else negative /// @return Zero on succes else negative
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
}; };

View File

@ -41,6 +41,14 @@ public:
/// ///
int open(); int open();
///
/// Switch the leds off
///
/// @return Zero on success else negative
///
virtual int switchOff();
private:
/// ///
/// Writes the RGB-Color values to the leds. /// Writes the RGB-Color values to the leds.
/// ///
@ -50,18 +58,9 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb>& ledValues); virtual int write(const std::vector<ColorRgb>& ledValues);
///
/// Switch the leds off
///
/// @return Zero on success else negative
///
virtual int switchOff();
private:
static std::list<std::string> getLightpackSerials(); static std::list<std::string> getLightpackSerials();
static std::string getString(libusb_device * device, int stringDescriptorIndex); static std::string getString(libusb_device * device, int stringDescriptorIndex);
private:
/// buffer for led data /// buffer for led data
std::vector<LedDeviceLightpack *> _lightpacks; std::vector<LedDeviceLightpack *> _lightpacks;
}; };

View File

@ -1,14 +1,3 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceP9813.h" #include "LedDeviceP9813.h"
LedDeviceP9813::LedDeviceP9813(const Json::Value &deviceConfig) LedDeviceP9813::LedDeviceP9813(const Json::Value &deviceConfig)
@ -23,10 +12,9 @@ LedDevice* LedDeviceP9813::construct(const Json::Value &deviceConfig)
int LedDeviceP9813::write(const std::vector<ColorRgb> &ledValues) int LedDeviceP9813::write(const std::vector<ColorRgb> &ledValues)
{ {
if (_ledCount != (signed)ledValues.size()) if (_ledBuffer.size() == 0)
{ {
_ledBuffer.resize(ledValues.size() * 4 + 8, 0x00); _ledBuffer.resize(ledValues.size() * 4 + 8, 0x00);
_ledCount = ledValues.size();
} }
uint8_t * dataPtr = _ledBuffer.data(); uint8_t * dataPtr = _ledBuffer.data();
@ -41,11 +29,6 @@ int LedDeviceP9813::write(const std::vector<ColorRgb> &ledValues)
return writeBytes(_ledBuffer.size(), _ledBuffer.data()); return writeBytes(_ledBuffer.size(), _ledBuffer.data());
} }
int LedDeviceP9813::switchOff()
{
return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0,0,0}));
}
uint8_t LedDeviceP9813::calculateChecksum(const ColorRgb & color) const uint8_t LedDeviceP9813::calculateChecksum(const ColorRgb & color) const
{ {
uint8_t res = 0; uint8_t res = 0;

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// hyperion include // hyperion include
#include "ProviderSpi.h" #include "ProviderSpi.h"
@ -22,6 +19,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -30,10 +28,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
private:
/// ///
/// Calculates the required checksum for one led /// Calculates the required checksum for one led
/// ///

View File

@ -1,5 +1,3 @@
// Hyperion includes
#include "LedDevicePaintpack.h" #include "LedDevicePaintpack.h"
// Use out report HID device // Use out report HID device
@ -16,30 +14,24 @@ LedDevice* LedDevicePaintpack::construct(const Json::Value &deviceConfig)
int LedDevicePaintpack::write(const std::vector<ColorRgb> & ledValues) int LedDevicePaintpack::write(const std::vector<ColorRgb> & ledValues)
{ {
if (_ledBuffer.size() < 2 + ledValues.size()*3) unsigned newSize = 3*_ledCount + 2;
if (_ledBuffer.size() < newSize)
{ {
_ledBuffer.resize(2 + ledValues.size()*3, uint8_t(0)); _ledBuffer.resize(newSize, uint8_t(0));
_ledBuffer[0] = 3; _ledBuffer[0] = 3;
_ledBuffer[1] = 0; _ledBuffer[1] = 0;
} }
auto bufIt = _ledBuffer.begin()+2; auto bufIt = _ledBuffer.begin()+2;
for (const ColorRgb & ledValue : ledValues) for (const ColorRgb & color : ledValues)
{ {
*bufIt = ledValue.red; *bufIt = color.red;
++bufIt; ++bufIt;
*bufIt = ledValue.green; *bufIt = color.green;
++bufIt; ++bufIt;
*bufIt = ledValue.blue; *bufIt = color.blue;
++bufIt; ++bufIt;
} }
return writeBytes(_ledBuffer.size(), _ledBuffer.data()); return writeBytes(_ledBuffer.size(), _ledBuffer.data());
} }
int LedDevicePaintpack::switchOff()
{
std::fill(_ledBuffer.begin() + 2, _ledBuffer.end(), uint8_t(0));
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <vector>
// Hyperion includes // Hyperion includes
#include "ProviderHID.h" #include "ProviderHID.h"
@ -22,6 +19,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the RGB-Color values to the leds. /// Writes the RGB-Color values to the leds.
/// ///
@ -30,11 +28,4 @@ public:
/// @return Zero on success else negative /// @return Zero on success else negative
/// ///
virtual int write(const std::vector<ColorRgb>& ledValues); virtual int write(const std::vector<ColorRgb>& ledValues);
///
/// Switch the leds off
///
/// @return Zero on success else negative
///
virtual int switchOff();
}; };

View File

@ -1,9 +1,6 @@
// Local-Hyperion includes // Local-Hyperion includes
#include "LedDevicePhilipsHue.h" #include "LedDevicePhilipsHue.h"
// jsoncpp includes
#include <json/json.h>
// qt includes // qt includes
#include <QtCore/qmath.h> #include <QtCore/qmath.h>
#include <QEventLoop> #include <QEventLoop>
@ -205,11 +202,11 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues)
// Save light states if not done before. // Save light states if not done before.
if (!areStatesSaved()) if (!areStatesSaved())
{ {
saveStates((unsigned int) ledValues.size()); saveStates((unsigned int) _ledCount);
switchOn((unsigned int) ledValues.size()); switchOn((unsigned int) _ledCount);
} }
// If there are less states saved than colors given, then maybe something went wrong before. // If there are less states saved than colors given, then maybe something went wrong before.
if (lights.size() != ledValues.size()) if (lights.size() != (unsigned)_ledCount)
{ {
restoreStates(); restoreStates();
return 0; return 0;

View File

@ -1,13 +1,9 @@
#pragma once #pragma once
// STL includes
#include <string>
// Qt includes // Qt includes
#include <QObject>
#include <QString>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QTimer> #include <QTimer>
// Leddevice includes // Leddevice includes
#include <leddevice/LedDevice.h> #include <leddevice/LedDevice.h>
@ -136,6 +132,14 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
/// Restores the original state of the leds.
virtual int switchOff();
private slots:
/// Restores the status of all lights.
void restoreStates();
private:
/// ///
/// Sends the given led-color values via put request to the hue system /// Sends the given led-color values via put request to the hue system
/// ///
@ -145,14 +149,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> & ledValues); virtual int write(const std::vector<ColorRgb> & ledValues);
/// Restores the original state of the leds.
virtual int switchOff();
private slots:
/// Restores the status of all lights.
void restoreStates();
private:
/// Array to save the lamps. /// Array to save the lamps.
std::vector<PhilipsHueLight> lights; std::vector<PhilipsHueLight> lights;
/// Ip address of the bridge /// Ip address of the bridge

View File

@ -3,10 +3,6 @@
#include <cstring> #include <cstring>
#include <csignal> #include <csignal>
// jsoncpp includes
#include <json/json.h>
// QT includes // QT includes
#include <QFile> #include <QFile>
@ -116,7 +112,7 @@ int LedDevicePiBlaster::write(const std::vector<ColorRgb> & ledValues)
for (unsigned int i=0; i < TABLE_SZ; i++ ) for (unsigned int i=0; i < TABLE_SZ; i++ )
{ {
valueIdx = _gpio_to_led[ i ]; valueIdx = _gpio_to_led[ i ];
if ( (valueIdx >= 0) && (valueIdx < (signed) ledValues.size()) ) if ( (valueIdx >= 0) && (valueIdx < _ledCount) )
{ {
double pwmDutyCycle = 0.0; double pwmDutyCycle = 0.0;
switch (_gpio_to_color[ i ]) switch (_gpio_to_color[ i ])
@ -157,24 +153,3 @@ int LedDevicePiBlaster::write(const std::vector<ColorRgb> & ledValues)
return 0; return 0;
} }
int LedDevicePiBlaster::switchOff()
{
// Attempt to open if not yet opened
if (_fid == nullptr && open() < 0)
{
return -1;
}
int valueIdx = -1;
for (unsigned int i=0; i < TABLE_SZ; i++ )
{
valueIdx = _gpio_to_led[ i ];
if (valueIdx >= 0)
{
fprintf(_fid, "%i=%f\n", i, 0.0);
fflush(_fid);
}
}
return 0;
}

View File

@ -1,12 +1,5 @@
#pragma once #pragma once
// STL includes
#include <cstdio>
// jsoncpp includes
#include <json/json.h>
// Hyperion-Leddevice includes // Hyperion-Leddevice includes
#include <leddevice/LedDevice.h> #include <leddevice/LedDevice.h>
@ -40,6 +33,7 @@ public:
/// ///
int open(); int open();
private:
/// ///
/// Writes the colors to the PiBlaster device /// Writes the colors to the PiBlaster device
/// ///
@ -49,15 +43,6 @@ public:
/// ///
int write(const std::vector<ColorRgb> &ledValues); int write(const std::vector<ColorRgb> &ledValues);
///
/// Switches off the leds
///
/// @return Zero on success else negative
///
int switchOff();
private:
/// The name of the output device (very likely '/dev/pi-blaster') /// The name of the output device (very likely '/dev/pi-blaster')
std::string _deviceName; std::string _deviceName;

View File

@ -1,14 +1,3 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceRawHID.h" #include "LedDeviceRawHID.h"
// Use feature report HID device // Use feature report HID device
@ -35,25 +24,17 @@ LedDevice* LedDeviceRawHID::construct(const Json::Value &deviceConfig)
int LedDeviceRawHID::write(const std::vector<ColorRgb> & ledValues) int LedDeviceRawHID::write(const std::vector<ColorRgb> & ledValues)
{ {
// Resize buffer if required // Resize buffer if required
if (_ledBuffer.size() < ledValues.size() * 3) { unsigned bufferSize = _ledCount * 3;
_ledBuffer.resize(3 * ledValues.size()); if (_ledBuffer.size() < bufferSize)
}
// restart the timer
_timer.start();
// write data
memcpy(_ledBuffer.data(), ledValues.data(), ledValues.size() * 3);
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}
int LedDeviceRawHID::switchOff()
{ {
_ledBuffer.resize(bufferSize);
}
// restart the timer // restart the timer
_timer.start(); _timer.start();
// write data // write data
std::fill(_ledBuffer.begin(), _ledBuffer.end(), uint8_t(0)); memcpy(_ledBuffer.data(), ledValues.data(), bufferSize);
return writeBytes(_ledBuffer.size(), _ledBuffer.data()); return writeBytes(_ledBuffer.size(), _ledBuffer.data());
} }

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// Qt includes // Qt includes
#include <QTimer> #include <QTimer>
@ -27,6 +24,11 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private slots:
/// Write the last data to the leds again
void rewriteLeds();
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -35,14 +37,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> & ledValues); virtual int write(const std::vector<ColorRgb> & ledValues);
/// Switch the leds off
virtual int switchOff();
private slots:
/// Write the last data to the leds again
void rewriteLeds();
private:
/// Timer object which makes sure that led data is written at a minimum rate /// Timer object which makes sure that led data is written at a minimum rate
/// The RawHID device will switch off when it does not receive data at least /// The RawHID device will switch off when it does not receive data at least
/// every 15 seconds /// every 15 seconds

View File

@ -1,14 +1,3 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceSedu.h" #include "LedDeviceSedu.h"
struct FrameSpec struct FrameSpec
@ -20,7 +9,6 @@ struct FrameSpec
LedDeviceSedu::LedDeviceSedu(const Json::Value &deviceConfig) LedDeviceSedu::LedDeviceSedu(const Json::Value &deviceConfig)
: ProviderRs232(deviceConfig) : ProviderRs232(deviceConfig)
{ {
// empty
} }
LedDevice* LedDeviceSedu::construct(const Json::Value &deviceConfig) LedDevice* LedDeviceSedu::construct(const Json::Value &deviceConfig)
@ -34,7 +22,7 @@ int LedDeviceSedu::write(const std::vector<ColorRgb> &ledValues)
{ {
std::vector<FrameSpec> frameSpecs{{0xA1, 256}, {0xA2, 512}, {0xB0, 768}, {0xB1, 1536}, {0xB2, 3072} }; std::vector<FrameSpec> frameSpecs{{0xA1, 256}, {0xA2, 512}, {0xB0, 768}, {0xB1, 1536}, {0xB2, 3072} };
const unsigned reqColorChannels = ledValues.size() * sizeof(ColorRgb); const unsigned reqColorChannels = _ledCount * sizeof(ColorRgb);
for (const FrameSpec& frameSpec : frameSpecs) for (const FrameSpec& frameSpec : frameSpecs)
{ {
@ -59,9 +47,3 @@ int LedDeviceSedu::write(const std::vector<ColorRgb> &ledValues)
memcpy(_ledBuffer.data()+2, ledValues.data(), ledValues.size() * sizeof(ColorRgb)); memcpy(_ledBuffer.data()+2, ledValues.data(), ledValues.size() * sizeof(ColorRgb));
return writeBytes(_ledBuffer.size(), _ledBuffer.data()); return writeBytes(_ledBuffer.size(), _ledBuffer.data());
} }
int LedDeviceSedu::switchOff()
{
memset(_ledBuffer.data()+2, 0, _ledBuffer.size()-3);
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// hyperion incluse // hyperion incluse
#include "ProviderRs232.h" #include "ProviderRs232.h"
@ -22,6 +19,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -29,7 +27,4 @@ public:
/// @return Zero on succes else negative /// @return Zero on succes else negative
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
}; };

View File

@ -1,14 +1,3 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceSk6812SPI.h" #include "LedDeviceSk6812SPI.h"
LedDeviceSk6812SPI::LedDeviceSk6812SPI(const Json::Value &deviceConfig) LedDeviceSk6812SPI::LedDeviceSk6812SPI(const Json::Value &deviceConfig)
@ -32,13 +21,7 @@ LedDevice* LedDeviceSk6812SPI::construct(const Json::Value &deviceConfig)
bool LedDeviceSk6812SPI::setConfig(const Json::Value &deviceConfig) bool LedDeviceSk6812SPI::setConfig(const Json::Value &deviceConfig)
{ {
ProviderSpi::setConfig(deviceConfig); ProviderSpi::setConfig(deviceConfig,3000000);
_baudRate_Hz = deviceConfig.get("rate",3000000).asInt();
if ( (_baudRate_Hz < 2050000) || (_baudRate_Hz > 4000000) )
{
Warning(_log, "SPI rate %d outside recommended range (2050000 -> 4000000)", _baudRate_Hz);
}
_whiteAlgorithm = deviceConfig.get("white_algorithm","").asString(); _whiteAlgorithm = deviceConfig.get("white_algorithm","").asString();
@ -47,21 +30,21 @@ bool LedDeviceSk6812SPI::setConfig(const Json::Value &deviceConfig)
int LedDeviceSk6812SPI::write(const std::vector<ColorRgb> &ledValues) int LedDeviceSk6812SPI::write(const std::vector<ColorRgb> &ledValues)
{ {
_ledCount = ledValues.size();
// 4 colours, 4 spi bytes per colour + 3 frame end latch bytes // 4 colours, 4 spi bytes per colour + 3 frame end latch bytes
#define COLOURS_PER_LED 4 #define COLOURS_PER_LED 4
#define SPI_BYTES_PER_COLOUR 4 #define SPI_BYTES_PER_COLOUR 4
#define SPI_BYTES_PER_LED COLOURS_PER_LED * SPI_BYTES_PER_COLOUR #define SPI_BYTES_PER_LED COLOURS_PER_LED * SPI_BYTES_PER_COLOUR
unsigned spi_size = _ledCount * SPI_BYTES_PER_LED + 3; unsigned spi_size = _ledCount * SPI_BYTES_PER_LED + 3;
if(_ledBuffer.size() != spi_size){ if(_ledBuffer.size() != spi_size)
{
_ledBuffer.resize(spi_size, 0x00); _ledBuffer.resize(spi_size, 0x00);
} }
unsigned spi_ptr = 0; unsigned spi_ptr = 0;
for (const ColorRgb& color : ledValues) { for (const ColorRgb& color : ledValues)
{
Rgb_to_Rgbw(color, &_temp_rgbw, _whiteAlgorithm); Rgb_to_Rgbw(color, &_temp_rgbw, _whiteAlgorithm);
uint32_t colorBits = uint32_t colorBits =
((uint32_t)_temp_rgbw.red << 24) + ((uint32_t)_temp_rgbw.red << 24) +
@ -75,14 +58,10 @@ int LedDeviceSk6812SPI::write(const std::vector<ColorRgb> &ledValues)
} }
spi_ptr += SPI_BYTES_PER_LED; spi_ptr += SPI_BYTES_PER_LED;
} }
_ledBuffer[spi_ptr++] = 0; _ledBuffer[spi_ptr++] = 0;
_ledBuffer[spi_ptr++] = 0; _ledBuffer[spi_ptr++] = 0;
_ledBuffer[spi_ptr++] = 0; _ledBuffer[spi_ptr++] = 0;
return writeBytes(spi_size, _ledBuffer.data()); return writeBytes(spi_size, _ledBuffer.data());
} }
int LedDeviceSk6812SPI::switchOff()
{
return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0,0,0}));
}

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// hyperion incluse // hyperion incluse
#include "ProviderSpi.h" #include "ProviderSpi.h"
@ -29,6 +26,7 @@ public:
/// @return true if success /// @return true if success
bool setConfig(const Json::Value &deviceConfig); bool setConfig(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -37,10 +35,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
private:
std::string _whiteAlgorithm; std::string _whiteAlgorithm;
uint8_t bitpair_to_byte[4]; uint8_t bitpair_to_byte[4];

View File

@ -82,21 +82,19 @@ int LedDeviceTinkerforge::open()
int LedDeviceTinkerforge::write(const std::vector<ColorRgb> &ledValues) int LedDeviceTinkerforge::write(const std::vector<ColorRgb> &ledValues)
{ {
unsigned nrLedValues = ledValues.size(); if ((unsigned)_ledCount > MAX_NUM_LEDS)
if (nrLedValues > MAX_NUM_LEDS)
{ {
Error(_log,"Invalid attempt to write led values. Not more than %d leds are allowed.", MAX_NUM_LEDS); Error(_log,"Invalid attempt to write led values. Not more than %d leds are allowed.", MAX_NUM_LEDS);
return -1; return -1;
} }
if (_colorChannelSize < nrLedValues) if (_colorChannelSize < (unsigned)_ledCount)
{ {
_redChannel.resize(nrLedValues, uint8_t(0)); _redChannel.resize(_ledCount, uint8_t(0));
_greenChannel.resize(nrLedValues, uint8_t(0)); _greenChannel.resize(_ledCount, uint8_t(0));
_blueChannel.resize(nrLedValues, uint8_t(0)); _blueChannel.resize(_ledCount, uint8_t(0));
} }
_colorChannelSize = nrLedValues; _colorChannelSize = _ledCount;
auto redIt = _redChannel.begin(); auto redIt = _redChannel.begin();
auto greenIt = _greenChannel.begin(); auto greenIt = _greenChannel.begin();
@ -115,15 +113,6 @@ int LedDeviceTinkerforge::write(const std::vector<ColorRgb> &ledValues)
return transferLedData(_ledStrip, 0, _colorChannelSize, _redChannel.data(), _greenChannel.data(), _blueChannel.data()); return transferLedData(_ledStrip, 0, _colorChannelSize, _redChannel.data(), _greenChannel.data(), _blueChannel.data());
} }
int LedDeviceTinkerforge::switchOff()
{
std::fill(_redChannel.begin(), _redChannel.end(), 0);
std::fill(_greenChannel.begin(), _greenChannel.end(), 0);
std::fill(_blueChannel.begin(), _blueChannel.end(), 0);
return transferLedData(_ledStrip, 0, _colorChannelSize, _redChannel.data(), _greenChannel.data(), _blueChannel.data());
}
int LedDeviceTinkerforge::transferLedData(LEDStrip *ledStrip, unsigned index, unsigned length, uint8_t *redChannel, uint8_t *greenChannel, uint8_t *blueChannel) int LedDeviceTinkerforge::transferLedData(LEDStrip *ledStrip, unsigned index, unsigned length, uint8_t *redChannel, uint8_t *greenChannel, uint8_t *blueChannel)
{ {
if (length == 0 || index >= length || length > MAX_NUM_LEDS) if (length == 0 || index >= length || length > MAX_NUM_LEDS)

View File

@ -1,4 +1,3 @@
#pragma once #pragma once
// STL includes // STL includes
@ -42,6 +41,7 @@ public:
/// ///
int open(); int open();
private:
/// ///
/// Writes the colors to the led strip bricklet /// Writes the colors to the led strip bricklet
/// ///
@ -51,14 +51,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
///
/// Switches off the leds
///
/// @return Zero on success else negative
///
virtual int switchOff();
private:
/// ///
/// Writes the data to the led strip blicklet /// Writes the data to the led strip blicklet
int transferLedData(LEDStrip *ledstrip, unsigned int index, unsigned int length, uint8_t *redChannel, uint8_t *greenChannel, uint8_t *blueChannel); int transferLedData(LEDStrip *ledstrip, unsigned int index, unsigned int length, uint8_t *redChannel, uint8_t *greenChannel, uint8_t *blueChannel);

View File

@ -1,12 +1,5 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// hyperion local includes
#include "LedDeviceTpm2.h" #include "LedDeviceTpm2.h"
#include <json/json.h>
LedDeviceTpm2::LedDeviceTpm2(const Json::Value &deviceConfig) LedDeviceTpm2::LedDeviceTpm2(const Json::Value &deviceConfig)
: ProviderRs232(deviceConfig) : ProviderRs232(deviceConfig)
@ -22,21 +15,15 @@ int LedDeviceTpm2::write(const std::vector<ColorRgb> &ledValues)
{ {
if (_ledBuffer.size() == 0) if (_ledBuffer.size() == 0)
{ {
_ledBuffer.resize(5 + 3*ledValues.size()); _ledBuffer.resize(5 + 3*_ledCount);
_ledBuffer[0] = 0xC9; // block-start byte _ledBuffer[0] = 0xC9; // block-start byte
_ledBuffer[1] = 0xDA; // DATA frame _ledBuffer[1] = 0xDA; // DATA frame
_ledBuffer[2] = ((3 * ledValues.size()) >> 8) & 0xFF; // frame size high byte _ledBuffer[2] = ((3 * _ledCount) >> 8) & 0xFF; // frame size high byte
_ledBuffer[3] = (3 * ledValues.size()) & 0xFF; // frame size low byte _ledBuffer[3] = (3 * _ledCount) & 0xFF; // frame size low byte
_ledBuffer.back() = 0x36; // block-end byte _ledBuffer.back() = 0x36; // block-end byte
} }
// write data // write data
memcpy(4 + _ledBuffer.data(), ledValues.data(), ledValues.size() * 3); memcpy(4 + _ledBuffer.data(), ledValues.data(), _ledCount * 3);
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}
int LedDeviceTpm2::switchOff()
{
memset(4 + _ledBuffer.data(), 0, _ledBuffer.size() - 5);
return writeBytes(_ledBuffer.size(), _ledBuffer.data()); return writeBytes(_ledBuffer.size(), _ledBuffer.data());
} }

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// hyperion incluse // hyperion incluse
#include "ProviderRs232.h" #include "ProviderRs232.h"
@ -22,6 +19,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -29,7 +27,4 @@ public:
/// @return Zero on succes else negative /// @return Zero on succes else negative
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
}; };

View File

@ -1,18 +1,3 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <QHostInfo>
#include <QUuid>
// hyperion local includes
#include "LedDeviceTpm2net.h" #include "LedDeviceTpm2net.h"
LedDeviceTpm2net::LedDeviceTpm2net(const Json::Value &deviceConfig) LedDeviceTpm2net::LedDeviceTpm2net(const Json::Value &deviceConfig)
@ -23,7 +8,7 @@ LedDeviceTpm2net::LedDeviceTpm2net(const Json::Value &deviceConfig)
bool LedDeviceTpm2net::setConfig(const Json::Value &deviceConfig) bool LedDeviceTpm2net::setConfig(const Json::Value &deviceConfig)
{ {
ProviderUdp::setConfig(deviceConfig,50200,104000); ProviderUdp::setConfig(deviceConfig, TPM2_DEFAULT_PORT, 104000);
_tpm2_max = deviceConfig.get("max-packet", 170).asInt(); _tpm2_max = deviceConfig.get("max-packet", 170).asInt();
return true; return true;
@ -43,7 +28,6 @@ int LedDeviceTpm2net::write(const std::vector<ColorRgb> &ledValues)
int retVal = 0; int retVal = 0;
_ledCount = ledValues.size();
_tpm2ByteCount = 3 * _ledCount; _tpm2ByteCount = 3 * _ledCount;
_tpm2TotalPackets = 1 + _tpm2ByteCount / _tpm2_max; _tpm2TotalPackets = 1 + _tpm2ByteCount / _tpm2_max;

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// hyperion includes // hyperion includes
#include "ProviderUdp.h" #include "ProviderUdp.h"
@ -31,6 +28,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -39,7 +37,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
private:
int _tpm2_max; int _tpm2_max;
int _tpm2ByteCount; int _tpm2ByteCount;
int _tpm2TotalPackets; int _tpm2TotalPackets;

View File

@ -1,213 +0,0 @@
// Local-Hyperion includes
#include "LedDeviceUdp.h"
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <assert.h>
struct addrinfo hints, *servinfo, *p;
//char udpbuffer[1024];
int sockfd;
int ledprotocol;
unsigned leds_per_pkt;
int update_number;
int fragment_number;
LedDeviceUdp::LedDeviceUdp(const std::string& output, const unsigned protocol, const unsigned maxPacket)
: LedDevice()
{
std::string hostname;
std::string port;
ledprotocol = protocol;
leds_per_pkt = ((maxPacket-4)/3);
if (leds_per_pkt <= 0) {
leds_per_pkt = 200;
}
int got_colon=0;
for (unsigned int i=0; i<output.length(); i++) {
if (output[i] == ':') {
got_colon++;
} else if (got_colon == 0) {
hostname+=output[i];
} else {
port+=output[i];
}
}
assert(got_colon==1);
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo(hostname.c_str() , port.c_str(), &hints, &servinfo)) != 0) {
Debug(_log, "getaddrinfo: %s", gai_strerror(rv));
assert(rv==0);
}
// loop through all the results and make a socket
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
Error(_log,"talker: socket %s", strerror(errno));
// perror("talker: socket");
continue;
}
break;
}
if (p == NULL) {
Error(_log,"talker: failed to create socket");
assert(p!=NULL);
}
}
LedDeviceUdp::~LedDeviceUdp()
{
// empty
}
int LedDeviceUdp::write(const std::vector<ColorRgb> & ledValues)
{
_ledCount = ledValues.size();
char udpbuffer[4096];
int udpPtr=0;
update_number++;
update_number &= 0xf;
if (ledprotocol == 0)
{
int i=0;
for (const ColorRgb& color : ledValues)
{
if (i<4090)
{
udpbuffer[i++] = color.red;
udpbuffer[i++] = color.green;
udpbuffer[i++] = color.blue;
}
}
sendto(sockfd, udpbuffer, i, 0, p->ai_addr, p->ai_addrlen);
}
if (ledprotocol == 1)
{
#define MAXLEDperFRAG 450
for (int frag=0; frag<4; frag++)
{
udpPtr=0;
udpbuffer[udpPtr++] = 0;
udpbuffer[udpPtr++] = 0;
udpbuffer[udpPtr++] = (frag*MAXLEDperFRAG)/256; // high byte
udpbuffer[udpPtr++] = (frag*MAXLEDperFRAG)%256; // low byte
int ct=0;
for (int this_led = frag*300; ((this_led<_ledCount) && (ct++<MAXLEDperFRAG)); this_led++)
{
const ColorRgb& color = ledValues[this_led];
if (udpPtr<4090)
{
udpbuffer[udpPtr++] = color.red;
udpbuffer[udpPtr++] = color.green;
udpbuffer[udpPtr++] = color.blue;
}
}
if (udpPtr > 7)
{
sendto(sockfd, udpbuffer, udpPtr, 0, p->ai_addr, p->ai_addrlen);
}
}
}
if (ledprotocol == 2)
{
udpPtr = 0;
unsigned int ledCtr = 0;
fragment_number = 0;
udpbuffer[udpPtr++] = update_number & 0xf;
udpbuffer[udpPtr++] = fragment_number++;
udpbuffer[udpPtr++] = ledCtr/256; // high byte
udpbuffer[udpPtr++] = ledCtr%256; // low byte
for (const ColorRgb& color : ledValues)
{
if (udpPtr<4090) {
udpbuffer[udpPtr++] = color.red;
udpbuffer[udpPtr++] = color.green;
udpbuffer[udpPtr++] = color.blue;
}
ledCtr++;
if ( (ledCtr % leds_per_pkt == 0) || (ledCtr == ledValues.size()) ) {
sendto(sockfd, udpbuffer, udpPtr, 0, p->ai_addr, p->ai_addrlen);
memset(udpbuffer, 0, sizeof udpbuffer);
udpPtr = 0;
udpbuffer[udpPtr++] = update_number & 0xf;
udpbuffer[udpPtr++] = fragment_number++;
udpbuffer[udpPtr++] = ledCtr/256; // high byte
udpbuffer[udpPtr++] = ledCtr%256; // low byte
}
}
}
if (ledprotocol == 3)
{
udpPtr = 0;
unsigned int ledCtr = 0;
unsigned int fragments = 1;
unsigned int datasize = ledValues.size() * 3;
if (ledValues.size() > leds_per_pkt) {
fragments = (ledValues.size() / leds_per_pkt) + 1;
}
fragment_number = 1;
udpbuffer[udpPtr++] = 0x9C;
udpbuffer[udpPtr++] = 0xDA;
udpbuffer[udpPtr++] = datasize/256; // high byte
udpbuffer[udpPtr++] = datasize%256; // low byte
udpbuffer[udpPtr++] = fragment_number++;
udpbuffer[udpPtr++] = fragments;
for (const ColorRgb& color : ledValues)
{
if (udpPtr<4090)
{
udpbuffer[udpPtr++] = color.red;
udpbuffer[udpPtr++] = color.green;
udpbuffer[udpPtr++] = color.blue;
}
ledCtr++;
if ( (ledCtr % leds_per_pkt == 0) || (ledCtr == ledValues.size()) )
{
udpbuffer[udpPtr++] = 0x36;
sendto(sockfd, udpbuffer, udpPtr, 0, p->ai_addr, p->ai_addrlen);
memset(udpbuffer, 0, sizeof udpbuffer);
udpPtr = 0;
udpbuffer[udpPtr++] = 0x9C;
udpbuffer[udpPtr++] = 0xDA;
udpbuffer[udpPtr++] = datasize/256; // high byte
udpbuffer[udpPtr++] = datasize%256; // low byte
udpbuffer[udpPtr++] = fragment_number++;
udpbuffer[udpPtr++] = fragments;
}
}
}
return 0;
}
int LedDeviceUdp::switchOff()
{
// return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0,0,0}));
return 0;
}

View File

@ -1,35 +0,0 @@
#pragma once
// Leddevice includes
#include <leddevice/LedDevice.h>
///
/// Implementation of the LedDevice that write the led-colors via udp
///
///
class LedDeviceUdp : public LedDevice
{
public:
///
/// Constructs the test-device, which opens an output stream to the file
///
LedDeviceUdp(const std::string& output, const unsigned protocol, const unsigned maxPacket);
///
/// Destructor of this test-device
///
virtual ~LedDeviceUdp();
///
/// Writes the given led-color values to the output stream
///
/// @param ledValues The color-value per led
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb> & ledValues);
/// Switch the leds off
virtual int switchOff();
};

View File

@ -1,16 +1,5 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <QHostInfo> #include <QHostInfo>
#include <QUuid>
// hyperion local includes // hyperion local includes
#include "LedDeviceUdpE131.h" #include "LedDeviceUdpE131.h"
@ -84,16 +73,11 @@ void LedDeviceUdpE131::prepare(const unsigned this_universe, const unsigned this
int LedDeviceUdpE131::write(const std::vector<ColorRgb> &ledValues) int LedDeviceUdpE131::write(const std::vector<ColorRgb> &ledValues)
{ {
int retVal = 0; int retVal = 0;
int _thisChannelCount = 0; int _thisChannelCount = 0;
int _dmxChannelCount = 3 * _ledCount;
_e131_seq++;
const uint8_t * rawdata = reinterpret_cast<const uint8_t *>(ledValues.data()); const uint8_t * rawdata = reinterpret_cast<const uint8_t *>(ledValues.data());
_ledCount = ledValues.size(); _e131_seq++;
int _dmxChannelCount = 3 * _ledCount;
for (int rawIdx = 0; rawIdx < _dmxChannelCount; rawIdx++) for (int rawIdx = 0; rawIdx < _dmxChannelCount; rawIdx++)
{ {
@ -113,7 +97,7 @@ int LedDeviceUdpE131::write(const std::vector<ColorRgb> &ledValues)
{ {
#undef e131debug #undef e131debug
#if e131debug #if e131debug
printf ( "send packet: rawidx %d dmxchannelcount %d universe: %d, packetsz %d\n" Debug (_log, "send packet: rawidx %d dmxchannelcount %d universe: %d, packetsz %d"
, rawIdx , rawIdx
, _dmxChannelCount , _dmxChannelCount
, _e131_universe + rawIdx / DMX_MAX , _e131_universe + rawIdx / DMX_MAX

View File

@ -1,14 +1,11 @@
#pragma once #pragma once
// STL includes
#include <string>
// hyperion includes // hyperion includes
#include "ProviderUdp.h" #include "ProviderUdp.h"
#include <QUuid> #include <QUuid>
/* /**
* *
* https://raw.githubusercontent.com/forkineye/ESPixelStick/master/_E131.h * https://raw.githubusercontent.com/forkineye/ESPixelStick/master/_E131.h
* Project: E131 - E.131 (sACN) library for Arduino * Project: E131 - E.131 (sACN) library for Arduino
@ -19,7 +16,7 @@
* subject to the laws and regulations where you are using it. Due diligence * subject to the laws and regulations where you are using it. Due diligence
* is strongly suggested before using this code. Please give credit where due. * is strongly suggested before using this code. Please give credit where due.
* *
*/ **/
#define E131_DEFAULT_PORT 5568 #define E131_DEFAULT_PORT 5568
@ -49,8 +46,10 @@
#define E131_DMP_DATA 125 #define E131_DMP_DATA 125
/* E1.31 Packet Structure */ /* E1.31 Packet Structure */
typedef union { typedef union
struct { {
struct
{
/* Root Layer */ /* Root Layer */
uint16_t preamble_size; uint16_t preamble_size;
uint16_t postamble_size; uint16_t postamble_size;
@ -119,6 +118,7 @@ public:
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -127,7 +127,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
private:
void prepare(const unsigned this_universe, const unsigned this_dmxChannelCount); void prepare(const unsigned this_universe, const unsigned this_dmxChannelCount);
e131_packet_t e131_packet; e131_packet_t e131_packet;

View File

@ -1,11 +1,3 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
#include <QHostInfo>
// hyperion local includes
#include "LedDeviceUdpH801.h" #include "LedDeviceUdpH801.h"
LedDeviceUdpH801::LedDeviceUdpH801(const Json::Value &deviceConfig) LedDeviceUdpH801::LedDeviceUdpH801(const Json::Value &deviceConfig)
@ -20,7 +12,8 @@ bool LedDeviceUdpH801::setConfig(const Json::Value &deviceConfig)
ProviderUdp::setConfig(deviceConfig, 10000000, 30977, "255.255.255.255"); ProviderUdp::setConfig(deviceConfig, 10000000, 30977, "255.255.255.255");
_ids.clear(); _ids.clear();
for (Json::Value::ArrayIndex i = 0; i < deviceConfig["lightIds"].size(); i++) { for (Json::Value::ArrayIndex i = 0; i < deviceConfig["lightIds"].size(); i++)
{
QString id(deviceConfig["lightIds"][i].asCString()); QString id(deviceConfig["lightIds"][i].asCString());
_ids.push_back(id.toInt(nullptr, 16)); _ids.push_back(id.toInt(nullptr, 16));
} }
@ -54,9 +47,3 @@ int LedDeviceUdpH801::write(const std::vector<ColorRgb> &ledValues)
return writeBytes(_message.size(), reinterpret_cast<const uint8_t*>(_message.data())); return writeBytes(_message.size(), reinterpret_cast<const uint8_t*>(_message.data()));
} }
int LedDeviceUdpH801::switchOff()
{
return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0, 0, 0}));
}

View File

@ -1,12 +1,5 @@
#pragma once #pragma once
#include <QtCore>
#include <QByteArray>
#include <QUdpSocket>
// STL includes
#include <string>
// hyperion includes // hyperion includes
#include "ProviderUdp.h" #include "ProviderUdp.h"
@ -41,6 +34,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -48,7 +42,4 @@ public:
/// @return Zero on succes else negative /// @return Zero on succes else negative
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
}; };

View File

@ -1,20 +1,9 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceUdpRaw.h" #include "LedDeviceUdpRaw.h"
LedDeviceUdpRaw::LedDeviceUdpRaw(const Json::Value &deviceConfig) LedDeviceUdpRaw::LedDeviceUdpRaw(const Json::Value &deviceConfig)
: ProviderUdp() : ProviderUdp()
{ {
ProviderUdp::setConfig(deviceConfig, 500000, 5568); setConfig(deviceConfig, 500000, 5568);
} }
LedDevice* LedDeviceUdpRaw::construct(const Json::Value &deviceConfig) LedDevice* LedDeviceUdpRaw::construct(const Json::Value &deviceConfig)
@ -24,8 +13,6 @@ LedDevice* LedDeviceUdpRaw::construct(const Json::Value &deviceConfig)
int LedDeviceUdpRaw::write(const std::vector<ColorRgb> &ledValues) int LedDeviceUdpRaw::write(const std::vector<ColorRgb> &ledValues)
{ {
_ledCount = ledValues.size();
const unsigned dataLen = _ledCount * sizeof(ColorRgb); const unsigned dataLen = _ledCount * sizeof(ColorRgb);
const uint8_t * dataPtr = reinterpret_cast<const uint8_t *>(ledValues.data()); const uint8_t * dataPtr = reinterpret_cast<const uint8_t *>(ledValues.data());

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// hyperion incluse // hyperion incluse
#include "ProviderUdp.h" #include "ProviderUdp.h"

View File

@ -307,8 +307,6 @@ int LedDeviceWS2812b::write(const std::vector<ColorRgb> &ledValues)
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &timeStart); clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &timeStart);
#endif #endif
_ledCount = ledValues.size();
// Read data from LEDBuffer[], translate it into wire format, and write to PWMWaveform // Read data from LEDBuffer[], translate it into wire format, and write to PWMWaveform
unsigned int colorBits = 0; // Holds the GRB color before conversion to wire bit pattern unsigned int colorBits = 0; // Holds the GRB color before conversion to wire bit pattern
unsigned int wireBit = 1; // Holds the current bit we will set in PWMWaveform, start with 1 and skip the other two for speed unsigned int wireBit = 1; // Holds the current bit we will set in PWMWaveform, start with 1 and skip the other two for speed
@ -454,11 +452,6 @@ int LedDeviceWS2812b::write(const std::vector<ColorRgb> &ledValues)
return 0; return 0;
} }
int LedDeviceWS2812b::switchOff()
{
return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0,0,0}));
}
LedDeviceWS2812b::~LedDeviceWS2812b() LedDeviceWS2812b::~LedDeviceWS2812b()
{ {
// Exit cleanly, freeing memory and stopping the DMA & PWM engines // Exit cleanly, freeing memory and stopping the DMA & PWM engines

View File

@ -151,6 +151,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &); static LedDevice* construct(const Json::Value &);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -159,10 +160,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
private:
page_map_t *page_map; // This will hold the page map, which we'll allocate page_map_t *page_map; // This will hold the page map, which we'll allocate
uint8_t *virtbase; // Pointer to some virtual memory that will be allocated uint8_t *virtbase; // Pointer to some virtual memory that will be allocated

View File

@ -1,4 +1,3 @@
#include <iostream>
#include <exception> #include <exception>
#include "LedDeviceWS281x.h" #include "LedDeviceWS281x.h"
@ -86,23 +85,6 @@ int LedDeviceWS281x::write(const std::vector<ColorRgb> &ledValues)
return ws2811_render(&_led_string) ? -1 : 0; return ws2811_render(&_led_string) ? -1 : 0;
} }
// Turn off the LEDs by sending 000000's
// TODO Allow optional power switch out another gpio, if this code handles it can
// make it more likely we don't accidentally drive data into an off strip
int LedDeviceWS281x::switchOff()
{
if (!_initialized)
{
return -1;
}
int idx = 0;
while (idx < _led_string.channel[_channel].count)
_led_string.channel[_channel].leds[idx++] = 0;
return ws2811_render(&_led_string) ? -1 : 0;
}
// Destructor // Destructor
LedDeviceWS281x::~LedDeviceWS281x() LedDeviceWS281x::~LedDeviceWS281x()
{ {

View File

@ -28,6 +28,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -36,10 +37,6 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
private:
ws2811_t _led_string; ws2811_t _led_string;
int _channel; int _channel;
bool _initialized; bool _initialized;

View File

@ -1,14 +1,3 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceWs2801.h" #include "LedDeviceWs2801.h"
LedDeviceWs2801::LedDeviceWs2801(const Json::Value &deviceConfig) LedDeviceWs2801::LedDeviceWs2801(const Json::Value &deviceConfig)
@ -23,15 +12,9 @@ LedDevice* LedDeviceWs2801::construct(const Json::Value &deviceConfig)
int LedDeviceWs2801::write(const std::vector<ColorRgb> &ledValues) int LedDeviceWs2801::write(const std::vector<ColorRgb> &ledValues)
{ {
_ledCount = ledValues.size(); const unsigned dataLen = _ledCount * sizeof(ColorRgb);
const unsigned dataLen = ledValues.size() * sizeof(ColorRgb);
const uint8_t * dataPtr = reinterpret_cast<const uint8_t *>(ledValues.data()); const uint8_t * dataPtr = reinterpret_cast<const uint8_t *>(ledValues.data());
return writeBytes(dataLen, dataPtr); return writeBytes(dataLen, dataPtr);
} }
int LedDeviceWs2801::switchOff()
{
return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0,0,0}));
}

View File

@ -1,9 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// hyperion incluse
#include "ProviderSpi.h" #include "ProviderSpi.h"
/// ///
@ -22,6 +18,7 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); static LedDevice* construct(const Json::Value &deviceConfig);
protected:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -29,7 +26,4 @@ public:
/// @return Zero on succes else negative /// @return Zero on succes else negative
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
}; };

View File

@ -1,28 +1,15 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceWs2812SPI.h" #include "LedDeviceWs2812SPI.h"
LedDeviceWs2812SPI::LedDeviceWs2812SPI(const Json::Value &deviceConfig) LedDeviceWs2812SPI::LedDeviceWs2812SPI(const Json::Value &deviceConfig)
: ProviderSpi(deviceConfig) : ProviderSpi()
, bitpair_to_byte { , bitpair_to_byte {
0b10001000, 0b10001000,
0b10001100, 0b10001100,
0b11001000, 0b11001000,
0b11001100, 0b11001100,
} }
{ {
setConfig(deviceConfig); setConfig(deviceConfig);
} }
LedDevice* LedDeviceWs2812SPI::construct(const Json::Value &deviceConfig) LedDevice* LedDeviceWs2812SPI::construct(const Json::Value &deviceConfig)
@ -32,20 +19,14 @@ LedDevice* LedDeviceWs2812SPI::construct(const Json::Value &deviceConfig)
bool LedDeviceWs2812SPI::setConfig(const Json::Value &deviceConfig) bool LedDeviceWs2812SPI::setConfig(const Json::Value &deviceConfig)
{ {
ProviderSpi::setConfig(deviceConfig); ProviderSpi::setConfig(deviceConfig,3000000);
WarningIf(( _baudRate_Hz < 2050000 || _baudRate_Hz > 4000000 ), _log, "SPI rate %d outside recommended range (2050000 -> 4000000)", _baudRate_Hz);
_baudRate_Hz = deviceConfig.get("rate",3000000).asInt();
if ( (_baudRate_Hz < 2050000) || (_baudRate_Hz > 4000000) )
{
Warning(_log, "SPI rate %d outside recommended range (2050000 -> 4000000)", _baudRate_Hz);
}
return true; return true;
} }
int LedDeviceWs2812SPI::write(const std::vector<ColorRgb> &ledValues) int LedDeviceWs2812SPI::write(const std::vector<ColorRgb> &ledValues)
{ {
_ledCount = ledValues.size();
// 3 colours, 4 spi bytes per colour + 3 frame end latch bytes // 3 colours, 4 spi bytes per colour + 3 frame end latch bytes
const int SPI_BYTES_PER_LED = 3 * 4; const int SPI_BYTES_PER_LED = 3 * 4;
unsigned spi_size = _ledCount * SPI_BYTES_PER_LED + 3; unsigned spi_size = _ledCount * SPI_BYTES_PER_LED + 3;
@ -75,8 +56,3 @@ int LedDeviceWs2812SPI::write(const std::vector<ColorRgb> &ledValues)
return writeBytes(spi_size, _ledBuffer.data()); return writeBytes(spi_size, _ledBuffer.data());
} }
int LedDeviceWs2812SPI::switchOff()
{
return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0,0,0}));
}

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// STL includes
#include <string>
// hyperion incluse // hyperion incluse
#include "ProviderSpi.h" #include "ProviderSpi.h"
@ -29,6 +26,7 @@ public:
/// @return true if success /// @return true if success
bool setConfig(const Json::Value &deviceConfig); bool setConfig(const Json::Value &deviceConfig);
private:
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device
/// ///
@ -37,9 +35,5 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> &ledValues); virtual int write(const std::vector<ColorRgb> &ledValues);
/// Switch the leds off
virtual int switchOff();
private:
uint8_t bitpair_to_byte[4]; uint8_t bitpair_to_byte[4];
}; };

View File

@ -17,8 +17,11 @@
ProviderSpi::ProviderSpi(const Json::Value &deviceConfig) ProviderSpi::ProviderSpi(const Json::Value &deviceConfig)
: LedDevice() : LedDevice()
, _fid(-1) , _fid(-1)
{
if (deviceConfig != Json::nullValue)
{ {
setConfig(deviceConfig); setConfig(deviceConfig);
}
memset(&_spi, 0, sizeof(_spi)); memset(&_spi, 0, sizeof(_spi));
} }
@ -27,10 +30,10 @@ ProviderSpi::~ProviderSpi()
// close(_fid); // close(_fid);
} }
bool ProviderSpi::setConfig(const Json::Value &deviceConfig) bool ProviderSpi::setConfig(const Json::Value &deviceConfig, int defaultBaudRateHz)
{ {
_deviceName = deviceConfig.get("output","/dev/spidev0.0").asString(); _deviceName = deviceConfig.get("output","/dev/spidev0.0").asString();
_baudRate_Hz = deviceConfig.get("rate",1000000).asInt(); _baudRate_Hz = deviceConfig.get("rate",defaultBaudRateHz).asInt();
_latchTime_ns = deviceConfig.get("latchtime",0).asInt(); _latchTime_ns = deviceConfig.get("latchtime",0).asInt();
_spiMode = deviceConfig.get("spimode",SPI_MODE_0).asInt(); _spiMode = deviceConfig.get("spimode",SPI_MODE_0).asInt();
_spiDataInvert = deviceConfig.get("invert",false).asBool(); _spiDataInvert = deviceConfig.get("invert",false).asBool();

View File

@ -17,14 +17,14 @@ public:
/// ///
/// @param deviceConfig json device config /// @param deviceConfig json device config
/// ///
ProviderSpi(const Json::Value &deviceConfig); ProviderSpi(const Json::Value &deviceConfig=Json::nullValue);
/// ///
/// Sets configuration /// Sets configuration
/// ///
/// @param deviceConfig the json device config /// @param deviceConfig the json device config
/// @return true if success /// @return true if success
virtual bool setConfig(const Json::Value &deviceConfig); virtual bool setConfig(const Json::Value &deviceConfig, int defaultBaudRateHz=1000000);
/// ///
/// Destructor of the LedDevice; closes the output device if it is open /// Destructor of the LedDevice; closes the output device if it is open

View File

@ -95,8 +95,3 @@ int ProviderUdp::writeBytes(const unsigned size, const uint8_t * data)
return retVal; return retVal;
} }
int ProviderUdp::switchOff()
{
return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0,0,0}));
}

View File

@ -36,9 +36,6 @@ public:
/// ///
int open(); int open();
/// Switch the leds off
virtual int switchOff();
protected: protected:
/// ///
/// Writes the given bytes/bits to the SPI-device and sleeps the latch time to ensure that the /// Writes the given bytes/bits to the SPI-device and sleeps the latch time to ensure that the

View File

@ -61,7 +61,7 @@ void setColor(char* colorStr)
LedDeviceWs2801 ledDevice(deviceConfig); LedDeviceWs2801 ledDevice(deviceConfig);
ledDevice.open(); ledDevice.open();
ledDevice.write(buff); ledDevice.setLedValues(buff);
} }
bool _running = true; bool _running = true;
@ -110,7 +110,7 @@ void doCircle()
data[curLed_2] = color_2; data[curLed_2] = color_2;
ledDevice.write(data); ledDevice.setLedValues(data);
nanosleep(&loopTime, NULL); nanosleep(&loopTime, NULL);
} }
@ -119,7 +119,7 @@ void doCircle()
data[curLed_1] = ColorRgb::BLACK; data[curLed_1] = ColorRgb::BLACK;
data[curLed_2] = ColorRgb::BLACK; data[curLed_2] = ColorRgb::BLACK;
ledDevice.write(data); ledDevice.setLedValues(data);
} }
#include <csignal> #include <csignal>