FastLED 3.9.15
Loading...
Searching...
No Matches
button.cpp.hpp
Go to the documentation of this file.
1
2
3#include "fl/stl/stdint.h"
4
5#include "fl/ui/ui.h"
6
7#include "fl/stl/assert.h"
8#include "fl/sensors/button.h"
10#include "fl/stl/noexcept.h"
11
12namespace fl {
13
15 : mPin(pin) {
16 setStrategy(strategy);
17}
18
20
22 // High-low floating detection: Set pin to high, check if high,
23 // set pin to low, check if low. If both conditions are true,
24 // the pin is floating and therefore the button is not pressed.
25 mPin.setPinMode(DigitalPin::kOutput);
26 mPin.write(true); // set pin to high
27 mPin.setPinMode(DigitalPin::kInput);
28 const bool was_high = mPin.high(); // check if pin is high
29 mPin.setPinMode(DigitalPin::kOutput);
30 mPin.write(false); // set pin to low
31 mPin.setPinMode(DigitalPin::kInput);
32 const bool was_low = !mPin.high(); // check if pin is low
33 const bool floating =
34 was_high && was_low; // if both are true, then the pin is floating
35 const bool pressed =
36 !floating; // if the pin is floating, then the button is not pressed
37 return pressed;
38}
39
41 switch (mStrategy) {
43 return highLowFloating();
45 // Active-low: Button pulls pin to ground when pressed
46 return !mPin.high();
48 // Active-high: Button pulls pin to VCC when pressed
49 return mPin.high();
50 default:
51 FASTLED_ASSERT(false, "Unknown ButtonLowLevel strategy");
52 return false;
53 }
54}
55
57 : mButton(pin, strategy), mListener(this), mPressedLastFrame(false), mClickedThisFrame(false) {}
58
60 const bool pressed_curr_frame = mOwner->mButton.isPressed();
61 const bool pressed_last_frame = mOwner->mPressedLastFrame;
62 // Rising edge of isPressed() == one click event for this frame.
63 // mClickedThisFrame must reflect only the current frame's transition;
64 // callers (UIDropdown::Listener, UIButton::clickedCount edge counter)
65 // rely on clicked() being transient, not latched.
66 const bool clicked_this_frame = pressed_curr_frame && !pressed_last_frame;
67 mOwner->mPressedLastFrame = pressed_curr_frame;
68 mOwner->mClickedThisFrame = clicked_this_frame;
69 if (clicked_this_frame) {
70 mOwner->mOnClickCallbacks.invoke();
71 }
72}
73
77
83
85 if (added) {
86 return;
87 }
88 EngineEvents::addListener(this, 1); // One high priority so that it runs before UI elements.
89 added = true;
90}
91
92int Button::onClick(function<void()> callback) {
93 int id = mOnClickCallbacks.add(callback);
94 return id;
95}
96
97
98
100 mStrategy = strategy;
101 switch (mStrategy) {
103 mPin.setPinMode(DigitalPin::kInput);
104 break;
106 mPin.setPinMode(DigitalPin::kInputPullup);
107 break;
110 break;
111 default:
112 FASTLED_ASSERT(false, "Unknown ButtonLowLevel strategy");
113 break;
114 }
115}
116
117} // namespace fl
Button(int pin, ButtonStrategy strategy=ButtonStrategy::kHighLowFloating)
fl::function_list< void()> mOnClickCallbacks
Definition button.h:104
Listener mListener
Definition button.h:100
bool mClickedThisFrame
Definition button.h:102
ButtonLowLevel mButton
Definition button.h:99
int onClick(fl::function< void()> callback)
bool mPressedLastFrame
Definition button.h:101
~ButtonLowLevel() FL_NOEXCEPT
void setStrategy(ButtonStrategy strategy)
ButtonLowLevel(int pin, ButtonStrategy strategy=ButtonStrategy::kHighLowFloating)
ButtonStrategy mStrategy
Definition button.h:47
fl::DigitalPin mPin
Definition button.h:46
static void removeListener(Listener *listener) FL_NOEXCEPT
static void addListener(Listener *listener, int priority=0) FL_NOEXCEPT
ButtonStrategy
Definition button.h:14
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Button * mOwner
Definition button.h:90
void onEndFrame() override
~Listener() FL_NOEXCEPT
Aggregator header for the fl/ui/ family of per-element UI types.