Advertisement
ind03

Timeout

Sep 30th, 2017
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. // TimeOut.h by Heiko Indenbirken
  2.  
  3. #ifndef _TIMEOUT_h
  4. #define _TIMEOUT_h
  5.  
  6. #include "arduino.h"
  7.  
  8.  
  9. class TimeOut
  10. {
  11.     unsigned ms;
  12.     unsigned long overflow;
  13.  
  14. public:
  15.     TimeOut (unsigned ms);
  16.  
  17.     inline operator bool () const { return millis() >= overflow; };
  18.  
  19.     void reset ();
  20.     void reset (unsigned ms);
  21.  
  22.     bool hasOverflow ();
  23.  
  24. private:
  25.     // No copy possible.
  26.     TimeOut (const TimeOut &c);
  27. };
  28.  
  29. #endif
  30.  
  31. //
  32. // TimeOut class by Heiko Indenbirken
  33. //
  34.  
  35. #include "TimeOut.h"
  36.  
  37. TimeOut::TimeOut (unsigned _ms)
  38.     : ms (_ms), overflow (millis()+ms)
  39. {
  40. }
  41.  
  42. bool TimeOut::hasOverflow ()
  43. {
  44.     if (*this)
  45.     {
  46.         reset ();
  47.         return true;
  48.     }
  49.  
  50.     return false;
  51. }
  52.  
  53. void TimeOut::reset ()
  54. {
  55.     overflow = millis () + ms;
  56. }
  57.  
  58. void TimeOut::reset (unsigned _ms)
  59. {
  60.     ms = _ms;
  61.     overflow = millis () + ms;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement