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