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

375 lines
9.5 KiB
C++

// Includes
//#include <WiFi.h>
//#include <WiFiServer.h>
#include <W5500lwIP.h>
#include <LEAmDNS.h>
#include <HTTPUpdateServer.h>
#include <WebServer.h>
#include "e131.h"
#include <FastLED.h>
#include "config.h"
int strips[LED_STRIPS] = {170, 170, 170, 170, 170, 170, 170, 170};
// Begin code
#ifdef DEBUG
#define PRINTFUNC print
#define PRINTLNFUNC println
#else
#define PRINTFUNC
#define PRINTLNFUNC
#endif
int calculate[LED_STRIPS * 4];
int universes[LED_STRIPS * 4];
CRGB ledstrip[MAX_LEDS];
int pins[8];
// Networking
WebServer httpServer(80);
HTTPUpdateServer httpUpdater;
bool status = 0;
int blankcount = 0;
bool ready = 0;
struct tm timeinfo;
WiFiServer server(8000);
String clientbuffer = "";
String initinfo = "";
bool debug = 0;
bool printer = 1;
// Colors (RGB)
const uint8_t RED[PIXEL_SIZE]= {0x20, 0x00, 0x00};
const uint8_t ORANGE[PIXEL_SIZE]= {0x20, 0x10, 0x00};
const uint8_t YELLOW[PIXEL_SIZE]= {0x20, 0x20, 0x00};
const uint8_t GREEN[PIXEL_SIZE]= {0x00, 0x20, 0x00};
const uint8_t CYAN[PIXEL_SIZE]= {0x00, 0x20, 0x20};
const uint8_t BLUE[PIXEL_SIZE]= {0x00, 0x00, 0x20};
const uint8_t PURPLE[PIXEL_SIZE]= {0x20, 0x00, 0x20};
const uint8_t BLACK[PIXEL_SIZE]= {0x00, 0x00, 0x00};
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 */
E131 e131;
template <class T> T print(T in) {
Serial.print(String(in));
if(printer) clientbuffer += String(in);
status = 1;
return (T)true;
}
template <class T> T println(T in) {
Serial.println(String(in));
if(printer) {
clientbuffer += String(in);
clientbuffer += "\n";
}
status = 1;
return (T)true;
}
void write_universe(int universe, uint8_t data[]) {
// universe starts at 0
//PRINTFUNC("Universe: ");
//PRINTLNFUNC(universe);
int offset = calculate[universe];
//PRINTFUNC("Offset: ");
//PRINTLNFUNC(offset);
int write_size = universes[universe];
//PRINTFUNC("Length: ");
//PRINTLNFUNC(write_size);
status = 0;
for (int i = 0; i < write_size; i++) {
int j = i * PIXEL_SIZE + (CHANNEL_START - 1);
//if(debug) {
// PRINTFUNC(data[j]);
// PRINTFUNC(" ");
// PRINTFUNC(data[j+1]);
// PRINTFUNC(" ");
// PRINTFUNC(data[j+2]);
// PRINTFUNC(" ");
//}
ledstrip[offset + i] = CRGB(data[j], data[j+1], data[j+2]);
//ledstrip[strip].setPixelColor(i + offset, data[j], data[j+1], data[j+2]);
}
status = 1;
//FastLED.show();
//PRINTLNFUNC("Done writing.");
}
const char* PARAM_INPUT_1 = "ipaddr";
const char* PARAM_INPUT_2 = "hostname";
const char* PARAM_INPUT_3 = "universe";
// HTML web page to handle 3 input fields (input1, input2, input3)
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
<title>ESP Input Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head><body>
All settings below will save to EEPROM flash and won't get overridden by a firmware upgrade.
Settings will take effect after reboot.
<form action="/get">
ipaddr: <input type="text" name="IP Address">
<input type="submit" value="Submit">
</form><br>
<form action="/get">
hostname: <input type="text" name="Hostname">
<input type="submit" value="Submit">
</form><br>
<form action="/get">
universe: <input type="text" name="Start Universe">
<input type="submit" value="Submit">
</form>
</body></html>)rawliteral";
//void notFound(AsyncWebServerRequest *request) {
// request->send(404, "text/plain", "Not found");
//}
void setup() {
pinMode(20, OUTPUT);
digitalWrite(20, LOW); // reset W5500 ethernet
delay(1);
digitalWrite(20, HIGH);
SPI.setRX(16);
SPI.setCS(17);
SPI.setSCK(18);
SPI.setTX(19);
pinMode(ENABLEPIN, OUTPUT);
digitalWrite(ENABLEPIN, LOW); // Enable buffer output!
//pinMode(0, OUTPUT);
//digitalWrite(0, HIGH);
Serial.begin(115200);
delay(3000);
println("========= PicoLighter v1.0 Initializing =========");
if (!e131.begin()) {
println("Connection failed. Retrying.");
rp2040.reboot();
}
while (ready == 0) {
delay(100);
}
// Populate universes and offsets
int offsetcount = 0;
int currentsize = 0;
for (int i = 0; i < LED_STRIPS; i++) {
int tmp = strips[i];
PRINTFUNC("Strip ");
PRINTFUNC(i);
PRINTFUNC(", Pin ");
PRINTFUNC(pins[i]);
PRINTFUNC(", Light count ");
PRINTLNFUNC(tmp);
while(tmp > MAX_PIXELS_PER_UNIVERSE) {
universes[currentsize] = MAX_PIXELS_PER_UNIVERSE;
calculate[currentsize] = offsetcount;
PRINTFUNC(" Universe ");
PRINTFUNC(currentsize + 1 + START_UNIVERSE);
PRINTFUNC(", Light count ");
PRINTFUNC(MAX_PIXELS_PER_UNIVERSE);
PRINTFUNC(", Size ");
PRINTLNFUNC(MAX_PIXELS_PER_UNIVERSE * PIXEL_SIZE);
offsetcount += MAX_PIXELS_PER_UNIVERSE;
currentsize += 1;
tmp -= MAX_PIXELS_PER_UNIVERSE;
}
universes[currentsize] = tmp;
calculate[currentsize] = offsetcount;
PRINTFUNC(" Universe ");
PRINTFUNC(currentsize + 1 + START_UNIVERSE);
PRINTFUNC(", Light count ");
PRINTFUNC(tmp);
PRINTFUNC(", Size ");
PRINTLNFUNC(tmp * PIXEL_SIZE);
offsetcount += tmp;
currentsize += 1;
}
println("========= PicoLighter v1.0 Initialized =========");
initinfo += clientbuffer;
//e131.beginMulticast(ssid, passphrase, UNIVERSE);
ready = 0;
printer = 0;
}
void setup1() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(32+1, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
#ifdef STRIP1
FastLED.addLeds<LED_TYPE, STRIP1, RGB_ORDER>(ledstrip, calculate[0], strips[0]);
pins[0] = STRIP1;
#endif
#ifdef STRIP2
FastLED.addLeds<LED_TYPE, STRIP2, RGB_ORDER>(ledstrip, calculate[1], strips[1]);
pins[1] = STRIP2;
#endif
#ifdef STRIP3
FastLED.addLeds<LED_TYPE, STRIP3, RGB_ORDER>(ledstrip, calculate[2], strips[2]);
pins[2] = STRIP3;
#endif
#ifdef STRIP4
FastLED.addLeds<LED_TYPE, STRIP4, RGB_ORDER>(ledstrip, calculate[3], strips[3]);
pins[3] = STRIP4;
#endif
#ifdef STRIP5
FastLED.addLeds<LED_TYPE, STRIP5, RGB_ORDER>(ledstrip, calculate[4], strips[4]);
pins[4] = STRIP5;
#endif
#ifdef STRIP6
FastLED.addLeds<LED_TYPE, STRIP6, RGB_ORDER>(ledstrip, calculate[5], strips[5]);
pins[5] = STRIP6;
#endif
#ifdef STRIP7
FastLED.addLeds<LED_TYPE, STRIP7, RGB_ORDER>(ledstrip, calculate[6], strips[6]);
pins[6] = STRIP7;
#endif
#ifdef STRIP8
FastLED.addLeds<LED_TYPE, STRIP8, RGB_ORDER>(ledstrip, calculate[7], strips[7]);
pins[7] = STRIP8;
#endif
// Test all lights
for (int i = 0; i < MAX_LEDS; i++) {
ledstrip[i] = CRGB(0, 0, 50);
FastLED.show();
delay(1);
ledstrip[i] = CRGB(0, 0, 0);
}
FastLED.show();
//delay(3000);
#ifdef INT_WIFI
WiFi.noLowPowerMode();
#endif
ready = 1;
while (ready == 1) {
delay(100);
}
// If we get here, then network is good to go
println("Starting mDNS client.");
MDNS.begin(HOSTNAME);
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
print("OTA Updates enabled. Open http://");
print(HOSTNAME);
print(update_path);
print(" in your browser and login with username ");
print(update_username);
print(" and password ");
println(update_password);
server.begin();
if(ENABLE_NTP) {
println("Starting NTP client.");
NTP.begin(ntpserver);
NTP.waitSet([]() { print("."); }, 15000);
time_t now = time(nullptr);
println("");
gmtime_r(&now, &timeinfo);
print("Current time: ");
println(asctime(&timeinfo));
}
//rp2040.wdt_begin(8000);
}
void loop() {
/* Parse a packet and update pixels */
if(e131.parsePacket()) {
// Offset by start universe
// as all local functions count from 0
write_universe(e131.universe - START_UNIVERSE, e131.data);
}
else if (blankcount > 1000) {
status = 0;
blankcount = 0;
} else {
blankcount ++;
}
}
void loop1() {
if(status == 1) {
FastLED.show();
}
httpServer.handleClient();
MDNS.update();
if (millis() % 100 > 50) { // reset LED
digitalWrite(LED_BUILTIN, HIGH);
}
else if (millis() % 100 < 50 && status == 1) {
//status = 0;
digitalWrite(LED_BUILTIN, LOW);
}
//status = 0;
//delay(50);
float temp = analogReadTemp();
if (temp > 50.0) {
println("ERROR: Overtemperature triggered!");
rp2040.reboot();
}
WiFiClient client = server.available();
if (client) {
PRINTLNFUNC("Client connected");
clientbuffer = "";
printer = 1;
client.println(initinfo);
client.println("Press d + ENTER to toggle RGB debug.");
while (client.connected()) {
if(status == 1) {
FastLED.show();
}
httpServer.handleClient();
MDNS.update();
if (millis() % 100 > 50) { // reset LED
digitalWrite(LED_BUILTIN, HIGH);
}
else if (millis() % 100 < 50 && status == 1) {
//status = 0;
digitalWrite(LED_BUILTIN, LOW);
}
//status = 0;
//delay(50);
float temp = analogReadTemp(); // read temp in celsius
if (temp > 50.0) {
println("ERROR: Overtemperature triggered!");
rp2040.reboot();
}
if(clientbuffer != (String) "") {
client.print(clientbuffer);
clientbuffer = "";
}
while(client.available()) {
char c = client.read();
if (c == 'd') {
if (debug == 1) {
debug = 0;
} else {
debug = 1;
}
}
}
}
client.stop();
printer = 0;
}
}