FastLED
3.9.15
Loading...
Searching...
No Matches
button.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <
stdint.h
>
4
5
#include "
fl/function_list.h
"
6
#include "
fl/namespace.h
"
7
#include "
fl/ptr.h
"
8
#include "
fl/ui.h
"
9
#include "
sensors/digital_pin.h
"
10
11
namespace
fl
{
12
13
enum
ButtonStrategy
{
14
15
// FastLED doesn't have reliable support for pullups/pulldowns.
16
// So we instead use a strategy where the pin is set to high, then
17
// checked if it's high, then set to low, and then checked if it's low
18
// if this is the case, then the pin is floating and thefore the button
19
// is not
20
// being pressed.
21
kHighLowFloating
,
22
kPullUp
,
23
24
};
25
26
// A simple digital pin. If we are compiling in an Arduino environment, then
27
// this class will bind to that. Otherwise it will fall back to the platform
28
// api. Note that this class does not support analog mode nor pullups/pulldowns.
29
class
ButtonLowLevel
{
30
public
:
31
ButtonLowLevel
(
int
pin,
ButtonStrategy
strategy =
kHighLowFloating
);
32
~ButtonLowLevel
();
33
ButtonLowLevel
(
const
ButtonLowLevel
&other) =
default
;
34
ButtonLowLevel
&
operator=
(
const
ButtonLowLevel
&other) =
delete
;
35
ButtonLowLevel
(
ButtonLowLevel
&&other) =
delete
;
36
bool
isPressed
();
37
38
bool
highLowFloating
();
39
40
void
setStrategy
(
ButtonStrategy
strategy);
41
42
private
:
43
fl::DigitalPin
mPin
;
44
ButtonStrategy
mStrategy
=
kHighLowFloating
;
45
};
46
47
// The default button type hooks into the FastLED EngineEvents to monitor
48
// whether the button is pressed or not. You do not need to run an update
49
// function. If you need more control, use ButtonLowLevel directly.
50
class
Button
{
51
public
:
52
Button
(
int
pin,
53
ButtonStrategy
strategy =
ButtonStrategy::kHighLowFloating
);
54
55
int
onClick
(
fl::function
<
void
()> callback);
56
void
removeOnClick
(
int
id
) {
57
mOnClickCallbacks
.remove(
id
);
58
}
59
60
void
setStrategy
(
ButtonStrategy
strategy) {
61
mButton
.setStrategy(strategy);
62
}
63
64
bool
isPressed
() {
65
return
mButton
.isPressed();
66
}
67
68
bool
clicked
()
const
{
69
// If we have a real button, check if it's pressed
70
return
mClickedThisFrame
;
71
}
72
73
protected
:
74
struct
Listener
:
public
EngineEvents::Listener
{
75
Listener
(
Button
*owner);
76
~Listener
();
77
void
addToEngineEventsOnce
();
78
79
// We do an experiment here, what about listening to the end frame event
80
// instea do of the begin frame event? This will put the activation of
81
// this button **before** the next frame. I think this pattern should be
82
// used for all UI elements, so that the button state is updated before
83
// the next frame is drawn. This seems like the only way to do this, or
84
// by using platform pre loop, but not all platforms support that.
85
void
onEndFrame
()
override
;
86
87
private
:
88
Button
*
mOwner
;
89
bool
added
=
false
;
90
};
91
92
private
:
93
ButtonLowLevel
mButton
;
94
Listener
mListener
;
95
bool
mPressedLastFrame
=
false
;
// Don't read this variale, it's used internally.
96
bool
mClickedThisFrame
=
false
;
// This is true if clicked this frame.
97
98
fl::FunctionList<void>
mOnClickCallbacks
;
99
// fl::FunctionList<void(Button&)> mOnChangedCallbacks;
100
};
101
102
}
// namespace fl
fl::Button::Button
Button(int pin, ButtonStrategy strategy=ButtonStrategy::kHighLowFloating)
Definition
button.cpp:64
fl::Button::mListener
Listener mListener
Definition
button.h:94
fl::Button::mClickedThisFrame
bool mClickedThisFrame
Definition
button.h:96
fl::Button::removeOnClick
void removeOnClick(int id)
Definition
button.h:56
fl::Button::mButton
ButtonLowLevel mButton
Definition
button.h:93
fl::Button::onClick
int onClick(fl::function< void()> callback)
Definition
button.cpp:96
fl::Button::clicked
bool clicked() const
Definition
button.h:68
fl::Button::mPressedLastFrame
bool mPressedLastFrame
Definition
button.h:95
fl::Button::mOnClickCallbacks
fl::FunctionList< void > mOnClickCallbacks
Definition
button.h:98
fl::Button::setStrategy
void setStrategy(ButtonStrategy strategy)
Definition
button.h:60
fl::Button::isPressed
bool isPressed()
Definition
button.h:64
fl::ButtonLowLevel::isPressed
bool isPressed()
Definition
button.cpp:43
fl::ButtonLowLevel::ButtonLowLevel
ButtonLowLevel(ButtonLowLevel &&other)=delete
fl::ButtonLowLevel::highLowFloating
bool highLowFloating()
Definition
button.cpp:22
fl::ButtonLowLevel::ButtonLowLevel
ButtonLowLevel(int pin, ButtonStrategy strategy=kHighLowFloating)
Definition
button.cpp:15
fl::ButtonLowLevel::setStrategy
void setStrategy(ButtonStrategy strategy)
Definition
button.cpp:103
fl::ButtonLowLevel::mStrategy
ButtonStrategy mStrategy
Definition
button.h:44
fl::ButtonLowLevel::ButtonLowLevel
ButtonLowLevel(const ButtonLowLevel &other)=default
fl::ButtonLowLevel::operator=
ButtonLowLevel & operator=(const ButtonLowLevel &other)=delete
fl::ButtonLowLevel::mPin
fl::DigitalPin mPin
Definition
button.h:43
fl::ButtonLowLevel::~ButtonLowLevel
~ButtonLowLevel()
Definition
button.cpp:20
fl::ButtonLowLevel
Definition
button.h:29
fl::DigitalPin
Definition
digital_pin.h:16
fl::EngineEvents::Listener::Listener
Listener()
Definition
engine_events.cpp:8
fl::EngineEvents::Listener
Definition
engine_events.h:25
fl::FunctionList
Definition
function_list.h:55
digital_pin.h
function_list.h
namespace.h
Implements the FastLED namespace macros.
fl::ButtonStrategy
ButtonStrategy
Definition
button.h:13
fl::kHighLowFloating
@ kHighLowFloating
Definition
button.h:21
fl::kPullUp
@ kPullUp
Definition
button.h:22
fl
Implements a simple red square effect for 2D LED grids.
Definition
crgb.h:16
fl::function
Definition
function.h:16
ptr.h
stdint.h
fl::Button::Listener::added
bool added
Definition
button.h:89
fl::Button::Listener::~Listener
~Listener()
Definition
button.cpp:82
fl::Button::Listener::mOwner
Button * mOwner
Definition
button.h:88
fl::Button::Listener::onEndFrame
void onEndFrame() override
Definition
button.cpp:67
fl::Button::Listener::Listener
Listener(Button *owner)
Definition
button.cpp:78
fl::Button::Listener::addToEngineEventsOnce
void addToEngineEventsOnce()
Definition
button.cpp:88
fl::Button::Listener
Definition
button.h:74
ui.h
sensors
button.h
Generated on Thu Jun 5 2025 04:29:38 for FastLED by
1.13.2