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"
9#include "fl/ui/ui.h" // For IButtonInput
10#include "fl/stl/noexcept.h"
11
12namespace fl {
13
14enum 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.
19
20 // Active-low button strategy: Uses internal pull-up resistor.
21 // Button connects pin to ground when pressed. Reading LOW = pressed.
23
24 // Active-high button strategy: Uses internal pull-down resistor.
25 // Button connects pin to VCC when pressed. Reading HIGH = pressed.
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.
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.
53class Button : public IButtonInput {
54 public:
55 Button(int pin,
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:
77 Listener(Button *owner);
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:
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.
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
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
void removeOnClick(int id)
Definition button.h:59
ButtonLowLevel mButton
Definition button.h:99
int onClick(fl::function< void()> callback)
bool isPressed() const FL_NOEXCEPT override
Definition button.h:67
bool mPressedLastFrame
Definition button.h:101
void setStrategy(ButtonStrategy strategy)
Definition button.h:63
bool clicked() const FL_NOEXCEPT override
Definition button.h:71
~ButtonLowLevel() FL_NOEXCEPT
ButtonLowLevel & operator=(const ButtonLowLevel &other) FL_NOEXCEPT=delete
ButtonLowLevel(ButtonLowLevel &&other) FL_NOEXCEPT=delete
void setStrategy(ButtonStrategy strategy)
ButtonLowLevel(const ButtonLowLevel &other) FL_NOEXCEPT=default
ButtonLowLevel(int pin, ButtonStrategy strategy=ButtonStrategy::kHighLowFloating)
ButtonStrategy mStrategy
Definition button.h:47
fl::DigitalPin mPin
Definition button.h:46
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(Button *owner)
~Listener() FL_NOEXCEPT
Aggregator header for the fl/ui/ family of per-element UI types.