Uniot Core
0.8.1
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
43namespace uniot {
54class Button : public IExecutor, public ObjectRegisterRecord {
55 public:
64
72 using ButtonCallback = std::function<void(Button *, Event)>;
73
83 Button(uint8_t pin, uint8_t activeLevel, uint8_t longPressTicks, ButtonCallback commonCallback = nullptr, uint8_t autoResetTicks = 100)
85 mPin(pin),
86 mActiveLevel(activeLevel),
87 mLongPressTicks(longPressTicks),
88 mAutoResetTicks(autoResetTicks),
89 mWasClick(false),
90 mWasLongPress(false),
91 OnLongPress(commonCallback),
92 OnClick(commonCallback),
93 mPrevState(false),
96 pinMode(mPin, INPUT);
97 }
98
105 bool resetClick() {
106 auto was = mWasClick;
107 mWasClick = false;
108 return was;
109 }
110
118 auto was = mWasLongPress;
119 mWasLongPress = false;
120 return was;
121 }
122
139 virtual void execute(short times) override {
140 bool curState = digitalRead(mPin) == mActiveLevel;
141 if (curState && ++mLongPressTicker == mLongPressTicks) {
142 mWasLongPress = true;
143 if (OnLongPress)
144 OnLongPress(this, LONG_PRESS);
145 }
146 if (mPrevState && !curState) {
148 mWasClick = true;
149 if (OnClick)
150 OnClick(this, CLICK);
151 }
154 }
155 mPrevState = curState;
156
158 resetClick();
161 }
162 }
163
169 virtual type_id getTypeId() const override {
171 }
172
173 protected:
174 uint8_t mPin;
175 uint8_t mActiveLevel;
176
179
182
185
189};
190 // End of hardware_button group
191} // namespace uniot
bool mWasLongPress
Flag indicating if a long press has been detected.
Definition Button.h:181
std::function< void(Button *, Event)> ButtonCallback
Callback function signature for button events.
Definition Button.h:72
ButtonCallback OnLongPress
Callback function for long press events.
Definition Button.h:183
virtual void execute(short times) override
Processes button state and detects events as part of the scheduler system.
Definition Button.h:139
uint8_t mActiveLevel
Logic level that represents button press (HIGH or LOW)
Definition Button.h:175
uint8_t mLongPressTicks
Number of ticks to define a long press.
Definition Button.h:177
uint8_t mAutoResetTicker
Counter for automatic state reset.
Definition Button.h:188
uint8_t mAutoResetTicks
Number of ticks after which button state auto-resets.
Definition Button.h:178
bool resetLongPress()
Resets the long press detection flag.
Definition Button.h:117
bool mPrevState
Previous state of the button.
Definition Button.h:186
virtual type_id getTypeId() const override
Returns the type identifier for this class.
Definition Button.h:169
ButtonCallback OnClick
Callback function for click events.
Definition Button.h:184
Event
Defines types of button events that can be triggered.
Definition Button.h:60
@ LONG_PRESS
Button held down for longer than the defined threshold.
Definition Button.h:62
@ CLICK
Regular short button press and release.
Definition Button.h:61
bool resetClick()
Resets the click detection flag.
Definition Button.h:105
bool mWasClick
Flag indicating if a click has been detected.
Definition Button.h:180
uint8_t mPin
GPIO pin number connected to the button.
Definition Button.h:174
uint8_t mLongPressTicker
Counter for tracking potential long presses.
Definition Button.h:187
Button(uint8_t pin, uint8_t activeLevel, uint8_t longPressTicks, ButtonCallback commonCallback=nullptr, uint8_t autoResetTicks=100)
Constructs a Button instance.
Definition Button.h:83
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.