COSA
An Object-Oriented Platform for Arduino Programming
Watchdog.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_WATCHDOG_HH
22 #define COSA_WATCHDOG_HH
23 
24 #include <avr/wdt.h>
25 
26 #include "Cosa/Types.h"
27 #include "Cosa/Job.hh"
28 #include "Cosa/Clock.hh"
29 
35 class Watchdog {
36 public:
41  static bool is_initiated()
42  {
43  return (s_initiated);
44  }
45 
51  static uint32_t millis()
52  __attribute__((always_inline))
53  {
54  uint32_t res;
55  synchronized res = s_millis;
56  return (res);
57  }
58 
64  static void millis(uint32_t ms)
65  {
66  synchronized s_millis = ms;
67  }
68 
73  static uint16_t ms_per_tick()
74  {
75  return (s_ms_per_tick);
76  }
77 
84  static void begin(uint16_t ms = 16);
85 
91  static void delay(uint32_t ms);
92 
96  static void await()
97  __attribute__((always_inline))
98  {
99  delay(0);
100  }
101 
108  static uint32_t since(uint32_t start)
109  __attribute__((always_inline))
110  {
111  uint32_t now = millis();
112  return (now - start);
113  }
114 
119  static void end()
120  {
121  wdt_disable();
122  s_initiated = false;
123  }
124 
129  class Scheduler : public Job::Scheduler {
130  public:
136  {
137  Watchdog::s_scheduler = this;
138  }
139 
146  virtual uint32_t time()
147  {
148  return (Watchdog::millis());
149  }
150  };
151 
159  {
160  synchronized s_scheduler = scheduler;
161  }
162 
168  {
169  return (s_scheduler);
170  }
171 
175  class Clock : public ::Clock {
176  public:
181  Clock() : ::Clock()
182  {
183  Watchdog::s_clock = this;
184  }
185  };
186 
192  static void wall(Clock* clock)
193  {
194  synchronized s_clock = clock;
195  }
196 
201  static Clock* clock()
202  {
203  return (s_clock);
204  }
205 
206 private:
207  static bool s_initiated;
208  static uint32_t s_millis;
209  static uint16_t s_ms_per_tick;
210  static Event::Handler* s_handler;
211  static Scheduler* s_scheduler;
212  static Clock* s_clock;
213 
217  Watchdog() {}
218 
224  static uint8_t as_prescale(uint16_t ms);
225 
227  friend void WDT_vect(void);
228 };
229 
230 #endif
static uint32_t millis()
Definition: Watchdog.hh:51
static uint16_t ms_per_tick()
Definition: Watchdog.hh:73
static uint32_t since(uint32_t start)
Definition: Watchdog.hh:108
Definition: Job.hh:33
friend void WDT_vect(void)
static void await()
Definition: Watchdog.hh:96
static void millis(uint32_t ms)
Definition: Watchdog.hh:64
static void begin(uint16_t ms=16)
Definition: Watchdog.cpp:49
static Watchdog::Scheduler * scheduler()
Definition: Watchdog.hh:167
static void wall(Clock *clock)
Definition: Watchdog.hh:192
static Clock * clock()
Definition: Watchdog.hh:201
virtual uint32_t time()
Definition: Watchdog.hh:146
static void end()
Definition: Watchdog.hh:119
static void job(Watchdog::Scheduler *scheduler)
Definition: Watchdog.hh:158
static bool is_initiated()
Definition: Watchdog.hh:41
static void delay(uint32_t ms)
Definition: Watchdog.cpp:73