COSA
An Object-Oriented Platform for Arduino Programming
Clock.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_CLOCK_HH
22 #define COSA_CLOCK_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/Job.hh"
26 
33 class Clock : public Job::Scheduler {
34 public:
38  Clock() :
39  Job::Scheduler(),
40  m_msec(0),
41  m_sec(0UL),
42  m_cal(0)
43  {}
44 
51  virtual uint32_t time()
52  {
53  uint32_t res;
54  synchronized res = m_sec;
55  return (res);
56  }
57 
64  void time(uint32_t sec)
65  {
66  synchronized {
67  m_msec = 0;
68  m_sec = sec;
69  }
70  }
71 
77  uint32_t await()
78  {
79  uint32_t start = time();
80  uint32_t now;
81  while (1) {
82  now = time();
83  if (now != start) return (now);
84  yield();
85  }
86  }
87 
94  void tick(uint16_t ms)
95  {
96  synchronized {
97  m_msec += ms;
98  if (m_msec >= 1000) {
99  do {
100  m_msec -= 1000 + m_cal;
101  m_sec += 1;
102  } while (m_msec >= 1000);
103  dispatch();
104  }
105  }
106  }
107 
114  void calibration(int16_t ms)
115  {
116  synchronized m_cal = ms;
117  }
118 
124  int16_t calibration()
125  {
126  int16_t res;
127  synchronized res = m_cal;
128  return (res);
129  }
130 
136  void adjust(int16_t ms)
137  {
138  synchronized m_cal += ms;
139  }
140 
141 protected:
142  int16_t m_msec;
143  uint32_t m_sec;
144  int16_t m_cal;
145 };
146 #endif
Clock()
Definition: Clock.hh:38
Definition: Clock.hh:33
uint32_t await()
Definition: Clock.hh:77
Definition: Job.hh:33
virtual uint32_t time()
Definition: Clock.hh:51
void time(uint32_t sec)
Definition: Clock.hh:64
int16_t m_cal
Milli-seconds calibration.
Definition: Clock.hh:144
int16_t calibration()
Definition: Clock.hh:124
int16_t m_msec
Milli-seconds fraction.
Definition: Clock.hh:142
void adjust(int16_t ms)
Definition: Clock.hh:136
Scheduler()
Definition: Job.hh:48
void(* yield)()
void tick(uint16_t ms)
Definition: Clock.hh:94
uint32_t m_sec
Seconds counter.
Definition: Clock.hh:143
void calibration(int16_t ms)
Definition: Clock.hh:114
virtual void dispatch()
virtual bool start(Job *job)