Skip to content

Instantly share code, notes, and snippets.

@cnf
Created May 5, 2023 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnf/36ff0779ba7639840cc90047da6f78ca to your computer and use it in GitHub Desktop.
Save cnf/36ff0779ba7639840cc90047da6f78ca to your computer and use it in GitHub Desktop.
esp dmx try
#include <Arduino.h>
#include <M5Unified.h>
#include <M5UnitLCD.h>
#include <esp_dmx.h>
// I tried Various combinations of transmit and enable pins here
int receivePin = GPIO_NUM_26;
int transmitPin = GPIO_NUM_36;
int enablePin = GPIO_NUM_0;
dmx_port_t dmxPort = DMX_NUM_1;
byte data[DMX_PACKET_SIZE];
bool dmxIsConnected = false;
unsigned long lastUpdate = millis();
esp_err_t dmxError;
int count = 0;
void setup() {
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
cfg.internal_mic = false;
M5.begin(cfg);
pinMode(enablePin, OUTPUT);
// Serial2.begin(250000, SERIAL_8N2, 26, 0);
// Serial2.setRxBufferSize(DMX_PACKET_SIZE);
dmx_set_pin(dmxPort, transmitPin, receivePin, enablePin);
dmxError = dmx_driver_install(dmxPort, DMX_DEFAULT_INTR_FLAGS);
M5.Lcd.setRotation(3);
M5.Lcd.setTextSize(2);
M5.Lcd.setTextColor(ORANGE);
M5.Lcd.setCursor(0, 0);
M5.Lcd.print("RS485 Demo");
M5.Lcd.setTextSize(1);
M5.Lcd.setCursor(10, 20);
M5.Lcd.print("Not Connected");
M5.Lcd.setCursor(10, 30);
M5.Lcd.print("AutoPrint Receive");
M5.Lcd.setCursor(10, 50);
}
void loop() {
M5.update();
dmx_packet_t packet;
// byte *packet[DMX_PACKET_SIZE];
int size = dmx_receive(dmxPort, &packet, DMX_TIMEOUT_TICK);
// Serial.printf("Error: %i\n", dmxError);
if (size > 0) {
Serial.print("+");
/* If this code gets called, it means we've received DMX data! */
/* Get the current time since boot in milliseconds so that we can find
out how long it has been since we last updated data and printed to the
Serial Monitor. */
unsigned long now = millis();
/* We should check to make sure that there weren't any DMX errors. */
if (!packet.err) {
/* If this is the first DMX data we've received, lets log it! */
if (!dmxIsConnected) {
Serial.println("DMX is connected!");
M5.Lcd.setTextSize(1);
M5.Lcd.setCursor(10, 20);
M5.Lcd.print("DMX is Connected");
dmxIsConnected = true;
}
/* Don't forget we need to actually read the DMX data into our buffer
so
that we can print it out. */
dmx_read(dmxPort, data, packet.size);
// channeldata = dmx_read_slot(dmxPort, 1);
if (now - lastUpdate > 1000) {
/* Print the received start code - it's usually 0. */
Serial.printf("Start code is 0x%02X and slot 1 is 0x%02X\n", data[0],
data[1]);
// Serial.println(channeldata);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(10, 50);
M5.Lcd.print(data[1]);
lastUpdate = now;
}
} else {
/* Oops! A DMX error occurred! Don't worry, this can happen when you
first
connect or disconnect your DMX devices. If you are consistently
getting DMX errors, then something may have gone wrong with your code
or something is seriously wrong with your DMX transmitter. */
Serial.println("A DMX error occurred.");
}
} else if (dmxIsConnected) {
/* If DMX times out after having been connected, it likely means that the
DMX cable was unplugged. When that happens in this example sketch,
we'll uninstall the DMX driver. */
Serial.println("DMX was disconnected.");
M5.Lcd.setTextSize(1);
M5.Lcd.setCursor(10, 20);
M5.Lcd.print("DMX was disconnected.");
dmx_driver_delete(dmxPort);
/* Stop the program. */
while (true) {
Serial.print("@");
yield();
}
} else {
Serial.print("-\n");
}
// // delay(500);
// while (Serial2.available()) {
// if (nl > 0) {
// Serial.print("\n\n\n+++++++++++++++++++++++++++++++++++++++++++++++++\n");
// Serial.println(count);
// Serial.print("\n+++++++++++++++++++++++++++++++++++++++++++++++++\n");
// nl = 0;
// }
// char ch = Serial2.read();
// // Serial2.readBytes(*packet, 513);
// // M5.Lcd.setTextSize(2);
// // M5.Lcd.print(packet[1]);
// // Serial.print("ch);
// if (ch > 0) {
// Serial.printf(" '%02X' ", ch);
// // Serial.printf("Start code is 0x%02X and slot 1 is 0x%02X\n",
// // packet[0],
// // packet[1]);
// }
// }
// Serial.println("------");
}
; 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
[platformio]
description = DMX Test
src_dir = src
default_envs = m5stick-c
extra_configs = platformio_override.ini // empty at the moment
[env:m5stick-c]
platform = espressif32
board = m5stick-c
framework = arduino
monitor_speed = 115200
lib_deps =
m5stack/M5Unified@^0.1.4
someweisguy/esp_dmx@^3.0.2-beta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment