Adds timing macro

This commit is contained in:
Rune Harlyk
2024-07-14 23:14:07 +02:00
committed by Rune Harlyk
parent c783793b5c
commit 200ea62d95
11 changed files with 41 additions and 62 deletions
@@ -47,12 +47,7 @@ void APSettingsService::recoveryMode() {
} }
void APSettingsService::loop() { void APSettingsService::loop() {
unsigned long currentMillis = millis(); EXECUTE_EVERY_N_MS(MANAGE_NETWORK_DELAY, manageAP());
unsigned long manageElapsed = (currentMillis - _lastManaged);
if (manageElapsed >= MANAGE_NETWORK_DELAY) {
_lastManaged = currentMillis;
manageAP();
}
handleDNS(); handleDNS();
} }
@@ -20,6 +20,7 @@
#include <FSPersistence.h> #include <FSPersistence.h>
#include <JsonUtils.h> #include <JsonUtils.h>
#include <WiFi.h> #include <WiFi.h>
#include <Timing.h>
#include <DNSServer.h> #include <DNSServer.h>
#include <IPAddress.h> #include <IPAddress.h>
+2 -9
View File
@@ -18,6 +18,7 @@
#include <EventSocket.h> #include <EventSocket.h>
#include <TaskManager.h> #include <TaskManager.h>
#include <WiFi.h> #include <WiFi.h>
#include <Timing.h>
#define MAX_ESP_ANALYTICS_SIZE 2024 #define MAX_ESP_ANALYTICS_SIZE 2024
#define EVENT_ANALYTICS "analytics" #define EVENT_ANALYTICS "analytics"
@@ -29,21 +30,13 @@ class AnalyticsService {
void begin() {}; void begin() {};
void loop() { void loop() { EXECUTE_EVERY_N_MS(ANALYTICS_INTERVAL, updateAnalytics()); };
unsigned long currentMillis = millis();
if (!_lastUpdate || (currentMillis - _lastUpdate) >= ANALYTICS_INTERVAL) {
_lastUpdate = currentMillis;
updateAnalytics();
}
};
JsonDocument doc; JsonDocument doc;
char message[MAX_ESP_ANALYTICS_SIZE]; char message[MAX_ESP_ANALYTICS_SIZE];
private: private:
EventSocket *_socket; EventSocket *_socket;
TaskManager *_taskManager; TaskManager *_taskManager;
unsigned long _lastUpdate;
void updateAnalytics() { void updateAnalytics() {
doc.clear(); doc.clear();
+3 -12
View File
@@ -17,6 +17,7 @@
#include <EventSocket.h> #include <EventSocket.h>
#include <JsonUtils.h> #include <JsonUtils.h>
#include <Peripherals.h> #include <Peripherals.h>
#include <Timing.h>
#define ADC_VOLTAGE 0 #define ADC_VOLTAGE 0
#define ADC_CURRENT 1 #define ADC_CURRENT 1
@@ -40,16 +41,8 @@ class BatteryService {
void begin(); void begin();
void loop() { void loop() {
unsigned long currentMillis = millis(); EXECUTE_EVERY_N_MS(BATTERY_CHECK_INTERVAL, updateBattery());
EXECUTE_EVERY_N_MS(BATTERY_INTERVAL, batteryEvent());
if (!_lastUpdate || (currentMillis - _lastUpdate) >= BATTERY_CHECK_INTERVAL) {
_lastUpdate = currentMillis;
updateBattery();
}
if (!_lastEmit || (currentMillis - _lastEmit) >= BATTERY_INTERVAL) {
_lastEmit = currentMillis;
batteryEvent();
}
} }
void updateBattery() { void updateBattery() {
@@ -67,8 +60,6 @@ class BatteryService {
EventSocket *_socket; EventSocket *_socket;
Peripherals *_peripherals; Peripherals *_peripherals;
unsigned long _lastUpdate;
unsigned long _lastEmit;
float _voltage = 0; float _voltage = 0;
float _current = 0; float _current = 0;
}; };
+2 -4
View File
@@ -19,8 +19,6 @@ class LEDService {
private: private:
TaskManager *_taskManager; TaskManager *_taskManager;
unsigned long _lastUpdate = 0;
CRGB leds[WS2812_NUM_LEDS]; CRGB leds[WS2812_NUM_LEDS];
CRGBPalette16 currentPalette; CRGBPalette16 currentPalette;
TBlendType currentBlending; TBlendType currentBlending;
@@ -39,13 +37,13 @@ class LEDService {
void begin() {} void begin() {}
void loop() { void loop() {
if (millis() - _lastUpdate < 1000 / 60) return; EXECUTE_EVERY_N_MS(1000 / 60, {
if (_brightness >= 200) direction = -5; if (_brightness >= 200) direction = -5;
if (_brightness <= 50) direction = 5; if (_brightness <= 50) direction = 5;
_brightness += direction; _brightness += direction;
fillFromPallette(0); fillFromPallette(0);
FastLED.show(); FastLED.show();
_lastUpdate = millis(); });
} }
void fillFromPallette(uint8_t colorIndex) { void fillFromPallette(uint8_t colorIndex) {
+3 -3
View File
@@ -4,6 +4,7 @@
#include <EventSocket.h> #include <EventSocket.h>
#include <TaskManager.h> #include <TaskManager.h>
#include <Kinematics.h> #include <Kinematics.h>
#include <Timing.h>
#include <MathUtils.h> #include <MathUtils.h>
#define DEFAULT_STATE false #define DEFAULT_STATE false
@@ -120,10 +121,9 @@ class MotionService {
} }
void loop() { void loop() {
if (int currentMillis = millis(); !_lastUpdate || (currentMillis - _lastUpdate) >= MotionInterval) { EXECUTE_EVERY_N_MS(MotionInterval, {
_lastUpdate = currentMillis;
if (updateMotion()) syncAngles(); if (updateMotion()) syncAngles();
} });
} }
private: private:
+2 -7
View File
@@ -7,6 +7,7 @@
#include <SecurityManager.h> #include <SecurityManager.h>
#include <StatefulService.h> #include <StatefulService.h>
#include <MathUtils.h> #include <MathUtils.h>
#include <Timing.h>
#include <list> #include <list>
#include <SPI.h> #include <SPI.h>
@@ -169,12 +170,7 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
}; };
void loop() { void loop() {
unsigned long currentMillis = millis(); EXECUTE_EVERY_N_MS(_updateInterval, updateImu());
if (currentMillis - _lastUpdate >= _updateInterval) {
_lastUpdate = currentMillis;
readIMU();
}
} }
void updatePins() { void updatePins() {
@@ -407,7 +403,6 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
std::list<uint8_t> addressList; std::list<uint8_t> addressList;
bool i2c_active = false; bool i2c_active = false;
unsigned long _lastUpdate {0};
unsigned long _updateInterval {I2C_INTERVAL}; unsigned long _updateInterval {I2C_INTERVAL};
}; };
+1 -5
View File
@@ -210,10 +210,7 @@ class ServoController : public Adafruit_PWMServoDriver, public StatefulService<S
} }
void loop() { void loop() {
if (int currentMillis = millis(); !_lastUpdate || (currentMillis - _lastUpdate) >= ServoInterval) { EXECUTE_EVERY_N_MS(ServoInterval, { updateServoState(); });
_lastUpdate = currentMillis;
updateServoState();
}
} }
private: private:
@@ -223,6 +220,5 @@ class ServoController : public Adafruit_PWMServoDriver, public StatefulService<S
FSPersistence<ServoConfiguration> _fsPersistence; FSPersistence<ServoConfiguration> _fsPersistence;
bool is_active {true}; bool is_active {true};
unsigned long _lastUpdate;
constexpr static int ServoInterval = 2; constexpr static int ServoInterval = 2;
}; };
+18
View File
@@ -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
@@ -60,16 +60,8 @@ void WiFiSettingsService::reconfigureWiFiConnection() {
} }
void WiFiSettingsService::loop() { void WiFiSettingsService::loop() {
unsigned long currentMillis = millis(); EXECUTE_EVERY_N_MS(WIFI_RECONNECTION_DELAY, manageSTA());
if (!_lastConnectionAttempt || (unsigned long)(currentMillis - _lastConnectionAttempt) >= WIFI_RECONNECTION_DELAY) { EXECUTE_EVERY_N_MS(RSSI_EVENT_DELAY, updateRSSI());
_lastConnectionAttempt = currentMillis;
manageSTA();
}
if (!_lastRssiUpdate || (unsigned long)(currentMillis - _lastRssiUpdate) >= RSSI_EVENT_DELAY) {
_lastRssiUpdate = currentMillis;
updateRSSI();
}
} }
String WiFiSettingsService::getHostname() { return _state.hostname; } String WiFiSettingsService::getHostname() { return _state.hostname; }
@@ -27,6 +27,7 @@
#include <WiFi.h> #include <WiFi.h>
#include <WiFiMulti.h> #include <WiFiMulti.h>
#include <vector> #include <vector>
#include <Timing.h>
#ifndef FACTORY_WIFI_SSID #ifndef FACTORY_WIFI_SSID
#define FACTORY_WIFI_SSID "" #define FACTORY_WIFI_SSID ""
@@ -203,7 +204,6 @@ class WiFiSettingsService : public StatefulService<WiFiSettings> {
FSPersistence<WiFiSettings> _fsPersistence; FSPersistence<WiFiSettings> _fsPersistence;
EventSocket *_socket; EventSocket *_socket;
unsigned long _lastConnectionAttempt; unsigned long _lastConnectionAttempt;
unsigned long _lastRssiUpdate;
bool _stopping; bool _stopping;
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info); void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);