diff --git a/src/ESPDMX.cpp b/src/ESPDMX.cpp index efdc7a2..3ac7775 100644 --- a/src/ESPDMX.cpp +++ b/src/ESPDMX.cpp @@ -5,7 +5,7 @@ // Copyright (C) 2015 Rick // This work is licensed under a GNU style license. // -// Last change: Marcel Seerig +// Last change: Hendrik Rauh // // Documentation and samples are available at https://github.com/Rickgg/ESP-Dmx // - - - - - @@ -13,6 +13,9 @@ /* ----- LIBRARIES ----- */ #include #include "ESPDMX.h" +#include + +#define MAX_IDS 3 #define DMXSPEED 250000 #define DMXFORMAT SERIAL_8N2 @@ -21,41 +24,45 @@ #define SERIALPORT Serial0 #define DMXCHANNELS 512 -bool dmxStarted = false; -int sendPin = 18; -int receivePin = -1; +int sendPin[] = {}; +int recvPin[] = {}; // DMX value array and size. Entry 0 will hold startbyte, so we need 512+1 elements -uint8_t dmxDataStore[DMXCHANNELS + 1] = {}; +// std::vector dmxDataStores(MAX_IDS); +// uint8_t dmxDataStores[MAX_IDS][DMXCHANNELS + 1]; + +struct dmxPorts +{ + uint8_t id; + int sendPin; + int recvPin; + bool started; + uint8_t dmxDataStore[DMXCHANNELS + 1]; +}; // Set up the DMX-Protocol -void DMXESPSerial::init() +void DMXESPSerial::init(int id, int pinSend, int pinRecv) { - SERIALPORT.begin(DMXSPEED, DMXFORMAT, receivePin, sendPin); - pinMode(sendPin, OUTPUT); - dmxStarted = true; + sendPin[id] = pinSend; + recvPin[id] = pinRecv; + SERIALPORT.begin(DMXSPEED, DMXFORMAT, recvPin[id], sendPin[id]); + pinMode(sendPin[id], OUTPUT); } // Function to read DMX data -uint8_t DMXESPSerial::read(int Channel) +uint8_t DMXESPSerial::read(int id, int Channel) { - if (dmxStarted == false) - init(); - if (Channel < 1) Channel = 1; if (Channel > DMXCHANNELS) Channel = DMXCHANNELS; - return (dmxDataStore[Channel]); + return (dmxDataStores[id][Channel]); } // Function to send DMX data -void DMXESPSerial::write(int Channel, uint8_t value) +void DMXESPSerial::write(int id, int Channel, uint8_t value) { - if (dmxStarted == false) - init(); - - if (Channel < 1) + s if (Channel < 1) Channel = 1; if (Channel > DMXCHANNELS) Channel = DMXCHANNELS; @@ -64,33 +71,29 @@ void DMXESPSerial::write(int Channel, uint8_t value) if (value > 255) value = 255; - dmxDataStore[Channel] = value; + dmxDataStores[id][Channel] = value; } void DMXESPSerial::end() { SERIALPORT.end(); - dmxStarted = false; } // Function to update the DMX bus -void DMXESPSerial::update() +void DMXESPSerial::update(int id) { - if (dmxStarted == false) - init(); - // Send break - digitalWrite(sendPin, HIGH); - SERIALPORT.begin(BREAKSPEED, BREAKFORMAT, receivePin, sendPin); + digitalWrite(sendPin[id], HIGH); + SERIALPORT.begin(BREAKSPEED, BREAKFORMAT, recvPin[id], sendPin[id]); SERIALPORT.write(0); SERIALPORT.flush(); delay(1); SERIALPORT.end(); // send data - SERIALPORT.begin(DMXSPEED, DMXFORMAT, receivePin, sendPin); - digitalWrite(sendPin, LOW); - SERIALPORT.write(dmxDataStore, DMXCHANNELS); + SERIALPORT.begin(DMXSPEED, DMXFORMAT, recvPin[id], sendPin[id]); + digitalWrite(sendPin[id], LOW); + SERIALPORT.write(dmxDataStores[id], DMXCHANNELS); SERIALPORT.flush(); delay(1); SERIALPORT.end(); diff --git a/src/ESPDMX.h b/src/ESPDMX.h index 8cdc522..748a727 100644 --- a/src/ESPDMX.h +++ b/src/ESPDMX.h @@ -5,7 +5,7 @@ // Copyright (C) 2015 Rick // This work is licensed under a GNU style license. // -// Last change: Marcel Seerig +// Last change: Hendrik Rauh // // Documentation and samples are available at https://github.com/Rickgg/ESP-Dmx // - - - - - @@ -20,10 +20,11 @@ class DMXESPSerial { public: - void init(); - uint8_t read(int Channel); - void write(int channel, uint8_t value);void update(); + void init(int id, int pinSend, int pinRecv); + uint8_t read(int id, int Channel); + void write(int id, int channel, uint8_t value);void update(); void end(); + void update(int id); }; #endif diff --git a/src/main.cpp b/src/main.cpp index eec27b7..91faa1c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,9 @@ #include Preferences config; -DMXESPSerial dmx; + +DMXESPSerial dmx1; +DMXESPSerial dmx2; AsyncWebServer server(80); @@ -16,6 +18,8 @@ ArtnetWiFi artnet; const uint16_t size = 512; uint8_t data[size]; +int selectedId = 0; + void setup() { Serial.begin(9600); @@ -51,17 +55,17 @@ void setup() artnet.begin(); // Initialize DMX ports - dmx.init(); + dmx.init(0, 19, -1); // if Artnet packet comes to this universe, this function is called artnet.subscribeArtDmxUniverse(universe, [&](const uint8_t *data, uint16_t size, const ArtDmxMetadata &metadata, const ArtNetRemoteInfo &remote) { for (size_t i = 0; i < size; ++i) { - dmx.write((i + 1), data[i]); + dmx.write(selectedId, (i + 1), data[i]); } - dmx.update(); }); + dmx.update(selectedId); }); // if Artnet packet comes, this function is called to every universe artnet.subscribeArtDmx([&](const uint8_t *data, uint16_t size, const ArtDmxMetadata &metadata, const ArtNetRemoteInfo &remote) {});