From 9a1d1cff3fe00b7765d18b2ed91321d741e3aa5c Mon Sep 17 00:00:00 2001 From: Jochen Niebuhr Date: Thu, 19 Dec 2024 15:01:59 +0100 Subject: [PATCH] chore: Reformat all files to c standard --- lib/NimBLEComm/src/NimBLEClientController.cpp | 4 ++- lib/NimBLEComm/src/NimBLEServerController.cpp | 12 +++++-- lib/OTA/src/GitHubOTA.cpp | 12 +++---- lib/OTA/src/GitHubOTA.h | 4 ++- lib/OTA/src/common.cpp | 4 +-- lib/OTA/src/common.h | 4 +-- scripts/format.sh | 3 ++ src/controller/main.cpp | 4 +-- src/controller/main.h | 18 +++++----- src/display/core/Controller.cpp | 34 +++++++++---------- src/display/core/Controller.h | 3 +- src/display/core/Event.h | 9 ++--- src/display/core/PluginManager.h | 16 ++++----- src/display/plugins/HomekitPlugin.cpp | 5 +-- src/display/plugins/WebUIPlugin.cpp | 3 +- src/display/plugins/WebUIPlugin.h | 7 ++-- src/display/plugins/mDNSPlugin.cpp | 3 +- src/display/plugins/mDNSPlugin.h | 2 +- 18 files changed, 75 insertions(+), 72 deletions(-) create mode 100755 scripts/format.sh diff --git a/lib/NimBLEComm/src/NimBLEClientController.cpp b/lib/NimBLEComm/src/NimBLEClientController.cpp index 8d5cb996..775c9aac 100644 --- a/lib/NimBLEComm/src/NimBLEClientController.cpp +++ b/lib/NimBLEComm/src/NimBLEClientController.cpp @@ -29,7 +29,9 @@ void NimBLEClientController::scan() { void NimBLEClientController::registerTempReadCallback(const temp_read_callback_t &callback) { tempReadCallback = callback; } -void NimBLEClientController::registerRemoteErrorCallback(const remote_err_callback_t &callback) { remoteErrorCallback = callback; } +void NimBLEClientController::registerRemoteErrorCallback(const remote_err_callback_t &callback) { + remoteErrorCallback = callback; +} bool NimBLEClientController::connectToServer() { Serial.println("Connecting to advertised device"); diff --git a/lib/NimBLEComm/src/NimBLEServerController.cpp b/lib/NimBLEComm/src/NimBLEServerController.cpp index b8180e87..989bb20b 100644 --- a/lib/NimBLEComm/src/NimBLEServerController.cpp +++ b/lib/NimBLEComm/src/NimBLEServerController.cpp @@ -78,11 +78,17 @@ void NimBLEServerController::sendError(int errorCode) { } } -void NimBLEServerController::registerTempControlCallback(const temp_control_callback_t &callback) { tempControlCallback = callback; } +void NimBLEServerController::registerTempControlCallback(const temp_control_callback_t &callback) { + tempControlCallback = callback; +} -void NimBLEServerController::registerPumpControlCallback(const pump_control_callback_t &callback) { pumpControlCallback = callback; } +void NimBLEServerController::registerPumpControlCallback(const pump_control_callback_t &callback) { + pumpControlCallback = callback; +} -void NimBLEServerController::registerValveControlCallback(const pin_control_callback_t &callback) { valveControlCallback = callback; } +void NimBLEServerController::registerValveControlCallback(const pin_control_callback_t &callback) { + valveControlCallback = callback; +} void NimBLEServerController::registerAltControlCallback(const pin_control_callback_t &callback) { altControlCallback = callback; } diff --git a/lib/OTA/src/GitHubOTA.cpp b/lib/OTA/src/GitHubOTA.cpp index b20c70ab..788de402 100644 --- a/lib/OTA/src/GitHubOTA.cpp +++ b/lib/OTA/src/GitHubOTA.cpp @@ -8,7 +8,8 @@ #include "semver_extensions.h" #include -GitHubOTA::GitHubOTA(const String &version, const String &release_url, const String &firmware_name, const String &filesystem_name) { +GitHubOTA::GitHubOTA(const String &version, const String &release_url, const String &firmware_name, + const String &filesystem_name) { ESP_LOGV("GitHubOTA", "GitHubOTA(version: %s, firmware_name: %s, fetch_url_via_redirect: %d)\n", version.c_str(), firmware_name.c_str(), fetch_url_via_redirect); @@ -44,10 +45,7 @@ String GitHubOTA::getCurrentVersion() const { return String(_latest_version.major) + "." + String(_latest_version.minor) + "." + _latest_version.patch; } - -bool GitHubOTA::isUpdateAvailable() const { - return update_required(_latest_version, _version); -} +bool GitHubOTA::isUpdateAvailable() const { return update_required(_latest_version, _version); } void GitHubOTA::update() { const char *TAG = "update"; @@ -76,9 +74,7 @@ void GitHubOTA::update() { ESP_LOGI(TAG, "No updates found\n"); } -void GitHubOTA::setReleaseUrl(const String& release_url) { - this->_release_url = release_url; -} +void GitHubOTA::setReleaseUrl(const String &release_url) { this->_release_url = release_url; } HTTPUpdateResult GitHubOTA::update_firmware(const String &url) { const char *TAG = "update_firmware"; diff --git a/lib/OTA/src/GitHubOTA.h b/lib/OTA/src/GitHubOTA.h index be3240ef..4b54a7e3 100644 --- a/lib/OTA/src/GitHubOTA.h +++ b/lib/OTA/src/GitHubOTA.h @@ -6,6 +6,8 @@ #include "semver.h" +enum class OTAPhase { DISPLAY_FIRMWARE, DISPLAY_FS, CONTROLLER_FS }; + extern const uint8_t x509_crt_imported_bundle_bin_start[] asm("_binary_x509_crt_bundle_start"); class GitHubOTA { @@ -17,7 +19,7 @@ class GitHubOTA { bool isUpdateAvailable() const; String getCurrentVersion() const; void update(); - void setReleaseUrl(const String& release_url); + void setReleaseUrl(const String &release_url); private: HTTPUpdate Updater; diff --git a/lib/OTA/src/common.cpp b/lib/OTA/src/common.cpp index 139ec1fc..ff338f7d 100644 --- a/lib/OTA/src/common.cpp +++ b/lib/OTA/src/common.cpp @@ -7,7 +7,7 @@ #include "semver_extensions.h" #include -String get_updated_base_url_via_redirect(WiFiClientSecure& wifi_client, String& release_url) { +String get_updated_base_url_via_redirect(WiFiClientSecure &wifi_client, String &release_url) { const char *TAG = "get_updated_base_url_via_redirect"; String location = get_redirect_location(wifi_client, release_url); @@ -26,7 +26,7 @@ String get_updated_base_url_via_redirect(WiFiClientSecure& wifi_client, String& return base_url; } -String get_redirect_location(WiFiClientSecure& wifi_client, String& initial_url) { +String get_redirect_location(WiFiClientSecure &wifi_client, String &initial_url) { const char *TAG = "get_redirect_location"; ESP_LOGV(TAG, "initial_url: %s\n", initial_url.c_str()); diff --git a/lib/OTA/src/common.h b/lib/OTA/src/common.h index a688d4bd..b8c0c167 100644 --- a/lib/OTA/src/common.h +++ b/lib/OTA/src/common.h @@ -9,8 +9,8 @@ using Updater = HTTPUpdate; -String get_updated_base_url_via_redirect(WiFiClientSecure& wifi_client, String& release_url); -String get_redirect_location(WiFiClientSecure& wifi_client, String& initial_url); +String get_updated_base_url_via_redirect(WiFiClientSecure &wifi_client, String &release_url); +String get_redirect_location(WiFiClientSecure &wifi_client, String &initial_url); void print_update_result(Updater updater, HTTPUpdateResult result, const char *TAG); diff --git a/scripts/format.sh b/scripts/format.sh new file mode 100755 index 00000000..d26a5871 --- /dev/null +++ b/scripts/format.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +find src lib \( -iname '*.h' -o -iname '*.c' -o -iname '*.cpp' \) ! -path './src/display/ui/**/*' ! -path './src/display/drivers/**/*' | xargs clang-format -i diff --git a/src/controller/main.cpp b/src/controller/main.cpp index 4182a36a..24382a35 100644 --- a/src/controller/main.cpp +++ b/src/controller/main.cpp @@ -161,9 +161,7 @@ void thermal_runaway_shutdown() { serverController.sendError(ERROR_CODE_RUNAWAY); } -void control_heater(int out) { - analogWrite(HEATER_PIN, out); -} +void control_heater(int out) { analogWrite(HEATER_PIN, out); } void control_pump() { unsigned long currentMillis = millis(); diff --git a/src/controller/main.h b/src/controller/main.h index 47bdb8dc..8cb1c85b 100644 --- a/src/controller/main.h +++ b/src/controller/main.h @@ -1,20 +1,20 @@ #ifndef MAIN_H #define MAIN_H +#include "NimBLEServerController.h" #include +#include #include #include #include -#include -#include "NimBLEServerController.h" // GPIO Pin Definitions -#define HEATER_PIN 14 -#define PUMP_PIN 9 -#define VALVE_PIN 10 -#define ALT_PIN 11 -#define MAX6675_SCK_PIN 6 -#define MAX6675_CS_PIN 7 +#define HEATER_PIN 14 +#define PUMP_PIN 9 +#define VALVE_PIN 10 +#define ALT_PIN 11 +#define MAX6675_SCK_PIN 6 +#define MAX6675_CS_PIN 7 #define MAX6675_MISO_PIN 4 constexpr size_t TEMP_UPDATE_INTERVAL_MS = 1000; @@ -48,4 +48,4 @@ void thermal_runaway_shutdown(void); void start_pid_autotune(void); void stop_pid_autotune(void); -#endif //MAIN_H +#endif // MAIN_H diff --git a/src/display/core/Controller.cpp b/src/display/core/Controller.cpp index 14468d4a..58f0f28f 100644 --- a/src/display/core/Controller.cpp +++ b/src/display/core/Controller.cpp @@ -6,9 +6,9 @@ #include "../plugins/mDNSPlugin.h" #include "../ui/ui.h" #include "constants.h" +#include #include #include -#include Controller::Controller() : timer(nullptr), mode(MODE_BREW), currentTemp(0), activeUntil(0), grindActiveUntil(0), lastPing(0), lastProgress(0), @@ -99,12 +99,8 @@ void Controller::setupWifi() { Serial.println(WiFi.localIP()); } - pluginManager->on("ota:update:start", [this](Event const &) { - this->updating = true; - }); - pluginManager->on("ota:update:end", [this](Event const &) { - this->updating = false; - }); + pluginManager->on("ota:update:start", [this](Event const &) { this->updating = true; }); + pluginManager->on("ota:update:end", [this](Event const &) { this->updating = false; }); pluginManager->trigger("controller:wifi:connect", "AP", isApConnection ? 1 : 0); } @@ -233,14 +229,14 @@ void Controller::updateUiActive() const { void Controller::updateUiSettings() { int16_t setTemp = getTargetTemp(); const int16_t angleRange = 3160; - double percentage = ((double)setTemp) / ((double) MAX_TEMP); - int16_t angle = (percentage * ((double) angleRange)) - angleRange / 2; - lv_img_set_angle(ui_BrewScreen_tempTarget,angle); - lv_img_set_angle(ui_StatusScreen_tempTarget,angle); - lv_img_set_angle(ui_MenuScreen_tempTarget,angle); - lv_img_set_angle(ui_SteamScreen_tempTarget,angle); - lv_img_set_angle(ui_WaterScreen_tempTarget,angle); - lv_img_set_angle(ui_GrindScreen_tempTarget,angle); + double percentage = ((double)setTemp) / ((double)MAX_TEMP); + int16_t angle = (percentage * ((double)angleRange)) - angleRange / 2; + lv_img_set_angle(ui_BrewScreen_tempTarget, angle); + lv_img_set_angle(ui_StatusScreen_tempTarget, angle); + lv_img_set_angle(ui_MenuScreen_tempTarget, angle); + lv_img_set_angle(ui_SteamScreen_tempTarget, angle); + lv_img_set_angle(ui_WaterScreen_tempTarget, angle); + lv_img_set_angle(ui_GrindScreen_tempTarget, angle); lv_label_set_text_fmt(ui_StatusScreen_targetTemp, "%d°C", settings.getTargetBrewTemp()); lv_label_set_text_fmt(ui_BrewScreen_targetTemp, "%d°C", settings.getTargetBrewTemp()); @@ -300,13 +296,15 @@ void Controller::updateStandby() { char time[6]; strftime(time, 6, "%H:%M", &timeinfo); lv_label_set_text(ui_StandbyScreen_time, time); - lv_obj_clear_flag(ui_StandbyScreen_time, LV_OBJ_FLAG_HIDDEN); + lv_obj_clear_flag(ui_StandbyScreen_time, LV_OBJ_FLAG_HIDDEN); } } else { lv_obj_add_flag(ui_StandbyScreen_time, LV_OBJ_FLAG_HIDDEN); } - clientController.isConnected() ? lv_obj_clear_flag(ui_StandbyScreen_bluetoothIcon, LV_OBJ_FLAG_HIDDEN) : lv_obj_add_flag(ui_StandbyScreen_bluetoothIcon, LV_OBJ_FLAG_HIDDEN); - !isApConnection && WiFi.status() == WL_CONNECTED ? lv_obj_clear_flag(ui_StandbyScreen_wifiIcon, LV_OBJ_FLAG_HIDDEN) : lv_obj_add_flag(ui_StandbyScreen_wifiIcon, LV_OBJ_FLAG_HIDDEN); + clientController.isConnected() ? lv_obj_clear_flag(ui_StandbyScreen_bluetoothIcon, LV_OBJ_FLAG_HIDDEN) + : lv_obj_add_flag(ui_StandbyScreen_bluetoothIcon, LV_OBJ_FLAG_HIDDEN); + !isApConnection &&WiFi.status() == WL_CONNECTED ? lv_obj_clear_flag(ui_StandbyScreen_wifiIcon, LV_OBJ_FLAG_HIDDEN) + : lv_obj_add_flag(ui_StandbyScreen_wifiIcon, LV_OBJ_FLAG_HIDDEN); } void Controller::activate() { diff --git a/src/display/core/Controller.h b/src/display/core/Controller.h index 770ce8ed..eeb83bc7 100644 --- a/src/display/core/Controller.h +++ b/src/display/core/Controller.h @@ -7,7 +7,6 @@ #include "Settings.h" #include - class Controller { public: Controller(); @@ -30,7 +29,7 @@ class Controller { bool isActive() const; bool isGrindActive() const; bool isUpdating() const; - Settings& getSettings() { return settings; } + Settings &getSettings() { return settings; } // Event callback methods void updateLastAction(); diff --git a/src/display/core/Event.h b/src/display/core/Event.h index 686ccc66..571393cd 100644 --- a/src/display/core/Event.h +++ b/src/display/core/Event.h @@ -15,14 +15,11 @@ struct EventDataEntry { EventDataEntry() = default; - EventDataEntry(const String &k, int value) - : key(k), type(EventDataType::EVENT_TYPE_INT), intValue(value) {} + EventDataEntry(const String &k, int value) : key(k), type(EventDataType::EVENT_TYPE_INT), intValue(value) {} - EventDataEntry(const String &k, float value) - : key(k), type(EventDataType::EVENT_TYPE_FLOAT), floatValue(value) {} + EventDataEntry(const String &k, float value) : key(k), type(EventDataType::EVENT_TYPE_FLOAT), floatValue(value) {} - EventDataEntry(const String &k, const String &value) - : key(k), type(EventDataType::EVENT_TYPE_STRING), stringValue(value) {} + EventDataEntry(const String &k, const String &value) : key(k), type(EventDataType::EVENT_TYPE_STRING), stringValue(value) {} }; using EventData = std::vector; diff --git a/src/display/core/PluginManager.h b/src/display/core/PluginManager.h index 4fb1a854..b5d6ae38 100644 --- a/src/display/core/PluginManager.h +++ b/src/display/core/PluginManager.h @@ -4,9 +4,9 @@ #include "Plugin.h" #include -#include -#include #include +#include +#include using EventCallback = std::function; @@ -18,17 +18,17 @@ class PluginManager { void setup(Controller *controller); void loop(); - void on(const String& eventId, const EventCallback &callback); + void on(const String &eventId, const EventCallback &callback); - Event trigger(const String& eventId); - Event trigger(const String& eventId, const String& key, const String &value); - Event trigger(const String& eventId, const String& key, int value); - Event trigger(const String& eventId, const String& key, float value); + Event trigger(const String &eventId); + Event trigger(const String &eventId, const String &key, const String &value); + Event trigger(const String &eventId, const String &key, int value); + Event trigger(const String &eventId, const String &key, float value); void trigger(Event &event); private: bool initialized = false; - std::vector plugins; + std::vector plugins; std::map> listeners = {}; }; diff --git a/src/display/plugins/HomekitPlugin.cpp b/src/display/plugins/HomekitPlugin.cpp index 679f51ef..e6abcbfb 100644 --- a/src/display/plugins/HomekitPlugin.cpp +++ b/src/display/plugins/HomekitPlugin.cpp @@ -55,9 +55,10 @@ void HomekitPlugin::clearAction() { actionRequired = false; } void HomekitPlugin::setup(Controller *controller, PluginManager *pluginManager) { this->controller = controller; - pluginManager->on("controller:wifi:connect", [this](Event & event) { + pluginManager->on("controller:wifi:connect", [this](Event &event) { int apMode = event.getInt("AP"); - if (apMode) return; + if (apMode) + return; homeSpan.setHostNameSuffix(""); homeSpan.setPortNum(HOMESPAN_PORT); homeSpan.begin(Category::Thermostats, DEVICE_NAME, this->controller->getSettings().getMdnsName().c_str()); diff --git a/src/display/plugins/WebUIPlugin.cpp b/src/display/plugins/WebUIPlugin.cpp index cf99633d..ca5ed170 100644 --- a/src/display/plugins/WebUIPlugin.cpp +++ b/src/display/plugins/WebUIPlugin.cpp @@ -1,9 +1,9 @@ #include "WebUIPlugin.h" #include "../core/Controller.h" +#include "../ui/ui.h" #include #include #include -#include "../ui/ui.h" WebUIPlugin::WebUIPlugin() : server(80) {} @@ -47,7 +47,6 @@ void WebUIPlugin::start() { Serial.print("OTA server started"); } - void WebUIPlugin::handleOTA(AsyncWebServerRequest *request) { if (request->method() == HTTP_POST) { diff --git a/src/display/plugins/WebUIPlugin.h b/src/display/plugins/WebUIPlugin.h index a3adfd3d..a51dfa69 100644 --- a/src/display/plugins/WebUIPlugin.h +++ b/src/display/plugins/WebUIPlugin.h @@ -4,8 +4,8 @@ #define ELEGANTOTA_USE_ASYNC_WEBSERVER 1 #include "../core/Plugin.h" -#include #include "GitHubOTA.h" +#include constexpr size_t UPDATE_CHECK_INTERVAL = 5 * 60 * 1000; @@ -16,10 +16,11 @@ class WebUIPlugin : public Plugin { WebUIPlugin(); void setup(Controller *controller, PluginManager *pluginManager) override; void loop() override; + private: void start(); - String processTemplate(const String& var); - String processOTATemplate(const String& var); + String processTemplate(const String &var); + String processOTATemplate(const String &var); void handleOTA(AsyncWebServerRequest *request); void handleSettings(AsyncWebServerRequest *request); diff --git a/src/display/plugins/mDNSPlugin.cpp b/src/display/plugins/mDNSPlugin.cpp index 21a1199d..a67d6566 100644 --- a/src/display/plugins/mDNSPlugin.cpp +++ b/src/display/plugins/mDNSPlugin.cpp @@ -10,7 +10,8 @@ void mDNSPlugin::setup(Controller *controller, PluginManager *pluginManager) { } void mDNSPlugin::start(Event const &event) const { const int apMode = event.getInt("AP"); - if (apMode) return; + if (apMode) + return; if (!MDNS.begin(controller->getSettings().getMdnsName().c_str())) { Serial.println("Error setting up MDNS responder!"); } diff --git a/src/display/plugins/mDNSPlugin.h b/src/display/plugins/mDNSPlugin.h index a5618c18..e36c7b2c 100644 --- a/src/display/plugins/mDNSPlugin.h +++ b/src/display/plugins/mDNSPlugin.h @@ -10,7 +10,7 @@ class mDNSPlugin : public Plugin { void loop() override {}; private: - void start(Event const & event) const; + void start(Event const &event) const; Controller *controller = nullptr; };