FastLED 3.9.15
Loading...
Searching...
No Matches
engine_events.cpp
Go to the documentation of this file.
1#include "fl/engine_events.h"
2#include "fl/namespace.h"
3
4using namespace fl;
5
7
9
11#if FASTLED_HAS_ENGINE_EVENTS
13 const bool has_listener = ptr && ptr->_hasListener(this);
14 if (has_listener) {
15 // Warning, the listener should be removed by the subclass. If we are
16 // here then the subclass did not remove the listener and we are now in
17 // a partial state of destruction and the results may be undefined for
18 // multithreaded applications. However, for single threaded (the only
19 // option as of 2024, October) this will be ok.
20 ptr->_removeListener(this);
21 }
22#endif
23}
24
26#if FASTLED_HAS_ENGINE_EVENTS
28#else
29 return nullptr; // strip out when engine events are disabled.
30#endif
31}
32
33#if FASTLED_HAS_ENGINE_EVENTS
35 for (auto &item : mListeners) {
36 auto listener = item.listener;
37 listener->onPlatformPreLoop();
38 }
39 for (auto &item : mListeners) {
40 auto listener = item.listener;
41 listener->onPlatformPreLoop2();
42 }
43}
44
46 auto predicate = [listener](const Pair &pair) {
47 return pair.listener == listener;
48 };
49 return mListeners.find_if(predicate) != mListeners.end();
50}
51
52void EngineEvents::_addListener(Listener *listener, int priority) {
53 if (_hasListener(listener)) {
54 return;
55 }
56 for (auto it = mListeners.begin(); it != mListeners.end(); ++it) {
57 if (it->priority < priority) {
58 // this is now the highest priority in this spot.
59 EngineEvents::Pair pair = EngineEvents::Pair(listener, priority);
60 mListeners.insert(it, pair);
61 return;
62 }
63 }
64 EngineEvents::Pair pair = EngineEvents::Pair(listener, priority);
65 mListeners.push_back(pair);
66}
67
69 auto predicate = [listener](const Pair &pair) {
70 return pair.listener == listener;
71 };
72 auto it = mListeners.find_if(predicate);
73 if (it != mListeners.end()) {
74 mListeners.erase(it);
75 }
76}
77
79 // Make the copy of the listener list to avoid issues with listeners being
80 // added or removed during the loop.
81 ListenerList copy = mListeners;
82 for (auto &item : copy) {
83 auto listener = item.listener;
84 listener->onBeginFrame();
85 }
86}
87
89 // Make the copy of the listener list to avoid issues with listeners being
90 // added or removed during the loop.
91 ListenerList copy = mListeners;
92 for (auto &item : copy) {
93 auto listener = item.listener;
94 listener->onEndShowLeds();
95 }
96}
97
99 // Make the copy of the listener list to avoid issues with listeners being
100 // added or removed during the loop.
101 ListenerList copy = mListeners;
102 for (auto &item : copy) {
103 auto listener = item.listener;
104 listener->onEndFrame();
105 }
106}
107
108void EngineEvents::_onStripAdded(CLEDController *strip, uint32_t num_leds) {
109 // Make the copy of the listener list to avoid issues with listeners being
110 // added or removed during the loop.
111 ListenerList copy = mListeners;
112 for (auto &item : copy) {
113 auto listener = item.listener;
114 listener->onStripAdded(strip, num_leds);
115 }
116}
117
118void EngineEvents::_onCanvasUiSet(CLEDController *strip,
119 const ScreenMap &screenmap) {
120 // Make the copy of the listener list to avoid issues with listeners being
121 // added or removed during the loop.
122 ListenerList copy = mListeners;
123 for (auto &item : copy) {
124 auto listener = item.listener;
125 listener->onCanvasUiSet(strip, screenmap);
126 }
127}
128
129#endif // FASTLED_HAS_ENGINE_EVENTS
130
void _onStripAdded(CLEDController *strip, uint32_t num_leds)
void _removeListener(Listener *listener)
EngineEvents()=default
void _addListener(Listener *listener, int priority)
static EngineEvents * getInstance()
bool _hasListener(Listener *listener)
void _onCanvasUiSet(CLEDController *strip, const ScreenMap &xymap)
void _onPlatformPreLoop()
static T & instance()
Definition singleton.h:11
#define FASTLED_NAMESPACE_END
Definition namespace.h:23
Implements the FastLED namespace macros.
Pair< Key, Value > pair
Definition pair.h:13
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16