Skip to content

Commit 843d97c

Browse files
committed
Temp commit
1 parent 9dddaea commit 843d97c

File tree

7 files changed

+71
-219
lines changed

7 files changed

+71
-219
lines changed

examples/Example-1-Raw-PPG-plot-openview/Example-1-Raw-PPG-plot-openview.ino

Lines changed: 20 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -27,81 +27,40 @@
2727
/////////////////////////////////////////////////////////////////////////////////////////
2828

2929
#include "protocentral_afe44xx.h"
30-
#include "Protocentral_spo2_algorithm.h"
31-
#include "protocentral_hr_algorithm.h"
3230

33-
// Default pins CS=7, PWDN=4, DRDY=2
34-
35-
#define AFE44XX_CS_PIN 10
36-
#define AFE44XX_PWDN_PIN 21
37-
#define AFE44XX_DRDY_PIN 14
38-
39-
#define CES_CMDIF_PKT_START_1 0x0A
40-
#define CES_CMDIF_PKT_START_2 0xFA
41-
#define CES_CMDIF_TYPE_DATA 0x02
42-
#define CES_CMDIF_PKT_STOP 0x0B
43-
#define DATA_LEN 10
31+
// Default pins used by this example
32+
#define AFE44XX_CS_PIN 7
33+
#define AFE44XX_PWDN_PIN 4
34+
#define AFE44XX_DRDY_PIN 2
4435

4536
AFE44XX afe44xx(AFE44XX_CS_PIN, AFE44XX_PWDN_PIN, AFE44XX_DRDY_PIN);
4637

47-
spo2_algorithm spo2Calc;
48-
hr_algo hrCalc;
49-
50-
uint16_t irBuffer[100];
51-
uint16_t redBuffer[100];
52-
uint8_t bufferIndex = 0;
53-
uint8_t decimationCounter = 0;
54-
const uint8_t DECIMATION_FACTOR = 20;
55-
56-
int32_t heartRate = 0;
57-
int32_t spo2 = 0;
58-
int8_t spo2Valid = 0;
59-
int8_t hrValid = 0;
60-
61-
uint8_t ppgDataBuff[20];
62-
uint16_t ppgStreamCnt = 0;
63-
bool ppgBufReady = false;
64-
bool sensorInitialized = false;
65-
66-
char dataPacket[DATA_LEN];
67-
const char dataPacketFooter[2] = {0x00, CES_CMDIF_PKT_STOP};
68-
const char dataPacketHeader[5] = {CES_CMDIF_PKT_START_1, CES_CMDIF_PKT_START_2, DATA_LEN,
69-
((uint8_t)(DATA_LEN >> 8)), CES_CMDIF_TYPE_DATA};
70-
7138
void setup()
7239
{
73-
Serial.begin(57600);
40+
Serial.begin(115200);
7441
while (!Serial)
7542
delay(10);
7643

77-
Serial.println("AFE44XX Raw PPG Data for ProtoCentral OpenView");
78-
Serial.println("==============================================");
44+
Serial.println("AFE44XX Raw PPG Plot - Example");
7945

8046
AFE44xxConfig config = AFE44XX::getDefaultConfig();
81-
config.chipType = AFE44xxChipType::AFE4490;
8247
config.led1Current = LEDCurrent::CURRENT_50MA;
8348
config.led2Current = LEDCurrent::CURRENT_50MA;
8449
config.tiaGain = TIAGain::GAIN_500K;
8550
config.sampleRate = SampleRate::RATE_500HZ;
86-
config.averagingFactor = 3;
8751
config.enableDiagnostics = true;
8852

8953
Serial.println("Initializing AFE44XX...");
9054
AFE44xxError error = afe44xx.begin(config);
91-
9255
if (error != AFE44xxError::NONE)
9356
{
94-
Serial.print("ERROR: Initialization failed - ");
57+
Serial.print("Initialization failed: ");
9558
Serial.println(afe44xx.getErrorString(error));
96-
Serial.println("Please check connections and reset Arduino");
97-
9859
while (1)
99-
{
100-
delay(5000);
101-
Serial.println("Check connections: CS=7, PWDN=4, DRDY=2, SPI pins");
102-
}
60+
delay(1000);
10361
}
10462

63+
// Optional: run self-test
10564
error = afe44xx.performSelfTest();
10665
if (error != AFE44xxError::NONE)
10766
{
@@ -114,148 +73,29 @@ void setup()
11473
Serial.println("Self-test passed!");
11574
}
11675

117-
hrCalc.initStatHRM();
118-
sensorInitialized = true;
119-
120-
Serial.println("Ready! Open ProtoCentral OpenView and connect to this COM port");
121-
Serial.println("Select 'AFE4490 Raw PPG' mode in OpenView");
122-
123-
delay(2000);
76+
Serial.println("Streaming raw PPG data (IR,RED) as CSV");
77+
Serial.println("IR,RED");
12478
}
12579

12680
void loop()
12781
{
128-
if (!sensorInitialized)
129-
{
130-
delay(1000);
131-
return;
132-
}
133-
13482
AFE44xxData data;
13583
AFE44xxError error = afe44xx.readData(data);
136-
13784
if (error != AFE44xxError::NONE)
13885
{
139-
initializeErrorPacket();
140-
sendDataSerialPort();
141-
delay(100);
142-
return;
143-
}
144-
145-
if (!data.dataValid)
146-
{
147-
delay(8);
86+
Serial.print("Read error: ");
87+
Serial.println(afe44xx.getErrorString(error));
88+
delay(50);
14889
return;
14990
}
15091

151-
processSpO2Calculation(data);
152-
153-
prepareDataPacket(data);
154-
155-
sendDataSerialPort();
156-
157-
static unsigned long lastDiagTime = 0;
158-
if (millis() - lastDiagTime > 10000)
159-
{
160-
checkSensorDiagnostics();
161-
lastDiagTime = millis();
162-
}
163-
164-
delay(8);
165-
}
166-
167-
void processSpO2Calculation(const AFE44xxData &data)
168-
{
169-
decimationCounter++;
170-
if (decimationCounter >= DECIMATION_FACTOR)
171-
{
172-
irBuffer[bufferIndex] = (uint16_t)(data.irData >> 4);
173-
redBuffer[bufferIndex] = (uint16_t)(data.redData >> 4);
174-
bufferIndex = (bufferIndex + 1) % 100;
175-
decimationCounter = 0;
176-
177-
static uint8_t sampleCount = 0;
178-
sampleCount++;
179-
if (sampleCount >= 100) {
180-
spo2Calc.estimate_spo2(irBuffer, 100, redBuffer, &spo2, &spo2Valid, &heartRate, &hrValid);
181-
sampleCount = 0;
182-
}
183-
}
184-
185-
hrCalc.statHRMAlgo(data.redData);
186-
if (hrCalc.HeartRate > 0)
187-
{
188-
heartRate = hrCalc.HeartRate;
189-
hrValid = 1;
190-
}
191-
}
192-
193-
void prepareDataPacket(const AFE44xxData &data)
194-
{
195-
memcpy(&dataPacket[0], &data.irData, sizeof(int32_t));
196-
memcpy(&dataPacket[4], &data.redData, sizeof(int32_t));
197-
198-
if (spo2Valid && spo2 > 0)
92+
if (data.dataValid)
19993
{
200-
dataPacket[8] = (uint8_t)constrain(spo2, 0, 100);
94+
// Output CSV for plotters or post-processing tools
95+
Serial.print(data.irData);
96+
Serial.print(",");
97+
Serial.println(data.redData);
20198
}
202-
else
203-
{
204-
dataPacket[8] = 0;
205-
}
206-
207-
if (hrValid && heartRate > 0)
208-
{
209-
dataPacket[9] = (uint8_t)constrain(heartRate, 0, 255);
210-
}
211-
else
212-
{
213-
dataPacket[9] = 0;
214-
}
215-
}
216-
217-
void initializeErrorPacket()
218-
{
219-
memset(dataPacket, 0, DATA_LEN);
220-
}
22199

222-
void sendDataSerialPort()
223-
{
224-
uint8_t fullPacket[5 + DATA_LEN + 2];
225-
uint8_t idx = 0;
226-
227-
memcpy(&fullPacket[idx], dataPacketHeader, 5);
228-
idx += 5;
229-
memcpy(&fullPacket[idx], dataPacket, DATA_LEN);
230-
idx += DATA_LEN;
231-
memcpy(&fullPacket[idx], dataPacketFooter, 2);
232-
233-
Serial.write(fullPacket, sizeof(fullPacket));
100+
delay(10);
234101
}
235-
236-
void checkSensorDiagnostics()
237-
{
238-
AFE44xxDiagnostics diag;
239-
AFE44xxError error = afe44xx.getDiagnostics(diag);
240-
241-
if (error != AFE44xxError::NONE)
242-
{
243-
return;
244-
}
245-
246-
if (diag.ledFault || diag.pdShort || diag.pdOpen || diag.gainError)
247-
{
248-
afe44xx.clearFaults();
249-
}
250-
251-
if (diag.ambientLight > 2000)
252-
{
253-
afe44xx.setLEDCurrent(1, LEDCurrent::CURRENT_75MA);
254-
afe44xx.setLEDCurrent(2, LEDCurrent::CURRENT_75MA);
255-
}
256-
else if (diag.ambientLight < 100)
257-
{
258-
afe44xx.setLEDCurrent(1, LEDCurrent::CURRENT_25MA);
259-
afe44xx.setLEDCurrent(2, LEDCurrent::CURRENT_25MA);
260-
}
261-
}

examples/Example-2-computed-spo2/Example-2-computed-spo2.ino

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,62 +43,59 @@ int8_t hrValid = 0;
4343
void setup() {
4444
Serial.begin(115200);
4545
while (!Serial) delay(10);
46-
47-
Serial.println("AFE44XX SPO2 Computation Example");
48-
Serial.println("================================");
49-
46+
47+
Serial.println("AFE44XX SPO2 Computation");
48+
5049
AFE44xxConfig config = AFE44XX::getDefaultConfig();
5150
config.led1Current = LEDCurrent::CURRENT_50MA;
5251
config.led2Current = LEDCurrent::CURRENT_50MA;
5352
config.tiaGain = TIAGain::GAIN_500K;
5453
config.sampleRate = SampleRate::RATE_500HZ;
55-
56-
Serial.println("Initializing AFE44XX...");
54+
5755
AFE44xxError error = afe44xx.begin(config);
58-
5956
if (error != AFE44xxError::NONE) {
6057
Serial.print("Initialization failed: ");
6158
Serial.println(afe44xx.getErrorString(error));
6259
while (1) {
6360
delay(1000);
64-
Serial.println("Please check connections and reset");
6561
}
6662
}
67-
63+
6864
hrCalc.initStatHRM();
69-
65+
7066
Serial.println("Place finger on sensor...");
7167
Serial.println("SPO2\tHR\tIR\tRed\tStatus");
7268
}
7369

7470
void loop() {
7571
AFE44xxData data;
7672
AFE44xxError error = afe44xx.readData(data);
77-
73+
7874
if (error != AFE44xxError::NONE) {
7975
Serial.print("Read error: ");
8076
Serial.println(afe44xx.getErrorString(error));
8177
delay(100);
8278
return;
8379
}
84-
80+
8581
if (!data.dataValid) {
8682
return;
8783
}
88-
84+
8985
decimationCounter++;
9086
if (decimationCounter >= DECIMATION_FACTOR) {
9187
irBuffer[bufferIndex] = (uint16_t)(data.irData >> 4);
9288
redBuffer[bufferIndex] = (uint16_t)(data.redData >> 4);
9389
bufferIndex = (bufferIndex + 1) % 100;
9490
decimationCounter = 0;
95-
91+
9692
static uint8_t sampleCount = 0;
9793
sampleCount++;
9894
if (sampleCount >= 100) {
9995
spo2Calc.estimate_spo2(irBuffer, 100, redBuffer, &spo2, &spo2Valid, &heartRate, &hrValid);
10096
sampleCount = 0;
101-
97+
}
98+
10299
if (spo2Valid && hrValid) {
103100
Serial.print(spo2);
104101
Serial.print("%\t");
@@ -115,13 +112,13 @@ void loop() {
115112
Serial.print(data.redData);
116113
Serial.println("\tCalculating...");
117114
}
118-
115+
119116
AFE44xxDiagnostics diag;
120117
error = afe44xx.getDiagnostics(diag);
121118
if (error == AFE44xxError::NONE && (diag.ledFault || diag.pdShort || diag.pdOpen)) {
122119
Serial.println("*** Sensor fault detected - check finger placement ***");
123120
}
124121
}
125-
122+
126123
delay(2);
127124
}

examples/Example-3-Raw-PPG-plot-Arduino-Plotter/Example-3-Raw-PPG-plot-Arduino-Plotter.ino

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,34 @@
2323

2424
#define AFE44XX_CS_PIN 7
2525
#define AFE44XX_PWDN_PIN 4
26-
#define AFE44XX_INTNUM 0
2726

2827
AFE44XX afe44xx(AFE44XX_CS_PIN, AFE44XX_PWDN_PIN);
2928

30-
afe44xx_data afe44xx_raw_data;
31-
3229
void setup()
3330
{
3431
Serial.begin(57600);
35-
Serial.println("Intilaziting AFE44xx.. ");
36-
32+
Serial.println("Initializing AFE44xx...");
33+
3734
SPI.begin();
38-
39-
afe44xx.afe44xx_init();
35+
36+
AFE44xxConfig cfg = AFE44XX::getDefaultConfig();
37+
AFE44xxError err = afe44xx.begin(cfg);
38+
if (err != AFE44xxError::NONE) {
39+
Serial.print("Init failed: ");
40+
Serial.println(afe44xx.getErrorString(err));
41+
while (1) delay(1000);
42+
}
43+
4044
Serial.println("Inited...");
4145
}
4246

4347
void loop()
4448
{
45-
afe44xx.get_AFE44XX_Data(&afe44xx_raw_data);
46-
47-
Serial.println(afe44xx_raw_data.RED_data);
48-
// Serial.println(afe44xx_raw_data.IR_data);
49-
delay(8);
50-
49+
AFE44xxData data;
50+
AFE44xxError err = afe44xx.readData(data);
51+
if (err == AFE44xxError::NONE && data.dataValid) {
52+
Serial.println(data.redData);
53+
// Serial.println(data.irData);
54+
}
55+
delay(8);
5156
}

0 commit comments

Comments
 (0)