diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..5e7a0eb --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +BasedOnStyle: Google +IndentWidth: 4 +ColumnLimit: 100 +AllowShortFunctionsOnASingleLine: false diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 080e70d..9a91518 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -2,7 +2,8 @@ // See http://go.microsoft.com/fwlink/?LinkId=827846 // for the documentation about the extensions.json format "recommendations": [ - "platformio.platformio-ide" + "platformio.platformio-ide", + "xaver.clang-format" ], "unwantedRecommendations": [ "ms-vscode.cpptools-extension-pack" diff --git a/.vscode/settings.json b/.vscode/settings.json index 6fd3eae..3de9908 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,7 @@ "istream": "cpp", "functional": "cpp" }, + "html.format.wrapAttributes": "aligned-multiple", "editor.formatOnSave": true, - "html.format.wrapAttributes": "aligned-multiple" + "C_Cpp.clang_format_fallbackStyle": "Google" } diff --git a/README.md b/README.md new file mode 100644 index 0000000..7177eb4 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Download `firmware.bin` and `spiffs.bin` from [releases](https://github.com/silvelo/7_segments_clock/releases) diff --git a/include/PreferencesManager.h b/include/PreferencesManager.h index 954d081..559d5c2 100644 --- a/include/PreferencesManager.h +++ b/include/PreferencesManager.h @@ -13,9 +13,9 @@ #define MINUTES_LED_2_COLOR "ml2c" #include -class PreferencesManager -{ -public: +#include +class PreferencesManager { + public: static PreferencesManager &getInstance(); void begin(); @@ -38,7 +38,7 @@ class PreferencesManager uint32_t getMinutesLed2Color(); void setMinutesLed2Color(uint32_t color); -private: + private: PreferencesManager() = default; PreferencesManager(const PreferencesManager &) = delete; PreferencesManager &operator=(const PreferencesManager &) = delete; diff --git a/include/ServerManager.h b/include/ServerManager.h index 10a0bc5..0dd6978 100644 --- a/include/ServerManager.h +++ b/include/ServerManager.h @@ -1,24 +1,25 @@ #ifndef SERVER_MANAGER_H #define SERVER_MANAGER_H +#include #include #include -#include -#include "PreferencesManager.h" +#include + #include "LedManager.h" +#include "PreferencesManager.h" #include "TimeManager.h" #define MIME_APPLICATION_JSON "application/json" -class ServerManager -{ -public: +class ServerManager { + public: static ServerManager &getInstance(); AsyncWebServer *getServer(); void begin(); -private: + private: ServerManager(); ServerManager(const ServerManager &) = delete; ServerManager &operator=(const ServerManager &) = delete; @@ -29,15 +30,19 @@ class ServerManager TimeManager &timeManager; void getColors(AsyncWebServerRequest *request); - void updateColors(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total); + void updateColors(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, + size_t total); void getTimezone(AsyncWebServerRequest *request); - void updateTimezone(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total); + void updateTimezone(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, + size_t total); void getDeepSleep(AsyncWebServerRequest *request); - void updateDeepSleep(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total); + void updateDeepSleep(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, + size_t total); void handleRoot(AsyncWebServerRequest *request); void handleCSS(AsyncWebServerRequest *request); void handleJS(AsyncWebServerRequest *request); - void serveFile(const String &filePath, const String &contentType, AsyncWebServerRequest *request); + void serveFile(const String &filePath, const String &contentType, + AsyncWebServerRequest *request); JsonDocument createColorJson(); uint32_t stringToHex(const String &hexString); String hexToString(uint32_t hex); diff --git a/src/LedManager.cpp b/src/LedManager.cpp index 99abee1..155e71b 100644 --- a/src/LedManager.cpp +++ b/src/LedManager.cpp @@ -1,7 +1,6 @@ #include "LedManager.h" -LedManager &LedManager::getInstance() -{ +LedManager &LedManager::getInstance() { static LedManager instance; return instance; } @@ -10,44 +9,39 @@ LedManager::LedManager() : hoursStrip(NUM_PIXELS, HOUR_PIN, NEO_GRB + NEO_KHZ800), minutesStrip(NUM_PIXELS, MINUTE_PIN, NEO_GRB + NEO_KHZ800), secondsStrip(SECOND_NUM_PIXELS, SECOND_PIN, NEO_GRB + NEO_KHZ800), - preferencesManager(PreferencesManager::getInstance()) -{ + preferencesManager(PreferencesManager::getInstance()) { } -void LedManager::begin() -{ +void LedManager::begin() { Serial.begin(115200); hoursStrip.begin(); minutesStrip.begin(); secondsStrip.begin(); + updateColorsFromPreferences(); Serial.println("Led Manager inicializado."); } -void LedManager::showHours(int hour) -{ +void LedManager::showHours(int hour) { this->showHourDigit(hour % 10, 0, this->hourColor1); this->showHourDigit(hour / 10, NUM_PIXELS / 2, this->hourColor2); hoursStrip.show(); } -void LedManager::showMinutes(int minutes) -{ +void LedManager::showMinutes(int minutes) { this->showMinuteDigit(minutes / 10, 0, this->minuteColor1); this->showMinuteDigit(minutes % 10, NUM_PIXELS / 2, this->minuteColor2); minutesStrip.show(); } -void LedManager::showColon() -{ +void LedManager::showColon() { secondsStrip.setPixelColor(0, this->dotsColor1); secondsStrip.setPixelColor(1, this->dotsColor2); secondsStrip.show(); } -void LedManager::updateColorsFromPreferences() -{ +void LedManager::updateColorsFromPreferences() { hourColor1 = preferencesManager.getHourLed1Color(); hourColor2 = preferencesManager.getHourLed2Color(); minuteColor1 = preferencesManager.getMinutesLed1Color(); @@ -56,34 +50,24 @@ void LedManager::updateColorsFromPreferences() dotsColor2 = preferencesManager.getDotsLed2Color(); } -void LedManager::showHourDigit(int number, int startPixel, uint32_t color) -{ - for (int i = 0; i < 7; i++) - { - if (hourSegments[number][i]) - { +void LedManager::showHourDigit(int number, int startPixel, uint32_t color) { + for (int i = 0; i < 7; i++) { + if (hourSegments[number][i]) { hoursStrip.setPixelColor(startPixel + (i * 2), color); hoursStrip.setPixelColor(startPixel + (i * 2) + 1, color); - } - else - { + } else { hoursStrip.setPixelColor(startPixel + (i * 2), hoursStrip.Color(0, 0, 0)); hoursStrip.setPixelColor(startPixel + (i * 2) + 1, hoursStrip.Color(0, 0, 0)); } } } -void LedManager::showMinuteDigit(int number, int startPixel, uint32_t color) -{ - for (int i = 0; i < 7; i++) - { - if (minuteSegments[number][i]) - { +void LedManager::showMinuteDigit(int number, int startPixel, uint32_t color) { + for (int i = 0; i < 7; i++) { + if (minuteSegments[number][i]) { minutesStrip.setPixelColor(startPixel + (i * 2), color); minutesStrip.setPixelColor(startPixel + (i * 2) + 1, color); - } - else - { + } else { minutesStrip.setPixelColor(startPixel + (i * 2), minutesStrip.Color(0, 0, 0)); minutesStrip.setPixelColor(startPixel + (i * 2) + 1, minutesStrip.Color(0, 0, 0)); } diff --git a/src/PreferencesManager.cpp b/src/PreferencesManager.cpp index 216cc99..104ec76 100644 --- a/src/PreferencesManager.cpp +++ b/src/PreferencesManager.cpp @@ -1,18 +1,14 @@ -#include #include "PreferencesManager.h" -PreferencesManager &PreferencesManager::getInstance() -{ +PreferencesManager &PreferencesManager::getInstance() { static PreferencesManager instance; return instance; } -void PreferencesManager::begin() -{ +void PreferencesManager::begin() { Serial.begin(115200); - if (!SPIFFS.begin()) - { + if (!SPIFFS.begin()) { Serial.println("Error al montar el sistema de archivos"); return; } @@ -21,97 +17,79 @@ void PreferencesManager::begin() Serial.println("Preferences inicializado."); } -int PreferencesManager::getTimeOffset() -{ +int PreferencesManager::getTimeOffset() { return preferences.getInt(TIME_OFFSET); } -void PreferencesManager::setTimeOffset(int timeOffset) -{ +void PreferencesManager::setTimeOffset(int timeOffset) { Serial.println("Set time offset: " + String(timeOffset) + " hours."); preferences.putInt(TIME_OFFSET, timeOffset); } -uint32_t PreferencesManager::getStartTimestamp() -{ +uint32_t PreferencesManager::getStartTimestamp() { Serial.println("Get start timestamp"); return preferences.getUInt(START_TIMESTAMP); } -void PreferencesManager::setStartTimestamp(uint32_t startTimestamp) -{ +void PreferencesManager::setStartTimestamp(uint32_t startTimestamp) { Serial.println("Set start timestamp: " + String(startTimestamp)); preferences.putUInt(START_TIMESTAMP, startTimestamp); } -uint32_t PreferencesManager::getEndTimestamp() -{ +uint32_t PreferencesManager::getEndTimestamp() { Serial.println("Get end timestamp"); return preferences.getUInt(END_TIMESTAMP, 0); } -void PreferencesManager::setEndTimestamp(uint32_t endTimestamp) -{ +void PreferencesManager::setEndTimestamp(uint32_t endTimestamp) { Serial.println("Set end timestamp: " + String(endTimestamp)); preferences.putUInt(END_TIMESTAMP, endTimestamp); } -uint32_t PreferencesManager::getHourLed1Color() -{ +uint32_t PreferencesManager::getHourLed1Color() { return preferences.getUInt(HOUR_LED_1_COLOR, DEFAULT_LED_COLOR); } -void PreferencesManager::setHourLed1Color(uint32_t color) -{ +void PreferencesManager::setHourLed1Color(uint32_t color) { preferences.putUInt(HOUR_LED_1_COLOR, color); } -uint32_t PreferencesManager::getHourLed2Color() -{ +uint32_t PreferencesManager::getHourLed2Color() { return preferences.getUInt(HOUR_LED_2_COLOR, DEFAULT_LED_COLOR); } -void PreferencesManager::setHourLed2Color(uint32_t color) -{ +void PreferencesManager::setHourLed2Color(uint32_t color) { preferences.putUInt(HOUR_LED_2_COLOR, color); } -uint32_t PreferencesManager::getDotsLed1Color() -{ +uint32_t PreferencesManager::getDotsLed1Color() { return preferences.getUInt(DOTS_LED_1_COLOR, DEFAULT_LED_COLOR); } -void PreferencesManager::setDotsLed1Color(uint32_t color) -{ +void PreferencesManager::setDotsLed1Color(uint32_t color) { preferences.putUInt(DOTS_LED_1_COLOR, color); } -uint32_t PreferencesManager::getDotsLed2Color() -{ +uint32_t PreferencesManager::getDotsLed2Color() { return preferences.getUInt(DOTS_LED_2_COLOR, DEFAULT_LED_COLOR); } -void PreferencesManager::setDotsLed2Color(uint32_t color) -{ +void PreferencesManager::setDotsLed2Color(uint32_t color) { preferences.putUInt(DOTS_LED_2_COLOR, color); } -uint32_t PreferencesManager::getMinutesLed1Color() -{ +uint32_t PreferencesManager::getMinutesLed1Color() { return preferences.getUInt(MINUTES_LED_1_COLOR, DEFAULT_LED_COLOR); } -void PreferencesManager::setMinutesLed1Color(uint32_t color) -{ +void PreferencesManager::setMinutesLed1Color(uint32_t color) { preferences.putUInt(MINUTES_LED_1_COLOR, color); } -uint32_t PreferencesManager::getMinutesLed2Color() -{ +uint32_t PreferencesManager::getMinutesLed2Color() { return preferences.getUInt(MINUTES_LED_2_COLOR, DEFAULT_LED_COLOR); } -void PreferencesManager::setMinutesLed2Color(uint32_t color) -{ +void PreferencesManager::setMinutesLed2Color(uint32_t color) { preferences.putUInt(MINUTES_LED_2_COLOR, color); } diff --git a/src/ServerManager.cpp b/src/ServerManager.cpp index 63240ae..c3a61a4 100644 --- a/src/ServerManager.cpp +++ b/src/ServerManager.cpp @@ -1,63 +1,70 @@ -#include #include "ServerManager.h" -ServerManager &ServerManager::getInstance() -{ +ServerManager &ServerManager::getInstance() { static ServerManager instance; return instance; } ServerManager::ServerManager() - : server(80), preferencesManager(PreferencesManager::getInstance()), ledManager(LedManager::getInstance()), timeManager(TimeManager::getInstance()) -{ + : server(80), + preferencesManager(PreferencesManager::getInstance()), + ledManager(LedManager::getInstance()), + timeManager(TimeManager::getInstance()) { } -void ServerManager::begin() -{ +void ServerManager::begin() { Serial.begin(115200); - if (!SPIFFS.begin()) - { + if (!SPIFFS.begin()) { Serial.println("Error al montar el sistema de archivos"); return; } server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); - server.on("/colors", HTTP_GET, std::bind(&ServerManager::getColors, this, std::placeholders::_1)); - server.on("/timezone", HTTP_GET, std::bind(&ServerManager::getTimezone, this, std::placeholders::_1)); - server.on("/deep-sleep", HTTP_GET, std::bind(&ServerManager::getDeepSleep, this, std::placeholders::_1)); - - server.on("/colors", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, std::bind(&ServerManager::updateColors, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)); - server.on("/timezone", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, std::bind(&ServerManager::updateTimezone, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)); - server.on("/deep-sleep", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, std::bind(&ServerManager::updateDeepSleep, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)); + server.on("/colors", HTTP_GET, + std::bind(&ServerManager::getColors, this, std::placeholders::_1)); + server.on("/timezone", HTTP_GET, + std::bind(&ServerManager::getTimezone, this, std::placeholders::_1)); + server.on("/deep-sleep", HTTP_GET, + std::bind(&ServerManager::getDeepSleep, this, std::placeholders::_1)); + + server.on( + "/colors", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, + std::bind(&ServerManager::updateColors, this, std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)); + server.on( + "/timezone", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, + std::bind(&ServerManager::updateTimezone, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, + std::placeholders::_5)); + server.on( + "/deep-sleep", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL, + std::bind(&ServerManager::updateDeepSleep, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, + std::placeholders::_5)); server.begin(); Serial.println("Servidor inicializado."); } -AsyncWebServer *ServerManager::getServer() -{ +AsyncWebServer *ServerManager::getServer() { return &server; } -void ServerManager::handleRoot(AsyncWebServerRequest *request) -{ +void ServerManager::handleRoot(AsyncWebServerRequest *request) { serveFile("/index.html", "text/html", request); } -void ServerManager::handleCSS(AsyncWebServerRequest *request) -{ +void ServerManager::handleCSS(AsyncWebServerRequest *request) { serveFile("/style.css", "text/css", request); } -void ServerManager::handleJS(AsyncWebServerRequest *request) -{ +void ServerManager::handleJS(AsyncWebServerRequest *request) { serveFile("/main.js", "text/javascript", request); } -void ServerManager::getColors(AsyncWebServerRequest *request) -{ +void ServerManager::getColors(AsyncWebServerRequest *request) { Serial.println(String("[") + request->methodToString() + "] " + request->url()); JsonDocument colorData = createColorJson(); @@ -66,26 +73,21 @@ void ServerManager::getColors(AsyncWebServerRequest *request) request->send(200, MIME_APPLICATION_JSON, jsonString); } -void ServerManager::updateColors(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) -{ - +void ServerManager::updateColors(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) - { + if (error) { request->send(400); return; } - if ( - body.containsKey("hour_led1_color") && body.containsKey("hour_led2_color") && + if (body.containsKey("hour_led1_color") && body.containsKey("hour_led2_color") && body.containsKey("dots_led1_color") && body.containsKey("dots_led2_color") && - body.containsKey("minutes_led1_color") && body.containsKey("minutes_led2_color")) - { - + body.containsKey("minutes_led1_color") && body.containsKey("minutes_led2_color")) { preferencesManager.setHourLed1Color(stringToHex(body["hour_led1_color"])); preferencesManager.setHourLed2Color(stringToHex(body["hour_led2_color"])); preferencesManager.setDotsLed1Color(stringToHex(body["dots_led1_color"])); @@ -102,8 +104,7 @@ void ServerManager::updateColors(AsyncWebServerRequest *request, uint8_t *data, request->send(400); } -void ServerManager::getTimezone(AsyncWebServerRequest *request) -{ +void ServerManager::getTimezone(AsyncWebServerRequest *request) { Serial.println(String("[") + request->methodToString() + "] " + request->url()); JsonDocument timezoneData; @@ -115,26 +116,22 @@ void ServerManager::getTimezone(AsyncWebServerRequest *request) request->send(200, MIME_APPLICATION_JSON, jsonString); } -void ServerManager::updateTimezone(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) -{ - +void ServerManager::updateTimezone(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) - { + if (error) { request->send(400); return; } - if (body.containsKey("timeOffset")) - { - + if (body.containsKey("timeOffset")) { int timeOffset = body["timeOffset"].as(); preferencesManager.setTimeOffset(timeOffset); - timeManager.setTimeOffset(timeOffset * 3600); + timeManager.setTimeOffset(timeOffset); request->send(204); return; @@ -143,8 +140,7 @@ void ServerManager::updateTimezone(AsyncWebServerRequest *request, uint8_t *data request->send(400); } -void ServerManager::getDeepSleep(AsyncWebServerRequest *request) -{ +void ServerManager::getDeepSleep(AsyncWebServerRequest *request) { Serial.println(String("[") + request->methodToString() + "] " + request->url()); JsonDocument deepSleepData; @@ -157,24 +153,19 @@ void ServerManager::getDeepSleep(AsyncWebServerRequest *request) request->send(200, MIME_APPLICATION_JSON, jsonString); } -void ServerManager::updateDeepSleep(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) -{ - +void ServerManager::updateDeepSleep(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) - { + if (error) { request->send(400); return; } - if ( - body.containsKey("start_timestamp") && body.containsKey("end_timestamp")) - { - + if (body.containsKey("start_timestamp") && body.containsKey("end_timestamp")) { preferencesManager.setStartTimestamp(body["start_timestamp"].as()); preferencesManager.setEndTimestamp(body["end_timestamp"].as()); @@ -185,8 +176,7 @@ void ServerManager::updateDeepSleep(AsyncWebServerRequest *request, uint8_t *dat request->send(400); } -JsonDocument ServerManager::createColorJson() -{ +JsonDocument ServerManager::createColorJson() { JsonDocument colorData; colorData["hour_led1_color"] = hexToString(preferencesManager.getHourLed1Color()); @@ -199,24 +189,20 @@ JsonDocument ServerManager::createColorJson() return colorData; } -uint32_t ServerManager::stringToHex(const String &hexColorString) -{ +uint32_t ServerManager::stringToHex(const String &hexColorString) { String hexWithoutPrefix = hexColorString.substring(1); uint32_t hexColor = strtoul(hexWithoutPrefix.c_str(), NULL, 16); return hexColor; } -String ServerManager::hexToString(uint32_t hexColor) -{ +String ServerManager::hexToString(uint32_t hexColor) { String hexColorString = String(hexColor, HEX); return "#" + hexColorString; } -String ServerManager::getBodyContent(uint8_t *data, size_t len) -{ +String ServerManager::getBodyContent(uint8_t *data, size_t len) { String content = ""; - for (size_t i = 0; i < len; i++) - { + for (size_t i = 0; i < len; i++) { content.concat((char)data[i]); } return content; diff --git a/src/TimeManager.cpp b/src/TimeManager.cpp index 7d24fb9..f320252 100644 --- a/src/TimeManager.cpp +++ b/src/TimeManager.cpp @@ -1,56 +1,47 @@ #include "TimeManager.h" -TimeManager &TimeManager::getInstance() -{ +TimeManager &TimeManager::getInstance() { static TimeManager instance; return instance; } -TimeManager::TimeManager() - : preferencesManager(PreferencesManager::getInstance()) -{ +TimeManager::TimeManager() : preferencesManager(PreferencesManager::getInstance()) { } -void TimeManager::begin() -{ +void TimeManager::begin() { Serial.begin(115200); - if (timeClient == nullptr) - { + if (timeClient == nullptr) { timeClient = new NTPClient(ntpUDP, "pool.ntp.org"); Serial.println("Cliente NTP inicializado."); } - int timeOffset = preferencesManager.getTimeOffset(); - timeClient->setTimeOffset(timeOffset); + timeClient->begin(); + setTimeOffset(preferencesManager.getTimeOffset()); + Serial.println("Time Manager inicializado."); } -void TimeManager::update() -{ +void TimeManager::update() { timeClient->update(); } -int TimeManager::getHours() -{ +int TimeManager::getHours() { Serial.println("Get Hours"); return timeClient->getHours(); } -int TimeManager::getMinutes() -{ +int TimeManager::getMinutes() { Serial.println("Get Minutes"); return timeClient->getMinutes(); } -void TimeManager::setTimeOffset(int offset) -{ +void TimeManager::setTimeOffset(int offset) { Serial.println("Change Time Offset"); - timeClient->setTimeOffset(offset); + timeClient->setTimeOffset(offset * 3600); } -String TimeManager::getFormattedTime() -{ +String TimeManager::getFormattedTime() { return timeClient->getFormattedTime(); } \ No newline at end of file