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