diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..691739c --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# Development + +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..e24696e --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": ["platformio.platformio-ide"], + "unwantedRecommendations": ["ms-vscode.cpptools-extension-pack"] +} diff --git a/README.md b/README.md deleted file mode 100644 index e750175..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# dmx-interface -Simple DIY DMX-Interface diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..07919a5 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,17 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:lolin_s2_mini] +platform = espressif32 +board = lolin_s2_mini +framework = arduino +lib_deps = + hideakitai/ArtNet @ ^0.8.0 + someweisguy/esp_dmx @ ^4.1.0 \ No newline at end of file diff --git a/src/ESPDMX.cpp b/src/ESPDMX.cpp new file mode 100644 index 0000000..efdc7a2 --- /dev/null +++ b/src/ESPDMX.cpp @@ -0,0 +1,97 @@ +// - - - - - +// ESPDMX - A Arduino library for sending and receiving DMX using the builtin serial hardware port. +// ESPDMX.cpp: Library implementation file +// +// Copyright (C) 2015 Rick +// This work is licensed under a GNU style license. +// +// Last change: Marcel Seerig +// +// Documentation and samples are available at https://github.com/Rickgg/ESP-Dmx +// - - - - - + +/* ----- LIBRARIES ----- */ +#include +#include "ESPDMX.h" + +#define DMXSPEED 250000 +#define DMXFORMAT SERIAL_8N2 +#define BREAKSPEED 83333 +#define BREAKFORMAT SERIAL_8N1 +#define SERIALPORT Serial0 +#define DMXCHANNELS 512 + +bool dmxStarted = false; +int sendPin = 18; +int receivePin = -1; + +// DMX value array and size. Entry 0 will hold startbyte, so we need 512+1 elements +uint8_t dmxDataStore[DMXCHANNELS + 1] = {}; + +// Set up the DMX-Protocol +void DMXESPSerial::init() +{ + SERIALPORT.begin(DMXSPEED, DMXFORMAT, receivePin, sendPin); + pinMode(sendPin, OUTPUT); + dmxStarted = true; +} + +// Function to read DMX data +uint8_t DMXESPSerial::read(int Channel) +{ + if (dmxStarted == false) + init(); + + if (Channel < 1) + Channel = 1; + if (Channel > DMXCHANNELS) + Channel = DMXCHANNELS; + return (dmxDataStore[Channel]); +} + +// Function to send DMX data +void DMXESPSerial::write(int Channel, uint8_t value) +{ + if (dmxStarted == false) + init(); + + if (Channel < 1) + Channel = 1; + if (Channel > DMXCHANNELS) + Channel = DMXCHANNELS; + if (value < 0) + value = 0; + if (value > 255) + value = 255; + + dmxDataStore[Channel] = value; +} + +void DMXESPSerial::end() +{ + SERIALPORT.end(); + dmxStarted = false; +} + +// Function to update the DMX bus +void DMXESPSerial::update() +{ + if (dmxStarted == false) + init(); + + // Send break + digitalWrite(sendPin, HIGH); + SERIALPORT.begin(BREAKSPEED, BREAKFORMAT, receivePin, sendPin); + 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.flush(); + delay(1); + SERIALPORT.end(); +} \ No newline at end of file diff --git a/src/ESPDMX.h b/src/ESPDMX.h new file mode 100644 index 0000000..8cdc522 --- /dev/null +++ b/src/ESPDMX.h @@ -0,0 +1,29 @@ +// - - - - - +// ESPDMX - A Arduino library for sending and receiving DMX using the builtin serial hardware port. +// ESPDMX.cpp: Library implementation file +// +// Copyright (C) 2015 Rick +// This work is licensed under a GNU style license. +// +// Last change: Marcel Seerig +// +// Documentation and samples are available at https://github.com/Rickgg/ESP-Dmx +// - - - - - + +#include + + +#ifndef ESPDMX_h +#define ESPDMX_h + +// ---- Methods ---- + +class DMXESPSerial { + public: + void init(); + uint8_t read(int Channel); + void write(int channel, uint8_t value);void update(); + void end(); +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..f703549 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,104 @@ +// Art-Net DMX Interface Demo +// 2024-10-17 Patrick Schwarz + +#include +// #include + +#include "ESPDMX.h" + +// WiFi stuff +const char *ssid = "artnet"; +const char *pwd = "mbgmbgmbg"; +const IPAddress ip(192, 168, 1, 201); +const IPAddress gateway(192, 168, 1, 1); +const IPAddress subnet(255, 255, 255, 0); + +// Art-Net stuff +ArtnetWiFi artnet; +// const String target_ip = "192.168.1.200"; +uint8_t universe = 1; // 0 - 15 +const uint16_t size = 512; +uint8_t data[size]; +uint8_t value = 0; + +// DMX stuff +DMXESPSerial dmx; + +void setup() +{ + + // Serial console + // Serial.begin(115200); + + // WiFi stuff + // WiFi.begin(ssid, pwd); + WiFi.softAP(ssid, pwd); + WiFi.softAPConfig(ip, gateway, subnet); + // WiFi.config(ip, gateway, subnet); + // while (WiFi.status() != WL_CONNECTED) { + // Serial.print("."); + delay(500); + //} + // Serial.print("WiFi connected, IP = "); + // Serial.println(WiFi.localIP()); + + // Initialize Art-Net + artnet.begin(); + + // Initialize DMX ports + dmx.init(); + + // 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) + { + /*Serial.print("lambda : artnet data from "); + Serial.print(remote.ip); + Serial.print(":"); + Serial.print(remote.port); + Serial.print(", universe = "); + Serial.print(universe); + Serial.print(", size = "); + Serial.print(size); + Serial.print(") :");*/ + + for (size_t i = 0; i < size; ++i) + { + dmx.write((i + 1), data[i]); + // Serial.print(data[i]); + // Serial.print(","); + } + // Serial.println(); + + dmx.update(); }); + + // 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) + { + /*Serial.print("received ArtNet data from "); + Serial.print(remote.ip); + Serial.print(":"); + Serial.print(remote.port); + Serial.print(", net = "); + Serial.print(metadata.net); + Serial.print(", subnet = "); + Serial.print(metadata.subnet); + Serial.print(", universe = "); + Serial.print(metadata.universe); + Serial.print(", sequence = "); + Serial.print(metadata.sequence); + Serial.print(", size = "); + Serial.print(size); + Serial.println(")");*/ }); +} + +void loop() +{ + artnet.parse(); // check if artnet packet has come and execute callback + + /*value = (millis() / 4) % 256; + memset(data, value, size); + + artnet.setArtDmxData(data, size); + artnet.streamArtDmxTo(target_ip, universe); // automatically send set data in 40fps + // artnet.streamArtDmxTo(target_ip, net, subnet, univ); // or you can set net, subnet, and universe */ +} \ No newline at end of file