#include #include #include #include "structs.h" #include "upload_page.h" #include "prototypes.h" const char* ssid = "PPIA"; const char* password = "pawelpdaldonejta"; unsigned char brightness = 100; WebServer server(80); Pixel new_image[16][16]; char* fileContent = nullptr; size_t fileContent_len = 0; String upload_error_message = ""; void handleBrightness() { if (server.hasArg("value")) { brightness = server.arg("value").toInt(); } server.send(200, "text/plain", "OK"); } void handleRoot() { server.send(200, "text/html", index_html); } void handleUploadPage() { server.send(200, "text/html", upload_page_html); } void handleBmpUpload() { HTTPUpload& upload = server.upload(); if (upload.status == UPLOAD_FILE_START) { upload_error_message = ""; 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 dataOffset = *(int*)&header[10]; int width = *(int*)&header[18]; int height = *(int*)&header[22]; short bitsPerPixel = *(short*)&header[28]; if (bitsPerPixel != 24) { upload_error_message = "Unsupported BMP format: Only 24-bit BMPs are supported"; return; } if (width < 1 || width > 48 || height < 1 || height > 16) { upload_error_message = "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 + dataOffset; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int bmp_row = height - 1 - y; saved_images[0].pixels[y][x].b = pixel_data[bmp_row*row_padded + x*3 + 0]; saved_images[0].pixels[y][x].g = pixel_data[bmp_row*row_padded + x*3 + 1]; saved_images[0].pixels[y][x].r = pixel_data[bmp_row*row_padded + x*3 + 2]; } } free(fileContent); fileContent = nullptr; fileContent_len = 0; } } } void handleShowSaved() { pixels.clear(); drawImageFromMemoryByIndex(0, 0, 0, brightness); 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("/brightness", HTTP_POST, handleBrightness); server.on("/show-saved", handleShowSaved); server.on("/upload-page", HTTP_GET, handleUploadPage); server.on("/upload-bmp", HTTP_POST, []() { if (upload_error_message.length() > 0) { server.send(400, "text/plain", upload_error_message); } else { server.send(200, "text/plain", "Upload OK"); } }, handleBmpUpload); server.begin(); } void handle_server() { server.handleClient(); }