You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
e131-pico-device/src/main.cpp

160 lines
3.5 KiB
C++

#include <Arduino.h>
#include <W5500lwIP.h>
#include <LEAmDNS.h>
#include <WebServer.h>
#include <HTTPUpdateServer.h>
#include <pico/stdlib.h>
#include <hardware/vreg.h>
# include <lwip/ip_addr.h>
# include <lwip/igmp.h>
# define _UDP WiFiUDP
// Begin code
bool core1_separate_stack = true;
uint8_t raw[4096];
_UDP udp;
int nopackets = 0;
Wiznet5500lwIP eth(17, SPI, 21); // 17 : CS, 21 : INTn
// don't think the interrupt pin is actually used
WebServer httpServer(80);
HTTPUpdateServer httpUpdater;
void initUnicast() {
delay(100);
udp.begin(5568);
if (Serial) {
Serial.print(F("- Unicast port: "));
Serial.println(5568);
}
}
int readPacket() {
int size = udp.parsePacket();
if (size) {
udp.readBytes(raw, size);
}
/*else {
Serial.println("Error: packet size " + String(size));
}*/
return size;
}
void setup() {
//vreg_voltage v = VREG_VOLTAGE_1_20;
//vreg_set_voltage(v);
//set_sys_clock_khz(252000, false); // play with this value
Serial.begin(115200);
//rp2040.wdt_begin(8000);
pinMode(24, INPUT); // VBUS detect - check for USB connection
if (digitalRead(24)) {
delay(3000); // Wait for USB serial if connected
}
Serial.println("\r\nStarting RGB Controller...");
pinMode(21, INPUT); // interrupt pin - probably unused
pinMode(20, OUTPUT); // W5500 RSTn wired to 20
Serial.println("Resetting W5500 Ethernet Driver...");
digitalWrite(20, LOW); // reset W5500 ethernet
delay(1); // for 1 ms
digitalWrite(20, HIGH);
SPI.setRX(16);
SPI.setCS(17);
SPI.setSCK(18);
SPI.setTX(19);
eth.setSPISpeed(10000000); // play with this value
lwipPollingPeriod(3); // play with this value
eth.setHostname("RGBController");
Serial.println(F("Setting IP"));
//eth.config(IPAddress(192,168,68,130), INADDR_NONE); // static IP; comment out for DHCP
if (!eth.begin()) {
Serial.println("No wired Ethernet hardware detected. Check pinouts, wiring.");
Serial.println("Connection failed. Retrying.");
rp2040.reboot();
}
int count = 0;
while (!eth.connected() && count < 32) { // wait 8 seconds for connection
rp2040.wdt_reset();
count++;
Serial.print(".");
delay(250);
}
if (!eth.connected()) {
Serial.println("Connection failed. Retrying.");
rp2040.reboot();
}
Serial.print(F("\r\n- IP Address: "));
Serial.println(eth.localIP());
initUnicast();
MDNS.begin("RGBController");
httpUpdater.setup(&httpServer, "/update", "admin", "admin");
httpServer.begin();
MDNS.addService("http", "tcp", 80);
#ifdef INT_WIFI
WiFi.noLowPowerMode();
#endif
Serial.println("Startup Complete. Listening for e1.31 (sACN) connections...");
}
void setup1() {
pinMode(LED_BUILTIN, OUTPUT);
//pinMode(32+1, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
/* Parse a packet */
//println("Start loop");
int size;
if(size = readPacket()) {
// Offset by start universe
// as all local functions count from 0
//delay(0);
Serial.println("Got valid packet of size " + String(size) + " at " + String(millis()/1000.0));
//delayMicroseconds(1000);
nopackets = 0;
}
else {
//delayMicroseconds(500);
nopackets++;
}
if(nopackets > 50000) {
nopackets = 0;
delay(5);
Serial.println("No packets recieved in a while.... at " + String(millis()/1000.0));
}
MDNS.update();
httpServer.handleClient();
}
void loop1() {
digitalWrite(LED_BUILTIN, HIGH);
delay(50);
digitalWrite(LED_BUILTIN, LOW);
delay(50);
}