utilities.h 2.48 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#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.
51
    //! If _tickInerval <= _timerInterval*_tickCount the counter gets reset and the function returns true. If the condition is not
Valentin Platzgummer's avatar
Valentin Platzgummer committed
52 53 54 55 56
    //! met the function returns false.
    //!
    bool ready(void) {
        assert(_isInitialized == true);
        _tickCount++;
57
        bool ready = _tickInerval <= _timerInterval*_tickCount;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
58 59 60 61 62 63 64 65 66 67 68 69 70
        if (ready)
            _tickCount = 0;
        return ready;
    }

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

#endif // UTILITIES_H