-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathESP32ArtNetDriver.ino
135 lines (118 loc) · 3.84 KB
/
ESP32ArtNetDriver.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
/*
Writen for the "NODE 32S" Dev Board
SPI details for this dev board:
SPI CLOCK pin 18
SPI DATA pin 23
*/
//Wifi Library
#include <WiFi.h>
//UPD libarry to read UDP packets from the network
#include <WiFiUdp.h>
//SPI libarry to enable us to use the SPI device to write data to the pixels
#include "SPI.h"
//A custome writen Pixel driver for the APA102/SK9822 pixels
#include "apa102LEDStrip.h"
//A custom written cross platform ArtNet driver
#include "artNetPacket.h"
//Networking
const char * ssid = "ArtNet";
const char * password = "megapixels";
unsigned int artNetPort = 6454;
const short int maxPacketBufferSize = 530;
char packetBuffer[maxPacketBufferSize];
WiFiUDP udp;
short int packetSize=0;
artNetPacket dmxData;
const byte universeRange[2] = {0,1};
const unsigned short int bytesPerFixture = 7*14*3; // Width x Height x 3 bytes(RGB)
//LED Stuff
const unsigned short int numberOfPixelsPerPanel = 14*14;;
const byte bytesPerLed=4, maxBrightness=255;
apa102LEDStrip leds;
byte tempColour[3];
//Counters
unsigned short int pixelIndex=0, dmxCounter=0;
void setup()
{
//Start WIFI using the SSID & Hostname above
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.setHostname("ESP32ANNode");
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
}
//Start the UDP subsytem
udp.begin(artNetPort);
//Start the SPI device
SPI.begin();
SPI.setBitOrder(MSBFIRST); //SPI mode for the apa102 and sk9822 pixels is Most Significant Bit 1st
SPI.setFrequency(10000000); //We are using a relatively HIGH driveing Frequency. You may need to lower this depending on your hardware set up
//Initiate the LEDs
leds.init(numberOfPixelsPerPanel, bytesPerLed, maxBrightness);
renderLEDs();
}
void loop()
{
//check to see if any data is ready to be read form the UDP buffer
pollDMX();
//If the flag dmxData.has Changed is 1 data has been read in form the network to the dmxData object and data is ready
if(dmxData.hasChanged)
{
//reset the flag for th enext read
dmxData.hasChanged=0;
//Translate the data inside the dmxData object to the LEDs
artNetToSPI();
}
yield();
}
void pollDMX()
{
//check to see if any data is available via UDP
packetSize = udp.parsePacket();
//If a udb packet is ready to read, check that its the size of an ArtNet Packet 530Bytes
if(packetSize==maxPacketBufferSize)
{
//Read maxPacketBufferSize bytesfrom the UDP buffer to the packetBuffer array
udp.read(packetBuffer, maxPacketBufferSize);
//Check that the packets are in the universe we need
//byte inside packetBuffer[14] is the unvirse ID of this packet
if(packetBuffer[14]>=universeRange[0] && packetBuffer[14]<=universeRange[1])
{
//Store packetBuffer into the dmxData object for ease of use
dmxData.parseArtNetPacket(packetBuffer);
}
}
udp.flush();
}
void artNetToSPI()
{
//If the current packets Universe is 0 we have data for the left part of the panel
if(dmxData.universe[0]==0)
{
//set the start pixel for the panel to be pixel 0
pixelIndex = 0;
}
//If the current packets Universe is 1 we have data for the right part of the panel
else if(dmxData.universe[0]==1)
{
//set the start pixel for the panel to be pixel 98
pixelIndex = 98;
}
for(dmxCounter=0; dmxCounter<bytesPerFixture; dmxCounter+=bytesPerLed-1)
{
//copy the RGB values from dmxData.data[dmxCounter into the tempColour object
memcpy(&tempColour, &dmxData.data[dmxCounter], bytesPerLed-1);
//Set pixel pixelIndex to color tempColour
leds.setPixel(pixelIndex, tempColour);
//Increment ot next pixel
pixelIndex++;
}
//render the data to the LEDs
renderLEDs();
}
void renderLEDs()
{
//Write leds._frameLength number of bytes from the array leds.LEDs
SPI.writeBytes(leds.LEDs, leds._frameLength);
}