mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
add udp protocol support
Former-commit-id: a92810f14cf2af15710a38246a742ca2b94fd3bd
This commit is contained in:
parent
847ab34183
commit
382f22f23c
@ -32,6 +32,7 @@ SET(Leddevice_HEADERS
|
|||||||
${CURRENT_SOURCE_DIR}/LedDevicePiBlaster.h
|
${CURRENT_SOURCE_DIR}/LedDevicePiBlaster.h
|
||||||
${CURRENT_SOURCE_DIR}/LedDeviceSedu.h
|
${CURRENT_SOURCE_DIR}/LedDeviceSedu.h
|
||||||
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
|
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
|
||||||
|
${CURRENT_SOURCE_DIR}/LedDeviceUdp.h
|
||||||
${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.h
|
${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.h
|
||||||
${CURRENT_SOURCE_DIR}/LedDeviceTpm2.h
|
${CURRENT_SOURCE_DIR}/LedDeviceTpm2.h
|
||||||
${CURRENT_SOURCE_DIR}/LedDeviceAtmo.h
|
${CURRENT_SOURCE_DIR}/LedDeviceAtmo.h
|
||||||
@ -53,6 +54,7 @@ SET(Leddevice_SOURCES
|
|||||||
${CURRENT_SOURCE_DIR}/LedDevicePiBlaster.cpp
|
${CURRENT_SOURCE_DIR}/LedDevicePiBlaster.cpp
|
||||||
${CURRENT_SOURCE_DIR}/LedDeviceSedu.cpp
|
${CURRENT_SOURCE_DIR}/LedDeviceSedu.cpp
|
||||||
${CURRENT_SOURCE_DIR}/LedDeviceTest.cpp
|
${CURRENT_SOURCE_DIR}/LedDeviceTest.cpp
|
||||||
|
${CURRENT_SOURCE_DIR}/LedDeviceUdp.cpp
|
||||||
${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.cpp
|
${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.cpp
|
||||||
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.cpp
|
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.cpp
|
||||||
${CURRENT_SOURCE_DIR}/LedDeviceTpm2.cpp
|
${CURRENT_SOURCE_DIR}/LedDeviceTpm2.cpp
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
#include "LedDevicePiBlaster.h"
|
#include "LedDevicePiBlaster.h"
|
||||||
#include "LedDeviceSedu.h"
|
#include "LedDeviceSedu.h"
|
||||||
#include "LedDeviceTest.h"
|
#include "LedDeviceTest.h"
|
||||||
|
#include "LedDeviceUdp.h"
|
||||||
#include "LedDeviceHyperionUsbasp.h"
|
#include "LedDeviceHyperionUsbasp.h"
|
||||||
#include "LedDevicePhilipsHue.h"
|
#include "LedDevicePhilipsHue.h"
|
||||||
#include "LedDeviceTpm2.h"
|
#include "LedDeviceTpm2.h"
|
||||||
@ -243,6 +244,14 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
|
|||||||
const std::string output = deviceConfig["output"].asString();
|
const std::string output = deviceConfig["output"].asString();
|
||||||
device = new LedDeviceTest(output);
|
device = new LedDeviceTest(output);
|
||||||
}
|
}
|
||||||
|
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")
|
else if (type == "tpm2")
|
||||||
{
|
{
|
||||||
const std::string output = deviceConfig["output"].asString();
|
const std::string output = deviceConfig["output"].asString();
|
||||||
|
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;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user