P10Panel/panel.ino
2025-02-24 20:23:44 +03:00

225 lines
6.3 KiB
C++

//------------------Include Libraries------------------//
#include <WiFi.h>
#include <WebServer.h>
#include <DMD32.h>
#include <fonts/Font_BOLD.h>
#include <SPIFFS.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
#include "data/output.h"
//------------------------------------------------------//
#define DEBUG 1
//------------------Settings for panel------------------//
#define FONT Font_BOLD
#define DISPLAYS_ACROSS 1
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);
//-------------------------------------------------------//
//------------------Wi-Fi Settings----------------------//
const char *ssid = "SKBKIT";
const char *password = "skbkit2024";
IPAddress staticIP(10, 131, 170, 4);
IPAddress gateway(10, 131, 170, 1);
IPAddress subnet(255, 255, 255, 0);
//-------------------------------------------------------//
//------------------Web Server Settings-----------------//
AsyncWebServer server(80);
//-------------------------------------------------------//
//------------------Other Variables---------------------//
String displayText = "Привет из СКБ \"КИТ\"";
bool isScrolling = true;
bool showImage = false;
unsigned long lastScrollTime = 0;
unsigned long scrollDuration = 5000;
unsigned long imageStartTime = 0;
hw_timer_t *timer = NULL;
//-------------------------------------------------------//
//------------------Wi-Fi Connection Setup-------------//
void setupWiFi() {
WiFi.begin(ssid, password);
if (!WiFi.config(staticIP, gateway, subnet)) {
Serial.println("Failed to configure Static IP");
} else {
Serial.println("Static IP configured!");
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
Serial.println(WiFi.localIP());
}
//-------------------------------------------------------//
//------------------Web Server Handlers-----------------//
void handleText(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
JsonDocument jsonReq;
String dataRequest = String((char *)data, len);
DeserializationError error = deserializeJson(jsonReq, dataRequest);
if (error) {
Serial.println(F("deserializeJson() failed"));
request->send(400, "text/html", "");
return;
}
if (jsonReq["text"].is<String>()) {
displayText = jsonReq["text"].as<String>();
}
request->send(200, "text/html; charset=UTF-8", "");
}
void handleStateChange(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
JsonDocument jsonReq;
String dataRequest = String((char *)data, len);
DeserializationError error = deserializeJson(jsonReq, dataRequest);
if (error) {
request->send(400, "text/html", "");
return;
}
String state = jsonReq["state"];
if (state == "1") {
dmd.clearScreen(true); // Reset the screen
displayText = "Привет из СКБ \"КИТ\"";
} else if (state == "2") {
isScrolling = true;
lastScrollTime = millis();
showImage = false;
} else if (state == "3") {
showImage = true;
imageStartTime = millis();
}
request->send(200, "text/html", "");
}
//-------------------------------------------------------//
//------------------Panel Management--------------------//
void handlePanelState(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
JsonDocument jsonReq;
String dataRequest = String((char *)data, len);
DeserializationError error = deserializeJson(jsonReq, dataRequest);
if (error) {
request->send(400, "text/html", "");
return;
}
String state = jsonReq["panel"];
if (state == "on") {
displayText = "Привет из СКБ \"КИТ\"";
} else if (state == "off") {
displayText = "";
}
request->send(200, "text/html", "");
}
//-------------------------------------------------------//
//------------------Display Binary Array Function------//
void displayBinaryArray() {
for (int y = 0; y < 32; y++) {
for (int x = 0; x < 16; x++) {
int idx = x * 32 + y;
if (output[idx] == 1) {
dmd.writePixel(x, y, GRAPHICS_NORMAL, true);
} else {
dmd.writePixel(x, y, GRAPHICS_NORMAL, false);
}
}
}
}
//-------------------------------------------------------//
//------------------Marquee Scroll Function-------------//
void scrollText() {
dmd.drawMarquee(displayText.c_str(), displayText.length(), (32 * DISPLAYS_ACROSS) - 1, 0);
long start = millis();
long timer = start;
bool ret = false;
while (!ret) {
if ((timer + 30) < millis()) {
ret = dmd.stepMarquee(-1, 0);
timer = millis();
}
}
if (millis() - lastScrollTime >= scrollDuration) {
isScrolling = false;
showImage = true;
imageStartTime = millis();
}
}
//-------------------------------------------------------//
//------------------Timer Interrupt Function------------//
void IRAM_ATTR triggerScan() {
dmd.scanDisplayBySPI();
}
//-------------------------------------------------------//
//------------------Setup Function-----------------------//
void setup() {
Serial.begin(115200);
dmd.selectFont(FONT);
SPIFFS.begin(true);
pinMode(22, OUTPUT);
setupWiFi();
server.on("/api/led", HTTP_GET, [](AsyncWebServerRequest *request) {
String state = isScrolling ? "true" : "false";
request->send(200, "application/json", "{\"state\": \"" + state + "\"}");
});
server.on("/api/led", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, handlePanelState);
server.on("/api/text", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, handleText);
server.on("/api/state", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, handleStateChange);
server.begin();
uint8_t cpuClock = ESP.getCpuFreqMHz();
timer = timerBegin(0, cpuClock, true);
timerAttachInterrupt(timer, &triggerScan, true);
timerAlarmWrite(timer, 300, true);
timerAlarmEnable(timer);
dmd.clearScreen(true);
}
//-------------------------------------------------------//
//------------------Loop Function------------------------//
void loop() {
dmd.clearScreen(true);
if (isScrolling) {
scrollText();
}
if (showImage) {
displayBinaryArray();
if (millis() - imageStartTime >= 5000) {
showImage = false;
isScrolling = true;
lastScrollTime = millis();
}
}
delay(500);
}
//-------------------------------------------------------//