AVRToolsPlus
A library wof higher-level tools for AVR ATmega328 and ATmega2560 Microcontrollers
EventManager.h
Go to the documentation of this file.
1 /*
2  * EventManager.h
3  * An event handling system for avr microcontrollers.
4  *
5  * Author: igormt@alumni.caltech.edu
6  * Copyright (c) 2017 Igor Mikolic-Torreira
7  *
8  * Inspired by and adapted from the
9  * Arduino Event System library by
10  * Author: mromani@ottotecnica.com
11  * Copyright (c) 2010 OTTOTECNICA Italy
12  *
13  * This library is free software; you can redistribute it
14  * and/or modify it under the terms of the GNU Lesser
15  * General Public License as published by the Free Software
16  * Foundation; either version 2.1 of the License, or (at
17  * your option) any later version.
18  *
19  * This library is distributed in the hope that it will
20  * be useful, but WITHOUT ANY WARRANTY; without even the
21  * implied warranty of MERCHANTABILITY or FITNESS FOR A
22  * PARTICULAR PURPOSE. See the GNU Lesser General Public
23  * License for more details.
24  *
25  * You should have received a copy of the GNU Lesser
26  * General Public License along with this library; if not,
27  * write to the Free Software Foundation, Inc.,
28  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29  *
30  */
31 
32 
58 #ifndef EventManager_h
59 #define EventManager_h
60 
61 
62 #include <stdint.h>
63 
64 
65 
66 // Size of the listener list. Adjust as appropriate for your application.
67 // Requires a total of sizeof(*f())+sizeof(int)+sizeof(bool) bytes of RAM for each unit of size
68 #ifndef EVENTMANAGER_DISPATCH_TABLE_SIZE
69 #define EVENTMANAGER_DISPATCH_TABLE_SIZE 8
70 #endif
71 
72 #if EVENTMANAGER_DISPATCH_TABLE_SIZE > 255
73 #error "EVENTMANAGER_DISPATCH_TABLE_SIZE exceeds size of a uint8_t"
74 #endif
75 
76 
77 
78 
79 // Size of the event two queues. Adjust as appropriate for your application.
80 // Requires a total of 4 * sizeof(int) bytes of RAM for each unit of size
81 #ifndef EVENTMANAGER_EVENT_QUEUE_SIZE
82 #define EVENTMANAGER_EVENT_QUEUE_SIZE 8
83 #endif
84 
85 #if EVENTMANAGER_EVENT_QUEUE_SIZE > 255
86 #error "EVENTMANAGER_EVENT_QUEUE_SIZE exceeds size of a uint8_t"
87 #endif
88 
89 
90 
91 
92 
93 
94 
100 namespace EventManager
101 {
102 
110  {
111  kEventNone,
112  kEventKeyPress,
113  kEventKeyRelease,
114  kEventChar,
115  kEventTime,
116  kEventTimer0,
117  kEventTimer1,
118  kEventTimer2,
119  kEventTimer3,
120  kEventAnalog0,
121  kEventAnalog1,
122  kEventAnalog2,
123  kEventAnalog3,
124  kEventAnalog4,
125  kEventAnalog5,
126  kEventMenu0,
127  kEventMenu1,
128  kEventMenu2,
129  kEventMenu3,
130  kEventMenu4,
131  kEventMenu5,
132  kEventMenu6,
133  kEventMenu7,
134  kEventMenu8,
135  kEventMenu9,
136  kEventSerial,
137  kEventPaint,
138  kEventUser0,
139  kEventUser1,
140  kEventUser2,
141  kEventUser3,
142  kEventUser4,
143  kEventUser5,
144  kEventUser6,
145  kEventUser7,
146  kEventUser8,
147  kEventUser9,
148  };
149 
150 
151 
157  typedef void ( *EventListener )( int eventCode, int eventParam );
158 
159 
160 
172  {
173  kHighPriority,
174  kLowPriority
175  };
176 
177 
178 
189  bool addListener( int eventCode, EventListener listener );
190 
191 
192 
203  bool removeListener( int eventCode, EventListener listener );
204 
205 
206 
219  int removeListener( EventListener listener );
220 
221 
222 
234  bool enableListener( int eventCode, EventListener listener, bool enable );
235 
236 
237 
248  bool isListenerEnabled( int eventCode, EventListener listener );
249 
250 
251 
261  bool setDefaultListener( EventListener listener );
262 
263 
264 
270  void removeDefaultListener();
271 
272 
273 
281  void enableDefaultListener( bool enable );
282 
283 
284 
291  bool isListenerListEmpty();
292 
293 
294 
301  bool isListenerListFull();
302 
303 
304 
311  int numListeners();
312 
313 
314 
323  bool isEventQueueEmpty( EventPriority pri = kLowPriority );
324 
325 
326 
335  bool isEventQueueFull( EventPriority pri = kLowPriority );
336 
337 
338 
347  int getNumEventsInQueue( EventPriority pri = kLowPriority );
348 
349 
350 
361  bool queueEvent( int eventCode, int eventParam, EventPriority pri = kLowPriority );
362 
363 
364 
382  int processEvent();
383 
384 
385 
404  int processAllEvents();
405 
406 };
407 
408 
409 
410 
411 #endif
int numListeners()
Get the number of listeners in the dispatch table.
Definition: EventManager.cpp:422
bool enableListener(int eventCode, EventListener listener, bool enable)
Enable or disable an (event, listener) pair entry in the dispatch table.
Definition: EventManager.cpp:208
void(* EventListener)(int eventCode, int eventParam)
Type for an event listener (a.k.a. callback) function.
Definition: EventManager.h:157
bool isListenerEnabled(int eventCode, EventListener listener)
Obtain the the current enabled/disabled state of an (eventCode, listener) pair.
Definition: EventManager.cpp:214
bool queueEvent(int eventCode, int eventParam, EventPriority pri=kLowPriority)
Tries to add an event into the event queue.
Definition: EventManager.cpp:268
bool setDefaultListener(EventListener listener)
Set a default listener. The default listener is a callback function that is called when an event with...
Definition: EventManager.cpp:220
bool removeListener(int eventCode, EventListener listener)
Remove this (event, listener) pair from the dispatch table. Other listener pairs with the same functi...
Definition: EventManager.cpp:196
bool isListenerListEmpty()
Check if the listener list (a.k.a., dispatch table) is empty.
Definition: EventManager.cpp:238
GenericEvents
This enum provides common event names, purely for user convenience.
Definition: EventManager.h:109
bool addListener(int eventCode, EventListener listener)
Add an (event, listener) pair listener to the dispatch table.
Definition: EventManager.cpp:190
void removeDefaultListener()
Remvoes the default listener. The default listener is a callback function that is called when an even...
Definition: EventManager.cpp:226
void enableDefaultListener(bool enable)
Enable or disable the default listener. The default listener is a callback function that is called wh...
Definition: EventManager.cpp:232
bool isEventQueueEmpty(EventPriority pri=kLowPriority)
Check if the event queue is empty.
Definition: EventManager.cpp:250
int processAllEvents()
Processes all the events in the event queues and dispatches them to the corresponding listeners store...
Definition: EventManager.cpp:373
This namespace bundles the Event Manager functionality. It provides logical cohesion for functions im...
Definition: EventManager.cpp:42
bool isListenerListFull()
Check if the listener list (a.k.a., dispatch table) is full.
Definition: EventManager.cpp:244
int processEvent()
Processes one event from the event queue and dispatches it to the corresponding listeners stored in t...
Definition: EventManager.cpp:337
EventPriority
EventManager recognizes two kinds of events. By default, events are are queued as low priority...
Definition: EventManager.h:171
int getNumEventsInQueue(EventPriority pri=kLowPriority)
Get the number of events in the event queue.
Definition: EventManager.cpp:262
bool isEventQueueFull(EventPriority pri=kLowPriority)
Check if the event queue is full.
Definition: EventManager.cpp:256