COSA
An Object-Oriented Platform for Arduino Programming
Periodic.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_PERIODIC_HH
22 #define COSA_PERIODIC_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/Job.hh"
26 
38 class Periodic : public Job {
39 public:
48  Periodic(Job::Scheduler* scheduler, uint32_t period) :
49  Job(scheduler),
50  m_period(period)
51  {}
52 
57  void period(uint32_t time)
58  __attribute__((always_inline))
59  {
60  m_period = time;
61  }
62 
67  uint32_t period() const
68  __attribute__((always_inline))
69  {
70  return (m_period);
71  }
72 
76  void reschedule()
77  {
78  if (m_period == 0) return;
80  start();
81  }
82 
83 protected:
91  virtual void on_event(uint8_t type, uint16_t value)
92  {
93  UNUSED(value);
94  if (UNLIKELY(type != Event::TIMEOUT_TYPE)) return;
95  run();
96  reschedule();
97  }
98 
100  uint32_t m_period;
101 };
102 
121 #define periodic(timer,ms) \
122  static uint32_t timer = RTT::millis(); \
123  for (int __UNIQUE(i) = 1; \
124  (__UNIQUE(i) != 0) && ((RTT::since(timer)) >= ms); \
125  __UNIQUE(i)--, timer += ms)
126 
145 #define PERIODIC(timer,us) \
146  static uint32_t timer = RTT::micros(); \
147  for (int __UNIQUE(i) = 1; \
148  (__UNIQUE(i) != 0) && ((RTT::micros() - timer) >= us); \
149  __UNIQUE(i)--, timer += us)
150 
151 #endif
virtual void on_event(uint8_t type, uint16_t value)
Definition: Periodic.hh:91
void reschedule()
Definition: Periodic.hh:76
int32_t expire_after() const
Definition: Job.hh:129
Definition: Job.hh:33
uint32_t period() const
Definition: Periodic.hh:67
Periodic(Job::Scheduler *scheduler, uint32_t period)
Definition: Periodic.hh:48
virtual void run()
Definition: Job.hh:210
void period(uint32_t time)
Definition: Periodic.hh:57
uint32_t time() const
Definition: Job.hh:139
#define UNUSED(x)
Definition: ATmega328P.hh:31
bool start()
Definition: Job.hh:159
#define UNLIKELY(x)
Definition: Types.h:153
uint32_t m_period
Definition: Periodic.hh:100