non workking vibe coded screen displaying
This commit is contained in:
+69
-48
@@ -2,6 +2,8 @@
|
||||
#include <WebServer.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include "structs.h"
|
||||
#include "upload_page.h"
|
||||
#include "prototypes.h"
|
||||
|
||||
const char* ssid = "PPIA";
|
||||
const char* password = "pawelpdaldonejta";
|
||||
@@ -9,62 +11,78 @@ const char* password = "pawelpdaldonejta";
|
||||
WebServer server(80);
|
||||
|
||||
Pixel new_image[16][16];
|
||||
char* fileContent = nullptr;
|
||||
size_t fileContent_len = 0;
|
||||
|
||||
|
||||
void handleRoot()
|
||||
{
|
||||
server.send(200, "text/html", index_html);
|
||||
}
|
||||
|
||||
void handleUpload()
|
||||
void handleUploadPage()
|
||||
{
|
||||
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++;
|
||||
server.send(200, "text/html", upload_page_html);
|
||||
}
|
||||
|
||||
|
||||
void handleBmpUpload() {
|
||||
HTTPUpload& upload = server.upload();
|
||||
if (upload.status == UPLOAD_FILE_START) {
|
||||
if (fileContent) {
|
||||
free(fileContent);
|
||||
fileContent = nullptr;
|
||||
fileContent_len = 0;
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||
if (upload.name == "image") {
|
||||
fileContent = (char*)realloc(fileContent, fileContent_len + upload.currentSize);
|
||||
memcpy(fileContent + fileContent_len, upload.buf, upload.currentSize);
|
||||
fileContent_len += upload.currentSize;
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_END) {
|
||||
if (upload.name == "image") {
|
||||
// Read BMP header
|
||||
char header[54];
|
||||
memcpy(header, fileContent, 54);
|
||||
|
||||
int width = *(int*)&header[18];
|
||||
int height = *(int*)&header[22];
|
||||
|
||||
if (width < 1 || width > 48 || height < 1 || height > 16) {
|
||||
server.send(400, "text/plain", "Invalid image dimensions");
|
||||
return;
|
||||
}
|
||||
|
||||
saved_images[0].width = width;
|
||||
saved_images[0].height = height;
|
||||
|
||||
// Read pixel data
|
||||
int row_padded = (width*3 + 3) & (~3);
|
||||
char* pixel_data = fileContent + 54;
|
||||
|
||||
for (int y = height - 1; y >= 0; y--) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
saved_images[0].pixels[y][x].b = pixel_data[y*row_padded + x*3 + 0];
|
||||
saved_images[0].pixels[y][x].g = pixel_data[y*row_padded + x*3 + 1];
|
||||
saved_images[0].pixels[y][x].r = pixel_data[y*row_padded + x*3 + 2];
|
||||
}
|
||||
}
|
||||
|
||||
pixels.clear();
|
||||
drawImageFromMemoryByIndex(0, 0, 0);
|
||||
pixels.show();
|
||||
server.send(200, "text/plain", "OK");
|
||||
|
||||
free(fileContent);
|
||||
fileContent = nullptr;
|
||||
fileContent_len = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void handleShowSaved()
|
||||
{
|
||||
pixels.clear();
|
||||
@@ -86,7 +104,10 @@ void start_server()
|
||||
|
||||
server.on("/", handleRoot);
|
||||
server.on("/show-saved", handleShowSaved);
|
||||
server.on("/upload", HTTP_POST, handleUpload);
|
||||
server.on("/upload-page", HTTP_GET, handleUploadPage);
|
||||
server.on("/upload-bmp", HTTP_POST, []() {
|
||||
server.send(200, "text/plain", "");
|
||||
}, handleBmpUpload);
|
||||
server.begin();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user