COSA
An Object-Oriented Platform for Arduino Programming
Button.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_BUTTON_HH
22 #define COSA_BUTTON_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/InputPin.hh"
26 #include "Cosa/Periodic.hh"
27 #include "Cosa/Watchdog.hh"
28 
56 class Button : public InputPin, public Periodic {
57 public:
62  enum Mode {
63  ON_FALLING_MODE = 0, // High to low transition.
64  ON_RISING_MODE = 1, // Low to high transition.
65  ON_CHANGE_MODE = 2 // Any transition.
66  } __attribute__((packed));
67 
77  Button(Job::Scheduler* scheduler,
81  Periodic(scheduler, SAMPLE_MS),
82  MODE(mode),
83  m_state(is_set())
84  {}
85 
94  virtual void on_change(uint8_t type) = 0;
95 
96 protected:
98  static const uint16_t SAMPLE_MS = 64;
99 
101  const Mode MODE;
102 
104  uint8_t m_state;
105 
112  virtual void run()
113  {
114  // Update the button state
115  uint8_t old_state = m_state;
116  m_state = is_set();
117  uint8_t new_state = m_state;
118 
119  // If changed according to mode call the pin change handler
120  if ((old_state != new_state) &&
121  ((MODE == ON_CHANGE_MODE) || (new_state == MODE)))
123  }
124 };
125 
126 #endif
uint8_t m_state
Definition: Button.hh:104
uint8_t pin() const
Definition: Pin.hh:103
Definition: Button.hh:56
Mode mode() const
Definition: InputPin.hh:52
bool is_set() const
Definition: Pin.hh:112
virtual void on_change(uint8_t type)=0
Mode
Definition: Button.hh:62
const Mode MODE
Definition: Button.hh:101
static const uint16_t SAMPLE_MS
Definition: Button.hh:98
virtual void run()
Definition: Button.hh:112
Button(Job::Scheduler *scheduler, Board::DigitalPin pin, Mode mode=ON_CHANGE_MODE)
Definition: Button.hh:77