Refactored code, unit tests added, GA activated.

This commit is contained in:
2025-03-07 20:54:10 +01:00
parent 1c870374cd
commit a653f0e3d6
9 changed files with 344 additions and 95 deletions

View File

@ -1,74 +1,82 @@
#include "interval.h"
// Public Methods //////////////////////////////////////////////////////////////
uint32_t Interval::remains() const
{
return _timeout - (millis() - _timefrom);
// Returns the remaining time until expiration, correctly handling overflow
uint32_t Interval::remains() const {
uint32_t currentTime = millis();
return _timeout - (currentTime - _timefrom);
}
uint32_t Interval::elapsed() const
{
// Returns the elapsed time since the interval started
uint32_t Interval::elapsed() const {
return millis() - _timefrom;
}
bool Interval::expired()
{
bool result = false;
// Checks if the interval has expired based on the mode
bool Interval::expired() {
uint32_t currentTime = millis();
uint32_t elapsedTime = currentTime - _timefrom;
if (_done) {
if (1 == _mode) {
// oneshot mode
if ((millis() - _timefrom) >= _timeout) {
_done = 0;
result = true;
switch (_mode) {
case ONESHOT:
// In oneshot mode, return true only once upon expiration
if (elapsedTime >= _timeout) {
if (_active) {
_active = false;
return true;
}
}
} else if (2 == _mode) {
// periodic mode
if ((millis() - _timefrom) >= _timeout) {
result = true;
_timefrom = millis();
break;
case PERIODIC:
// In periodic mode, return true and reset the timer on expiration
if (elapsedTime >= _timeout) {
_timefrom += _timeout;
if (currentTime - _timefrom >= _timeout) {
_timefrom = currentTime;
}
return true;
}
} else {
// compatibility mode
if ((millis() - _timefrom) >= _timeout)
result = true;
}
break;
case COMPATIBILITY:
default:
// In compatibility mode, always return true if the interval has expired
if (_active) {
if (elapsedTime >= _timeout) {
_active = false;
return true;
}
} else {
return true; // already expired
}
break;
}
return result;
return false;
}
void Interval::set(uint32_t tmout)
{
// Sets the interval in compatibility mode
void Interval::set(const uint32_t tmout) {
_timefrom = millis();
_timeout = tmout;
_mode = 0;
_done = 0xff;
_mode = COMPATIBILITY;
_active = true;
}
void Interval::setOneshot(uint32_t tmout)
{
// Sets the interval in oneshot mode
void Interval::setOneshot(const uint32_t tmout) {
_timefrom = millis();
_timeout = tmout;
_mode = 1;
_done = 0xff;
_mode = ONESHOT;
_active = true;
}
void Interval::setPeriodic(uint32_t tmout)
{
// Sets the interval in periodic mode
void Interval::setPeriodic(const uint32_t tmout) {
_timefrom = millis();
_timeout = tmout;
_mode = 2;
_done = 0xff;
_mode = PERIODIC;
}
void Interval::reload()
{
// Reloads the interval timer (resets the starting time)
void Interval::reload() {
_timefrom = millis();
_done = 0xff;
_active = true;
}