From 6ffd4a5a05766a0e3d56b1f3b76ed47c15ef0d41 Mon Sep 17 00:00:00 2001 From: Stefan Ostermann Date: Mon, 18 Nov 2024 22:12:57 +0100 Subject: [PATCH] Edit Mapping --- src/WebContent.h | 23 +++++++++++++++++++++++ src/main.cpp | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/WebContent.h b/src/WebContent.h index b158106..3619581 100644 --- a/src/WebContent.h +++ b/src/WebContent.h @@ -44,6 +44,15 @@ const char index_html[] PROGMEM = R"rawliteral(
+

Edit RFID Mapping

+
+ +
+ +
+ +
+ )rawliteral"; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index af51aaf..432dd0f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -144,7 +144,7 @@ void deactivateRFID() String humanReadableSize(const size_t bytes) { if (bytes < 1024) return String(bytes) + " B"; else if (bytes < (1024 * 1024)) return String(bytes / 1024.0) + " KB"; - else if (bytes < (1024 * 1024 * 1024)) return String(bytes / 1024.0 / 1024.0) + " MB"; + else if (bytes < (1024 * 1024 * 1024)) return String(bytes / 1024.0 / 1024.0) + " MB"; else return String(bytes / 1024.0 / 1024.0 / 1024.0) + " GB"; } @@ -176,10 +176,11 @@ void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint32_t getBatteryVoltageMv() { uint32_t voltage = analogReadMilliVolts(BAT_VOLTAGE_PIN); + voltage *= 2;//*2 because of the voltage divider. Serial.print("Battery Voltage: "); - Serial.println(voltage*2); + Serial.println(voltage); Serial.println(" mV"); - return voltage*2;//*2 because of the voltage divider. + return voltage; } @@ -365,6 +366,33 @@ String getState() return output; } +// Function to save the rfid_map to the mapping file +void saveMappingToFile(const char *filename) { + File file = SD.open(filename, FILE_WRITE); + if (file) { + for (const auto &pair : rfid_map) { + file.println(pair.first + "=" + pair.second); + } + file.close(); + Serial.println("Mapping saved to file."); + } else { + Serial.println("Error opening file for writing."); + } +} + +// Function to handle edit requests +void editMapping(AsyncWebServerRequest *request) { + if (request->hasParam("rfid", true) && request->hasParam("song", true)) { + String rfid = request->getParam("rfid", true)->value(); + String song = request->getParam("song", true)->value(); + rfid_map[rfid] = song; + saveMappingToFile(mapping_file.c_str()); + request->send(200, "text/plain", "Mapping updated"); + } else { + request->send(400, "text/plain", "Invalid parameters"); + } +} + std::map readDataFromFile(const char *filename) { File file = SD.open(filename); @@ -645,6 +673,8 @@ void setup() server.on("/volume", HTTP_POST, volume_action); + server.on("/edit_mapping", HTTP_POST, editMapping); + // run handleUpload function when any file is uploaded server.on("/upload", HTTP_POST, [](AsyncWebServerRequest *request) { request->send(200);