hyperion.ng/libsrc/udplistener/UDPListener.cpp
penfold42 a23735d1ef New builtin udp listener (#18)
* Fixed compile error when no grabbers are defined

* Remove stupid avahi warning...

* Started on the new integrated UDP listener to replace the python effect.

Cloned boblight server and rename it to be UDP listener
It compiles!, It starts! it seems to work as a second boblight protocol server...

* moving from the exsting TCP to UDP.
i can catch packets now.. need to consider ditching the connection handling

* It kinda works right now.
UDP packets are received, led data is sent and hyperion displays them.
.... for a moment before going back to what it was doing

* It works!

looks like the default priority of 900 was fighting with something else that was also 900

commented out some udp packet debugging

* oops, forgot to add the changes the the previous commit

* resolving merge conflicts
2016-06-20 08:38:12 +02:00

84 lines
2.0 KiB
C++

// system includes
#include <stdexcept>
// project includes
#include <udplistener/UDPListener.h>
// hyperion util includes
#include "hyperion/ImageProcessorFactory.h"
#include "hyperion/ImageProcessor.h"
#include "utils/ColorRgb.h"
#include "HyperionConfig.h"
UDPListener::UDPListener(Hyperion *hyperion, const int priority, const int timeout, uint16_t port) :
QObject(),
_hyperion(hyperion),
_server(),
_openConnections(),
_priority(priority),
_timeout(timeout),
_ledColors(hyperion->getLedCount(), ColorRgb::BLACK)
{
_server = new QUdpSocket(this);
if (!_server->bind(QHostAddress::Any, port))
{
throw std::runtime_error("UDPLISTENER ERROR: server could not bind to port");
}
// Set trigger for incoming connections
connect(_server, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
}
UDPListener::~UDPListener()
{
// clear the current channel
_hyperion->clear(_priority);
}
uint16_t UDPListener::getPort() const
{
return _server->localPort();
}
void UDPListener::readPendingDatagrams()
{
while (_server->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(_server->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
_server->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
// std::cout << "UDPLISTENER INFO: new packet from " << std::endl;
processTheDatagram(&datagram);
}
}
void UDPListener::processTheDatagram(const QByteArray * datagram)
{
// std::cout << "udp message: " << datagram->data() << std::endl;
int packlen = datagram->size()/3;
int ledlen = _ledColors.size();
// int maxled = std::min(datagram->size()/3, _ledColors.size());
int maxled = std::min(packlen , ledlen);
for (int ledIndex=0; ledIndex < maxled; ledIndex++) {
ColorRgb & rgb = _ledColors[ledIndex];
rgb.red = datagram->at(ledIndex*3+0);
rgb.green = datagram->at(ledIndex*3+1);
rgb.blue = datagram->at(ledIndex*3+2);
// printf("%02x%02x%02x%02x ", ledIndex, rgb.red, rgb.green, rgb.blue);
}
// printf ("\n");
_hyperion->setColors(_priority, _ledColors, _timeout, -1);
}