snooze_pos.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Platform Check
10 *****************************************************************************/
11 
12 #include <ecl/config.hpp>
13 #if defined(ECL_IS_POSIX)
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
20 #include "../../include/ecl/time/snooze_pos.hpp"
21 
22 /*****************************************************************************
23 ** Namespaces
24 *****************************************************************************/
25 
26 namespace ecl {
27 
28 /*****************************************************************************
29 ** Implementation [Snooze]
30 *****************************************************************************/
31 
32 Snooze::Snooze() {
33  period(Duration(0,10000000));
34 }
35 
36 Snooze::Snooze(const Duration &time, const bool& validate) { period(time, validate); }
37 
38 void Snooze::period(const Duration &time, const bool& validate) {
39  validate_times = validate;
40  time_period.tv_sec = time.sec();
41  time_period.tv_nsec = time.nsec();
42  wrap_value_ns = 1000000000L-time_period.tv_nsec;
43  initialise();
44 }
45 
46 void Snooze::initialise() {
47  epoch_time(time_value);
48 }
49 
50 void Snooze::operator()() {
51 
52  add_period();
53  validate();
54  sleep_until(time_value);
55 }
56 
57 void Snooze::add_period() {
58 
59  // Nanoseconds
60  if ( time_value.tv_nsec > wrap_value_ns ) { // wrap
61  time_value.tv_nsec = time_value.tv_nsec - wrap_value_ns;
62  time_value.tv_sec += 1;
63  } else {
64  time_value.tv_nsec += time_period.tv_nsec;
65  }
66  // Seconds
67  time_value.tv_sec += time_period.tv_sec;
68 
69  if ( validate_times ) {
70  validate();
71  }
72 }
73 
74 void Snooze::validate() {
75 
76  TimeStructure time_current;
77  epoch_time(time_current);
78 
79  if ( time_current.tv_sec > time_value.tv_sec ) {
80  // Revalidate
81  time_value.tv_sec = time_current.tv_sec;
82  time_value.tv_nsec = time_current.tv_nsec;
83  add_period();
84  } else if ( time_current.tv_sec == time_value.tv_sec ) {
85  if ( time_current.tv_nsec > time_value.tv_nsec ) {
86  // Revalidate
87  time_value.tv_sec = time_current.tv_sec;
88  time_value.tv_nsec = time_current.tv_nsec;
89  add_period();
90  }
91  }
92 }
93 
94 } // namespace ecl
95 
96 #endif /* ECL_IS_POSIX */
Embedded control libraries.
TimeStamp Duration
Convenience typedef to associate timestamps with the concept of durations.
Definition: duration.hpp:41


ecl_time
Author(s): Daniel Stonier
autogenerated on Mon Feb 28 2022 22:18:29