Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
clock.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 18.06.2013 15:54:46 EDT
3 // File: clock.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __CLOCK_HEADER
12 #define __CLOCK_HEADER
13 
14 #include <Arduino.h>
15 
16 namespace tool {
17  class clock_class {
18  public:
19  //------------------- ctors -------------------
20  clock_class(): zero_ref_(0), us_now_(0), last_us_(0) {
21  }
22  //------------------- getter -------------------
23  uint32_t sec() {
24  return (us_now_ - zero_ref_) / 1000000.0;
25  }
26  uint16_t msec(uint16_t const & mod = 1000) {
27  return ((us_now_ - zero_ref_) / 1000) % mod;
28  }
29  uint16_t usec(uint16_t const & mod) {
30  return (us_now_ - zero_ref_) % mod;
31  }
32  uint64_t micros() const {
33  return us_now_ - zero_ref_;
34  }
35  uint32_t millis() const {
36  return (us_now_ - zero_ref_) / 1000.0;
37  }
38  //------------------- update -------------------
39  void update() {
40  uint32_t us = ::micros();
41  us_now_ += uint32_t(us - last_us_); //works if there is an overflow
42  last_us_ = us;
43  }
44  //------------------- ops -------------------
45  void reset() {
46  zero_ref_ = us_now_;
47  }
48  private:
49  uint64_t zero_ref_;
50  uint64_t us_now_;
51  uint32_t last_us_;
52  } clock;
53 }//end namespace tool
54 
55 #endif //__CLOCK_HEADER
Definition: clock.hpp:17
class tool::clock_class clock
uint64_t micros() const
Definition: clock.hpp:32
void reset()
Definition: clock.hpp:45
clock_class()
Definition: clock.hpp:20
uint32_t millis() const
Definition: clock.hpp:35
uint16_t msec(uint16_t const &mod=1000)
Definition: clock.hpp:26
uint32_t sec()
Definition: clock.hpp:23
void update()
Definition: clock.hpp:39
uint16_t usec(uint16_t const &mod)
Definition: clock.hpp:29