2016-03-14 20:19:19 +01:00
|
|
|
// Local-Hyperion includes
|
|
|
|
#include "LedDeviceAtmoOrb.h"
|
|
|
|
|
|
|
|
// qt includes
|
|
|
|
#include <QtCore/qmath.h>
|
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QtNetwork>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
AtmoOrbLight::AtmoOrbLight(unsigned int id) {
|
2016-03-15 12:03:00 +01:00
|
|
|
// Not implemented
|
2016-03-14 20:19:19 +01:00
|
|
|
}
|
|
|
|
|
2016-04-02 00:07:28 +02:00
|
|
|
LedDeviceAtmoOrb::LedDeviceAtmoOrb(const std::string &output, bool useOrbSmoothing,
|
|
|
|
int transitiontime, int skipSmoothingDiff, int port, int numLeds, std::vector<unsigned int> orbIds) :
|
|
|
|
multicastGroup(output.c_str()), useOrbSmoothing(useOrbSmoothing), transitiontime(transitiontime), skipSmoothingDiff(skipSmoothingDiff),
|
2016-03-15 12:03:00 +01:00
|
|
|
multiCastGroupPort(port), numLeds(numLeds), orbIds(orbIds) {
|
|
|
|
manager = new QNetworkAccessManager();
|
|
|
|
groupAddress = QHostAddress(multicastGroup);
|
2016-03-14 20:19:19 +01:00
|
|
|
|
2016-03-15 12:03:00 +01:00
|
|
|
udpSocket = new QUdpSocket(this);
|
|
|
|
udpSocket->bind(multiCastGroupPort, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
|
2016-03-14 20:19:19 +01:00
|
|
|
|
2016-03-15 12:03:00 +01:00
|
|
|
joinedMulticastgroup = udpSocket->joinMulticastGroup(groupAddress);
|
2016-03-14 20:19:19 +01:00
|
|
|
}
|
|
|
|
|
2016-03-15 12:03:00 +01:00
|
|
|
int LedDeviceAtmoOrb::write(const std::vector <ColorRgb> &ledValues) {
|
2016-03-14 20:19:19 +01:00
|
|
|
|
2016-03-15 12:03:00 +01:00
|
|
|
// If not in multicast group return
|
|
|
|
if (!joinedMulticastgroup) {
|
|
|
|
return 0;
|
2016-03-14 20:19:19 +01:00
|
|
|
}
|
2016-03-15 12:03:00 +01:00
|
|
|
|
2016-04-02 00:07:28 +02:00
|
|
|
// Command options:
|
|
|
|
//
|
|
|
|
// 1 = force off
|
|
|
|
// 2 = use lamp smoothing and validate by Orb ID
|
|
|
|
// 4 = validate by Orb ID
|
|
|
|
|
|
|
|
// When setting useOrbSmoothing = true it's recommended to disable Hyperion's own smoothing as it will conflict (double smoothing)
|
|
|
|
int commandType = 4;
|
|
|
|
if(useOrbSmoothing)
|
|
|
|
{
|
|
|
|
commandType = 2;
|
|
|
|
}
|
|
|
|
|
2016-03-15 12:03:00 +01:00
|
|
|
// 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;
|
|
|
|
for (const ColorRgb &color : ledValues) {
|
2016-04-02 00:07:28 +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))
|
|
|
|
{
|
|
|
|
// Skip Orb smoothing when using (command type 4)
|
2016-03-15 12:03:00 +01:00
|
|
|
for (unsigned int i = 0; i < orbIds.size(); i++) {
|
|
|
|
if (orbIds[i] == idx) {
|
2016-04-02 00:07:28 +02:00
|
|
|
setColor(idx, color, 4);
|
2016-03-15 12:03:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-04-02 00:07:28 +02:00
|
|
|
// Send color
|
2016-03-15 12:03:00 +01:00
|
|
|
for (unsigned int i = 0; i < orbIds.size(); i++) {
|
|
|
|
if (orbIds[i] == idx) {
|
2016-04-02 00:07:28 +02:00
|
|
|
setColor(idx, color, commandType);
|
2016-03-15 12:03:00 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-14 23:19:20 +01:00
|
|
|
}
|
2016-03-14 20:19:19 +01:00
|
|
|
|
2016-03-15 12:03:00 +01:00
|
|
|
// Next light id.
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
return 0;
|
2016-03-14 20:19:19 +01:00
|
|
|
}
|
|
|
|
|
2016-03-15 12:03:00 +01:00
|
|
|
void LedDeviceAtmoOrb::setColor(unsigned int orbId, const ColorRgb &color, int commandType) {
|
|
|
|
QByteArray bytes;
|
|
|
|
bytes.resize(5 + numLeds * 3);
|
|
|
|
bytes.fill('\0');
|
|
|
|
|
|
|
|
// Command identifier: C0FFEE
|
|
|
|
bytes[0] = 0xC0;
|
|
|
|
bytes[1] = 0xFF;
|
|
|
|
bytes[2] = 0xEE;
|
|
|
|
|
|
|
|
// Command type
|
2016-04-02 00:07:28 +02:00
|
|
|
bytes[3] = commandType;
|
2016-03-15 12:03:00 +01:00
|
|
|
|
|
|
|
// Orb ID
|
|
|
|
bytes[4] = orbId;
|
|
|
|
|
|
|
|
// RED / GREEN / BLUE
|
|
|
|
bytes[5] = color.red;
|
|
|
|
bytes[6] = color.green;
|
|
|
|
bytes[7] = color.blue;
|
|
|
|
|
|
|
|
sendCommand(bytes);
|
2016-03-14 20:19:19 +01:00
|
|
|
}
|
|
|
|
|
2016-03-15 12:03:00 +01:00
|
|
|
void LedDeviceAtmoOrb::sendCommand(const QByteArray &bytes) {
|
|
|
|
QByteArray datagram = bytes;
|
|
|
|
udpSocket->writeDatagram(datagram.data(), datagram.size(),
|
|
|
|
groupAddress, multiCastGroupPort);
|
2016-03-14 20:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceAtmoOrb::switchOff() {
|
2016-03-15 12:03:00 +01:00
|
|
|
for (unsigned int i = 0; i < orbIds.size(); i++) {
|
|
|
|
QByteArray bytes;
|
|
|
|
bytes.resize(5 + numLeds * 3);
|
|
|
|
bytes.fill('\0');
|
2016-03-14 20:19:19 +01:00
|
|
|
|
2016-03-15 12:03:00 +01:00
|
|
|
// Command identifier: C0FFEE
|
|
|
|
bytes[0] = 0xC0;
|
|
|
|
bytes[1] = 0xFF;
|
|
|
|
bytes[2] = 0xEE;
|
|
|
|
|
|
|
|
// Command type
|
|
|
|
bytes[3] = 1;
|
|
|
|
|
|
|
|
// Orb ID
|
|
|
|
bytes[4] = orbIds[i];
|
|
|
|
|
|
|
|
// RED / GREEN / BLUE
|
|
|
|
bytes[5] = 0;
|
|
|
|
bytes[6] = 0;
|
|
|
|
bytes[7] = 0;
|
|
|
|
|
|
|
|
sendCommand(bytes);
|
|
|
|
}
|
|
|
|
return 0;
|
2016-03-14 20:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LedDeviceAtmoOrb::~LedDeviceAtmoOrb() {
|
2016-03-15 12:03:00 +01:00
|
|
|
delete manager;
|
2016-03-14 20:19:19 +01:00
|
|
|
}
|