/
BPEthUdpdualport.ino
212 lines (169 loc) · 4.7 KB
/
BPEthUdpdualport.ino
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <SPI.h>
#include <NativeEthernet.h>
#include <NativeEthernetUdp.h>
#include <FastLED.h>
#include "global.h"
CRGB * _fastLEDs;
CRGB * _fastLEDs1;
CLEDController * pLed = NULL;
CLEDController * pLed1 = NULL;
uint16_t numLEDs = 100; //must be same as num in bibliopixel file
uint16_t numLEDs1 = 100;//must be same as num in bibliopixel file
uint8_t bytesPerPixel = 3;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
unsigned int UdpPort = 8888; // local port to listen on
unsigned int UdpPort1 = 8889;
EthernetUDP Udp; // An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp1;
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(UdpPort);
Udp1.begin(UdpPort1);
_fastLEDs = (CRGB*)malloc(sizeof(CRGB)*numLEDs);
memset(_fastLEDs, 0, sizeof(CRGB)*numLEDs);
_fastLEDs1 = (CRGB*)malloc(sizeof(CRGB)*numLEDs1);
memset(_fastLEDs1, 0, sizeof(CRGB)*numLEDs1);
pLed = new APA102Controller<11, 10, BGR, 8950000>(); //change led type <data, clock, LedOrder,SpiSpeed>for whatever setup
pLed1 = new APA102Controller<26, 0, BGR, 8950000>(); //change led type <data, clock, LedOrder,SpiSpeed>for whatever setup
FastLED.addLeds(pLed, _fastLEDs, numLEDs);
FastLED.addLeds(pLed1, _fastLEDs1, numLEDs);
}
#define EMPTYMAX 100
void getData()
{
int packetSize = Udp.parsePacket();
if(packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
// read the packet into packetBufffer
static char cmd = 0;
static uint16_t size = 0;
static uint16_t count = 0;
static uint8_t emptyCount = 0;
static size_t c = 0;
static uint16_t packetSize = numLEDs * bytesPerPixel;
cmd = Udp.read();
size = 0;
Udp.read((char*)&size, 2);
if (cmd == CMDTYPE::PIXEL_DATA)
{
count = 0;
emptyCount = 0;
if (size == packetSize)
{
while (count < packetSize)
{
c = Udp.read(((char*)_fastLEDs) + count, packetSize - count);
if (c == 0)
{
emptyCount++;
if(emptyCount > EMPTYMAX) break;
}
else
{
emptyCount = 0;
}
count += c;
}
}
//uint8_t resp = RETURN_CODES::SUCCESS;
if (count == packetSize)
{
FastLED.show();
}
else if (cmd == CMDTYPE::BRIGHTNESS)
{
uint8_t result = RETURN_CODES::SUCCESS;
if (size != 1)
result = RETURN_CODES::ERROR_SIZE;
else
{
uint8_t brightness = 255;
size_t read = Udp.read((char*)&brightness, 1);
if (read != size)
result = RETURN_CODES::ERROR_SIZE;
else
{
FastLED.setBrightness(brightness);
}
}
//else
//resp = RETURN_CODES::ERROR_SIZE;
//Serial.write(resp);
}
}
}
}
void getData1()
{
int packetSize1 = Udp1.parsePacket();
if(packetSize1) {
// read the packet into packetBufffer
static char cmd = 0;
static uint16_t size = 0;
static uint16_t count = 0;
static uint8_t emptyCount = 0;
static size_t c = 0;
static uint16_t packetSize1 = numLEDs1 * bytesPerPixel;
cmd = Udp1.read();
size = 0;
Udp1.read((char*)&size, 2);
if (cmd == CMDTYPE::PIXEL_DATA)
{
count = 0;
emptyCount = 0;
if (size == packetSize1)
{
while (count < packetSize1)
{
c = Udp1.read(((char*)_fastLEDs1) + count, packetSize1 - count);
if (c == 0)
{
emptyCount++;
if(emptyCount > EMPTYMAX) break;
}
else
{
emptyCount = 0;
}
count += c;
}
}
//uint8_t resp = RETURN_CODES::SUCCESS;
if (count == packetSize1)
{
FastLED.show();
}
else if (cmd == CMDTYPE::BRIGHTNESS)
{
uint8_t result = RETURN_CODES::SUCCESS;
if (size != 1)
result = RETURN_CODES::ERROR_SIZE;
else
{
uint8_t brightness = 255;
size_t read = Udp1.read((char*)&brightness, 1);
if (read != size)
result = RETURN_CODES::ERROR_SIZE;
else
{
FastLED.setBrightness(brightness);
}
}
//else
//resp = RETURN_CODES::ERROR_SIZE;
//Serial.write(resp);
}
}
}
}
void loop() {
getData();
getData1();
}