Uniot Core
0.9.0
Loading...
Searching...
No Matches
Button.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2020 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
26
36
37#pragma once
38
39#include <IExecutor.h>
41#include <TaskScheduler.h>
42
43#if defined(ESP32)
44#include <driver/gpio.h>
45#endif
46
47namespace uniot {
58class Button : public IExecutor, public ObjectRegisterRecord {
59 public:
68
76 using ButtonCallback = std::function<void(Button *, Event)>;
77
87 Button(uint8_t pin, uint8_t activeLevel, uint8_t longPressTicks, ButtonCallback commonCallback = nullptr, uint8_t autoResetTicks = 100)
89 mPin(pin),
90 mActiveLevel(activeLevel),
91 mLongPressTicks(longPressTicks),
92 mAutoResetTicks(autoResetTicks),
93 mWasClick(false),
94 mWasLongPress(false),
95 OnLongPress(commonCallback),
96 OnClick(commonCallback),
97 mPrevState(false),
100 applyPinMode();
101 }
102
122 pinMode(mPin, _inputMode(mPin, mActiveLevel));
123 }
124
125 bool resetClick() {
126 auto was = mWasClick;
127 mWasClick = false;
128 return was;
129 }
130
138 auto was = mWasLongPress;
139 mWasLongPress = false;
140 return was;
141 }
142
159 virtual void execute(short times) override {
160 bool curState = digitalRead(mPin) == mActiveLevel;
161 // The counter stops at the threshold. It is a uint8_t, so left running it would wrap
162 // after 255 ticks: a long hold would fire LONG_PRESS again, and releasing just after the
163 // wrap would count as a click.
164 if (curState && mLongPressTicker < mLongPressTicks) {
166 mWasLongPress = true;
167 if (OnLongPress)
168 OnLongPress(this, LONG_PRESS);
169 }
170 }
171 if (mPrevState && !curState) {
173 mWasClick = true;
174 if (OnClick)
175 OnClick(this, CLICK);
176 }
179 }
180 mPrevState = curState;
181
183 resetClick();
186 }
187 }
188
194 virtual type_id getTypeId() const override {
196 }
197
198 protected:
206 static uint8_t _inputMode(uint8_t pin, uint8_t activeLevel) {
207#if defined(ESP8266)
208 if (pin == 16) {
209 return activeLevel == HIGH ? INPUT_PULLDOWN_16 : INPUT;
210 }
211 return activeLevel == LOW ? INPUT_PULLUP : INPUT;
212#elif defined(ESP32)
213 // Input-only pins have no pull resistors, and asking for one can fail the whole config.
214 if (!GPIO_IS_VALID_OUTPUT_GPIO(pin)) {
215 return INPUT;
216 }
217 return activeLevel == LOW ? INPUT_PULLUP : INPUT_PULLDOWN;
218#else
219 return INPUT;
220#endif
221 }
222
223 uint8_t mPin;
224 uint8_t mActiveLevel;
225
228
231
234
238};
239 // End of hardware_button group
240} // namespace uniot
bool mWasLongPress
Flag indicating if a long press has been detected.
Definition Button.h:230
std::function< void(Button *, Event)> ButtonCallback
Callback function signature for button events.
Definition Button.h:76
ButtonCallback OnLongPress
Callback function for long press events.
Definition Button.h:232
virtual void execute(short times) override
Processes button state and detects events as part of the scheduler system.
Definition Button.h:159
uint8_t mActiveLevel
Logic level that represents button press (HIGH or LOW)
Definition Button.h:224
uint8_t mLongPressTicks
Number of ticks to define a long press.
Definition Button.h:226
uint8_t mAutoResetTicker
Counter for automatic state reset.
Definition Button.h:237
uint8_t mAutoResetTicks
Number of ticks after which button state auto-resets.
Definition Button.h:227
bool resetLongPress()
Resets the long press detection flag.
Definition Button.h:137
bool mPrevState
Previous state of the button.
Definition Button.h:235
virtual type_id getTypeId() const override
Returns the type identifier for this class.
Definition Button.h:194
ButtonCallback OnClick
Callback function for click events.
Definition Button.h:233
Event
Defines types of button events that can be triggered.
Definition Button.h:64
@ LONG_PRESS
Button held down for longer than the defined threshold.
Definition Button.h:66
@ CLICK
Regular short button press and release.
Definition Button.h:65
bool resetClick()
Definition Button.h:125
bool mWasClick
Flag indicating if a click has been detected.
Definition Button.h:229
void applyPinMode()
Resets the click detection flag.
Definition Button.h:121
uint8_t mPin
GPIO pin number connected to the button.
Definition Button.h:223
static uint8_t _inputMode(uint8_t pin, uint8_t activeLevel)
The input mode for a pin, with the internal pull matching the active level.
Definition Button.h:206
uint8_t mLongPressTicker
Counter for tracking potential long presses.
Definition Button.h:236
Button(uint8_t pin, uint8_t activeLevel, uint8_t longPressTicks, ButtonCallback commonCallback=nullptr, uint8_t autoResetTicks=100)
Constructs a Button instance.
Definition Button.h:87
Interface for executing tasks in the scheduler system.
Definition IExecutor.h:31
ObjectRegisterRecord(const ObjectRegisterRecord &)=delete
Deleted copy constructor to prevent copying.
static type_id getTypeId()
Get the unique type identifier for a given type T.
Definition TypeId.h:82
const void * type_id
Type alias for a unique type identifier.
Definition TypeId.h:43
Contains all classes and functions related to the Uniot Core.