Last active
July 9, 2019 16:40
-
-
Save hpwit/95769fcd4ab19dc00d7d6090dbca316f to your computer and use it in GitHub Desktop.
arnetexmaple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This example will receive multiple universes via Art-Net and control a strip of | |
WS2812 LEDs via the FastLED library: https://github.com/FastLED/FastLED | |
This example may be copied under the terms of the MIT license, see the LICENSE file for details | |
*/ | |
#if defined(ARDUINO_ARCH_ESP32) | |
#include <WiFi.h> | |
#else | |
#include <ESP8266WiFi.h> | |
#endif | |
#include <WiFiUdp.h> | |
#include <ArtnetWifi.h> | |
#define FASTLED_ALLOW_INTERRUPTS 0 | |
#define FASTLED_ESP32_I2S //addition suggestion to use audio processor for output | |
#include <FastLED.h> | |
#define FASTLED_SHOW_CORE 0 | |
// Wifi settings | |
const char* ssid = "xxxx; | |
const char* password = "xxx"; | |
// LED settings | |
const int numLeds = 60; // CHANGE FOR YOUR SETUP | |
const int numberOfChannels = numLeds * 3; // Total number of channels you want to receive (1 led = 3 channels) | |
const byte dataPin = 27; | |
CRGB leds[numLeds]; | |
// Art-Net settings | |
ArtnetWifi artnet; | |
const int startUniverse = 0; // CHANGE FOR YOUR SETUP most software this is 1, some software send out artnet first universe as 0. | |
// Check if we got all universes | |
const int channelPerUniverse =180; | |
const int maxUniverses = numberOfChannels / channelPerUniverse + ((numberOfChannels % channelPerUniverse) ? 1 : 0); | |
bool universesReceived[maxUniverses]; | |
const uint16_t syncmax=(1<< maxUniverses)-1; | |
uint16_t sync=0; | |
bool sendFrame = 1; | |
int previousDataLength = 0; | |
static TaskHandle_t FastLEDshowTaskHandle2 = 0; | |
static TaskHandle_t userTaskHandle = 0; | |
void FastLEDshowESP322() | |
{ | |
if (userTaskHandle == 0) { | |
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 200 ); | |
// -- Store the handle of the current task, so that the show task can | |
// notify it when it's done | |
// noInterrupts(); | |
userTaskHandle = xTaskGetCurrentTaskHandle(); | |
// -- Trigger the show task | |
xTaskNotifyGive(FastLEDshowTaskHandle2); | |
//to thge contrary to the other one we do not wait for the display task to come back | |
} | |
} | |
void FastLEDshowTask2(void *pvParameters) | |
{ | |
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 500 ); | |
// -- Run forever... | |
for(;;) { | |
// -- Wait for the trigger | |
ulTaskNotifyTake(pdTRUE,portMAX_DELAY); | |
// memcpy(leds,Tpic,LED_WIDTH*LED_HEIGHT*sizeof(CRGB)); | |
// memcpy(leds,artnet.getframe(),NUM_LEDS*sizeof(CRGB)); | |
FastLED.show(); | |
// controller.showPixels(); | |
userTaskHandle=0; //so we can't have two display tasks at the same time | |
} | |
} | |
// connect to wifi – returns true if successful or false if not | |
boolean ConnectWifi(void) | |
{ | |
boolean state = true; | |
int i = 0; | |
WiFi.begin(ssid, password); | |
Serial.println(""); | |
Serial.println("Connecting to WiFi"); | |
// Wait for connection | |
Serial.print("Connecting"); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
if (i > 20){ | |
state = false; | |
break; | |
} | |
i++; | |
} | |
if (state){ | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} else { | |
Serial.println(""); | |
Serial.println("Connection failed."); | |
} | |
return state; | |
} | |
void initTest() | |
{ | |
for (int i = 0 ; i < numLeds ; i++) { | |
leds[i] = CRGB(127, 0, 0); | |
} | |
FastLED.show(); | |
delay(500); | |
for (int i = 0 ; i < numLeds ; i++) { | |
leds[i] = CRGB(0, 127, 0); | |
} | |
FastLED.show(); | |
delay(500); | |
for (int i = 0 ; i < numLeds ; i++) { | |
leds[i] = CRGB(0, 0, 127); | |
} | |
FastLED.show(); | |
delay(500); | |
for (int i = 0 ; i < numLeds ; i++) { | |
leds[i] = CRGB(0, 0, 0); | |
} | |
FastLED.show(); | |
} | |
void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data) | |
{ | |
//Serial.printf("universe: %d\n", universe); | |
//sendFrame = 1; | |
// set brightness of the whole strip | |
if (universe == 15) | |
{ | |
FastLED.setBrightness(data[0]); | |
//FastLED.show(); | |
FastLEDshowESP322(); | |
} | |
// Store which universe has got in | |
if ((universe - startUniverse) < maxUniverses) { | |
//universesReceived[universe - startUniverse] = 1; | |
sync=sync | (1<< (universe - startUniverse)); | |
} | |
else | |
return; | |
if(universe - startUniverse==0) | |
sync=1; | |
int length_file=0; | |
if(length+(universe - startUniverse) * channelPerUniverse >= numLeds*3 ) | |
length_file=numLeds*3-(universe - startUniverse) * channelPerUniverse; | |
else | |
length_file=length; | |
memcpy(&leds[(universe - startUniverse) * (channelPerUniverse/3)],data, length_file); //replacement suggestion by Yves | |
previousDataLength = length; | |
if(sync==syncmax) | |
{ | |
FastLEDshowESP322(); | |
sync=0; | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
ConnectWifi(); | |
artnet.begin(); | |
xTaskCreatePinnedToCore(FastLEDshowTask2, "FastLEDshowTask2", 1000, NULL,3, &FastLEDshowTaskHandle2, FASTLED_SHOW_CORE); | |
FastLED.addLeds<WS2812B, dataPin, GRB>(leds, numLeds); | |
initTest(); | |
// this will be called for each packet received | |
artnet.setArtDmxCallback(onDmxFrame); | |
} | |
void loop() | |
{ | |
// we call the read function inside the loop | |
artnet.read(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment