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-01 16:17:14 +01:00
|
|
|
, _ligthBerryAPA102Mode(false)
|
2016-10-08 08:14:36 +02:00
|
|
|
{
|
|
|
|
_deviceReady = init(deviceConfig);
|
2016-12-01 16:17:14 +01:00
|
|
|
_ligthBerryAPA102Mode = deviceConfig["lightberry_apa102_mode"].toBool(false);
|
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
|
|
|
{
|
|
|
|
ProviderRs232::init(deviceConfig);
|
|
|
|
|
2016-12-01 16:17:14 +01:00
|
|
|
// create ledBuffer
|
|
|
|
unsigned int bufferSize = 6; // 6 bytes header
|
|
|
|
unsigned int totalLedCount = _ledCount;
|
|
|
|
|
|
|
|
if (_ligthBerryAPA102Mode)
|
|
|
|
{
|
|
|
|
const unsigned int startFrameSize = 4;
|
|
|
|
const unsigned int bytesPerRGBLed = 4;
|
|
|
|
const unsigned int endFrameSize = std::max<unsigned int>(((_ledCount + 15) / 16), bytesPerRGBLed);
|
|
|
|
bufferSize += (_ledCount * bytesPerRGBLed) + startFrameSize + endFrameSize ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
totalLedCount -= 1;
|
|
|
|
bufferSize += _ledRGBCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ledBuffer.resize(bufferSize, 0x00);
|
2016-10-08 08:14:36 +02:00
|
|
|
_ledBuffer[0] = 'A';
|
|
|
|
_ledBuffer[1] = 'd';
|
|
|
|
_ledBuffer[2] = 'a';
|
2016-12-01 16:17:14 +01:00
|
|
|
_ledBuffer[3] = (((unsigned int)(totalLedCount)) >> 8) & 0xFF; // LED count high byte
|
|
|
|
_ledBuffer[4] = ((unsigned int)(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-01 16:17:14 +01:00
|
|
|
_ledBuffer[0], _ledBuffer[1], _ledBuffer[2], _ledBuffer[3], _ledBuffer[4], _ledBuffer[5]
|
|
|
|
);
|
2016-10-08 08:14:36 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
for (signed iLed=1; iLed<=_ledCount; iLed++)
|
|
|
|
{
|
|
|
|
const ColorRgb& rgb = ledValues[iLed-1];
|
|
|
|
_ledBuffer[iLed*4+6] = 0xFF;
|
|
|
|
_ledBuffer[iLed*4+1+6] = rgb.red;
|
|
|
|
_ledBuffer[iLed*4+2+6] = rgb.green;
|
|
|
|
_ledBuffer[iLed*4+3+6] = rgb.blue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(6 + _ledBuffer.data(), ledValues.data(), ledValues.size() * 3);
|
|
|
|
}
|
|
|
|
|
2013-11-11 21:07:24 +01:00
|
|
|
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
|
|
|
}
|
|
|
|
|