#pragma once

#include <Arduino.h>

// Generic class to handle intervals
template <unsigned long interval>
class OnEveryN {
public:
    OnEveryN() : lastTime(0) {}

    // Check if the interval has elapsed
    bool ready() {
        unsigned long currentTime = millis();
        if (currentTime - lastTime >= interval) {
            lastTime = currentTime;
            return true;
        }
        return false;
    }

private:
    unsigned long lastTime; // Stores the last execution time
};

// Helper macros to generate unique names
#define CONCATENATE_DETAIL(x, y) x##y
#define CONCATENATE(x, y) CONCATENATE_DETAIL(x, y)
#define UNIQUE_NAME(base) CONCATENATE(base, __LINE__)

// Macro for ON_EVERY_N_MILLISECONDS
#define ON_EVERY_N_MILLISECONDS(N) \
    static OnEveryN<N> UNIQUE_NAME(__on_everyN_); \
    if (UNIQUE_NAME(__on_everyN_).ready())

// Macro for ON_EVERY_N_SECONDS
#define ON_EVERY_N_SECONDS(N) ON_EVERY_N_MILLISECONDS((N) * 1000)

