forked from WorldFamousElectronics/PulseSensorPlayground
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPulseSensorPlayground.cpp
More file actions
238 lines (192 loc) · 6.73 KB
/
PulseSensorPlayground.cpp
File metadata and controls
238 lines (192 loc) · 6.73 KB
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
/*
A central Playground object to manage a set of PulseSensors.
See https://www.pulsesensor.com to get started.
Copyright World Famous Electronics LLC - see LICENSE
Contributors:
Joel Murphy, https://pulsesensor.com
Yury Gitman, https://pulsesensor.com
Bradford Needham, @bneedhamia, https://bluepapertech.com
Licensed under the MIT License, a copy of which
should have been included with this software.
This software is not intended for medical use.
*/
#include <PulseSensorPlayground.h>
// Define the "this" pointer for the ISR
PulseSensorPlayground *PulseSensorPlayground::OurThis;
PulseSensorPlayground::PulseSensorPlayground(int numberOfSensors) {
// Save a static pointer to our playground so the ISR can read it.
OurThis = this;
// By default, we attempt to use interrupts to sample.
UsingInterrupts = true;
// Dynamically create the array to minimize ram usage.
SensorCount = (byte) numberOfSensors;
Sensors = new PulseSensor[SensorCount];
#if PULSE_SENSOR_TIMING_ANALYSIS
// We want sample timing analysis, so we construct it.
pTiming = new PulseSensorTimingStatistics(MICROS_PER_READ, 500 * 30L);
#endif // PULSE_SENSOR_TIMING_ANALYSIS
}
boolean PulseSensorPlayground::PulseSensorPlayground::begin() {
for (int i = 0; i < SensorCount; ++i) {
Sensors[i].initializeLEDs();
}
// Note the time, for non-interrupt sampling and for timing statistics.
NextSampleMicros = micros() + MICROS_PER_READ;
SawNewSample = false;
#if PULSE_SENSOR_MEMORY_USAGE
// Report the RAM usage and hang.
printMemoryUsage();
for (;;);
#endif // PULSE_SENSOR_MEMORY_USAGE
// Lastly, set up and turn on the interrupts.
if (UsingInterrupts) {
// If this Arduino's interrupt isn't supported, don't use it.
if (!PulseSensorPlaygroundSetupInterrupt()) {
UsingInterrupts = false;
// The user requested interrupts, but didn't get them. Say so.
return false;
}
}
return true;
}
void PulseSensorPlayground::analogInput(int inputPin, int sensorIndex) {
if (sensorIndex != constrain(sensorIndex, 0, SensorCount)) {
return; // out of range.
}
Sensors[sensorIndex].analogInput(inputPin);
}
void PulseSensorPlayground::blinkOnPulse(int blinkPin, int sensorIndex) {
if (sensorIndex != constrain(sensorIndex, 0, SensorCount)) {
return; // out of range.
}
Sensors[sensorIndex].blinkOnPulse(blinkPin);
}
void PulseSensorPlayground::fadeOnPulse(int fadePin, int sensorIndex) {
if (sensorIndex != constrain(sensorIndex, 0, SensorCount)) {
return; // out of range.
}
Sensors[sensorIndex].fadeOnPulse(fadePin);
}
boolean PulseSensorPlayground::sawNewSample() {
/*
If using interrupts, this function reads and clears the
'saw a sample' flag that is set by the ISR.
When not using interrupts, this function sees whether it's time
to sample and, if so, reads the sample and processes it.
*/
if (UsingInterrupts) {
// Disable interrupts to avoid a race with the ISR.
DISABLE_PULSE_SENSOR_INTERRUPTS;
boolean sawOne = SawNewSample;
SawNewSample = false;
ENABLE_PULSE_SENSOR_INTERRUPTS;
return sawOne;
}
// Not using interrupts
unsigned long nowMicros = micros();
if ((long) (NextSampleMicros - nowMicros) > 0L) {
return false; // not time yet.
}
NextSampleMicros = nowMicros + MICROS_PER_READ;
#if PULSE_SENSOR_TIMING_ANALYSIS
if (pTiming->recordSampleTime() <= 0) {
pTiming->outputStatistics(SerialOutput.getSerial());
for (;;); // Hang because we've disturbed the timing.
}
#endif // PULSE_SENSOR_TIMING_ANALYSIS
// Act as if the ISR was called.
onSampleTime();
SawNewSample = false;
return true;
}
void PulseSensorPlayground::onSampleTime() {
// Typically called from the ISR.
/*
Read the voltage from each PulseSensor.
We do this separately from processing the voltages
to minimize jitter in acquiring the signal.
*/
for (int i = 0; i < SensorCount; ++i) {
Sensors[i].readNextSample();
}
// Process those voltages.
for (int i = 0; i < SensorCount; ++i) {
Sensors[i].processLatestSample();
Sensors[i].updateLEDs();
}
// Set the flag that says we've read a sample since the Sketch checked.
SawNewSample = true;
}
int PulseSensorPlayground::getLatestSample(int sensorIndex) {
if (sensorIndex != constrain(sensorIndex, 0, SensorCount)) {
return -1; // out of range.
}
return Sensors[sensorIndex].getLatestSample();
}
int PulseSensorPlayground::getBeatsPerMinute(int sensorIndex) {
if (sensorIndex != constrain(sensorIndex, 0, SensorCount)) {
return -1; // out of range.
}
return Sensors[sensorIndex].getBeatsPerMinute();
}
int PulseSensorPlayground::getInterBeatIntervalMs(int sensorIndex) {
if (sensorIndex != constrain(sensorIndex, 0, SensorCount)) {
return -1; // out of range.
}
return Sensors[sensorIndex].getInterBeatIntervalMs();
}
boolean PulseSensorPlayground::sawStartOfBeat(int sensorIndex) {
if (sensorIndex != constrain(sensorIndex, 0, SensorCount)) {
return false; // out of range.
}
return Sensors[sensorIndex].sawStartOfBeat();
}
boolean PulseSensorPlayground::isInsideBeat(int sensorIndex) {
if (sensorIndex != constrain(sensorIndex, 0, SensorCount)) {
return false; // out of range.
}
return Sensors[sensorIndex].isInsideBeat();
}
void PulseSensorPlayground::setSerial(Stream &output) {
SerialOutput.setSerial(output);
}
void PulseSensorPlayground::setOutputType(byte outputType) {
SerialOutput.setOutputType(outputType);
}
void PulseSensorPlayground::outputSample() {
SerialOutput.outputSample(Sensors, SensorCount);
}
void PulseSensorPlayground::outputBeat(int sensorIndex) {
SerialOutput.outputBeat(Sensors, SensorCount, sensorIndex);
}
#if PULSE_SENSOR_MEMORY_USAGE
void PulseSensorPlayground::printMemoryUsage() {
char stack = 1;
extern char *__data_start;
extern char *__data_end;
extern char *__bss_start;
extern char *__bss_end;
extern char *__heap_start;
extern char *__heap_end;
int data_size = (int)&__data_end - (int)&__data_start;
int bss_size = (int)&__bss_end - (int)&__data_end;
int heap_end = (int)&stack - (int)&__malloc_margin;
int heap_size = heap_end - (int)&__bss_end;
int stack_size = RAMEND - (int)&stack + 1;
int available = (RAMEND - (int)&__data_start + 1);
available -= data_size + bss_size + heap_size + stack_size;
Stream *pOut = SerialOutput.getSerial();
if (pOut) {
pOut->print(F("data "));
pOut->println(data_size);
pOut->print(F("bss "));
pOut->println(bss_size);
pOut->print(F("heap "));
pOut->println(heap_size);
pOut->print(F("stack "));
pOut->println(stack_size);
pOut->print(F("total "));
pOut->println(data_size + bss_size + heap_size + stack_size);
}
}
#endif // PULSE_SENSOR_MEMORY_USAGE