LCOV - code coverage report
Current view: top level - src/AH/Timing - MillisMicrosTimer.hpp (source / functions) Hit Total Coverage
Test: ffed98f648fe78e7aa7bdd228474317d40dadbec Lines: 11 11 100.0 %
Date: 2022-05-28 15:22:59 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <AH/Settings/Warnings.hpp>
       4             : AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
       5             : 
       6             : AH_DIAGNOSTIC_EXTERNAL_HEADER()
       7             : #include <AH/Arduino-Wrapper.h> // millis, micros
       8             : AH_DIAGNOSTIC_POP()
       9             : 
      10             : #include <AH/Settings/NamespaceSettings.hpp>
      11             : 
      12             : BEGIN_AH_NAMESPACE
      13             : 
      14             : /// @addtogroup    AH_Timing
      15             : /// @{
      16             : 
      17             : /// A function type that returns a time value.
      18             : using timefunction = unsigned long (*)();
      19             : 
      20             : /**
      21             :  * @brief   A class for easily managing timed events. A wrapper for "Blink 
      22             :  *          Without Delay".
      23             :  * 
      24             :  * @tparam  time
      25             :  *          The time function to use.
      26             :  */
      27             : template <timefunction time = micros>
      28             : class Timer {
      29             :   public:
      30             :     /**
      31             :      * @brief   Constructor.
      32             :      * @param   interval
      33             :      *          The interval between two events.
      34             :      */
      35         598 :     Timer(unsigned long interval) : interval(interval) {
      36             : #ifdef ARDUINO
      37             :         begin();
      38             : #endif
      39         598 :     }
      40             :     /// Initialize or reset the timer. The timer will fire immediately.
      41           2 :     void begin() { previous = time() - interval; }
      42             :     /// Initialize or reset the timer. The timer will fire after one period.
      43           6 :     void beginNextPeriod() { previous = time(); }
      44             :     /// Update the timer and return true if the event should fire.
      45          23 :     explicit operator bool() {
      46          23 :         auto now = time();
      47          23 :         if (now - previous >= interval) {
      48           9 :             previous += interval;
      49           9 :             return true;
      50             :         }
      51          14 :         return false;
      52             :     }
      53             : 
      54             :     /// Get the interval of the timer.
      55           3 :     unsigned long getInterval() const { return interval; }
      56             :     /// Set the interval of the timer.
      57             :     void setInterval(unsigned long interval) { this->interval = interval; }
      58             : 
      59             :   private:
      60             :     unsigned long interval;
      61             :     unsigned long previous = 0;
      62             : };
      63             : 
      64             : /// @}
      65             : 
      66             : END_AH_NAMESPACE
      67             : 
      68             : AH_DIAGNOSTIC_POP()

Generated by: LCOV version 1.15