forked from penfold42/ESPixelBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpraw.cpp
91 lines (69 loc) · 2.73 KB
/
udpraw.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <Arduino.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>
#include <lwip/ip_addr.h>
#include <lwip/igmp.h>
#include "ESPixelStick.h"
#include "udpraw.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef ESPS_ENABLE_UDPRAW
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
extern config_t config;
extern Ticker idleTicker;
#if defined(ESPS_MODE_PIXEL)
extern PixelDriver pixels; // Pixel object
#define _driver pixels
#elif defined(ESPS_MODE_SERIAL)
extern SerialDriver serial; // Serial object
#define _driver serial
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void UdpRaw::begin(uint16_t port /*= ESPS_UDP_RAW_DEFAULT_PORT*/)
{
bool isListening = false;
if(config.multicast)
{
uint16_t universe = config.universe;
IPAddress address = IPAddress(239, 255, ((universe >> 8) & 0xff), ((universe >> 0) & 0xff));
isListening = _udp.listenMulticast(address, port);
}
else
{
isListening = _udp.listen(port);
}
if(isListening)
{
MDNS.addService("hyperiond-rgbled", "udp", port);
_udp.onPacket(std::bind(&UdpRaw::onPacket, this, std::placeholders::_1));
LOG_PORT.print(" - Local UDP RAW Port: ");
LOG_PORT.println(port);
}
}
void UdpRaw::onPacket(AsyncUDPPacket packet)
{
stats.last_seen = millis();
stats.last_clientIP = packet.remoteIP();
stats.num_packets++;
if (packet.length() < config.channel_count)
stats.short_packets++;
if (packet.length() > config.channel_count)
stats.long_packets++;
// do not disturb effects...
if ( (config.ds == DataSource::E131) || (config.ds == DataSource::IDLEWEB) ) {
idleTicker.attach(config.effect_idletimeout, idleTimeout);
if (config.ds == DataSource::IDLEWEB) {
config.ds = DataSource::E131;
}
int nread = _min(packet.length(), config.channel_count);
memcpy(_driver.getData(), packet.data(), nread);
if (zeropad) {
int nzero = config.channel_count - nread;
if (nzero > 0)
memset(_driver.getData() + nread, 0, nzero);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif //#ifdef ESPS_ENABLE_UDPRAW
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////