-
Notifications
You must be signed in to change notification settings - Fork 0
/
PulseScanner.cpp
275 lines (225 loc) · 6.28 KB
/
PulseScanner.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
/*
* PulseScanner.cpp
*
* Created on: Feb 5, 2013
* Author: jpm8766
*/
#include "PulseScanner.h"
const struct sigevent * PulseScanner::interruptReceived(void *arg, int id) {
PulseScanner* self = (PulseScanner*) arg;
//update the value at the current position in the buffer
self->speedPulses[self->speedPulseIndex]++;
if(self->calcFlag)
self->distPulseCount++;
//clear the interrupt.
out8(self->cmd_handle, (0b00001111));
return NULL;
}
PulseScanner::PulseScanner(uintptr_t cmd, uintptr_t led) :
cmd_handle(cmd), ledHandle(led) {
this->scannerReset();
}
PulseScanner::~PulseScanner() {
}
void PulseScanner::scannerReset() {
calcCircumference = INITIAL_WHEEL_CIRCUMFERENCE;
circumference = INITIAL_WHEEL_CIRCUMFERENCE;
tripDistKM = 0;
clockCount = 0;
speedPulseIndex = 0;
distPulseCount = 0;
speed = 0;
units = KM;
tripMode = TRIP_MANUAL;
calcFlag = false;
settingCir = false;
for(int i = 0; i < MAX_TIME_CALC; i++) {
speedPulses[i] = 0;
}
}
void* PulseScanner::run() {
//try to associate our interrupt with the board...
interruptID = InterruptAttach(DAQ_IRQ, interruptReceived, this,
sizeof(this), 0);
if (interruptID == -1) {
fprintf(stderr, "can't attach to IRQ %d\n", DAQ_IRQ);
perror(NULL);
exit(-1);
}
char even = 0;
// clear any lingering interrupts.
out8(cmd_handle, (0b00001111));
// Timer for calculate functionality.
timer_t oneSecTimer;
// Create a timeout for the push button hold.
struct sigevent event;
SIGEV_THREAD_INIT(&event, PulseScanner::calculate, this, NULL);
// Configure the timeout spec.
itimerspec oneSecondTimerSpec;
oneSecondTimerSpec.it_value.tv_sec = 1;
oneSecondTimerSpec.it_value.tv_nsec = 0;
oneSecondTimerSpec.it_interval.tv_sec = 1;
oneSecondTimerSpec.it_interval.tv_nsec = 0;
// Create and set the timer.
timer_create(CLOCK_REALTIME, &event, &oneSecTimer);
timer_settime(oneSecTimer, 0, &oneSecondTimerSpec, NULL);
char ledMask = 0;
for(int i = 0; i < 3; i++) {
ledMask |= LED_MASK[i];
}
out8(ledHandle, in8(ledHandle) & ~ledMask);
//while running...
while (!killThread) {
//so long as we have gotten pulses...keep flashing the LEDs for calculating (if applicable)
// and the calculating LED (if applicable)
flashLEDs(even);
usleep(PULSE_SCAN_POLL_RATE);
//periodically clear the interrupt here so it can recover if the frequency changes too quickly
out8(cmd_handle, (0b00001111));
even = (even + 1) % 2;
}
return NULL;
}
double PulseScanner::averageSpeed() {
//return the Units/Hour
return (distance() / ((double)clockCount / (double)SECONDS_PER_HOUR));
}
double PulseScanner::currentSpeed() {
//set the scaleFactor to either be for KM or to MILES
double scaleFactor = (units == KM ? 1 : KM_TO_MILES);
return (speed * scaleFactor);
}
double PulseScanner::distance() {
//set the scaleFactor to either be for KM or to MILES
double scaleFactor = (units == KM ? 1 : KM_TO_MILES);
//return the Units/Hour
return (tripDistKM * scaleFactor);
}
unsigned int PulseScanner::elapsedTime() {
return clockCount;
}
void PulseScanner::incrementCircumference() {
settingCir = true;
circumference
= (circumference == MAX_WHEEL_CIRCUMFERENCE ? MIN_WHEEL_CIRCUMFERENCE
: circumference + 1);
}
void PulseScanner::setCircumference() {
settingCir = false;
calcCircumference = circumference;
}
int PulseScanner::getCircumference() {
if(settingCir) {
return circumference;
}
return calcCircumference;
}
void PulseScanner::resetTripValues() {
tripDistKM = 0;
clockCount = 0;
distPulseCount = 0;
}
void PulseScanner::toggleUnits() {
units = (units == KM ? MILES : KM);
updateUnitsLED();
}
DistanceUnit PulseScanner::getUnits() {
return units;
}
void PulseScanner::toggleTripMode() {
tripMode = (tripMode == TRIP_AUTO ? TRIP_MANUAL : TRIP_AUTO);
calcFlag = false;
}
void PulseScanner::toggleCalculate() {
if (tripMode == TRIP_MANUAL) {
calcFlag = !calcFlag;
}
}
void PulseScanner::setWheelLED(bool high) {
if (high) {
out8(ledHandle, in8(ledHandle) | LED_MASK[0]);
} else {
out8(ledHandle, in8(ledHandle) & ~LED_MASK[0]);
}
}
void PulseScanner::setCalcLED(bool high) {
if (tripMode == TRIP_MANUAL) {
if (calcFlag) {
if (high) {
out8(ledHandle, in8(ledHandle) | LED_MASK[1]);
} else {
out8(ledHandle, in8(ledHandle) & ~LED_MASK[1]);
}
} else {
out8(ledHandle, in8(ledHandle) & ~LED_MASK[1]);
}
} else {
if (calcFlag) {
if (high) {
out8(ledHandle, in8(ledHandle) | LED_MASK[1]);
} else {
out8(ledHandle, in8(ledHandle) & ~LED_MASK[1]);
}
} else {
out8(ledHandle, in8(ledHandle) | LED_MASK[1]);
}
}
}
void PulseScanner::updateUnitsLED() {
// LED on for miles, LED off for km.
if (units == MILES) {
out8(ledHandle, in8(ledHandle) | LED_MASK[2]);
} else {
out8(ledHandle, in8(ledHandle) & ~LED_MASK[2]);
}
}
void PulseScanner::flashLEDs(int even) {
int cachedPulse = 0;
for(int i = 0; i < MAX_TIME_CALC; i++) {
cachedPulse += speedPulses[i];
}
//if there have been pulses over the last 5 seconds...
if (cachedPulse > 0) {
if (tripMode == TRIP_AUTO) {
calcFlag = true;
}
if (even) {
setWheelLED(true);
} else {
setWheelLED(false);
}
// else if there havent been any pulses...
} else if (cachedPulse == 0) {
if (tripMode == TRIP_AUTO) {
calcFlag = false;
}
setWheelLED(false);
}
if (even) {
setCalcLED(true);
} else {
setCalcLED(false);
}
}
void PulseScanner::calculate(sigval arg) {
PulseScanner* self = (PulseScanner*)arg.sival_ptr;
//iterate over the available pulses and accumulate the total.
unsigned int cachedPulse = 0;
for(int i = 0; i < MAX_TIME_CALC; i++) {
cachedPulse += self->speedPulses[i];
}
//move to the next index and reset it (reset the oldest count in the array
self->speedPulseIndex = (self->speedPulseIndex + 1) % MAX_TIME_CALC;
self->speedPulses[self->speedPulseIndex] = 0;
//calculate the distance travelled in that time and the speed based on that distance.
double td = ((double)self->calcCircumference * cachedPulse * METERS_PER_CM) * KM_PER_METER;
double s = ((td) / MAX_TIME_CALC) * (SEC_PER_HOUR);
self->speed = s;
//if we are calculating, accrue time and distance travelled.
if (self->calcFlag) {
self->clockCount += 1;
cachedPulse = self->distPulseCount;
self->distPulseCount = 0;
self->tripDistKM += (self->calcCircumference * cachedPulse * METERS_PER_CM) * KM_PER_METER;
}
}