Disentangle LedDevice/LinearColorSmoothing, Bug Fixes & Test support (#654)

* Handle Exceptions in main & Pythoninit

* Have SSDPDiscover generic again

* Have SSDPDiscover generic again

* Change Info- to Debug logs as technical service messages

* Nanoleaf - When switched on, ensure UDP mode

* Include SQL Database in Cross-Compile instructions

* Fix Clazy (QT code checker) and clang Warnings

* Stop LedDevice:write for disabled device

* Nanoleaf: Fix uint printfs

* NanoLeaf: Fix indents to tabs

* NanoLeaf - Add debug verbosity switches

* Device switchability support, FileDevice with timestamp support

* Nanoleaf Light Panels now support External Control V2

* Enhance LedDeviceFile by Timestamp + fix readyness

* Stop color stream, if LedDevice disabled

* Nanoleaf - remove switchability

* Fix MultiColorAdjustment, if led-range is greater lednum

* Fix logging

* LedFileDevice/LedDevice - add testing support

* New "Led Test" effect

* LedDeviceFile - Add chrono include + Allow Led rewrites for testing

* Stabilize Effects for LedDevices where latchtime = 0

* Update LedDeviceFile, allow latchtime = 0

* Distangle LinearColorSmoothing and LEDDevice, Fix Effect configuration updates

* Updates LedDeviceFile - Initialize via Open

* Updates LedDeviceNanoleaf - Initialize via Open, Remove throwing exceptions

* Updates ProviderUDP - Remove throwing exceptions

* Framebuffer - Use precise timer

* TestSpi - Align to LedDevice updates

* Pretty Print CrossCompileHowTo as markdown-file

* Ensure that output is only written when LedDevice is ready

* Align APA102 Device to new device staging

* Logger - Remove clang warnings on extra semicolon

* Devices SPI - Align to Device stages and methods

* Fix cppcheck and clang findings

* Add Code-Template for new Devices

* Align devices to stages and methods, clean-up some code

* Allow to reopen LedDevice without restart

* Revert change "Remove Connect (PriorityMuxer::visiblePriorityChanged -> Hyperion::update) due to double writes"

* Remove visiblePriorityChanged from LedDevice to decouple LedDevice from hyperion logic

* Expose LedDevice getLedCount and align signedness
This commit is contained in:
LordGrey
2020-02-10 15:21:58 +01:00
committed by GitHub
parent 1aba51e85c
commit ed5455458b
107 changed files with 2980 additions and 1551 deletions

View File

@@ -3,7 +3,8 @@
LedDeviceAPA102::LedDeviceAPA102(const QJsonObject &deviceConfig)
: ProviderSpi()
{
_deviceReady = init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
}
LedDevice* LedDeviceAPA102::construct(const QJsonObject &deviceConfig)
@@ -13,24 +14,26 @@ LedDevice* LedDeviceAPA102::construct(const QJsonObject &deviceConfig)
bool LedDeviceAPA102::init(const QJsonObject &deviceConfig)
{
ProviderSpi::init(deviceConfig);
bool isInitOK = ProviderSpi::init(deviceConfig);
const unsigned int startFrameSize = 4;
const unsigned int endFrameSize = qMax<unsigned int>(((_ledCount + 15) / 16), 4);
const unsigned int APAbufferSize = (_ledCount * 4) + startFrameSize + endFrameSize;
if ( isInitOK )
{
const unsigned int startFrameSize = 4;
const unsigned int endFrameSize = qMax<unsigned int>(((_ledCount + 15) / 16), 4);
const unsigned int APAbufferSize = (_ledCount * 4) + startFrameSize + endFrameSize;
_ledBuffer.resize(APAbufferSize, 0xFF);
_ledBuffer[0] = 0x00;
_ledBuffer[1] = 0x00;
_ledBuffer[2] = 0x00;
_ledBuffer[3] = 0x00;
return true;
_ledBuffer.resize(APAbufferSize, 0xFF);
_ledBuffer[0] = 0x00;
_ledBuffer[1] = 0x00;
_ledBuffer[2] = 0x00;
_ledBuffer[3] = 0x00;
}
return isInitOK;
}
int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
{
for (signed iLed=0; iLed < _ledCount; ++iLed) {
for (signed iLed=0; iLed < static_cast<int>( _ledCount); ++iLed) {
const ColorRgb& rgb = ledValues[iLed];
_ledBuffer[4+iLed*4] = 0xFF;
_ledBuffer[4+iLed*4+1] = rgb.red;

View File

@@ -1,9 +1,8 @@
#pragma once
// hyperion incluse
// hyperion includes
#include "ProviderSpi.h"
///
/// Implementation of the LedDevice interface for writing to APA102 led device.
///
@@ -13,12 +12,17 @@ public:
///
/// Constructs specific LedDevice
///
LedDeviceAPA102(const QJsonObject &deviceConfig);
explicit LedDeviceAPA102(const QJsonObject &deviceConfig);
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
virtual bool init(const QJsonObject &deviceConfig);
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig) override;
private:
///
/// Writes the led color values to the led-device
@@ -26,5 +30,5 @@ private:
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> &ledValues);
virtual int write(const std::vector<ColorRgb> &ledValues) override;
};

View File

@@ -45,7 +45,8 @@ LedDeviceAPA104::LedDeviceAPA104(const QJsonObject &deviceConfig)
0b11101110,
}
{
_deviceReady = init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
}
LedDevice* LedDeviceAPA104::construct(const QJsonObject &deviceConfig)
@@ -56,15 +57,15 @@ LedDevice* LedDeviceAPA104::construct(const QJsonObject &deviceConfig)
bool LedDeviceAPA104::init(const QJsonObject &deviceConfig)
{
_baudRate_Hz = 2235000;
if ( !ProviderSpi::init(deviceConfig) )
bool isInitOK = ProviderSpi::init(deviceConfig);
if ( isInitOK )
{
return false;
WarningIf(( _baudRate_Hz < 2000000 || _baudRate_Hz > 2470000 ), _log, "SPI rate %d outside recommended range (2000000 -> 2470000)", _baudRate_Hz);
_ledBuffer.resize(_ledRGBCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
}
WarningIf(( _baudRate_Hz < 2000000 || _baudRate_Hz > 2470000 ), _log, "SPI rate %d outside recommended range (2000000 -> 2470000)", _baudRate_Hz);
_ledBuffer.resize(_ledRGBCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
return true;
return isInitOK;
}
int LedDeviceAPA104::write(const std::vector<ColorRgb> &ledValues)

View File

@@ -1,6 +1,6 @@
#pragma once
// hyperion incluse
// hyperion inclusdes
#include "ProviderSpi.h"
///
@@ -14,7 +14,7 @@ public:
///
/// @param deviceConfig json device config
///
LedDeviceAPA104(const QJsonObject &deviceConfig);
explicit LedDeviceAPA104(const QJsonObject &deviceConfig);
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
@@ -24,7 +24,7 @@ public:
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig);
virtual bool init(const QJsonObject &deviceConfig) override;
private:
///
@@ -33,10 +33,9 @@ private:
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> &ledValues);
const int SPI_BYTES_PER_COLOUR;
virtual int write(const std::vector<ColorRgb> &ledValues) override;
const int SPI_BYTES_PER_COLOUR;
const int SPI_FRAME_END_LATCH_BYTES;
uint8_t bitpair_to_byte[4];

View File

@@ -3,7 +3,8 @@
LedDeviceLpd6803::LedDeviceLpd6803(const QJsonObject &deviceConfig)
: ProviderSpi()
{
_deviceReady = init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
}
LedDevice* LedDeviceLpd6803::construct(const QJsonObject &deviceConfig)
@@ -13,13 +14,14 @@ LedDevice* LedDeviceLpd6803::construct(const QJsonObject &deviceConfig)
bool LedDeviceLpd6803::init(const QJsonObject &deviceConfig)
{
ProviderSpi::init(deviceConfig);
unsigned messageLength = 4 + 2*_ledCount + _ledCount/8 + 1;
// Initialise the buffer
_ledBuffer.resize(messageLength, 0x00);
return true;
bool isInitOK = ProviderSpi::init(deviceConfig);
if ( isInitOK )
{
unsigned messageLength = 4 + 2*_ledCount + _ledCount/8 + 1;
// Initialise the buffer
_ledBuffer.resize(messageLength, 0x00);
}
return isInitOK;
}
int LedDeviceLpd6803::write(const std::vector<ColorRgb> &ledValues)

View File

@@ -1,6 +1,6 @@
#pragma once
// Local hyperion incluse
// Local hyperion includes
#include "ProviderSpi.h"
///
@@ -22,12 +22,17 @@ public:
///
/// @param deviceConfig json device config
///
LedDeviceLpd6803(const QJsonObject &deviceConfig);
explicit LedDeviceLpd6803(const QJsonObject &deviceConfig);
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
virtual bool init(const QJsonObject &deviceConfig);
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig) override;
private:
///
@@ -36,5 +41,5 @@ private:
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> &ledValues);
virtual int write(const std::vector<ColorRgb> &ledValues) override;
};

View File

@@ -3,7 +3,8 @@
LedDeviceLpd8806::LedDeviceLpd8806(const QJsonObject &deviceConfig)
: ProviderSpi()
{
_deviceReady = init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
}
LedDevice* LedDeviceLpd8806::construct(const QJsonObject &deviceConfig)
@@ -13,15 +14,46 @@ LedDevice* LedDeviceLpd8806::construct(const QJsonObject &deviceConfig)
bool LedDeviceLpd8806::init(const QJsonObject &deviceConfig)
{
ProviderSpi::init(deviceConfig);
bool isInitOK = ProviderSpi::init(deviceConfig);
const unsigned clearSize = _ledCount/32+1;
unsigned messageLength = _ledRGBCount + clearSize;
// Initialise the buffer
_ledBuffer.resize(messageLength, 0x00);
// Perform an initial reset to start accepting data on the first led
return writeBytes(clearSize, _ledBuffer.data());
return isInitOK;
}
int LedDeviceLpd8806::open()
{
int retval = -1;
QString errortext;
_deviceReady = false;
// General initialisation and configuration of LedDevice
if ( init(_devConfig) )
{
// Perform an initial reset to start accepting data on the first led
const unsigned clearSize = _ledCount/32+1;
if ( writeBytes(clearSize, _ledBuffer.data()) < 0 )
{
errortext = QString ("Failed to do initial write");
}
else
{
// Everything is OK -> enable device
_deviceReady = true;
setEnable(true);
retval = 0;
}
// On error/exceptions, set LedDevice in error
if ( retval < 0 )
{
this->setInError( errortext );
}
}
return retval;
}
int LedDeviceLpd8806::write(const std::vector<ColorRgb> &ledValues)

View File

@@ -1,6 +1,6 @@
#pragma once
// Local hyperion incluse
// Local hyperion includes
#include "ProviderSpi.h"
///
@@ -83,12 +83,25 @@ public:
///
/// @param deviceConfig json device config
///
LedDeviceLpd8806(const QJsonObject &deviceConfig);
explicit LedDeviceLpd8806(const QJsonObject &deviceConfig);
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
virtual bool init(const QJsonObject &deviceConfig);
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig) override;
protected:
///
/// Opens and initiatialises the output device
///
/// @return Zero on succes (i.e. device is ready and enabled) else negative
///
virtual int open() override;
private:
///
@@ -97,5 +110,5 @@ private:
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> &ledValues);
virtual int write(const std::vector<ColorRgb> &ledValues) override;
};

View File

@@ -3,7 +3,8 @@
LedDeviceP9813::LedDeviceP9813(const QJsonObject &deviceConfig)
: ProviderSpi()
{
_deviceReady = init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
}
LedDevice* LedDeviceP9813::construct(const QJsonObject &deviceConfig)
@@ -13,11 +14,12 @@ LedDevice* LedDeviceP9813::construct(const QJsonObject &deviceConfig)
bool LedDeviceP9813::init(const QJsonObject &deviceConfig)
{
ProviderSpi::init(deviceConfig);
_ledBuffer.resize(_ledCount * 4 + 8, 0x00);
return true;
bool isInitOK = ProviderSpi::init(deviceConfig);
if ( isInitOK )
{
_ledBuffer.resize(_ledCount * 4 + 8, 0x00);
}
return isInitOK;
}
int LedDeviceP9813::write(const std::vector<ColorRgb> &ledValues)

View File

@@ -1,6 +1,6 @@
#pragma once
// hyperion include
// hyperion includes
#include "ProviderSpi.h"
///
@@ -14,12 +14,17 @@ public:
///
/// @param deviceConfig json device config
///
LedDeviceP9813(const QJsonObject &deviceConfig);
explicit LedDeviceP9813(const QJsonObject &deviceConfig);
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
virtual bool init(const QJsonObject &deviceConfig);
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig) override;
private:
///
@@ -28,7 +33,7 @@ private:
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> &ledValues);
virtual int write(const std::vector<ColorRgb> &ledValues) override;
///
/// Calculates the required checksum for one led

View File

@@ -1,17 +1,18 @@
#include "LedDeviceSk6812SPI.h"
LedDeviceSk6812SPI::LedDeviceSk6812SPI(const QJsonObject &deviceConfig)
LedDeviceSk6812SPI::LedDeviceSk6812SPI(const QJsonObject &deviceConfig)
: ProviderSpi()
, _whiteAlgorithm(RGBW::INVALID)
, SPI_BYTES_PER_COLOUR(4)
, bitpair_to_byte {
0b10001000,
0b10001100,
0b11001000,
0b11001100,
}
, _whiteAlgorithm(RGBW::INVALID)
, SPI_BYTES_PER_COLOUR(4)
, bitpair_to_byte {
0b10001000,
0b10001100,
0b11001000,
0b11001100,
}
{
_deviceReady = init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
}
LedDevice* LedDeviceSk6812SPI::construct(const QJsonObject &deviceConfig)
@@ -21,27 +22,31 @@ LedDevice* LedDeviceSk6812SPI::construct(const QJsonObject &deviceConfig)
bool LedDeviceSk6812SPI::init(const QJsonObject &deviceConfig)
{
QString whiteAlgorithm = deviceConfig["whiteAlgorithm"].toString("white_off");
_whiteAlgorithm = RGBW::stringToWhiteAlgorithm(whiteAlgorithm);
if (_whiteAlgorithm == RGBW::INVALID)
{
Error(_log, "unknown whiteAlgorithm %s", QSTRING_CSTR(whiteAlgorithm));
return false;
}
Debug( _log, "whiteAlgorithm : %s", QSTRING_CSTR(whiteAlgorithm));
_baudRate_Hz = 3000000;
if ( !ProviderSpi::init(deviceConfig) )
{
return false;
}
WarningIf(( _baudRate_Hz < 2050000 || _baudRate_Hz > 4000000 ), _log, "SPI rate %d outside recommended range (2050000 -> 4000000)", _baudRate_Hz);
const int SPI_FRAME_END_LATCH_BYTES = 3;
_ledBuffer.resize(_ledRGBWCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
return true;
bool isInitOK = ProviderSpi::init(deviceConfig);
if ( isInitOK )
{
QString whiteAlgorithm = deviceConfig["whiteAlgorithm"].toString("white_off");
_whiteAlgorithm = RGBW::stringToWhiteAlgorithm(whiteAlgorithm);
if (_whiteAlgorithm == RGBW::INVALID)
{
QString errortext = QString ("unknown whiteAlgorithm: %1").arg(whiteAlgorithm);
this->setInError(errortext);
isInitOK = false;
}
else
{
Debug( _log, "whiteAlgorithm : %s", QSTRING_CSTR(whiteAlgorithm));
WarningIf(( _baudRate_Hz < 2050000 || _baudRate_Hz > 4000000 ), _log, "SPI rate %d outside recommended range (2050000 -> 4000000)", _baudRate_Hz);
const int SPI_FRAME_END_LATCH_BYTES = 3;
_ledBuffer.resize(_ledRGBWCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
}
}
return isInitOK;
}
int LedDeviceSk6812SPI::write(const std::vector<ColorRgb> &ledValues)

View File

@@ -1,6 +1,6 @@
#pragma once
// hyperion incluse
// hyperion includes
#include "ProviderSpi.h"
///
@@ -14,7 +14,7 @@ public:
///
/// @param deviceConfig json device config
///
LedDeviceSk6812SPI(const QJsonObject &deviceConfig);
explicit LedDeviceSk6812SPI(const QJsonObject &deviceConfig);
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
@@ -24,7 +24,7 @@ public:
///
/// @param deviceConfig the json device config
/// @return true if success
bool init(const QJsonObject &deviceConfig);
bool init(const QJsonObject &deviceConfig) override;
private:
///
@@ -33,13 +33,12 @@ private:
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> &ledValues);
virtual int write(const std::vector<ColorRgb> &ledValues) override;
RGBW::WhiteAlgorithm _whiteAlgorithm;
const int SPI_BYTES_PER_COLOUR;
const int SPI_BYTES_PER_COLOUR;
uint8_t bitpair_to_byte[4];
ColorRgbw _temp_rgbw;
};

View File

@@ -36,20 +36,20 @@ Reset time is 50uS = 100 bits = 13 bytes
*/
LedDeviceSk6822SPI::LedDeviceSk6822SPI(const QJsonObject &deviceConfig)
: ProviderSpi()
, SPI_BYTES_PER_COLOUR(4)
, SPI_BYTES_WAIT_TIME(3)
, SPI_FRAME_END_LATCH_BYTES(13)
, bitpair_to_byte {
0b10001000,
0b10001110,
0b11101000,
0b11101110,
}
, SPI_BYTES_PER_COLOUR(4)
, SPI_BYTES_WAIT_TIME(3)
, SPI_FRAME_END_LATCH_BYTES(13)
, bitpair_to_byte {
0b10001000,
0b10001110,
0b11101000,
0b11101110,
}
{
_deviceReady = init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
}
LedDevice* LedDeviceSk6822SPI::construct(const QJsonObject &deviceConfig)
@@ -60,16 +60,17 @@ LedDevice* LedDeviceSk6822SPI::construct(const QJsonObject &deviceConfig)
bool LedDeviceSk6822SPI::init(const QJsonObject &deviceConfig)
{
_baudRate_Hz = 2230000;
if ( !ProviderSpi::init(deviceConfig) )
bool isInitOK = ProviderSpi::init(deviceConfig);
if ( isInitOK )
{
return false;
WarningIf(( _baudRate_Hz < 2000000 || _baudRate_Hz > 2460000 ), _log, "SPI rate %d outside recommended range (2000000 -> 2460000)", _baudRate_Hz);
_ledBuffer.resize( (_ledRGBCount * SPI_BYTES_PER_COLOUR) + (_ledCount * SPI_BYTES_WAIT_TIME ) + SPI_FRAME_END_LATCH_BYTES, 0x00);
// Debug(_log, "_ledBuffer.resize(_ledRGBCount:%d * SPI_BYTES_PER_COLOUR:%d) + ( _ledCount:%d * SPI_BYTES_WAIT_TIME:%d ) + SPI_FRAME_END_LATCH_BYTES:%d, 0x00)", _ledRGBCount, SPI_BYTES_PER_COLOUR, _ledCount, SPI_BYTES_WAIT_TIME, SPI_FRAME_END_LATCH_BYTES);
}
WarningIf(( _baudRate_Hz < 2000000 || _baudRate_Hz > 2460000 ), _log, "SPI rate %d outside recommended range (2000000 -> 2460000)", _baudRate_Hz);
_ledBuffer.resize( (_ledRGBCount * SPI_BYTES_PER_COLOUR) + (_ledCount * SPI_BYTES_WAIT_TIME ) + SPI_FRAME_END_LATCH_BYTES, 0x00);
// Debug(_log, "_ledBuffer.resize(_ledRGBCount:%d * SPI_BYTES_PER_COLOUR:%d) + ( _ledCount:%d * SPI_BYTES_WAIT_TIME:%d ) + SPI_FRAME_END_LATCH_BYTES:%d, 0x00)", _ledRGBCount, SPI_BYTES_PER_COLOUR, _ledCount, SPI_BYTES_WAIT_TIME, SPI_FRAME_END_LATCH_BYTES);
return true;
return isInitOK;
}
int LedDeviceSk6822SPI::write(const std::vector<ColorRgb> &ledValues)
@@ -80,8 +81,8 @@ int LedDeviceSk6822SPI::write(const std::vector<ColorRgb> &ledValues)
for (const ColorRgb& color : ledValues)
{
uint32_t colorBits = ((unsigned int)color.red << 16)
| ((unsigned int)color.green << 8)
| color.blue;
| ((unsigned int)color.green << 8)
| color.blue;
for (int j=SPI_BYTES_PER_LED - 1; j>=0; j--)
{
@@ -93,7 +94,7 @@ int LedDeviceSk6822SPI::write(const std::vector<ColorRgb> &ledValues)
}
/*
/*
// debug the whole SPI packet
char debug_line[2048];
int ptr=0;
@@ -104,14 +105,14 @@ int LedDeviceSk6822SPI::write(const std::vector<ColorRgb> &ledValues)
ptr += snprintf (ptr+debug_line, sizeof(debug_line)-ptr, "%03x: ", i);
}
ptr += snprintf (ptr+debug_line, sizeof(debug_line)-ptr, "%02x ", _ledBuffer.data()[i]);
ptr += snprintf (ptr+debug_line, sizeof(debug_line)-ptr, "%02x ", _ledBuffer.data()[i]);
if ( (i%16 == 15) || ( i == _ledBuffer.size()-1 ) )
{
Debug(_log, debug_line);
ptr = 0;
}
if ( (i%16 == 15) || ( i == _ledBuffer.size()-1 ) )
{
Debug(_log, debug_line);
ptr = 0;
}
}
*/
return writeBytes(_ledBuffer.size(), _ledBuffer.data());

View File

@@ -1,6 +1,6 @@
#pragma once
// hyperion incluse
// hyperion includes
#include "ProviderSpi.h"
///
@@ -14,7 +14,7 @@ public:
///
/// @param deviceConfig json device config
///
LedDeviceSk6822SPI(const QJsonObject &deviceConfig);
explicit LedDeviceSk6822SPI(const QJsonObject &deviceConfig);
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
@@ -24,7 +24,7 @@ public:
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig);
virtual bool init(const QJsonObject &deviceConfig) override;
private:
///
@@ -33,12 +33,11 @@ private:
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> &ledValues);
virtual int write(const std::vector<ColorRgb> &ledValues) override;
const int SPI_BYTES_PER_COLOUR;
const int SPI_BYTES_WAIT_TIME;
const int SPI_FRAME_END_LATCH_BYTES;
uint8_t bitpair_to_byte[4];
};

View File

@@ -3,7 +3,8 @@
LedDeviceWs2801::LedDeviceWs2801(const QJsonObject &deviceConfig)
: ProviderSpi()
{
_deviceReady = ProviderSpi::init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
}
LedDevice* LedDeviceWs2801::construct(const QJsonObject &deviceConfig)
@@ -11,6 +12,12 @@ LedDevice* LedDeviceWs2801::construct(const QJsonObject &deviceConfig)
return new LedDeviceWs2801(deviceConfig);
}
bool LedDeviceWs2801::init(const QJsonObject &deviceConfig)
{
bool isInitOK = ProviderSpi::init(deviceConfig);
return isInitOK;
}
int LedDeviceWs2801::write(const std::vector<ColorRgb> &ledValues)
{
const unsigned dataLen = _ledCount * sizeof(ColorRgb);

View File

@@ -1,5 +1,6 @@
#pragma once
// hyperion includes
#include "ProviderSpi.h"
///
@@ -13,11 +14,18 @@ public:
///
/// @param deviceConfig json device config
///
LedDeviceWs2801(const QJsonObject &deviceConfig);
explicit LedDeviceWs2801(const QJsonObject &deviceConfig);
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig) override;
protected:
///
/// Writes the led color values to the led-device
@@ -25,5 +33,5 @@ protected:
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> &ledValues);
virtual int write(const std::vector<ColorRgb> &ledValues) override;
};

View File

@@ -1,6 +1,6 @@
#include "LedDeviceWs2812SPI.h"
/*
/*
From the data sheet:
(TH+TL=1.25μs±600ns)
@@ -34,18 +34,19 @@ Reset time is 300uS = 923 bits = 116 bytes
*/
LedDeviceWs2812SPI::LedDeviceWs2812SPI(const QJsonObject &deviceConfig)
LedDeviceWs2812SPI::LedDeviceWs2812SPI(const QJsonObject &deviceConfig)
: ProviderSpi()
, SPI_BYTES_PER_COLOUR(4)
, SPI_FRAME_END_LATCH_BYTES(116)
, bitpair_to_byte {
0b10001000,
0b10001100,
0b11001000,
0b11001100,
}
, SPI_BYTES_PER_COLOUR(4)
, SPI_FRAME_END_LATCH_BYTES(116)
, bitpair_to_byte {
0b10001000,
0b10001100,
0b11001000,
0b11001100,
}
{
_deviceReady = init(deviceConfig);
_devConfig = deviceConfig;
_deviceReady = false;
}
LedDevice* LedDeviceWs2812SPI::construct(const QJsonObject &deviceConfig)
@@ -56,15 +57,15 @@ LedDevice* LedDeviceWs2812SPI::construct(const QJsonObject &deviceConfig)
bool LedDeviceWs2812SPI::init(const QJsonObject &deviceConfig)
{
_baudRate_Hz = 2600000;
if ( !ProviderSpi::init(deviceConfig) )
bool isInitOK = ProviderSpi::init(deviceConfig);
if ( isInitOK )
{
return false;
WarningIf(( _baudRate_Hz < 2106000 || _baudRate_Hz > 3075000 ), _log, "SPI rate %d outside recommended range (2106000 -> 3075000)", _baudRate_Hz);
_ledBuffer.resize(_ledRGBCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
}
WarningIf(( _baudRate_Hz < 2106000 || _baudRate_Hz > 3075000 ), _log, "SPI rate %d outside recommended range (2106000 -> 3075000)", _baudRate_Hz);
_ledBuffer.resize(_ledRGBCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
return true;
return isInitOK;
}
int LedDeviceWs2812SPI::write(const std::vector<ColorRgb> &ledValues)
@@ -75,8 +76,8 @@ int LedDeviceWs2812SPI::write(const std::vector<ColorRgb> &ledValues)
for (const ColorRgb& color : ledValues)
{
uint32_t colorBits = ((unsigned int)color.red << 16)
| ((unsigned int)color.green << 8)
| color.blue;
| ((unsigned int)color.green << 8)
| color.blue;
for (int j=SPI_BYTES_PER_LED - 1; j>=0; j--)
{

View File

@@ -1,6 +1,6 @@
#pragma once
// hyperion incluse
// hyperion includes
#include "ProviderSpi.h"
///
@@ -14,7 +14,7 @@ public:
///
/// @param deviceConfig json device config
///
LedDeviceWs2812SPI(const QJsonObject &deviceConfig);
explicit LedDeviceWs2812SPI(const QJsonObject &deviceConfig);
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
@@ -24,7 +24,7 @@ public:
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig);
virtual bool init(const QJsonObject &deviceConfig) override;
private:
///
@@ -33,10 +33,9 @@ private:
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> &ledValues);
const int SPI_BYTES_PER_COLOUR;
virtual int write(const std::vector<ColorRgb> &ledValues) override;
const int SPI_BYTES_PER_COLOUR;
const int SPI_FRAME_END_LATCH_BYTES;
uint8_t bitpair_to_byte[4];

View File

@@ -1,4 +1,4 @@

// STL includes
#include <cstring>
#include <cstdio>
@@ -7,6 +7,7 @@
// Linux includes
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
// Local Hyperion includes
@@ -28,52 +29,95 @@ ProviderSpi::ProviderSpi()
ProviderSpi::~ProviderSpi()
{
// close(_fid);
}
bool ProviderSpi::init(const QJsonObject &deviceConfig)
{
LedDevice::init(deviceConfig);
bool isInitOK = LedDevice::init(deviceConfig);
_deviceName = deviceConfig["output"].toString(_deviceName);
_baudRate_Hz = deviceConfig["rate"].toInt(_baudRate_Hz);
_spiMode = deviceConfig["spimode"].toInt(_spiMode);
_spiDataInvert = deviceConfig["invert"].toBool(_spiDataInvert);
return true;
return isInitOK;
}
int ProviderSpi::open()
{
Debug(_log, "_baudRate_Hz %d, _latchTime_ns %d", _baudRate_Hz, _latchTime_ms);
Debug(_log, "_spiDataInvert %d, _spiMode %d", _spiDataInvert, _spiMode);
int retval = -1;
QString errortext;
_deviceReady = false;
const int bitsPerWord = 8;
_fid = ::open(QSTRING_CSTR(_deviceName), O_RDWR);
if (_fid < 0)
if ( init(_devConfig) )
{
Error( _log, "Failed to open device (%s). Error message: %s", QSTRING_CSTR(_deviceName), strerror(errno) );
return -1;
Debug(_log, "_baudRate_Hz %d, _latchTime_ns %d", _baudRate_Hz, _latchTime_ms);
Debug(_log, "_spiDataInvert %d, _spiMode %d", _spiDataInvert, _spiMode);
const int bitsPerWord = 8;
_fid = ::open(QSTRING_CSTR(_deviceName), O_RDWR);
if (_fid < 0)
{
errortext = QString ("Failed to open device (%1). Error message: %2").arg(_deviceName, strerror(errno));
retval = -1;
}
else
{
if (ioctl(_fid, SPI_IOC_WR_MODE, &_spiMode) == -1 || ioctl(_fid, SPI_IOC_RD_MODE, &_spiMode) == -1)
{
retval = -2;
}
else
{
if (ioctl(_fid, SPI_IOC_WR_BITS_PER_WORD, &bitsPerWord) == -1 || ioctl(_fid, SPI_IOC_RD_BITS_PER_WORD, &bitsPerWord) == -1)
{
retval = -4;
}
else
{
if (ioctl(_fid, SPI_IOC_WR_MAX_SPEED_HZ, &_baudRate_Hz) == -1 || ioctl(_fid, SPI_IOC_RD_MAX_SPEED_HZ, &_baudRate_Hz) == -1)
{
retval = -6;
}
else
{
// Everything OK -> enable device
_deviceReady = true;
setEnable(true);
retval = 0;
}
}
}
if ( retval < 0 )
{
errortext = QString ("Failed to open device (%1). Error Code: %2").arg(_deviceName, retval);
}
}
if ( retval < 0 )
{
this->setInError( errortext );
}
}
if (ioctl(_fid, SPI_IOC_WR_MODE, &_spiMode) == -1 || ioctl(_fid, SPI_IOC_RD_MODE, &_spiMode) == -1)
{
return -2;
}
return retval;
}
if (ioctl(_fid, SPI_IOC_WR_BITS_PER_WORD, &bitsPerWord) == -1 || ioctl(_fid, SPI_IOC_RD_BITS_PER_WORD, &bitsPerWord) == -1)
{
return -4;
}
void ProviderSpi::close()
{
LedDevice::close();
if (ioctl(_fid, SPI_IOC_WR_MAX_SPEED_HZ, &_baudRate_Hz) == -1 || ioctl(_fid, SPI_IOC_RD_MAX_SPEED_HZ, &_baudRate_Hz) == -1)
// Device specific closing activites
if ( _fid > -1 )
{
return -6;
if ( ::close(_fid) != 0 )
{
Error( _log, "Failed to close device (%s). Error message: %s", QSTRING_CSTR(_deviceName), strerror(errno) );
}
}
return 0;
}
int ProviderSpi::writeBytes(const unsigned size, const uint8_t * data)

View File

@@ -22,19 +22,26 @@ public:
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig);
virtual bool init(const QJsonObject &deviceConfig) override;
///
/// Destructor of the LedDevice; closes the output device if it is open
///
virtual ~ProviderSpi();
virtual ~ProviderSpi() override;
///
/// Opens and configures the output device
///
/// @return Zero on succes else negative
///
int open();
int open() override;
public slots:
///
/// Closes the output device.
/// Includes switching-off the device and stopping refreshes
///
virtual void close() override;
protected:
///