From b67bd2c198ac60e081f6379815cdc1b47edd3e27 Mon Sep 17 00:00:00 2001 From: Cole Deck Date: Tue, 30 Jan 2024 12:15:32 -0600 Subject: [PATCH] Seems to be working exactly like this --- platformio.ini | 4 +-- src/config.h | 2 +- src/e131.cpp | 21 +++++------ src/e131.h | 4 +-- src/main.cpp | 98 ++++++++++++++++++++++++++++++++++++++++---------- 5 files changed, 95 insertions(+), 34 deletions(-) diff --git a/platformio.ini b/platformio.ini index c1d7ec3..537df9a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,11 +13,11 @@ platform = https://github.com/maxgerhardt/platform-raspberrypi.git board = pico framework = arduino platform_packages = - framework-arduinopico@https://github.com/earlephilhower/arduino-pico.git#master + framework-arduinopico@https://github.com/earlephilhower/arduino-pico.git#c64a4a58d793ee6d24c7df7ff3ec35adac3bbb45 board_build.core = earlephilhower upload_port = /run/media/amelia/RPI-RP2/ debug_tool = cmsis-dap -;upload_protocol = cmsis-dap +upload_protocol = cmsis-dap monitor_speed = 115200 board_build.filesystem_size = 1m board_build.f_cpu = 133000000L diff --git a/src/config.h b/src/config.h index db8b1f2..ab10218 100644 --- a/src/config.h +++ b/src/config.h @@ -46,7 +46,7 @@ #define INT_ETHERNET //#define INT_WIFI -#define ETH_SPI_SPD 10000000 +#define ETH_SPI_SPD 64000000 // network and universe settings diff --git a/src/e131.cpp b/src/e131.cpp index 1765522..06086d4 100644 --- a/src/e131.cpp +++ b/src/e131.cpp @@ -32,7 +32,7 @@ const PROGMEM byte E131::ACN_ID[12] = { 0x41, 0x53, 0x43, 0x2d, 0x45, 0x31, 0x2e const byte E131::ACN_ID[12] = { 0x41, 0x53, 0x43, 0x2d, 0x45, 0x31, 0x2e, 0x31, 0x37, 0x00, 0x00, 0x00 }; #endif -Wiznet5500lwIP eth(17, SPI, 21); // 17 : cs, 21 : INTn + @@ -125,12 +125,7 @@ int E131::initWiFi(const char *ssid, const char *passphrase) { return retval; } -void E131::begin(e131_listen_t type, uint16_t universe, uint8_t n) { - if (type == E131_UNICAST) - initUnicast(); - if (type == E131_MULTICAST) - initMulticast(universe, n); -} + int E131::begin(const char *ssid, const char *passphrase) { if (initWiFi(ssid, passphrase)) { @@ -197,12 +192,12 @@ int E131::beginMulticast(const char *ssid, const char *passphrase, #if defined (INT_ETHERNET) /* Unicast Ethernet Initializers */ -int E131::begin() { +int E131::begin(Wiznet5500lwIP eth) { int retval = 0; if(ETH_MODE == "staticip") { Serial.println("Setting static IP"); - eth.config(IP_ADDR, INADDR_NONE); + eth.config(IP_ADDR, INADDR_NONE); } else { Serial.println(F("Requesting Address via DHCP")); @@ -215,7 +210,7 @@ int E131::begin() { SPI.setSCK(18); SPI.setTX(19); eth.setSPISpeed(ETH_SPI_SPD); - lwipPollingPeriod(3); + lwipPollingPeriod(1); //char * hostname_char; //HOSTNAME.toCharArray(hostname_char, 32); @@ -248,6 +243,12 @@ int E131::begin() { return retval; } +void E131::begin(e131_listen_t type, uint16_t universe, uint8_t n) { + if (type == E131_UNICAST) + initUnicast(); + if (type == E131_MULTICAST) + initMulticast(universe, n); +} /* Multicast Ethernet Initializers */ int E131::beginMulticast(uint8_t *mac, uint16_t universe, uint8_t n) { diff --git a/src/e131.h b/src/e131.h index 52eb73b..f45f414 100644 --- a/src/e131.h +++ b/src/e131.h @@ -42,7 +42,7 @@ //# define INT_ETHERNET -//#define NO_DOUBLE_BUFFER 1 +#define NO_DOUBLE_BUFFER 1 /* Defaults */ #define E131_DEFAULT_PORT 5568 #define WIFI_CONNECT_TIMEOUT 15000 /* 15 seconds */ @@ -187,7 +187,7 @@ class E131 { /****** START - Ethernet ifdef block ******/ #if defined (INT_ETHERNET) /* Unicast Ethernet Initializers */ - int begin(); + int begin(Wiznet5500lwIP eth); /* Multicast Ethernet Initializers */ int beginMulticast(uint8_t *mac, uint16_t universe, uint8_t n = 1); diff --git a/src/main.cpp b/src/main.cpp index 09e86b7..eaff408 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,8 @@ #include #include "config.h" #include +#include +#include int strips[LED_STRIPS] = {170, 170, 170, 170, 170, 170, 170, 170}; // Begin code @@ -27,6 +29,7 @@ int universes[LED_STRIPS * 4]; CRGB ledstrip[MAX_LEDS]; int pins[8]; +uint8_t * livedata; // Networking WebServer httpServer(80); @@ -40,8 +43,10 @@ String clientbuffer = ""; String initinfo = ""; bool debug = 1; bool printer = 1; +int channels = 0; // Colors (RGB) int bootsel_count = 0; +int nopackets = 0; const uint8_t RED[PIXEL_SIZE]= {0x20, 0x00, 0x00}; const uint8_t ORANGE[PIXEL_SIZE]= {0x20, 0x10, 0x00}; @@ -55,8 +60,10 @@ const uint8_t WHITE[PIXEL_SIZE]= {0x20, 0x20, 0x20}; #define MAX_PIXELS_PER_UNIVERSE 512 / PIXEL_SIZE /* Number of pixels */ #define CHANNEL_START 1 /* Channel to start listening at */ +Wiznet5500lwIP eth(17, SPI, 21); //, 21); // 17 : cs, 21 : INTn E131 e131; + template T print(T in) { Serial.print(String(in)); if(printer) clientbuffer += String(in); @@ -212,10 +219,10 @@ void write_universe(long universe, uint8_t data[], long size) { PRINTFUNC("Universes size: "); PRINTLNFUNC(sizeof(universes));*/ int write_size = universes[universe]; - PRINTFUNC("Length: "); + /*PRINTFUNC("Length: "); PRINTLNFUNC(write_size * PIXEL_SIZE + (CHANNEL_START - 1) + 2); PRINTFUNC("Data: "); - PRINTLNFUNC(size); + PRINTLNFUNC(size);*/ if (write_size * PIXEL_SIZE + (CHANNEL_START - 1) + 2 > size) { println("Write size too big!!"); return; @@ -247,12 +254,16 @@ void write_universe(long universe, uint8_t data[], long size) { } void setup() { + vreg_voltage v = VREG_VOLTAGE_1_20; + vreg_set_voltage(v); + set_sys_clock_khz(252000, false); Serial.begin(115200); - //rp2040.wdt_begin(8000); + rp2040.wdt_begin(8000); pinMode(24, INPUT); // VBUS detect - check for USB connection if (digitalRead(24)) { delay(3000); // Wait for serial } + pinMode(21, INPUT); println("\r\nStarting RGB Controller..."); pinMode(20, OUTPUT); println("Resetting W5500 Ethernet Driver..."); @@ -312,10 +323,46 @@ void setup() { rp2040.wdt_reset(); println("Configuration loaded."); - if (!e131.begin()) { + + if(ETH_MODE == "staticip") { + Serial.println("Setting static IP"); + eth.config(IP_ADDR, INADDR_NONE); + } + else { + Serial.println(F("Requesting Address via DHCP")); + } + SPI.setRX(16); + SPI.setCS(17); + SPI.setSCK(18); + SPI.setTX(19); + eth.setSPISpeed(ETH_SPI_SPD); + lwipPollingPeriod(1); + + //char * hostname_char; + //HOSTNAME.toCharArray(hostname_char, 32); + eth.setHostname(HOSTNAME); + + if (!eth.begin()) { + Serial.println("No wired Ethernet hardware detected. Check pinouts, wiring."); + println("Connection failed. Retrying."); + rp2040.reboot(); + } + int count = 0; + while (!eth.connected() && count < 32) { + rp2040.wdt_reset(); + count++; + Serial.print("."); + delay(250); + } + if (!eth.connected()) { println("Connection failed. Retrying."); rp2040.reboot(); } + Serial.print(F("\r\n- IP Address: ")); + Serial.println(eth.localIP()); + + + e131.begin(E131_UNICAST); #ifdef INT_WIFI WiFi.noLowPowerMode(); #endif @@ -526,25 +573,33 @@ void setup1() { void loop() { /* Parse a packet */ - println("Start loop"); - int channels = 0; + //println("Start loop"); + if(channels = e131.parsePacket()) { // Offset by start universe // as all local functions count from 0 - status = 0; - if(e131.universe > START_UNIVERSE - 1) { - while (status2 == 1) { - delay(0); - } - write_universe(e131.universe - START_UNIVERSE, e131.data, channels); - println("Done Writing"); - } + //delay(0); + livedata = e131.data; status = 1; + delayMicroseconds(5000); + //Serial.print(eth.isLinked()); + nopackets = 0; + } + else { + nopackets++; + } + if(nopackets > 50000) { + nopackets = 0; + println("Resetting network"); + eth.end(); + delay(5); + eth.begin(); + println("Reset network"); } - println("mid loop"); + //println("mid loop"); httpServer.handleClient(); MDNS.update(); - println("end loop"); + //println("end loop"); if(newconfig == true) { println("Waiting for core 1 to idle..."); ready = 3; @@ -585,11 +640,16 @@ void loop1() { while(ready == 4) delay(50); } - if(status == 1) { - status2 = 1; + status2 = 1; + if(status == 1 && e131.universe > START_UNIVERSE - 1 && channels > 0) { + write_universe(e131.universe - START_UNIVERSE, livedata, channels); FastLED.show(); - status2 = 0; + //println("Done Writing"); + status = 0; } + + status2 = 0; + if (millis() % 100 > 50) { // reset LED digitalWrite(LED_BUILTIN, HIGH); }