sstaub / sACN

Send and receive sACN DMX packets following ANSI E1.31
MIT License
14 stars 1 forks source link

Arduino library for sending and receiving sACN DMX streams following ANSI E1.31-2018

This library send and receives sACN DMX streams following the ANSI E1.31-2018 standard.

!!! Attention !!! Version 1.1 changes

Limitations

Merging received data

Merging data from more than one source is not implemented. The goal of the E1.31 standard is to replace it with the priority concept.
Maybe priority per channel (ETC start code DD) merge will implemented in a later version.
In the moment, if there is more than one source with the same priority, the first source win the race all other sources are ignored.

Protocol limitations

Following parts of the ANSI E1.31 protocol are not supported yet:

There is also no support for RDM ANSI E1.20 and RDMNET ANSI E1.33

Ethernet library problems

While writing this library many problems occurs with different ethernet libraries.

void hardreset(uint8_t pinRST) {
  pinMode(pinRST, OUTPUT);
  digitalWrite(pinRST, HIGH);
  digitalWrite(pinRST, LOW);
  delay(1);
  digitalWrite(pinRST, HIGH);
  delay(150);
  }

Feedback from users will helpful.

Example

Here is a simple example for receiving. You find also a receive and sending examples in the dedicated folder. The examples are tested with an Arduino MEGA with Ethernet Shield 2. For work with other boards and libraries you need to modify them.

#include "Ethernet.h"
#include "sACN.h"

uint8_t mac[] = {0x90, 0xA2, 0xDA, 0x10, 0x14, 0x48}; // MAC Adress of your device
IPAddress ip(10, 101, 1, 201); // IP 
IPAddress dns(10, 101, 1, 100); // IP 
IPAddress gateway(10, 101, 1, 100); // IP 
IPAddress subnet(255, 255, 0, 0); // 

EthernetUDP sacn;
Receiver recv(sacn); // universe 1

void dmxReceived() {
  Serial.println("New DMX data received ");
  Serial.print("DMX Slot 1: ");
  Serial.print(recv.dmx(1));
  Serial.print(" DMX Slot 2: ");
  Serial.println(recv.dmx(2));
  }

void newSource() {
  Serial.print("new soure name: ");
  Serial.println(recv.name());
  }

void framerate() {
  Serial.print("Universe 1 DMX framerate ");
  Serial.println(recv.framerate());
  }

void timeOut() {
  Serial.println("Timeout!");
  }

void setup() {
  Serial.begin(9600);
  delay(2000);
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  recv.callbackDMX(dmxReceived);
  recv.callbackSource(newSource);
  recv.callbackTimeout(timeOut);
  recv.callbackFramerate(framerate);
  recv.begin(1);
  Serial.println("sACN start");
  }

void loop() {
  recv.update();
  }

Documentation

ID Tools

In IDTools.h you find some useful tools to generate an individual serial number (UUID/CID) and MAC address, also for formatted printing. The MAC address is only valid in local networks. All tools needs a random start number to generate, this can e.g. noise values from Analog inputs, RTC ...
The functions must call inside setup()

UUID/CID serial number Tools

void generateUUID(uint8_t uuid[], unsigned int srnd)
uint8_t* generateUUID(unsigned int srnd)

MAC address Tools

void generateMAC(uint8_t mac[], unsigned int srnd)
uint8_t* generateMAC(unsigned int srnd)

ID Tools for RaspberryPiPico

In IDToolsPico.h you find some useful tools to generate an individual serial number (UUID/CID) and MAC address, also for formatted printing. The MAC address is only valid in local networks. There is no random init necessary. The functions must call inside setup()

UUID/CID serial number Tools

void generateUUID(uint8_t uuid[])
uint8_t* generateUUID()

MAC address Tools

void generateMAC(uint8_t mac[])
uint8_t* generateMAC()

Receiver API

Constructor

Receiver(UDP& udp)

Create a Receiver object.

Example

EthernetUDP sacn1;
Receiver recv1(sacn1); // Universe 1, no Unicast

Methods

begin()

void begin(uint16_t universe, bool unicastMode = false)

Start the UDP connection of the receiver, this should happen in setup().

Example

recv1.begin(1);

stop()

void stop()

Stop UDP connection of the receiver.

Example

recv1.stop();

update()

bool update()

Proceed the sACN data of the UDP connection, return true if there is a valid sACN packet received. This must done inside loop().

Example

recv1.update();

dmx()

uint8_t* dmx()
void dmx(uint8_t *data)
uint8_t dmx(uint16_t slot);

Get the DMX data, you can get the whole universe or a single DMX slot.

Example

uint8_t buffer[512];
recv1.dmx(buffer); // copy the whole universe

uint8_t dmx1; 
dmx1 = recv1.dmx(1); // get data from slot 1

name()

char* name()
void name(char *sourceName)

Get the name of the active source.

Example

Serial.print("Source name: ");
Serial.println(recv.name());

framerate()

uint8_t framerate()

Get the frame rate in fps.

Example

uint8_t framerate = recv1. framerate();

sources()

bool sources()

Get the state of active sources, Return true if a source is active.

Example

bool state = recv1.sources();

callbackDMX()

void callbackDMX(fptr callDMX)

Set a callback when receiving only changed DMX data. This should configured in setup().

Example

// before setup()
void dmxReceived() {
  Serial.println("New DMX received ");
  }

// in setup()
recv1.callbackDMX(dmxReceived);

callbackSource()

void callbackSource(fptr callSource)

Set a callback when there is a new active Source. This should configured in setup().

callbackFramerate()

void callbackFramerate(fptr callFramerate)

Set a callback every second the current framerate. This should configured in setup().

Example

// before setup()
void framerate() {
  Serial.print("Universe framerate: ");
  Serial.println(recv1.framerate());
  }

// in setup()
recv1.callbackFramerate(framerate);

callbackTimeout()

void callbackTimeout(fptr callTimeout)

Set a callback when a timeout occurs. This happens after 2500 ms without receiving any valid sACN packet. This should configured in setup().

Example

// before setup()
void timeOut() {
  Serial.println("Timeout!");
  }

// in setup()
recv1.timeOut(timeOut);

Source API

Source helper functions

deviceCID

void deviceCID(uint8_t cid[16])

Set the device CID common for all sources. This must done in setup()) before begin(). You can generate the CID with the IDTools.

Example

// before setup
uint8_t id[16] {0xFD, 0x32, 0xAE, 0xDC, 0x7B, 0x94, 0x11, 0xE7, 0xBB, 0x31, 0xBE, 0x2E, 0x44, 0xB0, 0x6B, 0x34};

// in setup()
deviceCID(id);

deviceName

void deviceName(const char name[64])

Set the device name common for all sources. This must done in setup()) before begin().

Example

// in setup()
deviceName("Arduino");

Constructor

Source(UDP& udp)

Create a Source object.

Example

EthernetUDP sacn1;
Source send1(sacn1); // universe 1 with priority 100

Methods

begin()

void begin(uint16_t universe, uint16_t priority, bool priorityDD = false)
void begin(IPAddress unicastIp ,uint16_t universe, uint16_t priority, bool priorityDD = false)

Start the UDP connection of the source, this should happen in setup().

Example

send1.begin(1); // universe 1, default priority
send2.begin(4, 101, true); // universe 1, priority 101, with DD

stop()

void stop()

Stop UDP connection of the source.

Example

send1.stop();

dmx()

void dmx(uint8_t *data)
void dmx(uint16_t slot, uint8_t data)

Set DMX values.

Example

dmxBuffer[512];
dmxBuffer[0] = 128;
dmxBuffer[511] = 255;
send1.dmx(dmxBuffer);

dd()

void dd(uint8_t *priorityData)
void dd(uint16_t slot, uint8_t priorityData)

Set the priority per channel values.

Example

send1.dd(2, 0); // set the priority for DMX slot 2 to zero

send()

void send()

Send a sACN packet.

Example

send1.send();

idle()

void idle()

Send sACN packets regularly (each 800ms), this must done in loop(). Also avoid long delays inside loop() or use RTOS instead.

Example

loop() {
  send1.idle();
  }

sendDD()

void sendDD()

Send a sACN DD priority per channel packet.

Example

send1.sendDD();

idleDD()

void idleDD()

Send sACN DD priority per channel packets regularly (each 800ms), this must done in loop(). Also avoid long delays inside loop() or use RTOS instead.

Example

loop() {
  send1.idleDD();
  }