2015-10-13 23:35:27 +02:00
|
|
|
#include "LedDeviceAdalightApa102.h"
|
|
|
|
|
2016-08-23 20:07:12 +02:00
|
|
|
LedDeviceAdalightApa102::LedDeviceAdalightApa102(const Json::Value &deviceConfig)
|
2016-10-08 08:14:36 +02:00
|
|
|
: ProviderRs232()
|
2015-10-13 23:35:27 +02:00
|
|
|
{
|
2016-10-08 08:14:36 +02:00
|
|
|
_deviceReady = init(deviceConfig);
|
2015-10-13 23:35:27 +02:00
|
|
|
}
|
2016-08-14 10:46:44 +02:00
|
|
|
|
2016-08-23 20:07:12 +02:00
|
|
|
LedDevice* LedDeviceAdalightApa102::construct(const Json::Value &deviceConfig)
|
|
|
|
{
|
|
|
|
return new LedDeviceAdalightApa102(deviceConfig);
|
|
|
|
}
|
|
|
|
|
2016-10-08 08:14:36 +02:00
|
|
|
bool LedDeviceAdalightApa102::init(const Json::Value &deviceConfig)
|
2015-10-13 23:35:27 +02:00
|
|
|
{
|
2016-10-08 08:14:36 +02:00
|
|
|
ProviderRs232::init(deviceConfig);
|
|
|
|
|
2015-10-13 23:35:27 +02:00
|
|
|
const unsigned int startFrameSize = 4;
|
2016-08-14 10:46:44 +02:00
|
|
|
const unsigned int endFrameSize = std::max<unsigned int>(((_ledCount + 15) / 16), 4);
|
|
|
|
const unsigned int mLedCount = (_ledCount * 4) + startFrameSize + endFrameSize;
|
2016-09-23 08:49:22 +02:00
|
|
|
if(_ledBuffer.size() != mLedCount+6)
|
|
|
|
{
|
2015-11-12 00:22:11 +01:00
|
|
|
_ledBuffer.resize(mLedCount+6, 0x00);
|
2015-10-13 23:35:27 +02:00
|
|
|
_ledBuffer[0] = 'A';
|
|
|
|
_ledBuffer[1] = 'd';
|
|
|
|
_ledBuffer[2] = 'a';
|
2016-09-23 08:49:22 +02:00
|
|
|
_ledBuffer[3] = (((unsigned int)(_ledCount)) >> 8) & 0xFF; // LED count high byte
|
|
|
|
_ledBuffer[4] = ((unsigned int)(_ledCount)) & 0xFF; // LED count low byte
|
2015-10-13 23:35:27 +02:00
|
|
|
_ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
|
2016-09-23 08:49:22 +02:00
|
|
|
Debug( _log, "Adalight header for %d leds: %c%c%c 0x%02x 0x%02x 0x%02x", _ledCount,
|
|
|
|
_ledBuffer[0], _ledBuffer[1], _ledBuffer[2], _ledBuffer[3], _ledBuffer[4], _ledBuffer[5] );
|
2015-10-13 23:35:27 +02:00
|
|
|
}
|
2015-11-12 00:22:11 +01:00
|
|
|
|
2016-10-08 08:14:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//comparing to ws2801 adalight, the following changes were needed:
|
|
|
|
// 1- differnt data frame (4 bytes instead of 3)
|
|
|
|
// 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)
|
|
|
|
{
|
2016-09-23 08:49:22 +02:00
|
|
|
for (signed iLed=1; iLed<=_ledCount; iLed++)
|
|
|
|
{
|
2015-10-30 20:08:16 +01:00
|
|
|
const ColorRgb& rgb = ledValues[iLed-1];
|
2015-10-13 23:35:27 +02:00
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write data
|
|
|
|
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
|
|
|
}
|
|
|
|
|