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"
10
11namespace fl {
12
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.
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.
30 public:
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:
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.
50class Button {
51 public:
52 Button(int pin,
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:
75 Listener(Button *owner);
76 ~Listener();
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:
89 bool added = false;
90 };
91
92 private:
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
99 // fl::FunctionList<void(Button&)> mOnChangedCallbacks;
100};
101
102} // namespace fl
Button(int pin, ButtonStrategy strategy=ButtonStrategy::kHighLowFloating)
Definition button.cpp:64
Listener mListener
Definition button.h:94
bool mClickedThisFrame
Definition button.h:96
void removeOnClick(int id)
Definition button.h:56
ButtonLowLevel mButton
Definition button.h:93
int onClick(fl::function< void()> callback)
Definition button.cpp:96
bool clicked() const
Definition button.h:68
bool mPressedLastFrame
Definition button.h:95
fl::FunctionList< void > mOnClickCallbacks
Definition button.h:98
void setStrategy(ButtonStrategy strategy)
Definition button.h:60
bool isPressed()
Definition button.h:64
ButtonLowLevel(ButtonLowLevel &&other)=delete
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
ButtonLowLevel(const ButtonLowLevel &other)=default
ButtonLowLevel & operator=(const ButtonLowLevel &other)=delete
fl::DigitalPin mPin
Definition button.h:43
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
Listener(Button *owner)
Definition button.cpp:78
void addToEngineEventsOnce()
Definition button.cpp:88