Arduino-GPIO
General Purpose Input/Output (GPIO) library for Arduino
Button.h
Go to the documentation of this file.
1 
19 #ifndef BUTTON_H
20 #define BUTTON_H
21 
22 #include "GPIO.h"
23 
30 template<BOARD::pin_t PIN, uint16_t DEBOUNCE = 50>
31 class Button {
32 public:
37  Button() :
38  m_timestamp(0),
39  m_state(true)
40  {
41  m_pin.input().pullup();
42  }
43 
50  bool ischanged()
51  {
52  // Check if debounce time limit has elapsed
53  if (millis() - m_timestamp < DEBOUNCE) return (false);
54  m_timestamp = millis();
55 
56  // Check for the pin state has changed
57  bool state = m_pin;
58  if (state == m_state) return (false);
59  m_state = state;
60  return (true);
61  }
62 
67  bool read()
68  {
69  return (m_state);
70  }
71 
76  operator bool()
77  __attribute__((always_inline))
78  {
79  return (read());
80  }
81 
86  uint16_t timestamp()
87  {
88  return (m_timestamp);
89  }
90 
91 protected:
94 
96  uint16_t m_timestamp;
97 
99  bool m_state;
100 };
101 
102 #endif
Definition: Button.h:31
uint16_t m_timestamp
Definition: Button.h:96
Button()
Definition: Button.h:37
bool ischanged()
Definition: Button.h:50
bool read()
Definition: Button.h:67
bool m_state
Definition: Button.h:99
Definition: GPIO.h:31
GPIO< PIN > m_pin
Definition: Button.h:93
uint16_t timestamp()
Definition: Button.h:86