Skip to content

File RBCXTimers.h

File List > demo-projets > esp > RBCXTimers.h

Go to the documentation of this file.

#pragma once

#include <esp_timer.h>

#include <memory>
#include <mutex>
#include <vector>

namespace rb {

class Manager;
class Timers;

class Timers {
public:
    static constexpr uint16_t INVALID_ID = 0;

    static void deleteFreeRtOsTimerTask();

    static Timers& get();

    uint16_t schedule(uint32_t period_ms, std::function<bool()> callback);

    bool reset(uint16_t id, uint32_t period_ms);
    bool cancel(uint16_t id);
    bool stop(uint16_t id);

    // returns true if the calling code is running in the timer task.
    bool isOnTimerTask() const;

private:
    struct timer_t {
        std::function<bool()> callback;
        esp_timer_handle_t handle;
        uint16_t id;

        void swap(timer_t& o) {
            callback.swap(o.callback);
            std::swap(handle, o.handle);
            std::swap(id, o.id);
        }
    };

    static void timerCallback(void* handleVoid);

    Timers();
    ~Timers();

    void cancelByIdxLocked(size_t idx);
    uint16_t getFreeIdLocked();

    std::vector<timer_t> m_timers;
    std::recursive_mutex m_mutex;
    uint16_t m_id_counter;
};

};