2016-05-26 23:44:27 +02:00
|
|
|
// Local-Hyperion includes
|
|
|
|
#include "LedDeviceAtmoOrb.h"
|
|
|
|
|
|
|
|
// qt includes
|
|
|
|
#include <QtCore/qmath.h>
|
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QtNetwork>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
|
|
|
|
#include <stdexcept>
|
2016-08-23 20:07:12 +02:00
|
|
|
#include <sstream>
|
2016-05-26 23:44:27 +02:00
|
|
|
#include <string>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
AtmoOrbLight::AtmoOrbLight(unsigned int id) {
|
|
|
|
// Not implemented
|
|
|
|
}
|
|
|
|
|
2016-08-23 20:07:12 +02:00
|
|
|
LedDeviceAtmoOrb::LedDeviceAtmoOrb(const Json::Value &deviceConfig)
|
2016-08-14 10:46:44 +02:00
|
|
|
: LedDevice()
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2016-08-23 20:07:12 +02:00
|
|
|
setConfig(deviceConfig);
|
2016-08-14 10:46:44 +02:00
|
|
|
_manager = new QNetworkAccessManager();
|
|
|
|
_groupAddress = QHostAddress(_multicastGroup);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
_udpSocket = new QUdpSocket(this);
|
|
|
|
_udpSocket->bind(QHostAddress::Any, _multiCastGroupPort, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
joinedMulticastgroup = _udpSocket->joinMulticastGroup(_groupAddress);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
2016-08-23 20:07:12 +02:00
|
|
|
bool LedDeviceAtmoOrb::setConfig(const Json::Value &deviceConfig)
|
|
|
|
{
|
|
|
|
_multicastGroup = deviceConfig["output"].asString().c_str();
|
|
|
|
_useOrbSmoothing = deviceConfig.get("useOrbSmoothing", false).asBool();
|
|
|
|
_transitiontime = deviceConfig.get("transitiontime", 0).asInt();
|
|
|
|
_skipSmoothingDiff = deviceConfig.get("skipSmoothingDiff", 0).asInt();
|
|
|
|
_multiCastGroupPort = deviceConfig.get("port", 49692).asInt();
|
|
|
|
_numLeds = deviceConfig.get("numLeds", 24).asInt();
|
|
|
|
|
|
|
|
const std::string orbId = deviceConfig["orbIds"].asString();
|
|
|
|
_orbIds.clear();
|
|
|
|
|
|
|
|
// If we find multiple Orb ids separate them and add to list
|
|
|
|
const std::string separator (",");
|
|
|
|
if (orbId.find(separator) != std::string::npos)
|
|
|
|
{
|
|
|
|
std::stringstream ss(orbId);
|
|
|
|
std::vector<int> output;
|
|
|
|
unsigned int i;
|
|
|
|
while (ss >> i)
|
|
|
|
{
|
|
|
|
_orbIds.push_back(i);
|
|
|
|
if (ss.peek() == ',' || ss.peek() == ' ') ss.ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_orbIds.push_back(atoi(orbId.c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LedDevice* LedDeviceAtmoOrb::construct(const Json::Value &deviceConfig)
|
|
|
|
{
|
|
|
|
return new LedDeviceAtmoOrb(deviceConfig);
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
int LedDeviceAtmoOrb::write(const std::vector <ColorRgb> &ledValues)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
// If not in multicast group return
|
2016-08-14 10:46:44 +02:00
|
|
|
if (!joinedMulticastgroup)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command options:
|
|
|
|
//
|
|
|
|
// 1 = force off
|
|
|
|
// 2 = use lamp smoothing and validate by Orb ID
|
|
|
|
// 4 = validate by Orb ID
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
// When setting _useOrbSmoothing = true it's recommended to disable Hyperion's own smoothing as it will conflict (double smoothing)
|
2016-05-26 23:44:27 +02:00
|
|
|
int commandType = 4;
|
2016-08-14 10:46:44 +02:00
|
|
|
if(_useOrbSmoothing)
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
|
|
|
commandType = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through colors and set Orb color
|
|
|
|
// Start off with idx 1 as 0 is reserved for controlling all orbs at once
|
|
|
|
unsigned int idx = 1;
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
for (const ColorRgb &color : ledValues)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
// Retrieve last send colors
|
|
|
|
int lastRed = lastColorRedMap[idx];
|
|
|
|
int lastGreen = lastColorGreenMap[idx];
|
|
|
|
int lastBlue = lastColorBlueMap[idx];
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
// If color difference is higher than _skipSmoothingDiff than we skip Orb smoothing (if enabled) and send it right away
|
|
|
|
if ((_skipSmoothingDiff != 0 && _useOrbSmoothing) && (abs(color.red - lastRed) >= _skipSmoothingDiff || abs(color.blue - lastBlue) >= _skipSmoothingDiff ||
|
|
|
|
abs(color.green - lastGreen) >= _skipSmoothingDiff))
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
|
|
|
// Skip Orb smoothing when using (command type 4)
|
2016-08-14 10:46:44 +02:00
|
|
|
for (unsigned int i = 0; i < _orbIds.size(); i++)
|
|
|
|
{
|
|
|
|
if (_orbIds[i] == idx)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
setColor(idx, color, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 10:46:44 +02:00
|
|
|
else
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
// Send color
|
2016-08-14 10:46:44 +02:00
|
|
|
for (unsigned int i = 0; i < _orbIds.size(); i++)
|
|
|
|
{
|
|
|
|
if (_orbIds[i] == idx)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
setColor(idx, color, commandType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store last colors send for light id
|
2016-08-14 10:46:44 +02:00
|
|
|
lastColorRedMap[idx] = color.red;
|
2016-05-26 23:44:27 +02:00
|
|
|
lastColorGreenMap[idx] = color.green;
|
2016-08-14 10:46:44 +02:00
|
|
|
lastColorBlueMap[idx] = color.blue;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
// Next light id.
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
void LedDeviceAtmoOrb::setColor(unsigned int orbId, const ColorRgb &color, int commandType)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
QByteArray bytes;
|
2016-08-14 10:46:44 +02:00
|
|
|
bytes.resize(5 + _numLeds * 3);
|
2016-05-26 23:44:27 +02:00
|
|
|
bytes.fill('\0');
|
|
|
|
|
|
|
|
// Command identifier: C0FFEE
|
|
|
|
bytes[0] = 0xC0;
|
|
|
|
bytes[1] = 0xFF;
|
|
|
|
bytes[2] = 0xEE;
|
|
|
|
|
|
|
|
// Command type
|
|
|
|
bytes[3] = commandType;
|
|
|
|
|
|
|
|
// Orb ID
|
|
|
|
bytes[4] = orbId;
|
|
|
|
|
|
|
|
// RED / GREEN / BLUE
|
|
|
|
bytes[5] = color.red;
|
|
|
|
bytes[6] = color.green;
|
|
|
|
bytes[7] = color.blue;
|
|
|
|
|
|
|
|
sendCommand(bytes);
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
void LedDeviceAtmoOrb::sendCommand(const QByteArray &bytes)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
QByteArray datagram = bytes;
|
2016-08-14 10:46:44 +02:00
|
|
|
_udpSocket->writeDatagram(datagram.data(), datagram.size(), _groupAddress, _multiCastGroupPort);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceAtmoOrb::switchOff() {
|
2016-08-14 10:46:44 +02:00
|
|
|
for (unsigned int i = 0; i < _orbIds.size(); i++)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
QByteArray bytes;
|
2016-08-14 10:46:44 +02:00
|
|
|
bytes.resize(5 + _numLeds * 3);
|
2016-05-26 23:44:27 +02:00
|
|
|
bytes.fill('\0');
|
|
|
|
|
|
|
|
// Command identifier: C0FFEE
|
|
|
|
bytes[0] = 0xC0;
|
|
|
|
bytes[1] = 0xFF;
|
|
|
|
bytes[2] = 0xEE;
|
|
|
|
|
|
|
|
// Command type
|
|
|
|
bytes[3] = 1;
|
|
|
|
|
|
|
|
// Orb ID
|
2016-08-14 10:46:44 +02:00
|
|
|
bytes[4] = _orbIds[i];
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
// RED / GREEN / BLUE
|
|
|
|
bytes[5] = 0;
|
|
|
|
bytes[6] = 0;
|
|
|
|
bytes[7] = 0;
|
|
|
|
|
|
|
|
sendCommand(bytes);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
LedDeviceAtmoOrb::~LedDeviceAtmoOrb()
|
|
|
|
{
|
|
|
|
delete _manager;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|