FastLED 3.9.15
Loading...
Searching...
No Matches
button.cpp
Go to the documentation of this file.
1
2
3#include <stdint.h>
4
5#include "fl/ptr.h"
6#include "fl/ui.h"
7
8#include "fl/assert.h"
9#include "fl/namespace.h"
10#include "sensors/button.h"
11#include "sensors/digital_pin.h"
12
13namespace fl {
14
16 : mPin(pin) {
17 setStrategy(strategy);
18}
19
21
23 // FastLED doesn't have reliable support for pullups/pulldowns.
24 // So we instead use a strategy where the pin is set to high, then
25 // checked if it's high, then set to low, and then checked if it's low
26 // if this is the case, then the pin is floating and thefore the button is
27 // not being pressed.
28 mPin.setPinMode(DigitalPin::kOutput);
29 mPin.write(true); // set pin to high
30 mPin.setPinMode(DigitalPin::kInput);
31 const bool was_high = mPin.high(); // check if pin is high
32 mPin.setPinMode(DigitalPin::kOutput);
33 mPin.write(false); // set pin to low
34 mPin.setPinMode(DigitalPin::kInput);
35 const bool was_low = !mPin.high(); // check if pin is low
36 const bool floating =
37 was_high && was_low; // if both are true, then the pin is floating
38 const bool pressed =
39 !floating; // if the pin is floating, then the button is not pressed
40 return pressed;
41}
42
44 // FastLED doesn't have reliable support for pullups/pulldowns.
45 // So we instead use a strategy where the pin is set to high, then
46 // checked if it's high, then set to low, and then checked if it's low
47 // if this is the case, then the pin is floating and thefore the button is
48 // not being pressed. return (mStrategy == kHighLowFloating) ?
49 // highLowFloating() :
50 // (mStrategy == kPullUp) ? mPin.high() : // not implemented yet
51 // (mStrategy == kPullDown) ? !mPin.high() : // not implemented yet
52 // false; // unknown strategy, return false
53 switch (mStrategy) {
55 return highLowFloating();
56 case kPullUp:
57 return mPin.high(); // not implemented yet
58 default:
59 FASTLED_ASSERT(false, "Unknown ButtonLowLevel strategy");
60 return false; // unknown strategy, return false
61 }
62}
63
65 : mButton(pin, strategy), mListener(this) {}
66
68 const bool pressed_curr_frame = mOwner->mButton.isPressed();
69 const bool pressed_last_frame = mOwner->mPressedLastFrame;
70 const bool changed_this_frame = pressed_curr_frame != pressed_last_frame;
71 mOwner->mPressedLastFrame = pressed_curr_frame;
72 if (changed_this_frame && pressed_curr_frame) {
73 mOwner->mClickedThisFrame = true;
74 mOwner->mOnClickCallbacks.invoke();
75 }
76}
77
81
87
89 if (added) {
90 return;
91 }
92 EngineEvents::addListener(this, 1); // One high priority so that it runs before UI elements.
93 added = true;
94}
95
96int Button::onClick(function<void()> callback) {
97 int id = mOnClickCallbacks.add(callback);
98 return id;
99}
100
101
102
104 mStrategy = strategy;
105 switch (mStrategy) {
106 case kHighLowFloating:
107 mPin.setPinMode(DigitalPin::kInput); // Set pin to input mode
108 break;
109 case kPullUp:
110 mPin.setPinMode(
111 DigitalPin::kInputPullup); // Set pin to input pullup mode
112 break;
113 default:
114 // Unknown strategy, do nothing
115 FASTLED_ASSERT(false, "Unknown ButtonLowLevel strategy");
116 break;
117 }
118}
119
120} // namespace fl
Button(int pin, ButtonStrategy strategy=ButtonStrategy::kHighLowFloating)
Definition button.cpp:64
Listener mListener
Definition button.h:94
ButtonLowLevel mButton
Definition button.h:93
int onClick(fl::function< void()> callback)
Definition button.cpp:96
fl::FunctionList< void > mOnClickCallbacks
Definition button.h:98
bool highLowFloating()
Definition button.cpp:22
ButtonLowLevel(int pin, ButtonStrategy strategy=kHighLowFloating)
Definition button.cpp:15
void setStrategy(ButtonStrategy strategy)
Definition button.cpp:103
ButtonStrategy mStrategy
Definition button.h:44
fl::DigitalPin mPin
Definition button.h:43
static void addListener(Listener *listener, int priority=0)
static void removeListener(Listener *listener)
Implements the FastLED namespace macros.
ButtonStrategy
Definition button.h:13
@ kHighLowFloating
Definition button.h:21
@ kPullUp
Definition button.h:22
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Button * mOwner
Definition button.h:88
void onEndFrame() override
Definition button.cpp:67
void addToEngineEventsOnce()
Definition button.cpp:88