-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPciManager.cpp
172 lines (147 loc) · 5.08 KB
/
PciManager.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
/**
* File: PciManager.cpp
* Description:
* PciManager library is an easy to use Pin Change Interrupt manager for Arduino.
*
* Author: Balazs Kelemen
* Contact: [email protected]
* Copyright: 2017 Balazs Kelemen
* Copying permission statement:
This file is part of PciManager.
PciManager is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Arduino.h"
#include "PciManager.h"
//#include <avr/io.h>
//#include <avr/interrupt.h>
/**
* Add listener to the chain.
*/
void PciManagerClass::add(PciListener* listener) {
// -- A listener should be registered only once.
this->remove(listener);
if(this->_firstListener == NULL) {
// -- This is the first listener being registered.
this->_firstListener = listener;
} else {
PciListener* lastlistener = this->_firstListener;
// -- Find the last listener, to build a chain.
while(lastlistener->pciNextListener != NULL) {
lastlistener = lastlistener->pciNextListener;
}
// -- Last listener found, let's add this listener to the end of the chain.
lastlistener->pciNextListener = listener;
}
listener->pciNextListener = NULL;
}
/**
* Remove listener from the chain.
*/
void PciManagerClass::remove(PciListener* listener) {
if(this->_firstListener != NULL) {
if(this->_firstListener == listener) {
// -- This was the first listener.
this->_firstListener = listener->pciNextListener;
} else {
PciListener* lastlistener = this->_firstListener;
// -- Find this listener in the chain.
while(lastlistener->pciNextListener != NULL) {
if(lastlistener->pciNextListener == listener) {
// -- Remove the listener with joining the chain.
lastlistener->pciNextListener = listener->pciNextListener;
break;
}
lastlistener = lastlistener->pciNextListener;
}
}
}
}
void PciManagerClass::registerListener(byte pin, PciListener* listener) {
// -- PCINT vector will be 0, 1 or 2 depending on the pin.
// -- digitalPinToPCICRbit(pin) will calculate which one it is.
#if defined(digitalPinToPCICRbit)
listener->pciVector = digitalPinToPCICRbit(pin);
listener->pciPin = pin;
volatile uint8_t* pcicr = digitalPinToPCICR(pin);
*pcicr |= (1 << listener->pciVector);
volatile uint8_t* pcmsk = digitalPinToPCMSK(pin);
*pcmsk |= (1 << digitalPinToPCMSKbit(pin));
#endif
this->add(listener);
}
void PciManagerClass::registerListener(PciListener* listener) {
this->registerListener(listener->pciPin, listener);
}
void PciManagerClass::removeListener(PciListener* listenerToRemove) {
this->remove(listenerToRemove);
boolean hasMoreListenersOnSamePin = false;
boolean hasMoreListenersOnSameVector = false;
PciListener* listener = _firstListener;
while(listener != NULL) {
if(listener->pciPin == listenerToRemove->pciPin) {
hasMoreListenersOnSamePin = true;
hasMoreListenersOnSameVector = true;
break;
}
if(listener->pciVector == listenerToRemove->pciVector) {
hasMoreListenersOnSameVector = true;
}
listener = listener->pciNextListener;
}
if(!hasMoreListenersOnSamePin) {
// -- Remove mask if no other uses this pin.
#if defined(digitalPinToPCICRbit)
volatile uint8_t* pcmsk = digitalPinToPCMSK(listenerToRemove->pciPin);
*pcmsk &= ~(1 << digitalPinToPCMSKbit(listenerToRemove->pciPin));
if(!hasMoreListenersOnSameVector) {
// -- Remove vector registration if no listeners for this vector.
volatile uint8_t* pcicr = digitalPinToPCICR(listenerToRemove->pciPin);
*pcicr &= ~(1 << listenerToRemove->pciVector);
}
#endif
}
}
/**
* Walk through the chain and call all listener.
*/
void PciManagerClass::callListeners(byte pciVectorId) {
// -- (If first is NULL, than nothing is registered.)
PciListener* listener = _firstListener;
while(listener != NULL) {
if(listener->pciVector == pciVectorId) {
listener->pciHandleInterrupt(pciVectorId);
}
listener = listener->pciNextListener;
}
}
/**
* Global interrupt handling implementations.
*/
#if defined(PCINT0_vect)
ISR(PCINT0_vect){
PciManager.callListeners(0);
}
#endif
#if defined(PCINT1_vect)
ISR(PCINT1_vect){
PciManager.callListeners(1);
}
#endif
#if defined(PCINT2_vect)
ISR(PCINT2_vect){
PciManager.callListeners(2);
}
#endif
/**
* Create a singleton from this manager class.
*/
PciManagerClass PciManager;