AbleButtons V0.2.0
Lightweight button library for Arduino.
 
Loading...
Searching...
No Matches
Button.h
Go to the documentation of this file.
1/**
2 * @file Button.h Definition of the core Able Button template class.
3 *
4 * @copyright Copyright (c) 2022 John Scott.
5 */
6#pragma once
7#include "Circuits.h"
8#include "Pins.h"
9
10namespace able {
11 /**
12 * Core Button class. It supports pulldown and pull-up resistor circuits
13 * specified using the Circuit template parameter and different pin features
14 * using the Pin template parameter.
15 *
16 * @param Circuit Either a PullupResistorCircuit or PulldownResistorCircuit
17 * class matching the resistor circuit used with the button.
18 * @param Pin The Pin class, or a subclass (DebouncedPin or ClickerPin)
19 * providing additional debounced readings and clicked states.
20 */
21 template <typename Circuit, typename Pin>
22 class Button: public Pin {
23 public:
24 //
25 // Creators...
26 //
27
28 /**
29 * Create a button on the specified pin.
30 *
31 * @param pin The pin connected to the button.
32 */
33 Button(uint8_t pin)
34 :Pin(pin, Circuit::BUTTON_RELEASED) {}
35
36 private:
37 //
38 // Copying and assignment (not supported)...
39 //
40 Button(const Button &cpy) = delete; ///< Copying is not supported.
41 Button &operator=(const Button &) = delete; ///< Assigning buttons is not supported.
42
43 public:
44 //
45 // Modifiers...
46 //
47
48 /**
49 * Initialise the button. Called from setup() of an Arduino program.
50 */
51 void begin() {
52 pinMode(this->pin_, Circuit::PIN_MODE);
53 }
54
55 /**
56 * Handle the button. Called from loop() of an Arduino program.
57 */
58 void handle() {
59 this->readPin();
60 }
61
62 /**
63 * Reset the clicked state of the button, returning what is was. This
64 * allows the click state to be effectively read once so that a clicked
65 * state only triggers something once, when checked. For example toggling
66 * something on/off when the button is clicked. For buttons that don't
67 * support clicking, the compile will fail with errors.
68 *
69 * @return True if the button was clicked, else false.
70 */
71 bool resetClicked() {
72 bool rc = this->isClicked();
73 this->prevState_ = this->currState_;
74 return rc;
75 }
76
77 /**
78 * Reset the double-clicked state of the button, returning what is was.
79 * This allows the double-click state to be effectively read once so that
80 * a double-clicked state only triggers something once, when checked.
81 * For example toggling something on/off when the button is double-
82 * clicked. For buttons that don't support double-clicking, the compile
83 * will fail with errors.
84 *
85 * @return True if the button was double-clicked, else false.
86 */
88 bool rc = this->isDoubleClicked();
89 if(rc) this->stateCount_ = 0;
90 return rc;
91 }
92
93 public:
94 //
95 // Accessors...
96 //
97
98 /**
99 * Determine if the button is currently pressed.
100 *
101 * @return True if pressed, else false.
102 */
103 bool isPressed() const {
104 return this->currState_ == Circuit::BUTTON_PRESSED;
105 }
106
107 /**
108 * Determine if the button is currently held down.
109 *
110 * @return True if held, else false.
111 */
112 bool isHeld() const {
113 return isPressed() && ((millis() - this->millisStart_) >= this->heldTime_);
114 }
115
116 /**
117 * Determine if the button is currently idle (unpressed for a "long" time).
118 *
119 * @return True if idle, else false.
120 */
121 bool isIdle() const {
122 return !isPressed() && ((millis() - this->millisStart_) >= this->idleTime_);
123 }
124
125 /**
126 * Determine if the button is clicked. Clicks are registered as a press
127 * then release. If the ClickerPin (or subclass) is used, the button
128 * returns the click state, otherwise the compile will fail with errors.
129 *
130 * @return True if clicked else false.
131 */
132 bool isClicked() const {
133 return this->currState_ == Circuit::BUTTON_RELEASED && this->prevState_ == Circuit::BUTTON_PRESSED;
134 }
135
136 /**
137 * Determine if the button is double-clicked. Double-clicks are registered
138 * as two clicks within the double-click time. If the DoubleClickPin is
139 * used, the button returns the double-click state, otherwise the compile
140 * will fail with errors.
141 *
142 * @return True if double-clicked else false.
143 */
144 bool isDoubleClicked() const {
145 return this->stateCount_ >= 4;
146 }
147 };
148}
Definition of Pulldown and pull-up ressitor circuits.
Definition of the Pin class and subclasses (DebouncedPin, ClickerPin), providing debounce logic when ...
Core Button class.
Definition: Button.h:22
void begin()
Initialise the button.
Definition: Button.h:51
void handle()
Handle the button.
Definition: Button.h:58
bool resetDoubleClicked()
Reset the double-clicked state of the button, returning what is was.
Definition: Button.h:87
bool resetClicked()
Reset the clicked state of the button, returning what is was.
Definition: Button.h:71
bool isClicked() const
Determine if the button is clicked.
Definition: Button.h:132
bool isHeld() const
Determine if the button is currently held down.
Definition: Button.h:112
bool isIdle() const
Determine if the button is currently idle (unpressed for a "long" time).
Definition: Button.h:121
bool isDoubleClicked() const
Determine if the button is double-clicked.
Definition: Button.h:144
bool isPressed() const
Determine if the button is currently pressed.
Definition: Button.h:103
Button(uint8_t pin)
Create a button on the specified pin.
Definition: Button.h:33
Resistor circuit base class.
Definition: Circuits.h:16
Pin base class reading direct from the pin (without debouncing).
Definition: Pins.h:17
uint8_t pin_
The Arduino pin connected to the button.
Definition: Pins.h:72
void readPin()
Read the pin directly.
Definition: Pins.h:62
uint8_t currState_
The reading of the pin.
Definition: Pins.h:73