Replace millis with esp timer

This commit is contained in:
Rune Harlyk
2025-10-09 17:49:36 +02:00
parent 12e1f80830
commit bc31b1b2dd
6 changed files with 32 additions and 26 deletions
+2 -1
View File
@@ -2,6 +2,7 @@
#define MotionService_h
#include <ArduinoJson.h>
#include "esp_timer.h"
#include <kinematics.h>
#include <peripherals/gesture.h>
@@ -61,7 +62,7 @@ class MotionService {
float dir[12] = {1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1};
unsigned long lastUpdate = millis();
int64_t lastUpdate = esp_timer_get_time();
};
#endif
+16 -14
View File
@@ -1,14 +1,16 @@
#ifndef TIMING_H
#define TIMING_H
#include "esp_timer.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_) = 0; \
unsigned long currentMillis = millis(); \
static volatile uint64_t UNIQUE_VAR(lastExecution_) = 0; \
uint64_t currentMillis = esp_timer_get_time() / 1000; \
if (UNIQUE_VAR(lastExecution_) == 0 || currentMillis - UNIQUE_VAR(lastExecution_) >= n) { \
code; \
UNIQUE_VAR(lastExecution_) = currentMillis; \
@@ -17,26 +19,26 @@
#define TIME_IT(code) \
{ \
uint32_t time_it_start = micros(); \
uint64_t time_it_start = esp_timer_get_time(); \
code; \
uint32_t time_it_elapsed = micros() - time_it_start; \
uint64_t time_it_elapsed = esp_timer_get_time() - time_it_start; \
if (time_it_elapsed < 1000) { \
ESP_LOGI("Time It", "Time elapsed: %lu microseconds", time_it_elapsed); \
ESP_LOGI("Time It", "Time elapsed: %llu microseconds", time_it_elapsed); \
} else if (time_it_elapsed < 1000000) { \
ESP_LOGI("Time It", "Time elapsed: %lu milliseconds", time_it_elapsed / 1000); \
ESP_LOGI("Time It", "Time elapsed: %llu milliseconds", time_it_elapsed / 1000); \
} else { \
ESP_LOGI("Time It", "Time elapsed: %.2f seconds", time_it_elapsed / 1000000.0); \
} \
}
#define CALLS_PER_SECOND(name) \
static unsigned long name##_count = 0; \
static unsigned long last_time = 0; \
name##_count++; \
if (millis() - last_time >= 1000) { \
Serial.printf("%s: %lu calls per second\n", #name, name##_count); \
name##_count = 0; \
last_time = millis(); \
#define CALLS_PER_SECOND(name) \
static uint64_t name##_count = 0; \
static uint64_t last_time = 0; \
name##_count++; \
if (esp_timer_get_time() / 1000 - last_time >= 1000) { \
ESP_LOGI("Calls", "%s: %llu calls per second", #name, name##_count); \
name##_count = 0; \
last_time = esp_timer_get_time() / 1000; \
}
#endif