utilities.h 2.53 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
#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.
24
    //! \param targetTime The thick intervall (timerInterval <= targetTime), see \fn ready().
Valentin Platzgummer's avatar
Valentin Platzgummer committed
25
    //!
26
    EventTicker(double timerInterval, double targetTime) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
27
        _isInitialized = false;
28
        init(timerInterval, targetTime);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
29 30 31 32 33
    }

    //!
    //! \brief init Used to init the EventTicker.
    //! \param timerInterval The timer interval of the timer provided by the user.
34
    //! \param targetTime The thick intervall (timerInterval <= targetTime), see \fn ready().
Valentin Platzgummer's avatar
Valentin Platzgummer committed
35
    //!
36
    void init(double timerInterval, double targetTime){
Valentin Platzgummer's avatar
Valentin Platzgummer committed
37 38
        assert(_isInitialized == false);
        assert(timerInterval > 0);
39
        assert(targetTime >= timerInterval);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
40
        _timerInterval  = timerInterval;
41 42
        _targetTime    = targetTime;
        _ticks      = 0;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
43 44 45 46 47 48 49 50
        _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 _targetTime <= _timerInterval*_ticks 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
    //! met the function returns false.
    //!
    bool ready(void) {
        assert(_isInitialized == true);
56 57
        _ticks++;
        bool ready = _targetTime <= _timerInterval*_ticks;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
58
        if (ready)
59
            _ticks = 0;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
60 61 62
        return ready;
    }

63 64 65 66 67 68
    //!
    //! \brief Resets the tick count.
    void reset(void) {
        _ticks = 0;
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
69 70
private:
    double          _timerInterval;
71 72
    double          _targetTime;
    unsigned long   _ticks;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
73 74 75 76
    bool            _isInitialized;
};

#endif // UTILITIES_H