#ifndef UTILITIES_H
#define UTILITIES_H

#include <assert.h>

//!
//! \brief The EventTicker class is a helper class used to determine if a certain event is ready to be executed.
//!
//! The EventTicker class is a helper class used to determine if a certain event is ready to be executed. This class supposed to be used in combination with a Timer provided by the user.
//!
class EventTicker{
public:

    //!
    //! \brief EventTicker Constructor used to build non initilized Object. Call \fn init() befor use.
    //!
    EventTicker() : _isInitialized(false) {
        _isInitialized = false;
    }

    //!
    //! \brief EventTicker Constructor used for initilization. The function \fn must not be called if this constructor is used.
    //! \param timerInterval The timer interval of the timer provided by the user.
    //! \param tickInterval The thick intervall (timerInterval <= tickInterval), see \fn ready().
    //!
    EventTicker(double timerInterval, double tickInterval) {
        _isInitialized = false;
        init(timerInterval, tickInterval);
    }

    //!
    //! \brief init Used to init the EventTicker.
    //! \param timerInterval The timer interval of the timer provided by the user.
    //! \param tickInterval The thick intervall (timerInterval <= tickInterval), see \fn ready().
    //!
    void init(double timerInterval, double tickInterval){
        assert(_isInitialized == false);
        assert(timerInterval > 0);
        assert(tickInterval >= timerInterval);
        _timerInterval  = timerInterval;
        _tickInerval    = tickInterval;
        _tickCount      = 0;
        _isInitialized  = true;
    }

    //!
    //! \brief ready This function must be called by a user provided timer interrupt handler.
    //! \return True if the event is ready to be executed, false either.
    //!
    //! This function must be called by a user provided timer interrupt handler. A internal counter increases with every call.
    //! If _tickInerval <= _timerInterval*_tickCount the counter gets reset and the function returns true. If the condition is not
    //! met the function returns false.
    //!
    bool ready(void) {
        assert(_isInitialized == true);
        _tickCount++;
        bool ready = _tickInerval <= _timerInterval*_tickCount;
        if (ready)
            _tickCount = 0;
        return ready;
    }

private:
    double          _timerInterval;
    double          _tickInerval;
    unsigned long   _tickCount;
    bool            _isInitialized;
};

template <typename T>
struct Type2Type{
    typedef T OriginalType;
};


template <int k>
struct Int2Type{
    enum {value = k};
};

#endif // UTILITIES_H
