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