Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 47 additions & 73 deletions src/NonBlockingTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,68 @@

#include <Arduino.h>

class NonBlockingTimer
{
class NonBlockingTimer {
private:
unsigned long previousMillis;
unsigned long interval;
bool isRunning;
bool autoReset;
unsigned long previousMillis;
unsigned long interval;
bool isRunning;
bool autoReset;

public:
NonBlockingTimer(unsigned long intervalMs = 1000, bool autoResetEnabled = true)
: previousMillis(0), interval(intervalMs), isRunning(false), autoReset(autoResetEnabled) {}
NonBlockingTimer(unsigned long intervalMs = 1000,
bool autoResetEnabled = true)
: previousMillis(0), interval(intervalMs), isRunning(false),
autoReset(autoResetEnabled) {}

void setInterval(unsigned long intervalMs)
{
interval = intervalMs;
}
void setInterval(unsigned long intervalMs) { interval = intervalMs; }

void start()
{
previousMillis = millis();
isRunning = true;
}
void start() {
previousMillis = millis();
isRunning = true;
}

void startExpired()
{
// Start timer in already-expired state for immediate first trigger
previousMillis = millis() - interval - 1;
isRunning = true;
}
void startExpired() {
// Start timer in already-expired state for immediate first trigger
previousMillis = millis() - interval - 1;
isRunning = true;
}

void stop()
{
isRunning = false;
}
void stop() { isRunning = false; }

void reset()
{
previousMillis = millis();
}
void reset() { previousMillis = millis(); }

bool check()
{
if (!isRunning)
return false;

unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
if (autoReset)
{
previousMillis = currentMillis;
}
else
{
isRunning = false;
}
return true;
}
return false;
}
bool check() {
if (!isRunning)
return false;

bool isExpired()
{
if (!isRunning)
return false;
return (millis() - previousMillis >= interval);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
if (autoReset) {
previousMillis = currentMillis;
} else {
isRunning = false;
}
return true;
}
return false;
}

unsigned long getElapsed()
{
return millis() - previousMillis;
}
bool isExpired() {
if (!isRunning)
return false;
return (millis() - previousMillis >= interval);
}

unsigned long getRemaining()
{
unsigned long elapsed = getElapsed();
return (elapsed >= interval) ? 0 : (interval - elapsed);
}
unsigned long getElapsed() { return millis() - previousMillis; }

bool getIsRunning() const
{
return isRunning;
}
unsigned long getRemaining() {
unsigned long elapsed = getElapsed();
return (elapsed >= interval) ? 0 : (interval - elapsed);
}

unsigned long getInterval() const
{
return interval;
}
bool getIsRunning() const { return isRunning; }

unsigned long getInterval() const { return interval; }
};

#endif
110 changes: 54 additions & 56 deletions src/StateManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,77 @@
#include <functional>

enum class SystemState {
INITIALIZING,
CONNECTING_WIFI,
CONNECTING_SERVER,
CONNECTED,
DISCONNECTED,
ERROR,
MAINTENANCE
INITIALIZING,
CONNECTING_WIFI,
CONNECTING_SERVER,
CONNECTED,
DISCONNECTED,
ERROR,
MAINTENANCE
};

class StateManager {
private:
SystemState currentState;
SystemState previousState;
unsigned long stateEnterTime;
std::function<void(SystemState, SystemState)> stateChangeCallback;
SystemState currentState;
SystemState previousState;
unsigned long stateEnterTime;
std::function<void(SystemState, SystemState)> stateChangeCallback;

public:
StateManager() : currentState(SystemState::INITIALIZING),
previousState(SystemState::INITIALIZING),
stateEnterTime(millis()) {}
StateManager()
: currentState(SystemState::INITIALIZING),
previousState(SystemState::INITIALIZING), stateEnterTime(millis()) {}

void setState(SystemState newState) {
if (newState != currentState) {
previousState = currentState;
currentState = newState;
stateEnterTime = millis();

if (stateChangeCallback) {
stateChangeCallback(previousState, currentState);
}
}
}
void setState(SystemState newState) {
if (newState != currentState) {
previousState = currentState;
currentState = newState;
stateEnterTime = millis();

SystemState getState() const {
return currentState;
if (stateChangeCallback) {
stateChangeCallback(previousState, currentState);
}
}
}

SystemState getPreviousState() const {
return previousState;
}
SystemState getState() const { return currentState; }

unsigned long getStateDuration() const {
return millis() - stateEnterTime;
}
SystemState getPreviousState() const { return previousState; }

bool isInState(SystemState state) const {
return currentState == state;
}
unsigned long getStateDuration() const { return millis() - stateEnterTime; }

bool hasStateTimedOut(unsigned long timeoutMs) const {
return getStateDuration() >= timeoutMs;
}
bool isInState(SystemState state) const { return currentState == state; }

void onStateChange(std::function<void(SystemState, SystemState)> callback) {
stateChangeCallback = callback;
}
bool hasStateTimedOut(unsigned long timeoutMs) const {
return getStateDuration() >= timeoutMs;
}

String stateToString(SystemState state) const {
switch (state) {
case SystemState::INITIALIZING: return "INITIALIZING";
case SystemState::CONNECTING_WIFI: return "CONNECTING_WIFI";
case SystemState::CONNECTING_SERVER: return "CONNECTING_SERVER";
case SystemState::CONNECTED: return "CONNECTED";
case SystemState::DISCONNECTED: return "DISCONNECTED";
case SystemState::ERROR: return "ERROR";
case SystemState::MAINTENANCE: return "MAINTENANCE";
default: return "UNKNOWN";
}
}
void onStateChange(std::function<void(SystemState, SystemState)> callback) {
stateChangeCallback = callback;
}

String getCurrentStateString() const {
return stateToString(currentState);
String stateToString(SystemState state) const {
switch (state) {
case SystemState::INITIALIZING:
return "INITIALIZING";
case SystemState::CONNECTING_WIFI:
return "CONNECTING_WIFI";
case SystemState::CONNECTING_SERVER:
return "CONNECTING_SERVER";
case SystemState::CONNECTED:
return "CONNECTED";
case SystemState::DISCONNECTED:
return "DISCONNECTED";
case SystemState::ERROR:
return "ERROR";
case SystemState::MAINTENANCE:
return "MAINTENANCE";
default:
return "UNKNOWN";
}
}

String getCurrentStateString() const { return stateToString(currentState); }
};

#endif
Loading