mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Merge remote-tracking branch 'refs/remotes/tvdzwan/master'
# Conflicts: # include/hyperion/Hyperion.h # libsrc/hyperion/CMakeLists.txt # libsrc/hyperion/Hyperion.cpp Former-commit-id: 1144520581d4531952038d2118cb11e01bebc10e
This commit is contained in:
@@ -20,6 +20,8 @@ SET(Leddevice_QT_HEADERS
|
||||
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.h
|
||||
${CURRENT_SOURCE_DIR}/LedHIDDevice.h
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceRawHID.h
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceFadeCandy.h
|
||||
)
|
||||
|
||||
SET(Leddevice_HEADERS
|
||||
@@ -31,7 +33,9 @@ SET(Leddevice_HEADERS
|
||||
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.h
|
||||
${CURRENT_SOURCE_DIR}/LedDevicePiBlaster.h
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceSedu.h
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceFadeCandy.h
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceUdp.h
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.h
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceTpm2.h
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceAtmo.h
|
||||
@@ -52,7 +56,9 @@ SET(Leddevice_SOURCES
|
||||
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.cpp
|
||||
${CURRENT_SOURCE_DIR}/LedDevicePiBlaster.cpp
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceSedu.cpp
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceTest.cpp
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceTest.cpp
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceFadeCandy.cpp
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceUdp.cpp
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.cpp
|
||||
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.cpp
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceTpm2.cpp
|
||||
@@ -102,8 +108,12 @@ if(ENABLE_TINKERFORGE)
|
||||
)
|
||||
endif(ENABLE_TINKERFORGE)
|
||||
|
||||
|
||||
if(ENABLE_QT5)
|
||||
QT5_WRAP_CPP(Leddevice_HEADERS_MOC ${Leddevice_QT_HEADERS})
|
||||
else(ENABLE_QT5)
|
||||
QT4_WRAP_CPP(Leddevice_HEADERS_MOC ${Leddevice_QT_HEADERS})
|
||||
endif(ENABLE_QT5)
|
||||
|
||||
|
||||
add_library(leddevice
|
||||
${Leddevice_HEADERS}
|
||||
@@ -112,9 +122,13 @@ add_library(leddevice
|
||||
${Leddevice_SOURCES}
|
||||
)
|
||||
|
||||
if(ENABLE_QT5)
|
||||
qt5_use_modules(leddevice Widgets Network)
|
||||
endif(ENABLE_QT5)
|
||||
|
||||
target_link_libraries(leddevice
|
||||
hyperion-utils
|
||||
serialport
|
||||
serialport
|
||||
${LIBUSB_1_LIBRARIES} #apt-get install libusb-1.0-0-dev
|
||||
${CMAKE_THREAD_LIBS_INIT}
|
||||
${QT_LIBRARIES}
|
||||
|
@@ -3,7 +3,6 @@
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
// Linux includes
|
||||
#include <fcntl.h>
|
||||
@@ -13,30 +12,38 @@
|
||||
#include "LedDeviceAdalightApa102.h"
|
||||
|
||||
LedDeviceAdalightApa102::LedDeviceAdalightApa102(const std::string& outputDevice, const unsigned baudrate, int delayAfterConnect_ms) :
|
||||
LedDeviceAdalight(outputDevice, baudrate, delayAfterConnect_ms),
|
||||
LedRs232Device(outputDevice, baudrate, delayAfterConnect_ms),
|
||||
_ledBuffer(0),
|
||||
_timer()
|
||||
{
|
||||
// setup the timer
|
||||
_timer.setSingleShot(false);
|
||||
_timer.setInterval(5000);
|
||||
connect(&_timer, SIGNAL(timeout()), this, SLOT(rewriteLeds()));
|
||||
|
||||
// start the timer
|
||||
_timer.start();
|
||||
}
|
||||
//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)
|
||||
{
|
||||
ledCount = ledValues.size();
|
||||
const unsigned int startFrameSize = 4;
|
||||
const unsigned int endFrameSize = std::max<unsigned int>(((ledValues.size() + 15) / 16), 4);
|
||||
const unsigned int mLedCount = (ledValues.size() * 4) + startFrameSize + endFrameSize;
|
||||
if(_ledBuffer.size() != mLedCount){
|
||||
_ledBuffer.resize(mLedCount, 0xFF);
|
||||
const unsigned int endFrameSize = std::max<unsigned int>(((ledCount + 15) / 16), 4);
|
||||
const unsigned int mLedCount = (ledCount * 4) + startFrameSize + endFrameSize;
|
||||
if(_ledBuffer.size() != mLedCount+6){
|
||||
_ledBuffer.resize(mLedCount+6, 0x00);
|
||||
_ledBuffer[0] = 'A';
|
||||
_ledBuffer[1] = 'd';
|
||||
_ledBuffer[2] = 'a';
|
||||
_ledBuffer[3] = (((unsigned int)(ledValues.size() * 1.33) - 1) >> 8) & 0xFF; // LED count high byte
|
||||
_ledBuffer[4] = ((unsigned int)(ledValues.size() * 1.33) - 1) & 0xFF; // LED count low byte
|
||||
_ledBuffer[3] = (((unsigned int)(ledValues.size())) >> 8) & 0xFF; // LED count high byte
|
||||
_ledBuffer[4] = ((unsigned int)(ledValues.size())) & 0xFF; // LED count low byte
|
||||
_ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
|
||||
}
|
||||
|
||||
for (unsigned iLed=1; iLed<=ledValues.size(); iLed++) {
|
||||
|
||||
for (unsigned iLed=1; iLed<=ledCount; iLed++) {
|
||||
const ColorRgb& rgb = ledValues[iLed-1];
|
||||
_ledBuffer[iLed*4+6] = 0xFF;
|
||||
_ledBuffer[iLed*4+1+6] = rgb.red;
|
||||
@@ -51,4 +58,25 @@ int LedDeviceAdalightApa102::write(const std::vector<ColorRgb> & ledValues)
|
||||
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
||||
}
|
||||
|
||||
int LedDeviceAdalightApa102::switchOff()
|
||||
{
|
||||
for (unsigned iLed=1; iLed<=ledCount; iLed++) {
|
||||
_ledBuffer[iLed*4+6] = 0xFF;
|
||||
_ledBuffer[iLed*4+1+6] = 0x00;
|
||||
_ledBuffer[iLed*4+2+6] = 0x00;
|
||||
_ledBuffer[iLed*4+3+6] = 0x00;
|
||||
}
|
||||
|
||||
// restart the timer
|
||||
_timer.start();
|
||||
|
||||
// write data
|
||||
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
||||
|
||||
}
|
||||
|
||||
void LedDeviceAdalightApa102::rewriteLeds()
|
||||
{
|
||||
writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
||||
}
|
||||
|
||||
|
@@ -7,12 +7,12 @@
|
||||
#include <QTimer>
|
||||
|
||||
// hyperion incluse
|
||||
#include "LedDeviceAdalight.h"
|
||||
#include "LedRs232Device.h"
|
||||
|
||||
///
|
||||
/// Implementation of the LedDevice interface for writing to an Adalight led device for APA102.
|
||||
///
|
||||
class LedDeviceAdalightApa102 : public LedDeviceAdalight
|
||||
class LedDeviceAdalightApa102 : public LedRs232Device
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -32,13 +32,17 @@ public:
|
||||
/// @return Zero on succes else negative
|
||||
///
|
||||
virtual int write(const std::vector<ColorRgb> & ledValues);
|
||||
virtual int switchOff();
|
||||
|
||||
|
||||
|
||||
private slots:
|
||||
/// Write the last data to the leds again
|
||||
void rewriteLeds();
|
||||
|
||||
private:
|
||||
/// The buffer containing the packed RGB values
|
||||
std::vector<uint8_t> _ledBuffer;
|
||||
|
||||
unsigned int ledCount;
|
||||
/// Timer object which makes sure that led data is written at a minimum rate
|
||||
/// The Adalight device will switch off when it does not receive data at least
|
||||
/// every 15 seconds
|
||||
|
@@ -30,6 +30,8 @@
|
||||
#include "LedDevicePiBlaster.h"
|
||||
#include "LedDeviceSedu.h"
|
||||
#include "LedDeviceTest.h"
|
||||
#include "LedDeviceFadeCandy.h"
|
||||
#include "LedDeviceUdp.h"
|
||||
#include "LedDeviceHyperionUsbasp.h"
|
||||
#include "LedDevicePhilipsHue.h"
|
||||
#include "LedDeviceTpm2.h"
|
||||
@@ -127,8 +129,9 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
|
||||
{
|
||||
const std::string output = deviceConfig["output"].asString();
|
||||
const unsigned rate = deviceConfig["rate"].asInt();
|
||||
const unsigned latchtime = deviceConfig.get("latchtime",500000).asInt();
|
||||
|
||||
LedDeviceWs2801* deviceWs2801 = new LedDeviceWs2801(output, rate);
|
||||
LedDeviceWs2801* deviceWs2801 = new LedDeviceWs2801(output, rate, latchtime);
|
||||
deviceWs2801->open();
|
||||
|
||||
device = deviceWs2801;
|
||||
@@ -243,6 +246,21 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
|
||||
const std::string output = deviceConfig["output"].asString();
|
||||
device = new LedDeviceTest(output);
|
||||
}
|
||||
else if (type == "fadecandy")
|
||||
{
|
||||
const std::string host = deviceConfig.get("output", "127.0.0.1").asString();
|
||||
const uint16_t port = deviceConfig.get("port", 7890).asInt();
|
||||
const uint16_t channel = deviceConfig.get("channel", 0).asInt();
|
||||
device = new LedDeviceFadeCandy(host, port, channel);
|
||||
}
|
||||
else if (type == "udp")
|
||||
{
|
||||
const std::string output = deviceConfig["output"].asString();
|
||||
const unsigned rate = deviceConfig["rate"].asInt();
|
||||
const unsigned protocol = deviceConfig["protocol"].asInt();
|
||||
const unsigned maxPacket = deviceConfig["maxpacket"].asInt();
|
||||
device = new LedDeviceUdp(output, rate, protocol, maxPacket);
|
||||
}
|
||||
else if (type == "tpm2")
|
||||
{
|
||||
const std::string output = deviceConfig["output"].asString();
|
||||
|
90
libsrc/leddevice/LedDeviceFadeCandy.cpp
Normal file
90
libsrc/leddevice/LedDeviceFadeCandy.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "LedDeviceFadeCandy.h"
|
||||
|
||||
static const signed MAX_NUM_LEDS = 10000; // OPC can handle 21845 leds - in theory, fadecandy device should handle 10000 leds
|
||||
static const unsigned OPC_SET_PIXELS = 0; // OPC command codes
|
||||
static const unsigned OPC_HEADER_SIZE = 4; // OPC header size
|
||||
|
||||
|
||||
LedDeviceFadeCandy::LedDeviceFadeCandy(const std::string& host, const uint16_t port, const unsigned channel) :
|
||||
_host(host), _port(port), _channel(channel)
|
||||
{
|
||||
_opc_data.resize( OPC_HEADER_SIZE );
|
||||
_opc_data[0] = channel;
|
||||
_opc_data[1] = OPC_SET_PIXELS;
|
||||
_opc_data[2] = 0;
|
||||
_opc_data[3] = 0;
|
||||
}
|
||||
|
||||
|
||||
LedDeviceFadeCandy::~LedDeviceFadeCandy()
|
||||
{
|
||||
_client.close();
|
||||
}
|
||||
|
||||
|
||||
bool LedDeviceFadeCandy::isConnected()
|
||||
{
|
||||
return _client.state() == QAbstractSocket::ConnectedState;
|
||||
}
|
||||
|
||||
|
||||
bool LedDeviceFadeCandy::tryConnect()
|
||||
{
|
||||
if ( _client.state() == QAbstractSocket::UnconnectedState ) {
|
||||
_client.connectToHost( _host.c_str(), _port);
|
||||
if ( _client.waitForConnected(1000) )
|
||||
qDebug("fadecandy/opc: connected to %s:%i on channel %i", _host.c_str(), _port, _channel);
|
||||
}
|
||||
|
||||
return isConnected();
|
||||
}
|
||||
|
||||
|
||||
int LedDeviceFadeCandy::write( const std::vector<ColorRgb> & ledValues )
|
||||
{
|
||||
ssize_t nrLedValues = ledValues.size();
|
||||
ssize_t led_data_size = nrLedValues * 3; // 3 color bytes
|
||||
ssize_t opc_data_size = led_data_size + OPC_HEADER_SIZE;
|
||||
|
||||
if (nrLedValues > MAX_NUM_LEDS)
|
||||
{
|
||||
std::cerr << "fadecandy/opc: Invalid attempt to write led values. Not more than " << MAX_NUM_LEDS << " leds are allowed." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ( opc_data_size != _opc_data.size() )
|
||||
_opc_data.resize( opc_data_size );
|
||||
|
||||
_opc_data[2] = led_data_size >> 8;
|
||||
_opc_data[3] = led_data_size & 0xff;
|
||||
|
||||
uint idx = OPC_HEADER_SIZE;
|
||||
for (const ColorRgb& color : ledValues)
|
||||
{
|
||||
_opc_data[idx ] = unsigned( color.red );
|
||||
_opc_data[idx+1] = unsigned( color.green );
|
||||
_opc_data[idx+2] = unsigned( color.blue );
|
||||
idx += 3;
|
||||
}
|
||||
|
||||
return ( transferData()<0 ? -1 : 0 );
|
||||
}
|
||||
|
||||
|
||||
int LedDeviceFadeCandy::transferData()
|
||||
{
|
||||
if ( isConnected() || tryConnect() )
|
||||
return _client.write( _opc_data, _opc_data.size() );
|
||||
|
||||
return -2;
|
||||
}
|
||||
|
||||
|
||||
int LedDeviceFadeCandy::switchOff()
|
||||
{
|
||||
for ( int idx=OPC_HEADER_SIZE; idx < _opc_data.size(); idx++ )
|
||||
_opc_data[idx] = 0;
|
||||
|
||||
return ( transferData()<0 ? -1 : 0 );
|
||||
}
|
||||
|
71
libsrc/leddevice/LedDeviceFadeCandy.h
Normal file
71
libsrc/leddevice/LedDeviceFadeCandy.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
// STL/Qt includes
|
||||
#include <fstream>
|
||||
#include <QObject>
|
||||
#include <QTcpSocket>
|
||||
|
||||
// Leddevice includes
|
||||
#include <leddevice/LedDevice.h>
|
||||
|
||||
///
|
||||
/// Implementation of the LedDevice interface for sending to
|
||||
/// fadecandy/opc-server via network by using the 'open pixel control' protocol.
|
||||
///
|
||||
class LedDeviceFadeCandy : public QObject, public LedDevice
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
///
|
||||
/// Constructs the LedDevice for fadecandy/opc server
|
||||
///
|
||||
/// @param host The ip address/host name of fadecandy/opc server
|
||||
/// @param port The port to use (fadecandy default is 7890)
|
||||
///
|
||||
LedDeviceFadeCandy(const std::string& host, const uint16_t port, const unsigned channel);
|
||||
|
||||
///
|
||||
/// Destructor of the LedDevice; closes the tcp client
|
||||
///
|
||||
virtual ~LedDeviceFadeCandy();
|
||||
|
||||
///
|
||||
/// Writes the led color values to the led-device
|
||||
///
|
||||
/// @param ledValues The color-value per led
|
||||
/// @return Zero on succes else negative
|
||||
///
|
||||
virtual int write(const std::vector<ColorRgb> & ledValues);
|
||||
|
||||
/// Switch the leds off
|
||||
virtual int switchOff();
|
||||
|
||||
|
||||
private:
|
||||
QTcpSocket _client;
|
||||
const std::string _host;
|
||||
const uint16_t _port;
|
||||
const unsigned _channel;
|
||||
QByteArray _opc_data;
|
||||
|
||||
/// try to establish connection to opc server, if not connected yet
|
||||
///
|
||||
/// @return true if connection is established
|
||||
///
|
||||
bool tryConnect();
|
||||
|
||||
/// return the conenction state
|
||||
///
|
||||
/// @return True if connection established
|
||||
///
|
||||
bool isConnected();
|
||||
|
||||
|
||||
/// transfer current opc_data buffer to opc server
|
||||
///
|
||||
/// @return amount of transfered bytes. -1 error while transfering, -2 error while connecting
|
||||
///
|
||||
int transferData();
|
||||
|
||||
};
|
@@ -5,10 +5,9 @@
|
||||
#include <json/json.h>
|
||||
|
||||
// qt includes
|
||||
#include <QtCore/qmath.h>
|
||||
#include <QUrl>
|
||||
#include <QHttpRequestHeader>
|
||||
#include <QtCore/qmath.h>
|
||||
#include <QEventLoop>
|
||||
#include <QNetworkReply>
|
||||
|
||||
#include <set>
|
||||
|
||||
@@ -144,14 +143,14 @@ LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output, const std::s
|
||||
int transitiontime, std::vector<unsigned int> lightIds) :
|
||||
host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime(
|
||||
transitiontime), lightIds(lightIds) {
|
||||
http = new QHttp(host);
|
||||
manager = new QNetworkAccessManager();
|
||||
timer.setInterval(3000);
|
||||
timer.setSingleShot(true);
|
||||
connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));
|
||||
}
|
||||
|
||||
LedDevicePhilipsHue::~LedDevicePhilipsHue() {
|
||||
delete http;
|
||||
delete manager;
|
||||
}
|
||||
|
||||
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
|
||||
@@ -215,34 +214,30 @@ int LedDevicePhilipsHue::switchOff() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LedDevicePhilipsHue::put(QString route, QString content) {
|
||||
QString url = QString("/api/%1/%2").arg(username).arg(route);
|
||||
QHttpRequestHeader header("PUT", url);
|
||||
header.setValue("Host", host);
|
||||
header.setValue("Accept-Encoding", "identity");
|
||||
header.setValue("Connection", "keep-alive");
|
||||
header.setValue("Content-Length", QString("%1").arg(content.size()));
|
||||
QEventLoop loop;
|
||||
// Connect requestFinished signal to quit slot of the loop.
|
||||
loop.connect(http, SIGNAL(requestFinished(int, bool)), SLOT(quit()));
|
||||
void LedDevicePhilipsHue::put(QString route, QString content) {
|
||||
QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route);
|
||||
// Perfrom request
|
||||
http->request(header, content.toAscii());
|
||||
QNetworkRequest request(url);
|
||||
QNetworkReply* reply = manager->put(request, content.toLatin1());
|
||||
// Connect finished signal to quit slot of the loop.
|
||||
QEventLoop loop;
|
||||
loop.connect(reply, SIGNAL(finished()), SLOT(quit()));
|
||||
// Go into the loop until the request is finished.
|
||||
loop.exec();
|
||||
}
|
||||
|
||||
QByteArray LedDevicePhilipsHue::get(QString route) {
|
||||
QString url = QString("/api/%1/%2").arg(username).arg(route);
|
||||
// Event loop to block until request finished.
|
||||
QEventLoop loop;
|
||||
// Connect requestFinished signal to quit slot of the loop.
|
||||
loop.connect(http, SIGNAL(requestFinished(int, bool)), SLOT(quit()));
|
||||
QByteArray LedDevicePhilipsHue::get(QString route) {
|
||||
QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route);
|
||||
// Perfrom request
|
||||
http->get(url);
|
||||
QNetworkRequest request(url);
|
||||
QNetworkReply* reply = manager->get(request);
|
||||
// Connect requestFinished signal to quit slot of the loop.
|
||||
QEventLoop loop;
|
||||
loop.connect(reply, SIGNAL(finished()), SLOT(quit()));
|
||||
// Go into the loop until the request is finished.
|
||||
loop.exec();
|
||||
// Read all data of the response.
|
||||
return http->readAll();
|
||||
// Read all data of the response.
|
||||
return reply->readAll();
|
||||
}
|
||||
|
||||
QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) {
|
||||
|
@@ -6,9 +6,8 @@
|
||||
// Qt includes
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QHttp>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QTimer>
|
||||
|
||||
// Leddevice includes
|
||||
#include <leddevice/LedDevice.h>
|
||||
|
||||
@@ -164,8 +163,8 @@ private:
|
||||
QString host;
|
||||
/// User name for the API ("newdeveloper")
|
||||
QString username;
|
||||
/// Qhttp object for sending requests.
|
||||
QHttp* http;
|
||||
/// QNetworkAccessManager object for sending requests.
|
||||
QNetworkAccessManager* manager;
|
||||
/// Use timer to reset lights when we got into "GRABBINGMODE_OFF".
|
||||
QTimer timer;
|
||||
///
|
||||
|
165
libsrc/leddevice/LedDeviceUdp.cpp
Normal file
165
libsrc/leddevice/LedDeviceUdp.cpp
Normal file
@@ -0,0 +1,165 @@
|
||||
|
||||
// Local-Hyperion includes
|
||||
#include "LedDeviceUdp.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <assert.h>
|
||||
|
||||
struct addrinfo hints, *servinfo, *p;
|
||||
//char udpbuffer[1024];
|
||||
int sockfd;
|
||||
int ledprotocol;
|
||||
int leds_per_pkt;
|
||||
int update_number;
|
||||
int fragment_number;
|
||||
|
||||
LedDeviceUdp::LedDeviceUdp(const std::string& output, const unsigned baudrate, const unsigned protocol, const unsigned maxPacket)
|
||||
//LedDeviceUdp::LedDeviceUdp(const std::string& output, const unsigned baudrate) :
|
||||
// _ofs(output.empty()?"/home/pi/LedDevice.out":output.c_str())
|
||||
{
|
||||
|
||||
std::string hostname;
|
||||
std::string port;
|
||||
ledprotocol = protocol;
|
||||
leds_per_pkt = ((maxPacket-4)/3);
|
||||
if (leds_per_pkt <= 0) {
|
||||
leds_per_pkt = 200;
|
||||
}
|
||||
|
||||
//printf ("leds_per_pkt is %d\n", leds_per_pkt);
|
||||
int got_colon=0;
|
||||
for (unsigned int i=0; i<output.length(); i++) {
|
||||
if (output[i] == ':') {
|
||||
got_colon++;
|
||||
} else if (got_colon == 0) {
|
||||
hostname+=output[i];
|
||||
} else {
|
||||
port+=output[i];
|
||||
}
|
||||
}
|
||||
//std::cout << "output " << output << " hostname " << hostname << " port " << port <<std::endl;
|
||||
assert(got_colon==1);
|
||||
|
||||
int rv;
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
|
||||
if ((rv = getaddrinfo(hostname.c_str() , port.c_str(), &hints, &servinfo)) != 0) {
|
||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
|
||||
assert(rv==0);
|
||||
}
|
||||
|
||||
// loop through all the results and make a socket
|
||||
for(p = servinfo; p != NULL; p = p->ai_next) {
|
||||
if ((sockfd = socket(p->ai_family, p->ai_socktype,
|
||||
p->ai_protocol)) == -1) {
|
||||
perror("talker: socket");
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (p == NULL) {
|
||||
fprintf(stderr, "talker: failed to create socket\n");
|
||||
assert(p!=NULL);
|
||||
}
|
||||
}
|
||||
|
||||
LedDeviceUdp::~LedDeviceUdp()
|
||||
{
|
||||
// empty
|
||||
}
|
||||
|
||||
int LedDeviceUdp::write(const std::vector<ColorRgb> & ledValues)
|
||||
{
|
||||
|
||||
char udpbuffer[4096];
|
||||
int udpPtr=0;
|
||||
|
||||
update_number++;
|
||||
update_number &= 0xf;
|
||||
|
||||
if (ledprotocol == 0) {
|
||||
int i=0;
|
||||
for (const ColorRgb& color : ledValues)
|
||||
{
|
||||
if (i<4090) {
|
||||
udpbuffer[i++] = color.red;
|
||||
udpbuffer[i++] = color.green;
|
||||
udpbuffer[i++] = color.blue;
|
||||
}
|
||||
//printf ("c.red %d sz c.red %d\n", color.red, sizeof(color.red));
|
||||
}
|
||||
sendto(sockfd, udpbuffer, i, 0, p->ai_addr, p->ai_addrlen);
|
||||
}
|
||||
if (ledprotocol == 1) {
|
||||
#define MAXLEDperFRAG 450
|
||||
int mLedCount = ledValues.size();
|
||||
|
||||
for (int frag=0; frag<4; frag++) {
|
||||
udpPtr=0;
|
||||
udpbuffer[udpPtr++] = 0;
|
||||
udpbuffer[udpPtr++] = 0;
|
||||
udpbuffer[udpPtr++] = (frag*MAXLEDperFRAG)/256; // high byte
|
||||
udpbuffer[udpPtr++] = (frag*MAXLEDperFRAG)%256; // low byte
|
||||
int ct=0;
|
||||
for (int this_led = frag*300; ((this_led<mLedCount) && (ct++<MAXLEDperFRAG)); this_led++) {
|
||||
const ColorRgb& color = ledValues[this_led];
|
||||
if (udpPtr<4090) {
|
||||
udpbuffer[udpPtr++] = color.red;
|
||||
udpbuffer[udpPtr++] = color.green;
|
||||
udpbuffer[udpPtr++] = color.blue;
|
||||
}
|
||||
}
|
||||
if (udpPtr > 7)
|
||||
sendto(sockfd, udpbuffer, udpPtr, 0, p->ai_addr, p->ai_addrlen);
|
||||
}
|
||||
}
|
||||
if (ledprotocol == 2) {
|
||||
udpPtr = 0;
|
||||
unsigned int ledCtr = 0;
|
||||
fragment_number = 0;
|
||||
udpbuffer[udpPtr++] = update_number & 0xf;
|
||||
udpbuffer[udpPtr++] = fragment_number++;
|
||||
udpbuffer[udpPtr++] = ledCtr/256; // high byte
|
||||
udpbuffer[udpPtr++] = ledCtr%256; // low byte
|
||||
|
||||
for (const ColorRgb& color : ledValues)
|
||||
{
|
||||
if (udpPtr<4090) {
|
||||
udpbuffer[udpPtr++] = color.red;
|
||||
udpbuffer[udpPtr++] = color.green;
|
||||
udpbuffer[udpPtr++] = color.blue;
|
||||
}
|
||||
ledCtr++;
|
||||
if ( (ledCtr % leds_per_pkt == 0) || (ledCtr == ledValues.size()) ) {
|
||||
sendto(sockfd, udpbuffer, udpPtr, 0, p->ai_addr, p->ai_addrlen);
|
||||
memset(udpbuffer, 0, sizeof udpbuffer);
|
||||
udpPtr = 0;
|
||||
udpbuffer[udpPtr++] = update_number & 0xf;
|
||||
udpbuffer[udpPtr++] = fragment_number++;
|
||||
udpbuffer[udpPtr++] = ledCtr/256; // high byte
|
||||
udpbuffer[udpPtr++] = ledCtr%256; // low byte
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LedDeviceUdp::switchOff()
|
||||
{
|
||||
// return write(std::vector<ColorRgb>(mLedCount, ColorRgb{0,0,0}));
|
||||
return 0;
|
||||
}
|
44
libsrc/leddevice/LedDeviceUdp.h
Normal file
44
libsrc/leddevice/LedDeviceUdp.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
// STL includes0
|
||||
#include <fstream>
|
||||
|
||||
// Leddevice includes
|
||||
#include <leddevice/LedDevice.h>
|
||||
|
||||
///
|
||||
/// Implementation of the LedDevice that write the led-colors to an
|
||||
/// ASCII-textfile('/home/pi/LedDevice.out')
|
||||
///
|
||||
class LedDeviceUdp : public LedDevice
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Constructs the test-device, which opens an output stream to the file
|
||||
///
|
||||
LedDeviceUdp(const std::string& output, const unsigned baudrate, const unsigned protocol, const unsigned maxPacket);
|
||||
|
||||
///
|
||||
/// Destructor of this test-device
|
||||
///
|
||||
virtual ~LedDeviceUdp();
|
||||
|
||||
///
|
||||
/// Writes the given led-color values to the output stream
|
||||
///
|
||||
/// @param ledValues The color-value per led
|
||||
///
|
||||
/// @return Zero on success else negative
|
||||
///
|
||||
virtual int write(const std::vector<ColorRgb> & ledValues);
|
||||
|
||||
/// Switch the leds off
|
||||
virtual int switchOff();
|
||||
|
||||
private:
|
||||
/// The outputstream
|
||||
// std::ofstream _ofs;
|
||||
|
||||
/// the number of leds (needed when switching off)
|
||||
size_t mLedCount;
|
||||
};
|
@@ -18,6 +18,13 @@ LedDeviceWs2801::LedDeviceWs2801(const std::string& outputDevice, const unsigned
|
||||
// empty
|
||||
}
|
||||
|
||||
LedDeviceWs2801::LedDeviceWs2801(const std::string& outputDevice, const unsigned baudrate, const unsigned latchTime) :
|
||||
LedSpiDevice(outputDevice, baudrate, latchTime),
|
||||
mLedCount(0)
|
||||
{
|
||||
// empty
|
||||
}
|
||||
|
||||
int LedDeviceWs2801::write(const std::vector<ColorRgb> &ledValues)
|
||||
{
|
||||
mLedCount = ledValues.size();
|
||||
|
@@ -21,6 +21,10 @@ public:
|
||||
LedDeviceWs2801(const std::string& outputDevice,
|
||||
const unsigned baudrate);
|
||||
|
||||
LedDeviceWs2801(const std::string& outputDevice,
|
||||
const unsigned baudrate,
|
||||
const unsigned latchTime);
|
||||
|
||||
///
|
||||
/// Writes the led color values to the led-device
|
||||
///
|
||||
|
Reference in New Issue
Block a user