LCOV - code coverage report
Current view: top level - src/Helpers - MillisMicrosTimer.hpp (source / functions) Hit Total Coverage
Test: 19d2efc7037c2e176feca44750a12594c76f466f Lines: 11 11 100.0 %
Date: 2019-11-24 14:50:27 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <Arduino.h>
       4             : #include <Settings/NamespaceSettings.hpp>
       5             : 
       6             : BEGIN_CS_NAMESPACE
       7             : 
       8             : /// A function type that returns a time value.
       9             : using timefunction = unsigned long (*)();
      10             : 
      11             : /// @addtogroup    Utilities
      12             : /// @{
      13             : 
      14             : /**
      15             :  * @brief   A class for easily managing timed events. A wrapper for "Blink 
      16             :  *          Without Delay".
      17             :  * 
      18             :  * @tparam time
      19             :  *         The time function to use.
      20             :  */
      21             : template <timefunction time = micros>
      22             : class Timer {
      23             :   public:
      24             :     /**
      25             :      * @brief   Constructor.
      26             :      * @param   interval
      27             :      *          The interval between two events.
      28             :      */
      29           6 :     Timer(unsigned long interval) : interval(interval) {
      30             : #ifdef ARDUINO
      31             :         begin();
      32             : #endif
      33           6 :     }
      34             :     /// Initialize the timer.
      35           2 :     void begin() { previous = time() - interval; }
      36             :     /// Update the timer and return true if the event should fire.
      37          20 :     explicit operator bool() {
      38          20 :         auto now = time();
      39          20 :         if (now - previous >= interval) {
      40           8 :             previous += interval;
      41           8 :             return true;
      42             :         }
      43          12 :         return false;
      44          20 :     }
      45             : 
      46             :   private:
      47             :     const unsigned long interval;
      48           6 :     unsigned long previous = 0;
      49             : };
      50             : 
      51             : /// @}
      52             : 
      53             : END_CS_NAMESPACE

Generated by: LCOV version 1.14-5-g4ff2ed6