Files
flix/flix/time.ino
Oleg Kalachev c8e5e08b03 Move all global variable declarations to the appropriate subsystems
As it makes the subsystems code easier to understand.
Declare the most used variables in main sketch file as forward declarations.
Make all control input zero by default (except controlMode).
Minor changes.
2026-01-03 13:28:18 +03:00

32 lines
618 B
C++

// Copyright (c) 2023 Oleg Kalachev <okalachev@gmail.com>
// Repository: https://github.com/okalachev/flix
// Time related functions
float t = NAN; // current time, s
float dt; // time delta with the previous step, s
float loopRate; // Hz
void step() {
float now = micros() / 1000000.0;
dt = now - t;
t = now;
if (!(dt > 0)) {
dt = 0; // assume dt to be zero on first step and on reset
}
computeLoopRate();
}
void computeLoopRate() {
static float windowStart = 0;
static uint32_t rate = 0;
rate++;
if (t - windowStart >= 1) { // 1 second window
loopRate = rate;
windowStart = t;
rate = 0;
}
}