FastLED 3.9.7
Loading...
Searching...
No Matches
engine_events.h
1#pragma once
2
3#include "fl/vector.h"
4#include "fl/singleton.h"
5#include "fl/xymap.h"
6#include "fl/screenmap.h"
7#include "fl/namespace.h"
8
9
10
11#ifndef FASTLED_ENGINE_EVENTS_MAX_LISTENERS
12#define FASTLED_ENGINE_EVENTS_MAX_LISTENERS 8
13#endif
14
15#ifndef FASTLED_HAS_ENGINE_EVENTS
16#ifdef __AVR__
17#define FASTLED_HAS_ENGINE_EVENTS 0
18#else
19#define FASTLED_HAS_ENGINE_EVENTS 1
20#endif
21#endif // FASTLED_HAS_ENGINE_EVENTS
22
24class CLEDController;
26
27namespace fl {
28
30 public:
31 class Listener {
32 public:
33 // Note that the subclass must call EngineEvents::addListener(this) to
34 // start listening. In the subclass destructor, the subclass should call
35 // EngineEvents::removeListener(this).
36 Listener();
37 virtual ~Listener();
38 virtual void onBeginFrame() {}
39 virtual void onEndShowLeds() {}
40 virtual void onEndFrame() {}
41 virtual void onStripAdded(CLEDController *strip, uint32_t num_leds) {
42 (void)strip;
43 (void)num_leds;
44 }
45 // Called to set the canvas for UI elements for a particular strip.
46 virtual void onCanvasUiSet(CLEDController *strip, const ScreenMap& screenmap) {
47 (void)strip;
48 (void)screenmap;
49 }
50 virtual void onPlatformPreLoop() {}
51 virtual void onPlatformPreLoop2() {}
52 };
53
54
55 static void addListener(Listener *listener, int priority = 0) {
56 #if FASTLED_HAS_ENGINE_EVENTS
57 EngineEvents::getInstance()->_addListener(listener, priority);
58 #else
59 (void)listener;
60 (void)priority;
61 #endif
62 }
63
64 static void removeListener(Listener *listener) {
65 #if FASTLED_HAS_ENGINE_EVENTS
66 EngineEvents::getInstance()->_removeListener(listener);
67 #else
68 (void)listener;
69 #endif
70 }
71
72 static bool hasListener(Listener *listener) {
73 #if FASTLED_HAS_ENGINE_EVENTS
74 return EngineEvents::getInstance()->_hasListener(listener);
75 #else
76 (void)listener;
77 return false;
78 #endif
79 }
80
81 static void onBeginFrame() {
82 #if FASTLED_HAS_ENGINE_EVENTS
83 EngineEvents::getInstance()->_onBeginFrame();
84 #endif
85 }
86
87 static void onEndShowLeds() {
88 #if FASTLED_HAS_ENGINE_EVENTS
89 EngineEvents::getInstance()->_onEndShowLeds();
90 #endif
91 }
92
93 static void onEndFrame() {
94 #if FASTLED_HAS_ENGINE_EVENTS
95 EngineEvents::getInstance()->_onEndFrame();
96 #endif
97 }
98
99 static void onStripAdded(CLEDController *strip, uint32_t num_leds) {
100 #if FASTLED_HAS_ENGINE_EVENTS
101 EngineEvents::getInstance()->_onStripAdded(strip, num_leds);
102 #else
103 (void)strip;
104 (void)num_leds;
105 #endif
106 }
107
108
109 static void onCanvasUiSet(CLEDController *strip, const ScreenMap& xymap) {
110 #if FASTLED_HAS_ENGINE_EVENTS
111 EngineEvents::getInstance()->_onCanvasUiSet(strip, xymap);
112 #else
113 (void)strip;
114 (void)xymap;
115 #endif
116 }
117
118 static void onPlatformPreLoop() {
119 #if FASTLED_HAS_ENGINE_EVENTS
120 EngineEvents::getInstance()->_onPlatformPreLoop();
121 #endif
122 }
123
124 private:
125 // Safe to add a listeners during a callback.
126 void _addListener(Listener *listener, int priority);
127 // Safe to remove self during a callback.
128 void _removeListener(Listener *listener);
129 void _onBeginFrame();
130 void _onEndShowLeds();
131 void _onEndFrame();
132 void _onStripAdded(CLEDController *strip, uint32_t num_leds);
133 void _onCanvasUiSet(CLEDController *strip, const ScreenMap& xymap);
134 void _onPlatformPreLoop();
135 bool _hasListener(Listener *listener);
136#if FASTLED_HAS_ENGINE_EVENTS
137 struct Pair {
138 Listener *listener = nullptr;
139 int priority = 0;
140 Pair(Listener *listener, int priority) : listener(listener), priority(priority) {}
141 };
143 ListenerList;
144 ListenerList mListeners;
145#endif
146
147 static EngineEvents *getInstance();
148
149 EngineEvents() = default;
150 friend class fl::Singleton<EngineEvents>;
151};
152
153} // namespace fl
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