Skip to content

Commit

Permalink
[WIP] Receive artnet over serial
Browse files Browse the repository at this point in the history
  • Loading branch information
niliha committed Jul 19, 2023
1 parent f632472 commit 5e88201
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/ArtnetOverSerial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once
10 changes: 10 additions & 0 deletions src/ArtnetOverSerial.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once



class ArtnetOverSerial {
public:

private:

}
80 changes: 74 additions & 6 deletions src/PixelDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ template <int PIN_COUNT, const std::array<int, PIN_COUNT> &PINS, EOrder RGB_ORDE
lightsPerPin[3] * PIXELS_PER_LIGHT_);
pixelOffset += lightsPerPin[3] * PIXELS_PER_LIGHT_;
}
FastLED.setCorrection(TypicalLEDStrip);
}

void setArtnetCallback() {
Expand All @@ -152,21 +153,21 @@ template <int PIN_COUNT, const std::array<int, PIN_COUNT> &PINS, EOrder RGB_ORDE
fastLedPixels_ = *frameQueue_.front();
// Pop pointer to oldest frame from the queue
frameQueue_.pop();

// Write pixel buffer to the LEDs
FastLED.show();
}
}

// Write pixel buffer to the LEDs
FastLED.show();

// Wait until a frame period has passed
auto passedMillis = millis() - millisBefore;
auto millisToWait = FRAME_PERIOD_MS_ - passedMillis;
if (millisToWait < 0) {
Serial.print("WARN: FPS to high! Would need to wait negative time: ");
Serial.println(millisToWait);
return;
continue;
}
delay(passedMillis);
// delay(passedMillis);
}
}

Expand All @@ -182,7 +183,9 @@ template <int PIN_COUNT, const std::array<int, PIN_COUNT> &PINS, EOrder RGB_ORDE

while (true) {
// This calls onDmxFrame() whenever a ArtDMX packet is received
auto result = artnet_.read();
artnet_.read();

parseArtnetPacketOverSerial();
}
}

Expand Down Expand Up @@ -246,4 +249,69 @@ template <int PIN_COUNT, const std::array<int, PIN_COUNT> &PINS, EOrder RGB_ORDE
}
(*artnetFrame_)[pixelIndex][rgbChannelIndex] = value;
}

enum class ArtnetOverSerialState { WAITING_FOR_START, PARSING };

void parseArtnetPacketOverSerial() {
const char* ARTNET_HEADER = "Art-Net";

static uint8_t buffer[1024];
static int currentBufferIndex = -1;
static auto currentState = ArtnetOverSerialState::WAITING_FOR_START;
static uint16_t maximumBufferIndex = UINT16_MAX;
static uint16_t dmxDataLength = 0;

// TODO: Move this somewhere else
Serial2.begin(460800);
Serial2.setRxBufferSize(1024);

int incomingByte = Serial2.read();
if (incomingByte < 0) {
// Nothing available to read. Try again next time
return;
}

switch (currentState) {
case ArtnetOverSerialState::WAITING_FOR_START:
// All bytes of the Artnet header have been received. Switch to parsing mode
if (currentBufferIndex == sizeof(ARTNET_HEADER) - 1) {
currentState = ArtnetOverSerialState::PARSING;
break;
}

// The next character of the Artnet header has been received.
if (incomingByte == ARTNET_HEADER[currentBufferIndex + 1]) {
buffer[++currentBufferIndex] = incomingByte;
} else {
// An invalid artnet header byte has been received. Start over from the beginning
currentBufferIndex = 0;
}
break;

case ArtnetOverSerialState::PARSING:
buffer[++currentBufferIndex] = incomingByte;

if (currentBufferIndex == 17) {
dmxDataLength = buffer[17] | buffer[16] << 8;
if (dmxDataLength < 2 || dmxDataLength > 512) {
Serial.printf("Received invalid dmxDataLength %d. Skipping this frame", dmxDataLength);
currentBufferIndex = -1;
currentState = ArtnetOverSerialState::WAITING_FOR_START;
break;
}
maximumBufferIndex = currentBufferIndex + dmxDataLength;
break;
}

if (currentBufferIndex == maximumBufferIndex) {
uint8_t sequence = buffer[12];
uint16_t universe = buffer[14] | buffer[15] << 8;
onDmxFrame(universe, dmxDataLength, sequence, buffer + 18);
currentBufferIndex = -1;
maximumBufferIndex = UINT16_MAX;
currentState = ArtnetOverSerialState::WAITING_FOR_START;
break;
}
}
}
};
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void setup() {
*/

// PixelDriver<MAX_PIN_COUNT, PINS, RGB_ORDER> pixelDriver(lightsPerPin, 144, 255, false);
PixelDriver<MAX_PIN_COUNT, PINS, RGB_ORDER> pixelDriver(lightsPerPin, 20, 3, 144, true);
PixelDriver<MAX_PIN_COUNT, PINS, RGB_ORDER> pixelDriver(lightsPerPin, 30, 3, 144, true);

pixelDriver.testLeds();
Network::initWifiAccessPoint(WifiCredentials::ssid, WifiCredentials::password);
Expand Down

0 comments on commit 5e88201

Please sign in to comment.