2017-11-29 17:59:26 +01:00
|
|
|
// hyperion local includes
|
|
|
|
#include "LedDeviceKarate.h"
|
|
|
|
|
2021-03-19 22:52:04 +01:00
|
|
|
namespace {
|
|
|
|
const bool verbose = false;
|
|
|
|
} //End of constants
|
|
|
|
|
|
|
|
LedDeviceKarate::LedDeviceKarate(const QJsonObject& deviceConfig)
|
2020-08-08 00:21:19 +02:00
|
|
|
: ProviderRs232(deviceConfig)
|
2017-11-29 17:59:26 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-19 22:52:04 +01:00
|
|
|
LedDevice* LedDeviceKarate::construct(const QJsonObject& deviceConfig)
|
2017-11-29 17:59:26 +01:00
|
|
|
{
|
|
|
|
return new LedDeviceKarate(deviceConfig);
|
|
|
|
}
|
|
|
|
|
2021-03-19 22:52:04 +01:00
|
|
|
bool LedDeviceKarate::init(const QJsonObject& deviceConfig)
|
2017-11-29 17:59:26 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
bool isInitOK = false;
|
2017-11-29 17:59:26 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Initialise sub-class
|
2021-03-19 22:52:04 +01:00
|
|
|
if (ProviderRs232::init(deviceConfig))
|
2017-11-29 17:59:26 +01:00
|
|
|
{
|
2020-10-18 18:04:57 +02:00
|
|
|
if (_ledCount != 8 && _ledCount != 16)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2021-03-19 22:52:04 +01:00
|
|
|
QString errortext = QString("%1 channels configured. This should always be 8 or 16!").arg(_ledCount);
|
2020-02-10 15:21:58 +01:00
|
|
|
this->setInError(errortext);
|
|
|
|
isInitOK = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_ledBuffer.resize(4 + _ledCount * 3); // 4-byte header, 3 RGB values
|
2020-07-12 20:27:56 +02:00
|
|
|
_ledBuffer[0] = 0xAA; // Startbyte
|
|
|
|
_ledBuffer[1] = 0x12; // Send all Channels in Batch
|
|
|
|
_ledBuffer[2] = 0x00; // Checksum
|
|
|
|
_ledBuffer[3] = _ledCount * 3; // Number of Databytes send
|
2017-11-29 17:59:26 +01:00
|
|
|
|
2021-03-19 22:52:04 +01:00
|
|
|
Debug(_log, "Karatelight header for %d leds: 0x%02x 0x%02x 0x%02x 0x%02x", _ledCount,
|
|
|
|
_ledBuffer[0], _ledBuffer[1], _ledBuffer[2], _ledBuffer[3]);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
isInitOK = true;
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return isInitOK;
|
2017-11-29 17:59:26 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 22:52:04 +01:00
|
|
|
int LedDeviceKarate::write(const std::vector<ColorRgb>& ledValues)
|
2017-11-29 17:59:26 +01:00
|
|
|
{
|
2021-03-19 22:52:04 +01:00
|
|
|
for (signed iLed = 0; iLed < static_cast<int>(_ledCount); iLed++)
|
|
|
|
{
|
|
|
|
const ColorRgb& rgb = ledValues[iLed];
|
|
|
|
_ledBuffer[iLed * 3 + 4] = rgb.green;
|
|
|
|
_ledBuffer[iLed * 3 + 5] = rgb.blue;
|
|
|
|
_ledBuffer[iLed * 3 + 6] = rgb.red;
|
|
|
|
}
|
2017-11-29 17:59:26 +01:00
|
|
|
|
|
|
|
// Calc Checksum
|
2021-03-19 22:52:04 +01:00
|
|
|
_ledBuffer[2] = _ledBuffer[0] ^ _ledBuffer[1];
|
|
|
|
for (unsigned int i = 3; i < _ledBuffer.size(); i++)
|
|
|
|
_ledBuffer[2] ^= _ledBuffer[i];
|
2017-11-29 17:59:26 +01:00
|
|
|
|
|
|
|
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
|
|
|
}
|
2021-03-19 22:52:04 +01:00
|
|
|
|
|
|
|
QJsonObject LedDeviceKarate::getProperties(const QJsonObject& params)
|
|
|
|
{
|
|
|
|
DebugIf(verbose, _log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
|
|
|
QJsonObject properties;
|
|
|
|
|
|
|
|
QString serialPort = params["serialPort"].toString("");
|
|
|
|
|
|
|
|
QJsonObject propertiesDetails;
|
|
|
|
QJsonArray possibleLedCounts = { 16, 8 };
|
|
|
|
propertiesDetails.insert("ledCount", possibleLedCounts);
|
|
|
|
|
|
|
|
properties.insert("properties", propertiesDetails);
|
|
|
|
|
|
|
|
DebugIf(verbose, _log, "properties: [%s]", QString(QJsonDocument(properties).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
|
|
|
return properties;
|
|
|
|
}
|