diff --git a/esp32/lib/ESP32-sveltekit/APSettingsService.cpp b/esp32/lib/ESP32-sveltekit/APSettingsService.cpp index 0726f9c..f33ee74 100644 --- a/esp32/lib/ESP32-sveltekit/APSettingsService.cpp +++ b/esp32/lib/ESP32-sveltekit/APSettingsService.cpp @@ -47,12 +47,7 @@ void APSettingsService::recoveryMode() { } void APSettingsService::loop() { - unsigned long currentMillis = millis(); - unsigned long manageElapsed = (currentMillis - _lastManaged); - if (manageElapsed >= MANAGE_NETWORK_DELAY) { - _lastManaged = currentMillis; - manageAP(); - } + EXECUTE_EVERY_N_MS(MANAGE_NETWORK_DELAY, manageAP()); handleDNS(); } diff --git a/esp32/lib/ESP32-sveltekit/APSettingsService.h b/esp32/lib/ESP32-sveltekit/APSettingsService.h index d8e4e28..e5edddf 100644 --- a/esp32/lib/ESP32-sveltekit/APSettingsService.h +++ b/esp32/lib/ESP32-sveltekit/APSettingsService.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include diff --git a/esp32/lib/ESP32-sveltekit/AnalyticsService.h b/esp32/lib/ESP32-sveltekit/AnalyticsService.h index c2ef31b..2f9b754 100644 --- a/esp32/lib/ESP32-sveltekit/AnalyticsService.h +++ b/esp32/lib/ESP32-sveltekit/AnalyticsService.h @@ -18,6 +18,7 @@ #include #include #include +#include #define MAX_ESP_ANALYTICS_SIZE 2024 #define EVENT_ANALYTICS "analytics" @@ -29,21 +30,13 @@ class AnalyticsService { void begin() {}; - void loop() { - unsigned long currentMillis = millis(); - - if (!_lastUpdate || (currentMillis - _lastUpdate) >= ANALYTICS_INTERVAL) { - _lastUpdate = currentMillis; - updateAnalytics(); - } - }; + void loop() { EXECUTE_EVERY_N_MS(ANALYTICS_INTERVAL, updateAnalytics()); }; JsonDocument doc; char message[MAX_ESP_ANALYTICS_SIZE]; private: EventSocket *_socket; TaskManager *_taskManager; - unsigned long _lastUpdate; void updateAnalytics() { doc.clear(); diff --git a/esp32/lib/ESP32-sveltekit/BatteryService.h b/esp32/lib/ESP32-sveltekit/BatteryService.h index 3dfc48e..1605a11 100644 --- a/esp32/lib/ESP32-sveltekit/BatteryService.h +++ b/esp32/lib/ESP32-sveltekit/BatteryService.h @@ -17,6 +17,7 @@ #include #include #include +#include #define ADC_VOLTAGE 0 #define ADC_CURRENT 1 @@ -40,16 +41,8 @@ class BatteryService { void begin(); void loop() { - unsigned long currentMillis = millis(); - - if (!_lastUpdate || (currentMillis - _lastUpdate) >= BATTERY_CHECK_INTERVAL) { - _lastUpdate = currentMillis; - updateBattery(); - } - if (!_lastEmit || (currentMillis - _lastEmit) >= BATTERY_INTERVAL) { - _lastEmit = currentMillis; - batteryEvent(); - } + EXECUTE_EVERY_N_MS(BATTERY_CHECK_INTERVAL, updateBattery()); + EXECUTE_EVERY_N_MS(BATTERY_INTERVAL, batteryEvent()); } void updateBattery() { @@ -67,8 +60,6 @@ class BatteryService { EventSocket *_socket; Peripherals *_peripherals; - unsigned long _lastUpdate; - unsigned long _lastEmit; float _voltage = 0; float _current = 0; }; diff --git a/esp32/lib/ESP32-sveltekit/LEDService.h b/esp32/lib/ESP32-sveltekit/LEDService.h index d35378e..4306f27 100644 --- a/esp32/lib/ESP32-sveltekit/LEDService.h +++ b/esp32/lib/ESP32-sveltekit/LEDService.h @@ -19,8 +19,6 @@ class LEDService { private: TaskManager *_taskManager; - unsigned long _lastUpdate = 0; - CRGB leds[WS2812_NUM_LEDS]; CRGBPalette16 currentPalette; TBlendType currentBlending; @@ -39,13 +37,13 @@ class LEDService { void begin() {} void loop() { - if (millis() - _lastUpdate < 1000 / 60) return; - if (_brightness >= 200) direction = -5; - if (_brightness <= 50) direction = 5; - _brightness += direction; - fillFromPallette(0); - FastLED.show(); - _lastUpdate = millis(); + EXECUTE_EVERY_N_MS(1000 / 60, { + if (_brightness >= 200) direction = -5; + if (_brightness <= 50) direction = 5; + _brightness += direction; + fillFromPallette(0); + FastLED.show(); + }); } void fillFromPallette(uint8_t colorIndex) { diff --git a/esp32/lib/ESP32-sveltekit/MotionService.h b/esp32/lib/ESP32-sveltekit/MotionService.h index ae7a4f1..a31b78f 100644 --- a/esp32/lib/ESP32-sveltekit/MotionService.h +++ b/esp32/lib/ESP32-sveltekit/MotionService.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #define DEFAULT_STATE false @@ -120,10 +121,9 @@ class MotionService { } void loop() { - if (int currentMillis = millis(); !_lastUpdate || (currentMillis - _lastUpdate) >= MotionInterval) { - _lastUpdate = currentMillis; + EXECUTE_EVERY_N_MS(MotionInterval, { if (updateMotion()) syncAngles(); - } + }); } private: diff --git a/esp32/lib/ESP32-sveltekit/Peripherals.h b/esp32/lib/ESP32-sveltekit/Peripherals.h index 95fccfc..c1465fc 100644 --- a/esp32/lib/ESP32-sveltekit/Peripherals.h +++ b/esp32/lib/ESP32-sveltekit/Peripherals.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -169,12 +170,7 @@ class Peripherals : public StatefulService { }; void loop() { - unsigned long currentMillis = millis(); - - if (currentMillis - _lastUpdate >= _updateInterval) { - _lastUpdate = currentMillis; - readIMU(); - } + EXECUTE_EVERY_N_MS(_updateInterval, updateImu()); } void updatePins() { @@ -407,7 +403,6 @@ class Peripherals : public StatefulService { std::list addressList; bool i2c_active = false; - unsigned long _lastUpdate {0}; unsigned long _updateInterval {I2C_INTERVAL}; }; diff --git a/esp32/lib/ESP32-sveltekit/ServoController.h b/esp32/lib/ESP32-sveltekit/ServoController.h index d2f5b72..bf795c6 100644 --- a/esp32/lib/ESP32-sveltekit/ServoController.h +++ b/esp32/lib/ESP32-sveltekit/ServoController.h @@ -210,10 +210,7 @@ class ServoController : public Adafruit_PWMServoDriver, public StatefulService= ServoInterval) { - _lastUpdate = currentMillis; - updateServoState(); - } + EXECUTE_EVERY_N_MS(ServoInterval, { updateServoState(); }); } private: @@ -223,6 +220,5 @@ class ServoController : public Adafruit_PWMServoDriver, public StatefulService _fsPersistence; bool is_active {true}; - unsigned long _lastUpdate; constexpr static int ServoInterval = 2; }; diff --git a/esp32/lib/ESP32-sveltekit/Timing.h b/esp32/lib/ESP32-sveltekit/Timing.h new file mode 100644 index 0000000..9cfaa84 --- /dev/null +++ b/esp32/lib/ESP32-sveltekit/Timing.h @@ -0,0 +1,18 @@ +#ifndef TIMING_H +#define TIMING_H + +#define CONCAT(a, b) a##b + +#define UNIQUE_VAR(base) CONCAT(base, __LINE__) + +#define EXECUTE_EVERY_N_MS(n, code) \ + do { \ + static volatile unsigned long UNIQUE_VAR(lastExecution_) = ULONG_MAX; \ + unsigned long currentMillis = millis(); \ + if (currentMillis - UNIQUE_VAR(lastExecution_) >= n) { \ + code; \ + UNIQUE_VAR(lastExecution_) = currentMillis; \ + } \ + } while (0) + +#endif \ No newline at end of file diff --git a/esp32/lib/ESP32-sveltekit/WiFiSettingsService.cpp b/esp32/lib/ESP32-sveltekit/WiFiSettingsService.cpp index c845bef..535a817 100644 --- a/esp32/lib/ESP32-sveltekit/WiFiSettingsService.cpp +++ b/esp32/lib/ESP32-sveltekit/WiFiSettingsService.cpp @@ -60,16 +60,8 @@ void WiFiSettingsService::reconfigureWiFiConnection() { } void WiFiSettingsService::loop() { - unsigned long currentMillis = millis(); - if (!_lastConnectionAttempt || (unsigned long)(currentMillis - _lastConnectionAttempt) >= WIFI_RECONNECTION_DELAY) { - _lastConnectionAttempt = currentMillis; - manageSTA(); - } - - if (!_lastRssiUpdate || (unsigned long)(currentMillis - _lastRssiUpdate) >= RSSI_EVENT_DELAY) { - _lastRssiUpdate = currentMillis; - updateRSSI(); - } + EXECUTE_EVERY_N_MS(WIFI_RECONNECTION_DELAY, manageSTA()); + EXECUTE_EVERY_N_MS(RSSI_EVENT_DELAY, updateRSSI()); } String WiFiSettingsService::getHostname() { return _state.hostname; } diff --git a/esp32/lib/ESP32-sveltekit/WiFiSettingsService.h b/esp32/lib/ESP32-sveltekit/WiFiSettingsService.h index ef3952f..aa7082b 100644 --- a/esp32/lib/ESP32-sveltekit/WiFiSettingsService.h +++ b/esp32/lib/ESP32-sveltekit/WiFiSettingsService.h @@ -27,6 +27,7 @@ #include #include #include +#include #ifndef FACTORY_WIFI_SSID #define FACTORY_WIFI_SSID "" @@ -203,7 +204,6 @@ class WiFiSettingsService : public StatefulService { FSPersistence _fsPersistence; EventSocket *_socket; unsigned long _lastConnectionAttempt; - unsigned long _lastRssiUpdate; bool _stopping; void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);