-
Notifications
You must be signed in to change notification settings - Fork 1
/
pinda.cpp
241 lines (192 loc) · 5.9 KB
/
pinda.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
/**
@file
@see pinda.h for details
@version 1.0
@author Arco van Geest <[email protected]>
@copyright 2013 Arco van Geest <[email protected]> All right reserved.
This file is part of PinDA.
PinDA 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.
PinDA 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 PinDA. If not, see <http://www.gnu.org/licenses/>.
@date 20130520 Initial documented version
@brief Base of the PinDA framework
*/
#include "pinda.h"
#ifdef ARDUINO
#define println(line) Serial.println(line)
#define print(line) Serial.print(line)
#endif
const int maxInterrupts=32;
const int maxLoop=32;
// constructor
Pinda::Pinda(void) {
interruptCounter=0;
interruptTop=0;
interruptFNTop=0;
//interruptptr = &PindaObj::interrupt;
//serviceloopptr = &PindaObj::serviceLoop;
// void (PindaObj::*PindaInterrupt)(void) = &PindaObj::interrupt;
// void (PindaObj::*PindaServiceLoop)(void) = &PindaObj::serviceLoop;
}
String PindaObj::getName(void){
return objName;
}
void PindaObj::interrupt(void){}
void PindaObj::serviceLoop(void){}
//void PindaObj::on(void){}
void PindaObj::on(uint16_t delay){}
void PindaObj::off(void){}
void PindaObj::toggle(void){}
String PindaObj::getString(void){}
String PindaObj::getFlank(void){}
bool PindaObj::isChanged(void){}
bool PindaObj::hasFlank(void){}
voidFunction interruptFunctions[ maxInterrupts ];
unsigned int interruptInterval[maxInterrupts];
voidFunction loopFunctions[maxLoop];
//pinda timer(s)
void Pinda::StartTimer(void){
// initialize timer5
noInterrupts(); // disable all interrupts
TCCR5A = 0;
TCCR5B = 0;
TCNT5 = 0;
// OCR1A = 31250; // compare match register 16MHz/256/2Hz
OCR5A = 62; // 16Mhz/256=62500 -> every ms..
//OCR5A = 125;
// OCR1A = 6200; // 16Mhz/256=62500 -> every 100ms..
TCCR5B |= (1 << WGM12); // CTC mode
TCCR5B |= (1 << CS12); // 256 prescaler
//tijdelijk uit
TIMSK5 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts();
}
// timer compare interrupt service routine
ISR(TIMER5_COMPA_vect)
{
//tijdelijk uit
pinda.mainInterrupt();
} //ISR pindaInterrupt
/*
//= &<PindaObj>solenoid22::getName();
void (PindaObj::*PindaInterrupt)(void) = &PindaObj::interrupt;
void (PindaObj::*PindaServiceLoop)(void) = &PindaObj::serviceLoop;
String (PindaObj::*getnameptr)(void) = &PindaObj::getName;
PindaFunc pf = &PindaObj::interrupt;
SOLENOID & t= solenoid22;
PindaObj & s= solenoid22;
PindaObj * u = &solenoid22;
Serial.println("pointer...");
Serial.println( (t.*getnameptr)() );
Serial.println( (s.*getnameptr)() );
Serial.println( (u->*getnameptr)() );
Serial.println( t.getName() );
Serial.println("pointer...");
*/
void Pinda::mainInterrupt(void){
static unsigned int interruptCounter;
interruptCounter++;
void (PindaObj::*PindaInterrupt)(void) = &PindaObj::interrupt;
if ( interruptTop) {
for(int i = 0; i < interruptTop; ++i) {
if ( interruptPointers[ i ] ) {
if ( interruptCounter % intteruptInterval[i] == 0 ) {
(interruptPointers[ i ]->*PindaInterrupt)();
}
}
}
}
if ( interruptFNTop ) {
for(int i = 0; i < interruptFNTop; ++i) {
if ( interruptFunctions[ i ] ) {
if ( interruptCounter % intteruptFNInterval[i] == 0 ) {
interruptFunctions[ i ]();
}
}
}
}
} //Pinda::mainInterrupt
int Pinda::AddLoop ( voidFunction fun ){
for(int i = 0; i < maxServiceFunctions; ++i) {
if ( serviceFunctions[i] == NULL ) {
serviceFunctions[i]=fun;
return i;
}
}
println("{ERROR:Out of loopslots}");
return -1;
}
int Pinda::AddInterrupt ( voidFunction fun, unsigned int interval ){
for(int i = 0; i < maxFNInterrupts; ++i) {
if ( interruptFunctions[i] == NULL ) {
interruptFunctions[i]=fun;
intteruptFNInterval[i]=interval;
if ( i > interruptFNTop) interruptFNTop=i;
return i;
}
}
println("{ERROR:Out of interruptslots}");
return -1;
}
int Pinda::AddInterrupt ( PindaObj * obj, unsigned int interval ){
for(int i = 0; i < maxInterrupts; ++i) {
if ( interruptPointers[i] == NULL ) {
interruptPointers[i]=obj;
intteruptInterval[i]=interval;
if ( i > interruptTop) interruptTop=i;
return i;
}
}
println("{ERROR:Out of interruptslots}");
return -1;
}
/** Pinda Main non-timed service loop
* this starts all attached functions @see pindaAddLoop
* it should be placed in the arduino "loop" section
*/
void Pinda::loop(void){
void (PindaObj::*PindaServiceLoop)(void) = &PindaObj::serviceLoop;
for(int i = 0; i < maxInterrupts; ++i) {
if ( servicePointers[ i ] ) {
(servicePointers[ i ]->*PindaServiceLoop)();
}
}
for(int i = 0; i < maxServiceFunctions; ++i) {
if ( serviceFunctions[ i ] ) {
serviceFunctions[ i ]();
}
}
}
int Pinda::freeRam ( void) {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
} //Pinda::freeRam
String Pinda::status(void) {
String s;
s+= "\n### PinDA status ###";
s+= "\nMemory Free : " + String( freeRam() );
s+= "\nPindaInterrupts : " + String( interruptTop );
s+= "\nFunctionInterrupts : " + String( interruptFNTop );
unsigned int count;
count=0;
for(int i = 0; i < maxInterrupts; ++i) {
if ( servicePointers[ i ] ) count++;
}
s+= "\nPindaLoop : " + String( count );
count=0;
for(int i = 0; i < maxServiceFunctions; ++i) {
if ( serviceFunctions[ i ] ) count++;
}
s+= "\nFunctionLoop : " + String( count );
s+= "\n\n";
return s;
} // Pinda::status
// pinda.cpp