Initial AtmoOrb support including sample json config.

Former-commit-id: 51ad57afd39695095b5886966e8c71c4e47f269e
This commit is contained in:
RickDB
2016-03-14 20:19:19 +01:00
parent 935e400a4a
commit bd1a45216a
5 changed files with 444 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ SET(Leddevice_QT_HEADERS
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.h
${CURRENT_SOURCE_DIR}/LedDeviceAdalightApa102.h
${CURRENT_SOURCE_DIR}/LedDeviceAmbiLed.h
${CURRENT_SOURCE_DIR}/LedDeviceAtmoOrb.h
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.h
${CURRENT_SOURCE_DIR}/LedHIDDevice.h
${CURRENT_SOURCE_DIR}/LedDeviceRawHID.h
@@ -50,6 +51,7 @@ SET(Leddevice_SOURCES
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.cpp
${CURRENT_SOURCE_DIR}/LedDeviceAdalightApa102.cpp
${CURRENT_SOURCE_DIR}/LedDeviceAmbiLed.cpp
${CURRENT_SOURCE_DIR}/LedDeviceAtmoOrb.cpp
${CURRENT_SOURCE_DIR}/LedDeviceRawHID.cpp
${CURRENT_SOURCE_DIR}/LedDeviceLightpack.cpp
${CURRENT_SOURCE_DIR}/LedDeviceMultiLightpack.cpp

View File

@@ -0,0 +1,152 @@
// Local-Hyperion includes
#include "LedDeviceAtmoOrb.h"
// qt includes
#include <QtCore/qmath.h>
#include <QEventLoop>
#include <QtNetwork>
#include <QNetworkReply>
#include <QTime>
#include <stdexcept>
#include <string>
#include <set>
AtmoOrbLight::AtmoOrbLight(unsigned int id) {
// Not implemented
}
LedDeviceAtmoOrb::LedDeviceAtmoOrb(const std::string& output, bool switchOffOnBlack,
int transitiontime, int port, int numLeds, std::vector<unsigned int> orbIds) :
multicastGroup(output.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime(transitiontime),
multiCastGroupPort(port), numLeds(numLeds), orbIds(orbIds) {
manager = new QNetworkAccessManager();
groupAddress = QHostAddress(multicastGroup);
udpSocket = new QUdpSocket(this);
udpSocket->bind(multiCastGroupPort, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
if (!udpSocket->joinMulticastGroup(groupAddress))
{
joinedMulticastgroup = false;
}
else
{
joinedMulticastgroup = true;
}
}
int LedDeviceAtmoOrb::write(const std::vector<ColorRgb> & ledValues) {
// If not in multicast group return
if (!joinedMulticastgroup)
{
return 0;
}
// Iterate through colors and set Orb color.
unsigned int idx = 0;
for (const ColorRgb& color : ledValues)
{
// If color is identical skip color setter
if(color.red == lastRed && color.green == lastGreen && color.blue == lastBlue)
{
continue;
}
// Options parameter:
//
// 1 = force off
// 2 = use lamp smoothing and validate by Orb ID
// 4 = validate by Orb ID
//
if (switchOffOnBlack && color.red == 0 && color.green == 0 && color.blue == 0) {
// Force to black
for (int i = 0; i < orbIds.size(); i++) {
setColor(orbIds[i], color, 1);
}
}
else
{
// Default send color
for (int i = 0; i < orbIds.size(); i++) {
setColor(orbIds[i], color, 4);
}
}
// Store current colors
lastRed = color.red;
lastGreen = color.green;
lastBlue = color.blue;
// Next light id.
idx++;
}
return 0;
}
void LedDeviceAtmoOrb::setColor(unsigned int orbId, const ColorRgb& color, int commandType) {
QByteArray bytes;
bytes.resize(5 + 24 * 3);
// Command identifier: C0FFEE
bytes[0] = 0xC0;
bytes[1] = 0xFF;
bytes[2] = 0xEE;
// Command type
bytes[3] = 2;
// Orb ID
bytes[4] = orbId;
// RED / GREEN / BLUE
bytes[5] = color.red;
bytes[6] = color.green;
bytes[7] = color.blue;
sendCommand(bytes);
}
void LedDeviceAtmoOrb::sendCommand(const QByteArray & bytes) {
QByteArray datagram = bytes;
udpSocket->writeDatagram(datagram.data(), datagram.size(),
groupAddress, multiCastGroupPort);
}
int LedDeviceAtmoOrb::switchOff() {
// Default send color
for (int i = 0; i < orbIds.size(); i++) {
QByteArray bytes;
bytes.resize(5 + 24 * 3);
// 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;
}
void LedDeviceAtmoOrb::switchOn(unsigned int nLights) {
// Not implemented
}
LedDeviceAtmoOrb::~LedDeviceAtmoOrb() {
delete manager;
}

View File

@@ -0,0 +1,111 @@
#pragma once
// STL includes
#include <string>
// Qt includes
#include <QObject>
#include <QString>
#include <QNetworkAccessManager>
#include <QHostAddress>
#include <QTime>
// Leddevice includes
#include <leddevice/LedDevice.h>
class QUdpSocket;
class AtmoOrbLight {
public:
unsigned int id;
///
/// Constructs the light.
///
/// @param id the orb id
AtmoOrbLight(unsigned int id);
};
/**
* Implementation for the AtmoOrb
*
* To use set the device to "atmoorb".
*
* @author RickDB (github)
*/
class LedDeviceAtmoOrb : public QObject, public LedDevice {
Q_OBJECT
public:
// Last color sent
int lastRed;
int lastGreen;
int lastBlue;
// Last command sent timer
QTime timer;
// Multicast status
bool joinedMulticastgroup;
///
/// Constructs the device.
///
/// @param output is the multicast address of Orbs
///
/// @param switchOffOnBlack kill lights for black (default: false)
///
/// @param transitiontime is optional and not used at the moment
///
/// @param port is the multicast port.
///
/// @param numLeds is the total amount of leds per Orb
///
/// @param orb ids to control
///
LedDeviceAtmoOrb(const std::string& output, bool switchOffOnBlack =
false, int transitiontime = 0, int port = 49692, int numLeds = 24, std::vector<unsigned int> orbIds = std::vector<unsigned int>());
///
/// Destructor of this device
///
virtual ~LedDeviceAtmoOrb();
///
/// Sends the given led-color values to the Orbs
///
/// @param ledValues The color-value per led
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb> & ledValues);
virtual int switchOff();
private:
/// Array to save the lamps.
std::vector<AtmoOrbLight> lights;
/// QNetworkAccessManager object for sending requests.
QNetworkAccessManager* manager;
QString multicastGroup;
bool switchOffOnBlack;
int transitiontime;
int multiCastGroupPort;
int numLeds;
QHostAddress groupAddress;
QUdpSocket *udpSocket;
/// Array of the light ids.
std::vector<unsigned int> orbIds;
///
/// Switches the leds on.
///
/// @param nLights the number of lights
///
void switchOn(unsigned int nLights);
// Set color
void setColor(unsigned int orbId, const ColorRgb& color, int commandType);
// Send color command
void sendCommand(const QByteArray & bytes);
};

View File

@@ -37,6 +37,7 @@
#include "LedDeviceTpm2.h"
#include "LedDeviceAtmo.h"
#include "LedDeviceAdalightApa102.h"
#include "LedDeviceAtmoOrb.h"
#ifdef ENABLE_WS2812BPWM
#include "LedDeviceWS2812b.h"
@@ -245,6 +246,21 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
}
device = new LedDevicePhilipsHue(output, username, switchOffOnBlack, transitiontime, lightIds);
}
else if (type == "atmoorb")
{
const std::string output = deviceConfig["output"].asString();
const bool switchOffOnBlack = deviceConfig.get("switchOffOnBlack", true).asBool();
const int transitiontime = deviceConfig.get("transitiontime", 1).asInt();
const int port = deviceConfig.get("port", 1).asInt();
const int numLeds = deviceConfig.get("numLeds", 1).asInt();
std::vector<unsigned int> orbIds;
for (Json::Value::ArrayIndex i = 0; i < deviceConfig["orbIds"].size(); i++) {
orbIds.push_back(deviceConfig["orbIds"][i].asInt());
}
device = new LedDeviceAtmoOrb(output, switchOffOnBlack, transitiontime, port, numLeds, orbIds);
}
else if (type == "test")
{
const std::string output = deviceConfig["output"].asString();