2013-11-11 21:07:24 +01:00
|
|
|
#include "LedDeviceAdalight.h"
|
|
|
|
|
2016-10-13 21:59:58 +02:00
|
|
|
LedDeviceAdalight::LedDeviceAdalight(const QJsonObject &deviceConfig)
|
2016-10-08 08:14:36 +02:00
|
|
|
: ProviderRs232()
|
2016-12-14 22:45:00 +01:00
|
|
|
, _headerSize(6)
|
2016-12-01 16:17:14 +01:00
|
|
|
, _ligthBerryAPA102Mode(false)
|
2016-10-08 08:14:36 +02:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
_devConfig = deviceConfig;
|
|
|
|
_deviceReady = false;
|
|
|
|
|
2017-02-09 20:10:57 +01:00
|
|
|
connect(this,SIGNAL(receivedData(QByteArray)),this,SLOT(receivedData(QByteArray)));
|
2013-11-11 21:07:24 +01:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:59:58 +02:00
|
|
|
LedDevice* LedDeviceAdalight::construct(const QJsonObject &deviceConfig)
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
|
|
|
return new LedDeviceAdalight(deviceConfig);
|
|
|
|
}
|
|
|
|
|
2016-10-13 21:59:58 +02:00
|
|
|
bool LedDeviceAdalight::init(const QJsonObject &deviceConfig)
|
2016-10-08 08:14:36 +02:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
bool isInitOK = ProviderRs232::init(deviceConfig);
|
|
|
|
|
2016-12-12 23:28:27 +01:00
|
|
|
_ligthBerryAPA102Mode = deviceConfig["lightberry_apa102_mode"].toBool(false);
|
2016-10-08 08:14:36 +02:00
|
|
|
|
2016-12-01 16:17:14 +01:00
|
|
|
// create ledBuffer
|
|
|
|
unsigned int totalLedCount = _ledCount;
|
|
|
|
|
|
|
|
if (_ligthBerryAPA102Mode)
|
|
|
|
{
|
|
|
|
const unsigned int startFrameSize = 4;
|
|
|
|
const unsigned int bytesPerRGBLed = 4;
|
2017-08-04 12:01:45 +02:00
|
|
|
const unsigned int endFrameSize = qMax<unsigned int>(((_ledCount + 15) / 16), bytesPerRGBLed);
|
2016-12-14 22:45:00 +01:00
|
|
|
_ledBuffer.resize(_headerSize + (_ledCount * bytesPerRGBLed) + startFrameSize + endFrameSize, 0x00);
|
|
|
|
|
|
|
|
// init constant data values
|
2020-02-10 15:21:58 +01:00
|
|
|
for (signed iLed=1; iLed<= static_cast<int>( _ledCount); iLed++)
|
2016-12-14 22:45:00 +01:00
|
|
|
{
|
|
|
|
_ledBuffer[iLed*4+_headerSize] = 0xFF;
|
|
|
|
}
|
|
|
|
Debug( _log, "Adalight driver with activated LightBerry APA102 mode");
|
2016-12-01 16:17:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
totalLedCount -= 1;
|
2016-12-14 22:45:00 +01:00
|
|
|
_ledBuffer.resize(_headerSize + _ledRGBCount, 0x00);
|
2016-12-01 16:17:14 +01:00
|
|
|
}
|
|
|
|
|
2016-10-08 08:14:36 +02:00
|
|
|
_ledBuffer[0] = 'A';
|
|
|
|
_ledBuffer[1] = 'd';
|
|
|
|
_ledBuffer[2] = 'a';
|
2016-12-14 22:45:00 +01:00
|
|
|
_ledBuffer[3] = (totalLedCount >> 8) & 0xFF; // LED count high byte
|
|
|
|
_ledBuffer[4] = totalLedCount & 0xFF; // LED count low byte
|
2016-10-08 08:14:36 +02:00
|
|
|
_ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
|
2016-12-01 16:17:14 +01:00
|
|
|
|
2016-10-08 08:14:36 +02:00
|
|
|
Debug( _log, "Adalight header for %d leds: %c%c%c 0x%02x 0x%02x 0x%02x", _ledCount,
|
2016-12-14 22:45:00 +01:00
|
|
|
_ledBuffer[0], _ledBuffer[1], _ledBuffer[2], _ledBuffer[3], _ledBuffer[4], _ledBuffer[5] );
|
2016-10-08 08:14:36 +02:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
return isInitOK;
|
2016-10-08 08:14:36 +02:00
|
|
|
}
|
|
|
|
|
2013-11-12 07:52:00 +01:00
|
|
|
int LedDeviceAdalight::write(const std::vector<ColorRgb> & ledValues)
|
2013-11-11 21:07:24 +01:00
|
|
|
{
|
2016-12-01 16:17:14 +01:00
|
|
|
if(_ligthBerryAPA102Mode)
|
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
for (signed iLed=1; iLed<=static_cast<int>( _ledCount); iLed++)
|
2016-12-01 16:17:14 +01:00
|
|
|
{
|
|
|
|
const ColorRgb& rgb = ledValues[iLed-1];
|
2016-12-14 22:45:00 +01:00
|
|
|
_ledBuffer[iLed*4+7] = rgb.red;
|
|
|
|
_ledBuffer[iLed*4+8] = rgb.green;
|
|
|
|
_ledBuffer[iLed*4+9] = rgb.blue;
|
2016-12-01 16:17:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-14 22:45:00 +01:00
|
|
|
memcpy(_headerSize + _ledBuffer.data(), ledValues.data(), ledValues.size() * 3);
|
2016-12-01 16:17:14 +01:00
|
|
|
}
|
|
|
|
|
2013-11-11 21:07:24 +01:00
|
|
|
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:10:57 +01:00
|
|
|
void LedDeviceAdalight::receivedData(QByteArray data)
|
|
|
|
{
|
|
|
|
Debug(_log, ">>received %d bytes data", data.size());
|
|
|
|
}
|