LCOV - code coverage report
Current view: top level - src/AH/Timing - MillisMicrosTimer.hpp (source / functions) Hit Total Coverage
Test: 90a1b9beff85a60dc6ebcea034a947a845e56960 Lines: 11 11 100.0 %
Date: 2019-11-30 15:53:32 Functions: 6 6 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 <Arduino.h> // millis, micros
       8             : AH_DIAGNOSTIC_POP()
       9             : 
      10             : #include <AH/Settings/NamespaceSettings.hpp>
      11             : 
      12             : BEGIN_AH_NAMESPACE
      13             : 
      14             : /// A function type that returns a time value.
      15             : using timefunction = unsigned long (*)();
      16             : 
      17             : /// @addtogroup    AH_Timing
      18             : /// @{
      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           6 :     Timer(unsigned long interval) : interval(interval) {
      36             : #ifdef ARDUINO
      37             :         begin();
      38             : #endif
      39           6 :     }
      40             :     /// Initialize the timer.
      41           2 :     void begin() { previous = time() - interval; }
      42             :     /// Update the timer and return true if the event should fire.
      43          20 :     explicit operator bool() {
      44          20 :         auto now = time();
      45          20 :         if (now - previous >= interval) {
      46           8 :             previous += interval;
      47           8 :             return true;
      48             :         }
      49          12 :         return false;
      50          20 :     }
      51             : 
      52             :   private:
      53             :     const unsigned long interval;
      54           6 :     unsigned long previous = 0;
      55             : };
      56             : 
      57             : /// @}
      58             : 
      59             : END_AH_NAMESPACE
      60             : 
      61             : AH_DIAGNOSTIC_POP()

Generated by: LCOV version 1.14-5-g4ff2ed6