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

363 lines
9.0 KiB
C++

// Includes
#include <WiFi.h>
//#include <WiFiServer.h>
#include <LEAmDNS.h>
#include <HTTPUpdateServer.h>
#include <WebServer.h>
#include "e131.h"
#include <FastLED.h>
// User configurable
const char* ssid = "iPhone 14"; // WiFi SSID
const char* password = "givemewifi"; // WiFi Password
const char* ntpserver = "pool.ntp.org"; // Address of NTP server. Example: pool.ntp.org
const char* HOSTNAME = "lighttest";
const char* update_path = "/firmware";
const char* update_username = "admin";
const char* update_password = "pico-stripper";
//#define DEBUG
// Total LED count
#define MAX_LEDS 50
#define LED_TYPE WS2812B
// Amount of color channels per pixel - i.e. RGB = 3, RGBW = 4
#define PIXEL_SIZE 3
// Number of LED strips connected
#define LED_STRIPS 1
// Define the data pin connection to each strip
// Only uncomment the strips you use
#define STRIP1 28
//#define STRIP2 0
//#define STRIP3 2
//#define STRIP4 3
//#define STRIP5 4
//#define STRIP6 5
//#define STRIP7 6
//#define STRIP8 7
#define RGB_ORDER RGB
// define how many LEDs / zones are in each strip
int strips[LED_STRIPS] = {50};
// 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.");
}
void setup() {
pinMode(22, OUTPUT);
digitalWrite(22, LOW); // Enable buffer output!
//pinMode(0, OUTPUT);
//digitalWrite(0, HIGH);
Serial.begin(115200);
delay(3000);
PRINTLNFUNC("========= PicoLighter v1.0 Initializing =========");
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);
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);
PRINTFUNC(", Light count ");
PRINTFUNC(tmp);
PRINTFUNC(", Size ");
PRINTLNFUNC(tmp * PIXEL_SIZE);
offsetcount += tmp;
currentsize += 1;
}
PRINTLNFUNC("========= 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(130, 130, 130);
FastLED.show();
delay(30);
ledstrip[i] = CRGB(0, 0, 0);
}
FastLED.show();
delay(3000);
WiFi.noLowPowerMode();
if (e131.begin(ssid, password) != WL_CONNECTED) {
PRINTFUNC("Connection failed. Retrying.");
rp2040.reboot();
}
PRINTLNFUNC("Starting mDNS client.");
MDNS.begin(HOSTNAME);
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
PRINTFUNC("OTA Updates enabled. Open http://");
PRINTFUNC(HOSTNAME);
PRINTFUNC(update_path);
PRINTFUNC(" in your browser and login with username ");
PRINTFUNC(update_username);
PRINTFUNC(" and password ");
PRINTLNFUNC(update_password);
server.begin();
ready = 1;
while (ready == 1) {
delay(100);
}
// If we get here, then WiFi is good to go
PRINTFUNC("Starting NTP client.");
NTP.begin(ntpserver);
NTP.waitSet([]() { PRINTFUNC("."); }, 15000);
time_t now = time(nullptr);
PRINTLNFUNC("");
gmtime_r(&now, &timeinfo);
PRINTFUNC("Current time: ");
PRINTFUNC(asctime(&timeinfo));
//rp2040.wdt_begin(8000);
}
void loop() {
/* Parse a packet and update pixels */
if(e131.parsePacket()) {
write_universe(e131.universe - 1, 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) {
PRINTLNFUNC("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) {
PRINTLNFUNC("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;
}
}