FastLED
3.9.15
Loading...
Searching...
No Matches
button.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
fl/stl/stdint.h
"
4
5
#include "
fl/stl/function.h
"
6
#include "
fl/stl/shared_ptr.h
"
7
#include "
fl/system/engine_events.h
"
8
#include "
fl/sensors/digital_pin.h
"
9
#include "
fl/ui/ui.h
"
// For IButtonInput
10
#include "
fl/stl/noexcept.h
"
11
12
namespace
fl
{
13
14
enum class
ButtonStrategy
{
15
// High-low floating detection strategy: Sets pin to high, checks if high,
16
// sets pin to low, checks if low. If both are true, pin is floating and
17
// button is not pressed. Useful for detecting buttons without pull resistors.
18
kHighLowFloating
,
19
20
// Active-low button strategy: Uses internal pull-up resistor.
21
// Button connects pin to ground when pressed. Reading LOW = pressed.
22
kPullUp
,
23
24
// Active-high button strategy: Uses internal pull-down resistor.
25
// Button connects pin to VCC when pressed. Reading HIGH = pressed.
26
kPullDown
,
27
};
28
29
// A simple digital pin. If we are compiling in an Arduino environment, then
30
// this class will bind to that. Otherwise it will fall back to the platform
31
// api. Note that this class does not support analog mode nor pullups/pulldowns.
32
class
ButtonLowLevel
{
33
public
:
34
ButtonLowLevel
(
int
pin,
ButtonStrategy
strategy =
ButtonStrategy::kHighLowFloating
);
35
~ButtonLowLevel
()
FL_NOEXCEPT
;
36
ButtonLowLevel
(
const
ButtonLowLevel
&other)
FL_NOEXCEPT
=
default
;
37
ButtonLowLevel
&
operator=
(
const
ButtonLowLevel
&other)
FL_NOEXCEPT
=
delete
;
38
ButtonLowLevel
(
ButtonLowLevel
&&other)
FL_NOEXCEPT
=
delete
;
39
bool
isPressed
();
40
41
bool
highLowFloating
();
42
43
void
setStrategy
(
ButtonStrategy
strategy);
44
45
private
:
46
fl::DigitalPin
mPin
;
47
ButtonStrategy
mStrategy
=
ButtonStrategy::kHighLowFloating
;
48
};
49
50
// The default button type hooks into the FastLED EngineEvents to monitor
51
// whether the button is pressed or not. You do not need to run an update
52
// function. If you need more control, use ButtonLowLevel directly.
53
class
Button
:
public
IButtonInput
{
54
public
:
55
Button
(
int
pin,
56
ButtonStrategy
strategy =
ButtonStrategy::kHighLowFloating
);
57
58
int
onClick
(
fl::function
<
void
()> callback);
59
void
removeOnClick
(
int
id
) {
60
mOnClickCallbacks
.remove(
id
);
61
}
62
63
void
setStrategy
(
ButtonStrategy
strategy) {
64
mButton
.setStrategy(strategy);
65
}
66
67
bool
isPressed
() const
FL_NOEXCEPT
override
{
68
return
mButton
.isPressed();
69
}
70
71
bool
clicked
() const
FL_NOEXCEPT
override
{
72
return
mClickedThisFrame
;
73
}
74
75
protected
:
76
struct
Listener
:
public
EngineEvents::Listener
{
77
Listener
(
Button
*owner);
78
~Listener
()
FL_NOEXCEPT
;
79
void
addToEngineEventsOnce
();
80
81
// We do an experiment here, what about listening to the end frame event
82
// instea do of the begin frame event? This will put the activation of
83
// this button **before** the next frame. I think this pattern should be
84
// used for all UI elements, so that the button state is updated before
85
// the next frame is drawn. This seems like the only way to do this, or
86
// by using platform pre loop, but not all platforms support that.
87
void
onEndFrame
()
override
;
88
89
private
:
90
Button
*
mOwner
;
91
bool
added
=
false
;
92
};
93
94
private
:
95
// mButton is mutable because querying button state requires toggling pin
96
// mode/level on platforms using kHighLowFloating strategy. The const-ness
97
// refers to the logical observable state of the Button object, not the
98
// underlying hardware probe.
99
mutable
ButtonLowLevel
mButton
;
100
Listener
mListener
;
101
bool
mPressedLastFrame
=
false
;
// Don't read this variale, it's used internally.
102
bool
mClickedThisFrame
=
false
;
// This is true if clicked this frame.
103
104
fl::function_list<void()>
mOnClickCallbacks
;
105
// fl::function_list<void(Button&)> mOnChangedCallbacks;
106
};
107
108
}
// namespace fl
fl::Button::Button
Button(int pin, ButtonStrategy strategy=ButtonStrategy::kHighLowFloating)
Definition
button.cpp.hpp:56
fl::Button::mOnClickCallbacks
fl::function_list< void()> mOnClickCallbacks
Definition
button.h:104
fl::Button::mListener
Listener mListener
Definition
button.h:100
fl::Button::mClickedThisFrame
bool mClickedThisFrame
Definition
button.h:102
fl::Button::removeOnClick
void removeOnClick(int id)
Definition
button.h:59
fl::Button::mButton
ButtonLowLevel mButton
Definition
button.h:99
fl::Button::onClick
int onClick(fl::function< void()> callback)
Definition
button.cpp.hpp:92
fl::Button::isPressed
bool isPressed() const FL_NOEXCEPT override
Definition
button.h:67
fl::Button::mPressedLastFrame
bool mPressedLastFrame
Definition
button.h:101
fl::Button::setStrategy
void setStrategy(ButtonStrategy strategy)
Definition
button.h:63
fl::Button::clicked
bool clicked() const FL_NOEXCEPT override
Definition
button.h:71
fl::ButtonLowLevel::~ButtonLowLevel
~ButtonLowLevel() FL_NOEXCEPT
Definition
button.cpp.hpp:19
fl::ButtonLowLevel::isPressed
bool isPressed()
Definition
button.cpp.hpp:40
fl::ButtonLowLevel::operator=
ButtonLowLevel & operator=(const ButtonLowLevel &other) FL_NOEXCEPT=delete
fl::ButtonLowLevel::ButtonLowLevel
ButtonLowLevel(ButtonLowLevel &&other) FL_NOEXCEPT=delete
fl::ButtonLowLevel::highLowFloating
bool highLowFloating()
Definition
button.cpp.hpp:21
fl::ButtonLowLevel::setStrategy
void setStrategy(ButtonStrategy strategy)
Definition
button.cpp.hpp:99
fl::ButtonLowLevel::ButtonLowLevel
ButtonLowLevel(const ButtonLowLevel &other) FL_NOEXCEPT=default
fl::ButtonLowLevel::ButtonLowLevel
ButtonLowLevel(int pin, ButtonStrategy strategy=ButtonStrategy::kHighLowFloating)
Definition
button.cpp.hpp:14
fl::ButtonLowLevel::mStrategy
ButtonStrategy mStrategy
Definition
button.h:47
fl::ButtonLowLevel::mPin
fl::DigitalPin mPin
Definition
button.h:46
fl::ButtonLowLevel
Definition
button.h:32
fl::DigitalPin
Definition
digital_pin.h:13
fl::EngineEvents::Listener::Listener
Listener() FL_NOEXCEPT
Definition
engine_events.cpp.hpp:16
fl::EngineEvents::Listener
Definition
engine_events.h:23
fl::IButtonInput
Definition
element.h:16
digital_pin.h
engine_events.h
function.h
fl::ButtonStrategy
ButtonStrategy
Definition
button.h:14
fl::ButtonStrategy::kPullDown
@ kPullDown
Definition
button.h:26
fl::ButtonStrategy::kHighLowFloating
@ kHighLowFloating
Definition
button.h:18
fl::ButtonStrategy::kPullUp
@ kPullUp
Definition
button.h:22
fl
Base definition for an LED controller.
Definition
crgb.hpp:179
fl::function
Definition
xypath.h:27
noexcept.h
FL_NOEXCEPT
#define FL_NOEXCEPT
shared_ptr.h
stdint.h
fl::Button::Listener::added
bool added
Definition
button.h:91
fl::Button::Listener::mOwner
Button * mOwner
Definition
button.h:90
fl::Button::Listener::onEndFrame
void onEndFrame() override
Definition
button.cpp.hpp:59
fl::Button::Listener::Listener
Listener(Button *owner)
Definition
button.cpp.hpp:74
fl::Button::Listener::addToEngineEventsOnce
void addToEngineEventsOnce()
Definition
button.cpp.hpp:84
fl::Button::Listener::~Listener
~Listener() FL_NOEXCEPT
Definition
button.cpp.hpp:78
fl::Button::Listener
Definition
button.h:76
ui.h
Aggregator header for the fl/ui/ family of per-element UI types.
fl
sensors
button.h
Generated on Tue Jun 16 2026 00:07:00 for FastLED by
1.13.2