

#ifndef TG_TIMEDFUN_H
#define TG_TIMEDFUN_H

#include <map>
#include <stdint.h>
#include <Arduino.h>

typedef void (*timedfun_ptr)();

class TimedFun {
    public:
        TimedFun(){};

        void registerFun(timedfun_ptr p, uint64_t s){
            funmap[p] = s;
        };

        void registerFunCond(timedfun_ptr p, uint64_t s){
            funmap[p] = s;
        };

        void minuteTick(){
            ++counter;
        }

        void update(){
            for (const auto &e : funmap){
                if((counter % e.second == 0) ||
                  ((counter-old_counter) >= e.second) ||
                  ((counter%e.second) < (old_counter%e.second))){ 
                    e.first();
                }
            }
            old_counter = counter;
        };

        void updateForce(){
            for (const auto &e : funmap){
                e.first();
            }
        }

    private:
        uint64_t counter = 0;
        uint64_t old_counter = 0;
        std::map<timedfun_ptr,uint64_t> funmap;

};

#endif