FastLED 3.9.15
Loading...
Searching...
No Matches
engine_events.cpp.hpp
Go to the documentation of this file.
2#include "fl/stl/int.h"
3// Note: fl/stl/cstdio.h intentionally NOT included — workaround for
4// zackees/zccache#619 (Windows PCH path-spelling drift). The comment
5// above said "for printf debug" but no printf-debug call survived.
6#include "fl/stl/noexcept.h"
7
8
9namespace fl {
10
11// Explicitly define constructor and destructor
14
15
17
19#if FASTLED_HAS_ENGINE_EVENTS
20 EngineEvents *ptr = EngineEvents::getInstance();
21 const bool has_listener = ptr && ptr->_hasListener(this);
22 if (has_listener) {
23 // Warning, the listener should be removed by the subclass. If we are
24 // here then the subclass did not remove the listener and we are now in
25 // a partial state of destruction and the results may be undefined for
26 // multithreaded applications. However, for single threaded (the only
27 // option as of 2024, October) this will be ok.
28 ptr->_removeListener(this);
29 }
30#endif
31}
32
33#if FASTLED_HAS_ENGINE_EVENTS
34EngineEvents *EngineEvents::getInstance() {
36}
37#endif
38
39#if FASTLED_HAS_ENGINE_EVENTS
41 for (auto &item : mListeners) {
42 auto listener = item.listener;
43 listener->onPlatformPreLoop();
44 }
45 for (auto &item : mListeners) {
46 auto listener = item.listener;
47 listener->onPlatformPreLoop2();
48 }
49}
50
52 auto predicate = [listener](const Pair &pair) {
53 return pair.listener == listener;
54 };
55 return mListeners.find_if(predicate) != mListeners.end();
56}
57
58void EngineEvents::_addListener(Listener *listener, int priority) {
59 if (_hasListener(listener)) {
60 return;
61 }
62 for (auto it = mListeners.begin(); it != mListeners.end(); ++it) {
63 if (it->priority < priority) {
64 // this is now the highest priority in this spot.
65 EngineEvents::Pair pair = EngineEvents::Pair(listener, priority);
66 mListeners.insert(it, pair);
67 return;
68 }
69 }
70 EngineEvents::Pair pair = EngineEvents::Pair(listener, priority);
71 mListeners.push_back(pair);
72}
73
75 auto predicate = [listener](const Pair &pair) {
76 return pair.listener == listener;
77 };
78 auto it = mListeners.find_if(predicate);
79 if (it != mListeners.end()) {
80 mListeners.erase(it);
81 }
82}
83
85 // Make the copy of the listener list to avoid issues with listeners being
86 // added or removed during the loop.
87 ListenerList copy = mListeners;
88 for (auto &item : copy) {
89 auto listener = item.listener;
90 listener->onBeginFrame();
91 }
92}
93
95 // Make the copy of the listener list to avoid issues with listeners being
96 // added or removed during the loop.
97 ListenerList copy = mListeners;
98 for (auto &item : copy) {
99 auto listener = item.listener;
100 listener->onEndShowLeds();
101 }
102}
103
105 // Make the copy of the listener list to avoid issues with listeners being
106 // added or removed during the loop.
107 ListenerList copy = mListeners;
108 for (auto &item : copy) {
109 auto listener = item.listener;
110 listener->onEndFrame();
111 }
112}
113
114void EngineEvents::_onStripAdded(CLEDController *strip, fl::u32 num_leds) {
115 // Make the copy of the listener list to avoid issues with listeners being
116 // added or removed during the loop.
117 ListenerList copy = mListeners;
118 for (auto &item : copy) {
119 auto listener = item.listener;
120 listener->onStripAdded(strip, num_leds);
121 }
122}
123
125 const ScreenMap &screenmap) {
126 // Make the copy of the listener list to avoid issues with listeners being
127 // added or removed during the loop.
128 ListenerList copy = mListeners;
129 for (auto &item : copy) {
130 auto listener = item.listener;
131 listener->onCanvasUiSet(strip, screenmap);
132 }
133}
134
136 // Make the copy of the listener list to avoid issues with listeners being
137 // added or removed during the loop.
138 ListenerList copy = mListeners;
139 for (auto &item : copy) {
140 auto listener = item.listener;
141 listener->onExit();
142 }
143}
144
145#endif // FASTLED_HAS_ENGINE_EVENTS
146
147} // namespace fl
fl::ScreenMap screenmap
virtual ~Listener() FL_NOEXCEPT
~EngineEvents() FL_NOEXCEPT
void _onStripAdded(CLEDController *strip, fl::u32 num_leds) FL_NOEXCEPT
EngineEvents() FL_NOEXCEPT
void _onCanvasUiSet(CLEDController *strip, const ScreenMap &xymap) FL_NOEXCEPT
void _addListener(Listener *listener, int priority) FL_NOEXCEPT
void _onEndShowLeds() FL_NOEXCEPT
void _onPlatformPreLoop() FL_NOEXCEPT
void _removeListener(Listener *listener) FL_NOEXCEPT
bool _hasListener(Listener *listener) FL_NOEXCEPT
void _onBeginFrame() FL_NOEXCEPT
void _onEndFrame() FL_NOEXCEPT
void _onExit() FL_NOEXCEPT
static T & instance() FL_NOEXCEPT
Definition singleton.h:41
fl::CLEDController CLEDController
pair< T1, T2 > Pair
Definition pair.h:207
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT