FastLED 3.9.15
Loading...
Searching...
No Matches
engine_events.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/namespace.h"
4#include "fl/screenmap.h"
5#include "fl/singleton.h"
6#include "fl/vector.h"
7#include "fl/xymap.h"
8
9#ifndef FASTLED_HAS_ENGINE_EVENTS
10#ifdef __AVR__
11#define FASTLED_HAS_ENGINE_EVENTS 0
12#else
13#define FASTLED_HAS_ENGINE_EVENTS 1
14#endif
15#endif // FASTLED_HAS_ENGINE_EVENTS
16
18class CLEDController;
20
21namespace fl {
22
24 public:
25 class Listener {
26 public:
27 // Note that the subclass must call EngineEvents::addListener(this) to
28 // start listening. In the subclass destructor, the subclass should call
29 // EngineEvents::removeListener(this).
30 Listener();
31 virtual ~Listener();
32 virtual void onBeginFrame() {}
33 virtual void onEndShowLeds() {}
34 virtual void onEndFrame() {}
35 virtual void onStripAdded(CLEDController *strip, uint32_t num_leds) {
36 (void)strip;
37 (void)num_leds;
38 }
39 // Called to set the canvas for UI elements for a particular strip.
40 virtual void onCanvasUiSet(CLEDController *strip,
41 const ScreenMap &screenmap) {
42 (void)strip;
43 (void)screenmap;
44 }
45 virtual void onPlatformPreLoop() {}
46 virtual void onPlatformPreLoop2() {}
47 };
48
49 static void addListener(Listener *listener, int priority = 0) {
50#if FASTLED_HAS_ENGINE_EVENTS
51 EngineEvents::getInstance()->_addListener(listener, priority);
52#else
53 (void)listener;
54 (void)priority;
55#endif
56 }
57
58 static void removeListener(Listener *listener) {
59#if FASTLED_HAS_ENGINE_EVENTS
61#else
62 (void)listener;
63#endif
64 }
65
66 static bool hasListener(Listener *listener) {
67#if FASTLED_HAS_ENGINE_EVENTS
68 return EngineEvents::getInstance()->_hasListener(listener);
69#else
70 (void)listener;
71 return false;
72#endif
73 }
74
75 static void onBeginFrame() {
76#if FASTLED_HAS_ENGINE_EVENTS
78#endif
79 }
80
81 static void onEndShowLeds() {
82#if FASTLED_HAS_ENGINE_EVENTS
84#endif
85 }
86
87 static void onEndFrame() {
88#if FASTLED_HAS_ENGINE_EVENTS
90#endif
91 }
92
93 static void onStripAdded(CLEDController *strip, uint32_t num_leds) {
94#if FASTLED_HAS_ENGINE_EVENTS
95 EngineEvents::getInstance()->_onStripAdded(strip, num_leds);
96#else
97 (void)strip;
98 (void)num_leds;
99#endif
100 }
101
102 static void onCanvasUiSet(CLEDController *strip, const ScreenMap &xymap) {
103#if FASTLED_HAS_ENGINE_EVENTS
105#else
106 (void)strip;
107 (void)xymap;
108#endif
109 }
110
111 static void onPlatformPreLoop() {
112#if FASTLED_HAS_ENGINE_EVENTS
114#endif
115 }
116
117 // Needed by fl::vector<T>
118 EngineEvents() = default;
119
120 private:
121 // Safe to add a listeners during a callback.
122 void _addListener(Listener *listener, int priority);
123 // Safe to remove self during a callback.
124 void _removeListener(Listener *listener);
128 void _onStripAdded(CLEDController *strip, uint32_t num_leds);
131 bool _hasListener(Listener *listener);
132#if FASTLED_HAS_ENGINE_EVENTS
133 struct Pair {
134 Pair() = default;
135 Listener *listener = nullptr;
136 int priority = 0;
137 Pair(Listener *listener, int priority)
138 : listener(listener), priority(priority) {}
139 };
140#if defined(__EMSCRIPTEN__) && 0
141 // Not sure what is going on here, but emscripten seems to have a problem
142 // with FixedVector.
143 typedef fl::vector<Pair> ListenerList;
144#else
145 typedef fl::vector_inlined<Pair, 16> ListenerList;
146#endif
147 ListenerList mListeners;
148#endif
149
151
152 friend class fl::Singleton<EngineEvents>;
153};
154
155} // namespace fl
XYMap xymap(WIDTH, HEIGHT, SERPENTINE)
Base definition for an LED controller.
virtual void onCanvasUiSet(CLEDController *strip, const ScreenMap &screenmap)
virtual void onPlatformPreLoop()
virtual void onStripAdded(CLEDController *strip, uint32_t num_leds)
virtual void onPlatformPreLoop2()
void _onStripAdded(CLEDController *strip, uint32_t num_leds)
void _removeListener(Listener *listener)
EngineEvents()=default
static void addListener(Listener *listener, int priority=0)
void _addListener(Listener *listener, int priority)
static EngineEvents * getInstance()
static void removeListener(Listener *listener)
static void onBeginFrame()
bool _hasListener(Listener *listener)
void _onCanvasUiSet(CLEDController *strip, const ScreenMap &xymap)
static void onStripAdded(CLEDController *strip, uint32_t num_leds)
static bool hasListener(Listener *listener)
static void onEndFrame()
static void onPlatformPreLoop()
void _onPlatformPreLoop()
static void onCanvasUiSet(CLEDController *strip, const ScreenMap &xymap)
static void onEndShowLeds()
#define FASTLED_NAMESPACE_END
Definition namespace.h:23
Implements the FastLED namespace macros.
InlinedVector< T, INLINED_SIZE > vector_inlined
Definition vector.h:1034
HeapVector< T > vector
Definition vector.h:1028
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Definition pair.h:5