From 61c6a35dfb9fce6394669ddff8df995f9bc2530b Mon Sep 17 00:00:00 2001 From: Arturo Silvelo Date: Fri, 19 Jul 2024 17:18:44 +0200 Subject: [PATCH] fix: number matrix --- src/LedManager.cpp | 24 ++++++++++++++++++----- src/PreferencesManager.cpp | 7 +++++++ src/ServerManager.cpp | 40 ++++++++++++++++++++++++++++++++++++++ src/main.cpp | 34 +++++++++++++++++++------------- 4 files changed, 86 insertions(+), 19 deletions(-) diff --git a/src/LedManager.cpp b/src/LedManager.cpp index b9821a5..172fdea 100644 --- a/src/LedManager.cpp +++ b/src/LedManager.cpp @@ -49,6 +49,20 @@ void LedManager::showMinutes(int minutes) { minutesStrip.show(); } +void LedManager::showHoursTest(int hour) { + this->showHourDigit(hour, 0, this->hourColor1); + this->showHourDigit(hour, hoursLength / 2, this->hourColor2); + + hoursStrip.show(); +} + +void LedManager::showMinutesTest(int minutes) { + this->showMinuteDigit(minutes, 0, this->minuteColor1); + this->showMinuteDigit(minutes, minutesLength / 2, this->minuteColor2); + + minutesStrip.show(); +} + void LedManager::showColon() { for (int i = 0; i < secondsLength / 2; i++) { secondsStrip.setPixelColor(i, this->dotsColor1); @@ -90,13 +104,13 @@ void LedManager::showHourDigit(int number, int startPixel, uint32_t color) { } void LedManager::showMinuteDigit(int number, int startPixel, uint32_t color) { - Serial.println("Number " + String(number)); + /* Serial.println("Number " + String(number)); */ for (int i = 0; i < MINUTE_SEGMENT; i++) { - Serial.println("Segment number " + String(i)); + /* Serial.println("Segment number " + String(i)); */ if (minuteSegments[number][i]) { - Serial.println("Power On pixels:"); + /* Serial.println("Power On pixels:"); */ for (int j = 0; j < ledsPerSegment; j++) { - Serial.print("["); + /* Serial.print("["); Serial.print(" Start Pixel: "); Serial.print(startPixel); Serial.print(" i: "); @@ -107,8 +121,8 @@ void LedManager::showMinuteDigit(int number, int startPixel, uint32_t color) { Serial.print(j); Serial.print("] - "); Serial.print(startPixel + (i * ledsPerSegment) + j); + Serial.println(); */ minutesStrip.setPixelColor(startPixel + (i * ledsPerSegment) + j, color); - Serial.println(); } } else { for (int j = 0; j < ledsPerSegment; j++) { diff --git a/src/PreferencesManager.cpp b/src/PreferencesManager.cpp index 819da13..7fb8a65 100644 --- a/src/PreferencesManager.cpp +++ b/src/PreferencesManager.cpp @@ -98,6 +98,13 @@ int PreferencesManager::getLedsPerSegment() { return preferences.getInt(LEDS_PER_SEGMENT, DEFAULT_LEDS_PER_SEGMENT); } +bool PreferencesManager::getDebug() { + return preferences.getBool(DEBUG, false); +} +void PreferencesManager::setDebug(boolean debug) { + preferences.putBool(DEBUG, debug); +} + void PreferencesManager::setLedsPerSegment(int numLeds) { preferences.putInt(LEDS_PER_SEGMENT, numLeds); } diff --git a/src/ServerManager.cpp b/src/ServerManager.cpp index 4436549..3c36b89 100644 --- a/src/ServerManager.cpp +++ b/src/ServerManager.cpp @@ -25,6 +25,7 @@ void ServerManager::begin() { server.on("/colors", HTTP_GET, std::bind(&ServerManager::getColors, this, std::placeholders::_1)); + server.on("/debug", HTTP_GET, std::bind(&ServerManager::getDebug, this, std::placeholders::_1)); server.on("/leds", HTTP_GET, std::bind(&ServerManager::getLedsPerSegment, this, std::placeholders::_1)); server.on("/timezone", HTTP_GET, @@ -32,6 +33,11 @@ void ServerManager::begin() { server.on("/deep-sleep", HTTP_GET, std::bind(&ServerManager::getDeepSleep, this, std::placeholders::_1)); + server.on( + "/debug", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, + std::bind(&ServerManager::updateDebug, this, std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)); + server.on( "/colors", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, std::bind(&ServerManager::updateColors, this, std::placeholders::_1, std::placeholders::_2, @@ -147,6 +153,40 @@ void ServerManager::updateLedsPerSegment(AsyncWebServerRequest *request, uint8_t request->send(400); } +void ServerManager::getDebug(AsyncWebServerRequest *request) { + Serial.println(String("[") + request->methodToString() + "] " + request->url()); + JsonDocument debugData; + + debugData["debug"] = preferencesManager.getDebug(); + + String jsonString; + serializeJson(debugData, jsonString); + + request->send(200, MIME_APPLICATION_JSON, jsonString); +} + +void ServerManager::updateDebug(AsyncWebServerRequest *request, uint8_t *data, size_t len, + size_t index, size_t total) { + Serial.println(String("[") + request->methodToString() + "] " + request->url()); + String bodyContent = getBodyContent(data, len); + + JsonDocument body; + DeserializationError error = deserializeJson(body, bodyContent); + if (error) { + request->send(400); + return; + } + + if (body.containsKey("debug")) { + bool debug = body["debug"].as(); + preferencesManager.setDebug(debug); + request->send(204); + return; + } + + request->send(400); +} + void ServerManager::getTimezone(AsyncWebServerRequest *request) { Serial.println(String("[") + request->methodToString() + "] " + request->url()); JsonDocument timezoneData; diff --git a/src/main.cpp b/src/main.cpp index 771e077..9320026 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,21 +60,27 @@ void setup() { void loop() { ElegantOTA.loop(); timeManager.update(); - - bool isSleep = sleepManager.update(timeManager.getFormattedTime()); - if (isSleep) { - Serial.println("Is Sleep:" + String(isSleep)); - ledManager.end(); + if (preferencesManager.getDebug()) { + for (int i = 0; i < 10; i++) { + ledManager.showHoursTest(i); + ledManager.showMinutesTest(i); + delay(1000); + } } else { - int hours = timeManager.getHours(); - int minutes = timeManager.getMinutes(); - // int minutes = timeManager.getSeconds(); - - Serial.println(timeManager.getFormattedTime()); - - ledManager.showHours(hours); - ledManager.showMinutes(minutes); - ledManager.showColon(); + bool isSleep = sleepManager.update(timeManager.getFormattedTime()); + if (isSleep) { + Serial.println("Is Sleep:" + String(isSleep)); + ledManager.end(); + } else { + int hours = timeManager.getHours(); + int minutes = timeManager.getMinutes(); + + Serial.println(timeManager.getFormattedTime()); + + ledManager.showHours(hours); + ledManager.showMinutes(minutes); + ledManager.showColon(); + } } delay(1000);