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() {
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();
}
@@ -20,6 +20,7 @@
#include <FSPersistence.h>
#include <JsonUtils.h>
#include <WiFi.h>
#include <Timing.h>
#include <DNSServer.h>
#include <IPAddress.h>
+2 -9
View File
@@ -18,6 +18,7 @@
#include <EventSocket.h>
#include <TaskManager.h>
#include <WiFi.h>
#include <Timing.h>
#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();
+3 -12
View File
@@ -17,6 +17,7 @@
#include <EventSocket.h>
#include <JsonUtils.h>
#include <Peripherals.h>
#include <Timing.h>
#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;
};
+7 -9
View File
@@ -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) {
+3 -3
View File
@@ -4,6 +4,7 @@
#include <EventSocket.h>
#include <TaskManager.h>
#include <Kinematics.h>
#include <Timing.h>
#include <MathUtils.h>
#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:
+2 -7
View File
@@ -7,6 +7,7 @@
#include <SecurityManager.h>
#include <StatefulService.h>
#include <MathUtils.h>
#include <Timing.h>
#include <list>
#include <SPI.h>
@@ -169,12 +170,7 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
};
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<PeripheralsConfiguration> {
std::list<uint8_t> addressList;
bool i2c_active = false;
unsigned long _lastUpdate {0};
unsigned long _updateInterval {I2C_INTERVAL};
};
+1 -5
View File
@@ -210,10 +210,7 @@ class ServoController : public Adafruit_PWMServoDriver, public StatefulService<S
}
void loop() {
if (int currentMillis = millis(); !_lastUpdate || (currentMillis - _lastUpdate) >= ServoInterval) {
_lastUpdate = currentMillis;
updateServoState();
}
EXECUTE_EVERY_N_MS(ServoInterval, { updateServoState(); });
}
private:
@@ -223,6 +220,5 @@ class ServoController : public Adafruit_PWMServoDriver, public StatefulService<S
FSPersistence<ServoConfiguration> _fsPersistence;
bool is_active {true};
unsigned long _lastUpdate;
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() {
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; }
@@ -27,6 +27,7 @@
#include <WiFi.h>
#include <WiFiMulti.h>
#include <vector>
#include <Timing.h>
#ifndef FACTORY_WIFI_SSID
#define FACTORY_WIFI_SSID ""
@@ -203,7 +204,6 @@ class WiFiSettingsService : public StatefulService<WiFiSettings> {
FSPersistence<WiFiSettings> _fsPersistence;
EventSocket *_socket;
unsigned long _lastConnectionAttempt;
unsigned long _lastRssiUpdate;
bool _stopping;
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);