Skip to content

Instantly share code, notes, and snippets.

@natcl
Created May 1, 2014 18:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natcl/312769bf51ebce3e2db8 to your computer and use it in GitHub Desktop.
Save natcl/312769bf51ebce3e2db8 to your computer and use it in GitHub Desktop.
// libraries
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <Adafruit_NeoPixel.h>
#include <mac.h>
//-------------------- for NeoPixel--------------------//
const int ledsPerStrip = 492;//CHANGE FOR YOUR SETUP
Adafruit_NeoPixel leds = Adafruit_NeoPixel(ledsPerStrip, 2, NEO_GRB + NEO_KHZ800);
byte ip[] = {192, 168, 2, 2}; // the IP adress of your device, that should be in same universe of the network you are using
unsigned int localPort = 6454; // DO NOT CHANGE artnet UDP port is by default 6454
//---------------artnet and buffers------------//
const int startUniverse = 0; // CHANGE FOR YOUR SETUP most software this is 1, some software send out artnet first universe as zero.
const int numberOfChannels= ledsPerStrip * 3; // Total number of channels you want to receive (1 led = 3 channels)
const int ArtNetHeaderSize = 18; // Do not change
byte channelBuffer[numberOfChannels]; // buffer to store filetered DMX data//SHOULD BE SAME AS numberOfChannels
// Check if we got all universes
const int maxUniverses = ceil(numberOfChannels / 512.);
bool universesReceived[maxUniverses];
bool sendFrame = 1;
const int MAX_BUFFER_UDP = 530; // leave as is
char packetBuffer[MAX_BUFFER_UDP]; //buffer to store incoming data
EthernetUDP Udp;
// Art-Net packet parsing
char packetID[8]; // ID of the packet, should be "Art-Net" else ignore
byte sequence;
int incomingUniverse;
int dmxDataLength;
int packetSize;
elapsedMillis timer;
void setup()
{
read_mac();
Serial.begin(115200);
//setup ethernet and udp socket
Ethernet.begin(mac,ip);
Udp.begin(localPort);
delay(250);
Serial.println("end of setup");
Serial.println("waiting for incoming Artnet to start");
//if nothing appears after this, then the loop is not receiving your
//artnet package
leds.begin();
showInit(); //show an led array to confirm
//for (int i = 0 ; i < maxUniverses ; i++)
//{
//universesReceived[i] = 0;
//}
}
void loop()
{
sendFrame = 1;
packetSize = Udp.parsePacket();
if (packetSize)
{
Udp.read(packetBuffer, MAX_BUFFER_UDP);
//-------read incoming universe and sequence number and check for data series-------------//
for (int i = 0 ; i < 8 ; i++)
packetID[i] = packetBuffer[i];
sequence = packetBuffer[12];
incomingUniverse = packetBuffer[14] | packetBuffer[15] << 8;
dmxDataLength = packetBuffer[17] | packetBuffer[16] << 8;
//-----set brightness of the whole strip
if (incomingUniverse == 15)
{
leds.setBrightness(packetBuffer[ArtNetHeaderSize + 0]);
}
// Store which universe has got in
if (incomingUniverse < maxUniverses)
universesReceived[incomingUniverse] = 1;
for (int i = 0 ; i < maxUniverses ; i++)
{
if (universesReceived[i] == 0)
{
//Serial.println("Broke");
sendFrame = 0;
break;
}
}
//Serial.println(gotUni1);
//Serial.println(gotUni2);
//Serial.println(gotUni3);
//-----this section can be commented out if not debugging
//printPacketInfo();
//------read one universe and put into the right part of the display buffer----//
for (int i = 0 ; i < dmxDataLength ; i++)
{
int bufferIndex = i + ((incomingUniverse - startUniverse) * dmxDataLength);
if (bufferIndex < numberOfChannels) // to verify
channelBuffer[bufferIndex] = byte(packetBuffer[ArtNetHeaderSize + i]);
}
//------send to leds----//
for (int i = 0; i < ledsPerStrip; i++)
{
leds.setPixelColor(i, channelBuffer[(i) * 3], channelBuffer[(i * 3) + 1], channelBuffer[(i * 3) + 2]);
}
if (sendFrame)
{
leds.show();
// Reset universeReceived to 0
memset(universesReceived, 0, maxUniverses);
Serial.println(timer);
timer = 0;
}
}
}
void printPacketInfo()
{
Serial.print("Received packet of size ");//all print can be removed if not debugging
Serial.println(packetSize);
Serial.print("universe number = ");
Serial.print(incomingUniverse);
Serial.print(" ");
Serial.print("data length = ");
Serial.print(dmxDataLength);
Serial.print(" ");
Serial.print("sequence n0. = ");
Serial.println(sequence);
//for (int i = 0 ; i < numberOfChannels ; i++){
//Serial.print(channelBuffer[i], DEC);
//Serial.print(" ");
//}
}
// --------------------------------------------
//---create initial image---//
// --------------------------------------------
void showInit()
{
//-----set all black----//
for (int i=0; i < ledsPerStrip; i++)
leds.setPixelColor(i, 0,0,0);
leds.show();
delay(100);
//-----set display area size to blue-----//
for (int i=0; i < ledsPerStrip; i++)
leds.setPixelColor(i, 0,0,50);
leds.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment