diff --git a/ESP32LedBars/.clang-format b/ESP32LedBars/.clang-format index 1bdff23..ac1699d 100644 --- a/ESP32LedBars/.clang-format +++ b/ESP32LedBars/.clang-format @@ -54,7 +54,7 @@ BreakConstructorInitializersBeforeComma: false BreakConstructorInitializers: BeforeColon BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true -ColumnLimit: 120 +ColumnLimit: 100 CommentPragmas: '^ IWYU pragma:' CompactNamespaces: false ConstructorInitializerAllOnOneLineOrOnePerLine: false diff --git a/ESP32LedBars/platformio.ini b/ESP32LedBars/platformio.ini index 460ad12..2364c26 100644 --- a/ESP32LedBars/platformio.ini +++ b/ESP32LedBars/platformio.ini @@ -16,7 +16,7 @@ monitor_speed = 115200 build_flags = -Wall lib_deps = fastled/FastLED - ; ArtnetWifi + ArtnetWifi ; tzapu/WiFiManager#master [env:nodemcu-32s] diff --git a/ESP32LedBars/src/main.cpp b/ESP32LedBars/src/main.cpp index 344f589..ba6f0e1 100644 --- a/ESP32LedBars/src/main.cpp +++ b/ESP32LedBars/src/main.cpp @@ -1,8 +1,12 @@ #include // #include -// #include +#include #include -#include +#include +// #include + +/* Types and Constants */ +/*------------------------------------------------------------------------------------------------*/ #define LED_TYPE WS2812B #define COLOR_ORDER GRB @@ -20,120 +24,103 @@ ledBar_t ledBarArray[MAX_BAR_NUMBER] = {{.numLeds = 15, .pin = 22}, {.numLeds = #define FOREACH_BARS(FUNC) FUNC(22) FUNC(23) // LED settings -const int numLeds{8 * 30}; +const int numLeds{165}; // Total number of channels you want to receive (1 led = 3 channels) CRGB leds[numLeds]; CRGB leds2[15]; CRGB leds3[10]; // Art-Net settings -// ArtnetWifi artnet; - -// most software this is 1, some software send out artnet first universe as 0. -const int startUniverse = 0; +ArtnetWifi artnet; +/* Global variables */ +/*------------------------------------------------------------------------------------------------*/ int count = 0; +/* Private function declarations */ +/*------------------------------------------------------------------------------------------------*/ +void init_wifi(); void pride(); void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t *data); // void setupOTA(); void initTest(CRGB *leds, int numLeds); -WiFiManager wifiManager; - +/* Pulic function definitions */ +/*------------------------------------------------------------------------------------------------*/ void setup() { delay(3000); // 3 second delay for recovery + // initialize serial Serial.begin(115200); Serial.println("Init"); -#define ADD_LEDS_BARS(PIN) \ - FastLED.addLeds(leds, 60).setCorrection(TypicalLEDStrip).setDither(BRIGHTNESS < 255); + // initialize wifi + init_wifi(); + + // #define ADD_LEDS_BARS(PIN) \ +// FastLED.addLeds(leds, 60) \ +// .setCorrection(TypicalLEDStrip) \ +// .setDither(BRIGHTNESS < 255); // FOREACH_BARS(ADD_LEDS_BARS) - FastLED.addLeds(leds, numLeds).setCorrection(TypicalLEDStrip).setDither(0); + FastLED.addLeds(leds, numLeds) + .setCorrection(TypicalLEDStrip) + .setDither(0); // set master brightness control FastLED.setBrightness(BRIGHTNESS); initTest(leds, numLeds); - IPAddress _ip = IPAddress(192, 168, 1, 78); - IPAddress _gw = IPAddress(192, 168, 1, 1); - IPAddress _sn = IPAddress(255, 255, 255, 0); - // end-block2 - - wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn); - if (!wifiManager.autoConnect()) { - Serial.println("failed to connect, we should reset as see if it connects"); - delay(3000); - ESP.restart(); - delay(5000); - } - // setupOTA(); // this will be called for each packet received - // artnet.begin(); - // artnet.setArtDmxCallback(onDmxFrame); + artnet.begin(); + artnet.setArtDmxCallback(onDmxFrame); } void loop() { // ArduinoOTA.handle(); - // artnet.read(); + artnet.read(); - static uint16_t sLastMillis = 0; + static uint16_t sLastMillis = millis(); uint16_t ms = millis(); uint16_t deltams = ms - sLastMillis; - if (deltams >= 1000) { + if (deltams >= 5000) { Serial.print("FPS : "); - Serial.println(count); + Serial.println(count / 5); count = 0; sLastMillis = ms; } ++count; - pride(); + FastLED.show(); } -// Check if we got all universes -const int numberOfChannels = (numLeds)*3; -const int maxUniverses = numberOfChannels / 512 + ((numberOfChannels % 512) ? 1 : 0); -bool universesReceived[maxUniverses]; -bool sendFrame = 1; -int previousDataLength = 0; +/* Private function definitions */ +/*------------------------------------------------------------------------------------------------*/ -void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t *data) { - Serial.println("Frame received"); - sendFrame = 1; - - // Store which universe has got in - if ((universe - startUniverse) < maxUniverses) { - universesReceived[universe - startUniverse] = 1; +/* Initialize wifi connection */ +void init_wifi() { + IPAddress localIp = IPAddress(192, 168, 1, 78); + IPAddress gateway = IPAddress(192, 168, 1, 1); + IPAddress subnet = IPAddress(255, 255, 255, 0); + // end-block2 + if (!WiFi.config(localIp, gateway, subnet)) { + Serial.println("STA Failed to configure"); } + WiFi.begin("BertheBox", "3615BertheBerthelot"); - for (int i = 0; i < maxUniverses; i++) { - if (universesReceived[i] == 0) { - // Serial.println("Broke"); - sendFrame = 0; - break; - } + Serial.print("Connecting"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); } + Serial.println(); - // read universe and put into the right part of the display buffer - for (int i = 0; i < length / 3; i++) { - int led = i * (previousDataLength / 3); - if (led < numLeds) - leds[led] = CRGB(data[i * 3], data[i * 3 + 1], data[i * 3 + 2]); - } - previousDataLength = length; - - if (sendFrame) { - FastLED.show(); - // Reset universeReceived to 0 - memset(universesReceived, 0, maxUniverses); - } + Serial.print("Connected, IP address: "); + Serial.println(WiFi.localIP()); } // void setupOTA() { @@ -144,7 +131,7 @@ void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t *d // type = "sketch"; // else // U_SPIFFS // type = "filesystem"; - +// // // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() // Serial.println("Start updating " + type); // }) @@ -165,14 +152,63 @@ void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t *d // else if (error == OTA_END_ERROR) // Serial.println("End Failed"); // }); - +// // ArduinoOTA.begin(); - +// // Serial.println("Ready"); // Serial.print("IP address: "); // Serial.println(WiFi.localIP()); // } +/* initialize led bars */ +void setup_ledbars() {} + +const int numberOfChannels = numLeds * 3; +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 maxUniverses = numberOfChannels / 512 + ((numberOfChannels % 512) ? 1 : 0); +bool universesReceived[maxUniverses]; +bool sendFrame = 1; +int previousDataLength = 0; + +void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t *data) { + sendFrame = 1; + // set brightness of the whole strip + if (universe == 15) { + FastLED.setBrightness(data[0]); + FastLED.show(); + } + + // Store which universe has got in + if ((universe - startUniverse) < maxUniverses) { + universesReceived[universe - startUniverse] = 1; + } + + for (int i = 0; i < maxUniverses; i++) { + if (universesReceived[i] == 0) { + // Serial.println("Broke"); + sendFrame = 0; + break; + } + } + + // read universe and put into the right part of the display buffer + for (int i = 0; i < length / 3; i++) { + int led = i + (universe - startUniverse) * (previousDataLength / 3); + if (led < numLeds) + leds[led] = CRGB(data[i * 3], data[i * 3 + 1], data[i * 3 + 2]); + } + previousDataLength = length; + + if (sendFrame) { + FastLED.show(); + // Reset universeReceived to 0 + memset(universesReceived, 0, maxUniverses); + } +} + void initTest(CRGB *leds, int numLeds) { Serial.println("Green"); for (int i = 0; i < numLeds; i++) {