COSA
An Object-Oriented Platform for Arduino Programming
HCSR04.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_HCSR04_HH
22 #define COSA_HCSR04_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/InputPin.hh"
26 #include "Cosa/OutputPin.hh"
27 #include "Cosa/Periodic.hh"
28 #include "Cosa/Watchdog.hh"
29 
52 class HCSR04 : public Periodic {
53 public:
59  HCSR04(Job::Scheduler* scheduler,
60  Board::DigitalPin trigger,
61  Board::DigitalPin echo) :
62  Periodic(scheduler, 250),
63  m_trigger(trigger),
64  m_echo(echo),
65  m_distance(0)
66  {}
67 
72  uint16_t distance() const
73  {
74  return (m_distance);
75  }
76 
83  bool read(uint16_t& distance);
84 
90  void schedule(uint16_t ms)
91  __attribute__((always_inline))
92  {
93  stop();
94  period(ms);
95  expire_after(ms);
96  start();
97  }
98 
105  virtual void on_change(uint16_t distance)
106  {
107  UNUSED(distance);
108  }
109 
110 private:
112  static const uint16_t TIMEOUT = 0xffffU;
113 
115  static const uint16_t COUNT_PER_DM = (555 * I_CPU) / 16;
116 
118  OutputPin m_trigger;
119 
121  InputPin m_echo;
122 
124  uint16_t m_distance;
125 
133  virtual void on_event(uint8_t type, uint16_t value);
134 };
135 
136 #endif
int32_t expire_after() const
Definition: Job.hh:129
virtual void on_change(uint16_t distance)
Definition: HCSR04.hh:105
bool read(uint16_t &distance)
Definition: HCSR04.cpp:24
Definition: HCSR04.hh:52
uint32_t period() const
Definition: Periodic.hh:67
#define UNUSED(x)
Definition: ATmega328P.hh:31
bool stop()
Definition: Job.hh:169
bool start()
Definition: Job.hh:159
uint16_t distance() const
Definition: HCSR04.hh:72
#define I_CPU
Definition: Types.h:274
HCSR04(Job::Scheduler *scheduler, Board::DigitalPin trigger, Board::DigitalPin echo)
Definition: HCSR04.hh:59
void schedule(uint16_t ms)
Definition: HCSR04.hh:90