Files
pti-ledy/server.ino
T
2026-02-04 09:41:32 +01:00

97 lines
2.1 KiB
Arduino

#include <WiFi.h>
#include <WebServer.h>
#include <ArduinoJson.h>
#include "structs.h"
const char* ssid = "PPIA";
const char* password = "pawelpdaldonejta";
WebServer server(80);
Pixel new_image[16][16];
void handleRoot()
{
server.send(200, "text/html", index_html);
}
void handleUpload()
{
if (server.hasArg("plain") == false)
{
server.send(400, "text/plain", "body not received");
return;
}
String body = server.arg("plain");
DynamicJsonDocument doc(8192);
deserializeJson(doc, body);
JsonArray arr = doc.as<JsonArray>();
int row = 0;
for (JsonVariant val : arr)
{
int col = 0;
for (JsonVariant val2 : val.as<JsonArray>())
{
unsigned long color = strtoul(val2.as<const char*>(), NULL, 16);
new_image[row][col] = { (unsigned short)col, (unsigned short)row, (uint32_t)color };
col++;
}
row++;
}
// Save the new image to the next slot
if (saved_images_count >= MAX_IMAGES_SAVED) {
saved_images_count = 0;
}
// Copy new_image data to saved_images array
for (int r = 0; r < 16; r++)
{
for (int c = 0; c < 16; c++)
{
uint32_t color = new_image[r][c].color;
RGB rgb = {(unsigned char)((color >> 16) & 0xFF), (unsigned char)((color >> 8) & 0xFF), (unsigned char)(color & 0xFF)};
saved_images[saved_images_count].pixels[r][c] = rgb;
}
}
saved_images[saved_images_count].width = 16;
saved_images[saved_images_count].height = 16;
pixels.clear();
drawImageFromMemoryByIndex(saved_images_count, 0, 0);
pixels.show();
server.send(200, "text/plain", "OK");
saved_images_count++;
}
void handleShowSaved()
{
pixels.clear();
drawImageFromMemoryByIndex(0, 0, 0);
pixels.show();
server.send(200, "text/plain", "OK");
}
void start_server()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/show-saved", handleShowSaved);
server.on("/upload", HTTP_POST, handleUpload);
server.begin();
}
void handle_server()
{
server.handleClient();
}