Find video and code here:
So... after thinking I was curious if it would be possible to run the same code on other microcontrollers? Is there anything particularly special about the Teensy other than that it is fast?
I pulled out a USB C ESP32 board from SparkFun and thought I would just upload the code and try? https://www.sparkfun.com/products/18018
I was able to get the LEDs to blink just fine by commenting out the "JUST_TEST_LEDS" section. Everything looks good.
Though, when I went for the rest of the code (which works great on the Teensy!) I was not able to pickup the board in MadMapper preferences. I double checked all I could and am still not able to make headway.
So that is what I come here to post and ask: what is going on?
Is it possible that this board just won't work because of some weird USB serial issue?
Maybe I am using the wrong pin?
Or perhaps the code in MadMapper is only good for a Teensy?
Pictures show the Teensy and the ESP32. For the code I only changed the PIN from 2 on the Teensy to 12 on the ESP32.
Thank you for any help!
I am running both MM 4.2.1 & 5.0.2
Mac OSX 11.5.1
Code below didn't paste very well so here is a link to the GitHub where you can view it easier.
https://github.com/CaseyJScalf/MadMappe ... Teensy.ino
Code: Select all
// Read data from MadMapper and write to LEDs... but for ESP32?
////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <SPI.h>
#include <FastLED.h>
#define MAD_LED_PACKET_HEADER 0xFF
#define MAD_LED_DATA 0xBE
#define MAD_LED_DETECTION 0xAE
#define MAD_LED_DETECTION_REPLY 0xEA
#define MAD_LED_PROTOCOL_VERSION 0x01
// Uncomment this line to see verbose debug prints in the Seril Monitor
// #define DEBUG_MODE
// Uncomment the line below test LEDs without the need of MadMapper or MadRouter sending data on serial port
// #define JUST_TEST_LEDS
// How many LEDs are in the strip?
#define NUM_LEDS 3
// What data pin to send this out of?
// Put a 330 Ohm resistor between pin and Data In for WS2812
#define DATA_PIN 12
// Fast LED Buffers
CRGB leds[NUM_LEDS];
// MadLED protocol buffer
char dataFrame[NUM_LEDS * 3];
int readingFrameOnLine = -1;
bool gotNewDataFrame = false;
enum State {
State_WaitingNextPacket,
State_GotPacketHeader,
State_WaitingLineNumber,
State_WaitingChannelCountByte1,
State_WaitingChannelCountByte2,
State_ReadingDmxFrame
};
State inputState = State_WaitingNextPacket;
unsigned int channelsLeftToRead = 0;
char* frameWritePtr = dataFrame;
////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(921600);
for (unsigned int i = 0; i < sizeof(dataFrame); i++) dataFrame[i] = 0;
// Create LED - type, what pin, color mode, object, number of LEDs
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
Serial.print("Setup done");
} // End Setup
////////////////////////////////////////////////////////////////////////////////////////////////////////
void processByte(unsigned char currentByte) {
#ifdef DEBUG_MODE
Serial.print("GOT BYTE: "); Serial.print(currentByte, HEX);
#endif
if (currentByte == MAD_LED_PACKET_HEADER) {
inputState = State_GotPacketHeader;
#ifdef DEBUG_MODE
Serial.print("GOT PH ");
#endif
} else if (inputState == State_WaitingNextPacket) {
// Just ignore this byte, we're not processing a packet at the moment
// Wait for next packet start (xFF)
} else if (inputState == State_GotPacketHeader) {
if (currentByte == MAD_LED_DETECTION) {
// Send back detection reply
Serial.write(MAD_LED_DETECTION_REPLY);
Serial.write(MAD_LED_PROTOCOL_VERSION);
inputState = State_WaitingNextPacket;
} else if (currentByte == MAD_LED_DATA) {
inputState = State_WaitingLineNumber;
#ifdef DEBUG_MODE
Serial.print("GOT LD ");
#endif
} else {
// Unknown packet start, reset
inputState = State_WaitingNextPacket;
}
} else if (inputState == State_WaitingLineNumber) {
if (currentByte > 0x7F) {
// Error, reset
inputState = State_WaitingNextPacket;
#ifdef DEBUG_MODE
Serial.print("ErrLineNum: "); Serial.print(currentByte);
#endif
} else {
readingFrameOnLine = currentByte;
inputState = State_WaitingChannelCountByte1;
#ifdef DEBUG_MODE
Serial.print("GOT LN ");
#endif
}
} else if (inputState == State_WaitingChannelCountByte1) {
if (currentByte > 0x7F) {
// Error, reset
inputState = State_WaitingNextPacket;
#ifdef DEBUG_MODE
Serial.print("ErrChCNT1: "); Serial.print(currentByte);
#endif
} else {
channelsLeftToRead = currentByte;
inputState = State_WaitingChannelCountByte2;
#ifdef DEBUG_MODE
Serial.print("GOT CHC1 ");
#endif
}
} else if (inputState == State_WaitingChannelCountByte2) {
if (currentByte > 0x7F) {
// Error, reset
inputState = State_WaitingNextPacket;
#ifdef DEBUG_MODE
Serial.print("ErrChCNT2: "); Serial.print(currentByte);
#endif
} else {
channelsLeftToRead += (int(currentByte) << 7);
if (channelsLeftToRead == 0) {
// Error, reset
inputState = State_WaitingNextPacket;
#ifdef DEBUG_MODE
Serial.print("ErrChCNT=0");
#endif
} else {
frameWritePtr = dataFrame;
inputState = State_ReadingDmxFrame;
#ifdef DEBUG_MODE
Serial.print("GOT CHC2 ");
#endif
}
}
} else if (inputState == State_ReadingDmxFrame) {
*frameWritePtr++ = currentByte;
channelsLeftToRead--;
if (channelsLeftToRead == 0) {
// Finished reading DMX Frame
inputState = State_WaitingNextPacket;
gotNewDataFrame = true;
#ifdef DEBUG_MODE
Serial.print("GOT DATA ");
#endif
}
}
} // End processByte()
////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
// If in this mode the entire strip will fade black to white
#ifdef JUST_TEST_LEDS
Serial.println("Currently testing...");
static int value = 254;
value = (value + 1) % 254;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(value, value, value);
}
#else
// We read a maximum of 30000 bytes before we should call FastLED.show again
// This is a good setting for the teensy, it depends on CPU speed, so it should be set lower on a slower CPU (ie arduino)
// This limit (bytesRead<30000) is useless for protocols with a clock
// But necessary when controlling more than 600 hundred RGB leds with WS2811 / WS2812
int bytesRead = 0;
while (Serial.available() > 0 && bytesRead < 30000) {
processByte(Serial.read());
bytesRead++;
}
if (gotNewDataFrame) {
gotNewDataFrame = false;
char* dataPtr = dataFrame;
// Copy the data frame we received in the correct FastLED buffer
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(dataPtr[0], dataPtr[1], dataPtr[2]);
dataPtr += 3;
}
}
#endif
// Actually show the LEDs
FastLED.show();
} // End Loop
////////////////////////////////////////////////////////////////////////////////////////////////////////