-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventManager.cpp
641 lines (498 loc) · 16.5 KB
/
EventManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
* EventManager.cpp
*
* An event handling system for Arduino.
*
* Author: igormt@alumni.caltech.edu
* Copyright (c) 2013 Igor Mikolic-Torreira
*
* Inspired by and adapted from the
* Arduino Event System library by
* Author: mromani@ottotecnica.com
* Copyright (c) 2010 OTTOTECNICA Italy
*
* This library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser
* General Public License as published by the Free Software
* Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser
* General Public License along with this library; if not,
* write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "EventManager.h"
namespace
{
// This class takes care of turning interrupts on and off.
// There is a different implementation of this class for each architecture that
// has a different interrupt model. #if macros ensure only one version is defined.
#if defined( __AVR_ARCH__ )
class SuppressInterrupts
{
public:
// Record the current state and suppress interrupts when the object is instantiated.
SuppressInterrupts()
{
mInterruptsWereOn = (SREG & (1<<SREG_I));
cli();
}
// Restore whatever interrupt state was active before
~SuppressInterrupts()
{
// Turn on global interrupts, only if they were already on
if ( mInterruptsWereOn )
{
sei();
}
}
private:
uint8_t mInterruptsWereOn;
};
#elif defined( SAM )
class SuppressInterrupts
{
public:
// Record the current state and suppress interrupts when the object is instantiated.
SuppressInterrupts()
{
mInterruptsWereOn = (__get_PRIMASK() == 0);
__disable_irq();
}
// Restore whatever interrupt state was active before
~SuppressInterrupts()
{
// Turn on interrupts, only if they were already on
if ( mInterruptsWereOn )
{
__enable_irq();
}
}
private:
uint8_t mInterruptsWereOn;
};
#elif defined( ESP8266 )
class SuppressInterrupts
{
public:
// Record the current state and suppress interrupts when the object is instantiated.
SuppressInterrupts()
{
// This turns off interrupts and gets the old state in one function call
// See https://github.com/esp8266/Arduino/issues/615 for details
// level 15 will disable ALL interrupts,
// level 0 will enable ALL interrupts
mSavedInterruptState = xt_rsil( 15 );
}
// Restore whatever interrupt state was active before
~SuppressInterrupts()
{
// Restore the old interrupt state
xt_wsr_ps( mSavedInterruptState );
}
private:
uint32_t mSavedInterruptState;
};
#elif defined( CORE_TEENSY )
class SuppressInterrupts
{
public:
//Reference: https://www.pjrc.com/teensy/interrupts.html
//Backup the interrupt enable state and restore it
SuppressInterrupts()
{
mSregBackup = SREG; /* save interrupt enable/disable state */
cli(); /* disable the global interrupt */
}
~SuppressInterrupts()
{
SREG = mSregBackup; /* restore interrupt state */
}
private:
uint8_t mSregBackup;
};
#else
#error "Unknown microcontroller: Need to implement class SuppressInterrupts for this microcontroller."
#endif
}
#if EVENTMANAGER_DEBUG
#define EVTMGR_DEBUG_PRINT( x ) Serial.print( x );
#define EVTMGR_DEBUG_PRINTLN( x ) Serial.println( x );
#define EVTMGR_DEBUG_PRINT_PTR( x ) Serial.print( reinterpret_cast<unsigned long>( x ), HEX );
#define EVTMGR_DEBUG_PRINTLN_PTR( x ) Serial.println( reinterpret_cast<unsigned long>( x ), HEX );
#else
#define EVTMGR_DEBUG_PRINT( x )
#define EVTMGR_DEBUG_PRINTLN( x )
#define EVTMGR_DEBUG_PRINT_PTR( x )
#define EVTMGR_DEBUG_PRINTLN_PTR( x )
#endif
EventManager::EventManager()
{
}
int EventManager::processEvent()
{
int eventCode;
int param;
int handledCount = 0;
if ( mHighPriorityQueue.popEvent( &eventCode, ¶m ) )
{
handledCount = mListeners.sendEvent( eventCode, param );
EVTMGR_DEBUG_PRINT( "processEvent() hi-pri event " )
EVTMGR_DEBUG_PRINT( eventCode )
EVTMGR_DEBUG_PRINT( ", " )
EVTMGR_DEBUG_PRINT( param )
EVTMGR_DEBUG_PRINT( " sent to " )
EVTMGR_DEBUG_PRINTLN( handledCount )
}
// If no high-pri events handled (either because there are no high-pri events or
// because there are no listeners for them), then try low-pri events
if ( !handledCount && mLowPriorityQueue.popEvent( &eventCode, ¶m ) )
{
handledCount = mListeners.sendEvent( eventCode, param );
EVTMGR_DEBUG_PRINT( "processEvent() lo-pri event " )
EVTMGR_DEBUG_PRINT( eventCode )
EVTMGR_DEBUG_PRINT( ", " )
EVTMGR_DEBUG_PRINT( param )
EVTMGR_DEBUG_PRINT( " sent to " )
EVTMGR_DEBUG_PRINTLN( handledCount )
}
return handledCount;
}
int EventManager::processAllEvents()
{
int eventCode;
int param;
int handledCount = 0;
while ( mHighPriorityQueue.popEvent( &eventCode, ¶m ) )
{
handledCount += mListeners.sendEvent( eventCode, param );
EVTMGR_DEBUG_PRINT( "processEvent() hi-pri event " )
EVTMGR_DEBUG_PRINT( eventCode )
EVTMGR_DEBUG_PRINT( ", " )
EVTMGR_DEBUG_PRINT( param )
EVTMGR_DEBUG_PRINT( " sent to " )
EVTMGR_DEBUG_PRINTLN( handledCount )
}
while ( mLowPriorityQueue.popEvent( &eventCode, ¶m ) )
{
handledCount += mListeners.sendEvent( eventCode, param );
EVTMGR_DEBUG_PRINT( "processEvent() lo-pri event " )
EVTMGR_DEBUG_PRINT( eventCode )
EVTMGR_DEBUG_PRINT( ", " )
EVTMGR_DEBUG_PRINT( param )
EVTMGR_DEBUG_PRINT( " sent to " )
EVTMGR_DEBUG_PRINTLN( handledCount )
}
return handledCount;
}
/********************************************************************/
EventManager::ListenerList::ListenerList() :
mNumListeners( 0 ), mDefaultCallback( 0 )
{
}
int EventManager::ListenerList::numListeners()
{
return mNumListeners;
};
int EventManager::numListeners()
{
return mListeners.numListeners();
};
boolean EventManager::ListenerList::addListener( int eventCode, EventListener listener )
{
EVTMGR_DEBUG_PRINT( "addListener() enter " )
EVTMGR_DEBUG_PRINT( eventCode )
EVTMGR_DEBUG_PRINT( ", " )
EVTMGR_DEBUG_PRINTLN_PTR( listener )
// Argument check
if ( !listener )
{
return false;
}
// Check for full dispatch table
if ( isFull() )
{
EVTMGR_DEBUG_PRINTLN( "addListener() list full" )
return false;
}
mListeners[ mNumListeners ].callback = listener;
mListeners[ mNumListeners ].eventCode = eventCode;
mListeners[ mNumListeners ].enabled = true;
mNumListeners++;
EVTMGR_DEBUG_PRINTLN( "addListener() listener added" )
return true;
}
boolean EventManager::ListenerList::removeListener( int eventCode, EventListener listener )
{
EVTMGR_DEBUG_PRINT( "removeListener() enter " )
EVTMGR_DEBUG_PRINT( eventCode )
EVTMGR_DEBUG_PRINT( ", " )
EVTMGR_DEBUG_PRINTLN_PTR( listener )
if ( mNumListeners == 0 )
{
EVTMGR_DEBUG_PRINTLN( "removeListener() no listeners" )
return false;
}
int k = searchListeners( eventCode, listener );
if ( k < 0 )
{
EVTMGR_DEBUG_PRINTLN( "removeListener() not found" )
return false;
}
for ( int i = k; i < mNumListeners - 1; i++ )
{
mListeners[ i ].callback = mListeners[ i + 1 ].callback;
mListeners[ i ].eventCode = mListeners[ i + 1 ].eventCode;
mListeners[ i ].enabled = mListeners[ i + 1 ].enabled;
}
mNumListeners--;
EVTMGR_DEBUG_PRINTLN( "removeListener() removed" )
return true;
}
int EventManager::ListenerList::removeListener( EventListener listener )
{
EVTMGR_DEBUG_PRINT( "removeListener() enter " )
EVTMGR_DEBUG_PRINTLN_PTR( listener )
if ( mNumListeners == 0 )
{
EVTMGR_DEBUG_PRINTLN( " removeListener() no listeners" )
return 0;
}
int removed = 0;
int k;
while ((k = searchListeners( listener )) >= 0 )
{
for ( int i = k; i < mNumListeners - 1; i++ )
{
mListeners[ i ].callback = mListeners[ i + 1 ].callback;
mListeners[ i ].eventCode = mListeners[ i + 1 ].eventCode;
mListeners[ i ].enabled = mListeners[ i + 1 ].enabled;
}
mNumListeners--;
removed++;
}
EVTMGR_DEBUG_PRINT( " removeListener() removed " )
EVTMGR_DEBUG_PRINTLN( removed )
return removed;
}
boolean EventManager::ListenerList::enableListener( int eventCode, EventListener listener, boolean enable )
{
EVTMGR_DEBUG_PRINT( "enableListener() enter " )
EVTMGR_DEBUG_PRINT( eventCode )
EVTMGR_DEBUG_PRINT( ", " )
EVTMGR_DEBUG_PRINT_PTR( listener )
EVTMGR_DEBUG_PRINT( ", " )
EVTMGR_DEBUG_PRINTLN( enable )
if ( mNumListeners == 0 )
{
EVTMGR_DEBUG_PRINTLN( "enableListener() no listeners" )
return false;
}
int k = searchListeners( eventCode, listener );
if ( k < 0 )
{
EVTMGR_DEBUG_PRINTLN( "enableListener() not found fail" )
return false;
}
mListeners[ k ].enabled = enable;
EVTMGR_DEBUG_PRINTLN( "enableListener() success" )
return true;
}
boolean EventManager::ListenerList::isListenerEnabled( int eventCode, EventListener listener )
{
if ( mNumListeners == 0 )
{
return false;
}
int k = searchListeners( eventCode, listener );
if ( k < 0 )
{
return false;
}
return mListeners[ k ].enabled;
}
int EventManager::ListenerList::sendEvent( int eventCode, int param )
{
EVTMGR_DEBUG_PRINT( "sendEvent() enter " )
EVTMGR_DEBUG_PRINT( eventCode )
EVTMGR_DEBUG_PRINT( ", " )
EVTMGR_DEBUG_PRINTLN( param )
int handlerCount = 0;
for ( int i = 0; i < mNumListeners; i++ )
{
if ( ( mListeners[ i ].callback != 0 ) && ( mListeners[ i ].eventCode == eventCode ) && mListeners[ i ].enabled )
{
handlerCount++;
(*mListeners[ i ].callback)( eventCode, param );
}
}
EVTMGR_DEBUG_PRINT( "sendEvent() sent to " )
EVTMGR_DEBUG_PRINTLN( handlerCount )
if ( !handlerCount )
{
if ( ( mDefaultCallback != 0 ) && mDefaultCallbackEnabled )
{
handlerCount++;
(*mDefaultCallback)( eventCode, param );
EVTMGR_DEBUG_PRINTLN( "sendEvent() event sent to default" )
}
#if EVENTMANAGER_DEBUG
else
{
EVTMGR_DEBUG_PRINTLN( "sendEvent() no default" )
}
#endif
}
return handlerCount;
}
boolean EventManager::ListenerList::setDefaultListener( EventListener listener )
{
EVTMGR_DEBUG_PRINT( "setDefaultListener() enter " )
EVTMGR_DEBUG_PRINTLN_PTR( listener )
if ( listener == 0 )
{
return false;
}
mDefaultCallback = listener;
mDefaultCallbackEnabled = true;
return true;
}
void EventManager::ListenerList::removeDefaultListener()
{
mDefaultCallback = 0;
mDefaultCallbackEnabled = false;
}
void EventManager::ListenerList::enableDefaultListener( boolean enable )
{
mDefaultCallbackEnabled = enable;
}
int EventManager::ListenerList::searchListeners( int eventCode, EventListener listener )
{
for ( int i = 0; i < mNumListeners; i++ )
{
if ( ( mListeners[i].eventCode == eventCode ) && ( mListeners[i].callback == listener ) )
{
return i;
}
}
return -1;
}
int EventManager::ListenerList::searchListeners( EventListener listener )
{
for ( int i = 0; i < mNumListeners; i++ )
{
if ( mListeners[i].callback == listener )
{
return i;
}
}
return -1;
}
int EventManager::ListenerList::searchEventCode( int eventCode )
{
for ( int i = 0; i < mNumListeners; i++ )
{
if ( mListeners[i].eventCode == eventCode )
{
return i;
}
}
return -1;
}
/******************************************************************************/
EventManager::EventQueue::EventQueue() :
mEventQueueHead( 0 ),
mEventQueueTail( 0 ),
mNumEvents( 0 )
{
for ( int i = 0; i < kEventQueueSize; i++ )
{
mEventQueue[i].code = EventManager::kEventNone;
mEventQueue[i].param = 0;
}
}
boolean EventManager::EventQueue::queueEvent( int eventCode, int eventParam )
{
/*
* The call to noInterrupts() MUST come BEFORE the full queue check.
*
* If the call to isFull() returns FALSE but an asynchronous interrupt queues
* an event, making the queue full, before we finish inserting here, we will then
* corrupt the queue (we'll add an event to an already full queue). So the entire
* operation, from the call to isFull() to completing the inserting (if not full)
* must be atomic.
*
* Note that this race condition can only arise IF both interrupt and non-interrupt (normal)
* code add events to the queue. If only normal code adds events, this can't happen
* because then there are no asynchronous additions to the queue. If only interrupt
* handlers add events to the queue, this can't happen because further interrupts are
* blocked while an interrupt handler is executing. This race condition can only happen
* when an event is added to the queue by normal (non-interrupt) code and simultaneously
* an interrupt handler tries to add an event to the queue. This is the case that the
* cli() (= noInterrupts()) call protects against.
*
* Contrast this with the logic in popEvent().
*
*/
SuppressInterrupts interruptsOff; // Interrupts automatically restored when exit block
// ATOMIC BLOCK BEGIN
boolean retVal = false;
if ( !isFull() )
{
// Store the event at the tail of the queue
mEventQueue[ mEventQueueTail ].code = eventCode;
mEventQueue[ mEventQueueTail ].param = eventParam;
// Update queue tail value
mEventQueueTail = ( mEventQueueTail + 1 ) % kEventQueueSize;;
// Update number of events in queue
mNumEvents++;
retVal = true;
}
// ATOMIC BLOCK END
return retVal;
}
boolean EventManager::EventQueue::popEvent( int* eventCode, int* eventParam )
{
/*
* The call to noInterrupts() MUST come AFTER the empty queue check.
*
* There is no harm if the isEmpty() call returns an "incorrect" TRUE response because
* an asynchronous interrupt queued an event after isEmpty() was called but before the
* return is executed. We'll pick up that asynchronously queued event the next time
* popEvent() is called.
*
* If interrupts are suppressed before the isEmpty() check, we pretty much lock-up the Arduino.
* This is because popEvent(), via processEvents(), is normally called inside loop(), which
* means it is called VERY OFTEN. Most of the time (>99%), the event queue will be empty.
* But that means that we'll have interrupts turned off for a significant fraction of the
* time. We don't want to do that. We only want interrupts turned off when we are
* actually manipulating the queue.
*
* Contrast this with the logic in queueEvent().
*
*/
if ( isEmpty() )
{
return false;
}
SuppressInterrupts interruptsOff; // Interrupts automatically restored when exit block
// Pop the event from the head of the queue
// Store event code and event parameter into the user-supplied variables
*eventCode = mEventQueue[ mEventQueueHead ].code;
*eventParam = mEventQueue[ mEventQueueHead ].param;
// Clear the event (paranoia)
mEventQueue[ mEventQueueHead ].code = EventManager::kEventNone;
// Update the queue head value
mEventQueueHead = ( mEventQueueHead + 1 ) % kEventQueueSize;
// Update number of events in queue
mNumEvents--;
return true;
}