John Greene / Mbed OS MAX30101_HR_SPO2

Dependencies:   MAX30101 max32630fthr

Fork of MAX30101_Demo_Plot_Algorithm by John Greene

Committer:
coreyharris
Date:
Mon Aug 21 21:41:14 2017 +0000
Revision:
2:54182d6a168f
Parent:
1:471e2b722d24
Child:
3:a580414c44ce
Turned raw Byte data into samples by concatenating every three bytes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
coreyharris 0:0bd4103885bf 1 #include "mbed.h"
coreyharris 0:0bd4103885bf 2 #include "max32630fthr.h"
coreyharris 0:0bd4103885bf 3 #include "MAX30101.h"
coreyharris 0:0bd4103885bf 4
coreyharris 0:0bd4103885bf 5 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
coreyharris 0:0bd4103885bf 6
coreyharris 0:0bd4103885bf 7 bool op_sensor_config(MAX30101 &op_sensor);
coreyharris 0:0bd4103885bf 8
coreyharris 0:0bd4103885bf 9 /* Op Sensor FIFO nearly full callback */
coreyharris 1:471e2b722d24 10 volatile bool op_sensorIntFlag = 0;
coreyharris 0:0bd4103885bf 11 void op_sensor_callback()
coreyharris 0:0bd4103885bf 12 {
coreyharris 1:471e2b722d24 13 op_sensorIntFlag = 1;
coreyharris 0:0bd4103885bf 14 }
coreyharris 0:0bd4103885bf 15
coreyharris 0:0bd4103885bf 16 int main()
coreyharris 0:0bd4103885bf 17 {
coreyharris 1:471e2b722d24 18 Serial pc(USBTX, USBRX); // Use USB debug probe for serial link
coreyharris 1:471e2b722d24 19 pc.baud(115200); // Baud rate = 115200
coreyharris 0:0bd4103885bf 20
coreyharris 1:471e2b722d24 21 DigitalOut rLed(LED1, LED_OFF); // Debug LEDs
coreyharris 0:0bd4103885bf 22 DigitalOut gLed(LED2, LED_OFF);
coreyharris 0:0bd4103885bf 23 DigitalOut bLed(LED3, LED_OFF);
coreyharris 0:0bd4103885bf 24
coreyharris 1:471e2b722d24 25 InterruptIn op_sensor_int(P3_2); // Config P3_2 as int. in for
coreyharris 1:471e2b722d24 26 op_sensor_int.fall(&op_sensor_callback); // FIFO ready interrupt
coreyharris 0:0bd4103885bf 27
coreyharris 1:471e2b722d24 28 I2C i2cBus(I2C1_SDA, I2C1_SCL); // I2C bus, P3_4 = SDA, P3_5 = SCL
coreyharris 0:0bd4103885bf 29
coreyharris 0:0bd4103885bf 30 MAX30101 * op_sensor;
coreyharris 0:0bd4103885bf 31 op_sensor = new MAX30101(i2cBus);
coreyharris 1:471e2b722d24 32 int rc = op_sensor_config(*op_sensor);
coreyharris 0:0bd4103885bf 33
coreyharris 0:0bd4103885bf 34 MAX30101::InterruptBitField_u ints;
coreyharris 0:0bd4103885bf 35 uint8_t fifoData[MAX30101::MAX_FIFO_BYTES];
coreyharris 0:0bd4103885bf 36 uint16_t idx, readBytes;
coreyharris 2:54182d6a168f 37 int16_t opSample;
coreyharris 2:54182d6a168f 38 uint32_t sample;
coreyharris 0:0bd4103885bf 39 while(1) {
coreyharris 2:54182d6a168f 40
coreyharris 1:471e2b722d24 41 if( rc == 0 ) {
coreyharris 2:54182d6a168f 42
coreyharris 1:471e2b722d24 43 if(op_sensorIntFlag) {
coreyharris 0:0bd4103885bf 44 pc.printf("Interrupt seen...\r\n");
coreyharris 1:471e2b722d24 45 op_sensorIntFlag = 0; // Lower interrupt flag
coreyharris 1:471e2b722d24 46 rc = op_sensor->getInterruptStatus(ints); // Read interrupt status
coreyharris 0:0bd4103885bf 47
coreyharris 1:471e2b722d24 48 if((rc == 0) && (ints.bits.a_full)) {
coreyharris 1:471e2b722d24 49 rc = op_sensor->readFIFO(MAX30101::OneLedChannel, fifoData, readBytes); // Read FIFO
coreyharris 0:0bd4103885bf 50
coreyharris 1:471e2b722d24 51 if(rc == 0) {
coreyharris 2:54182d6a168f 52 pc.printf("FIFO Read, received %d samples\r\n\r\n", readBytes/3);
coreyharris 0:0bd4103885bf 53
coreyharris 2:54182d6a168f 54 for(idx = 0; idx < readBytes; idx+=3) {
coreyharris 2:54182d6a168f 55 sample = (fifoData[idx]<<16) | (fifoData[idx+1]<<8) | (fifoData[idx+2]);
coreyharris 2:54182d6a168f 56 opSample = sample >> 8;
coreyharris 2:54182d6a168f 57 pc.printf("Op. Sample : %i\r\n", opSample); // Print results
coreyharris 0:0bd4103885bf 58 }
coreyharris 2:54182d6a168f 59 pc.printf("\r\n\r\n");
coreyharris 2:54182d6a168f 60
coreyharris 0:0bd4103885bf 61 }
coreyharris 0:0bd4103885bf 62 }
coreyharris 0:0bd4103885bf 63 }
coreyharris 1:471e2b722d24 64
coreyharris 0:0bd4103885bf 65
coreyharris 0:0bd4103885bf 66 pc.printf("Something went wrong, check the I2C bus or power connections... \r\n");
coreyharris 0:0bd4103885bf 67 bLed = LED_OFF;
coreyharris 0:0bd4103885bf 68 gLed = LED_OFF;
coreyharris 0:0bd4103885bf 69
coreyharris 0:0bd4103885bf 70 while(1)
coreyharris 0:0bd4103885bf 71 {
coreyharris 0:0bd4103885bf 72 rLed = !rLed;
coreyharris 0:0bd4103885bf 73 wait(0.5);
coreyharris 0:0bd4103885bf 74 }
coreyharris 0:0bd4103885bf 75
coreyharris 0:0bd4103885bf 76 }
coreyharris 0:0bd4103885bf 77
coreyharris 0:0bd4103885bf 78 }
coreyharris 0:0bd4103885bf 79
coreyharris 0:0bd4103885bf 80 }
coreyharris 0:0bd4103885bf 81
coreyharris 0:0bd4103885bf 82
coreyharris 0:0bd4103885bf 83 bool op_sensor_config(MAX30101 &op_sensor) {
coreyharris 0:0bd4103885bf 84
coreyharris 0:0bd4103885bf 85 //Reset Device
coreyharris 0:0bd4103885bf 86 MAX30101::ModeConfiguration_u modeConfig;
coreyharris 0:0bd4103885bf 87 modeConfig.all = 0;
coreyharris 0:0bd4103885bf 88 modeConfig.bits.reset = 1;
coreyharris 0:0bd4103885bf 89 int32_t rc = op_sensor.setModeConfiguration(modeConfig);
coreyharris 0:0bd4103885bf 90
coreyharris 0:0bd4103885bf 91
coreyharris 0:0bd4103885bf 92 //enable MAX30101 interrupts
coreyharris 0:0bd4103885bf 93 MAX30101::InterruptBitField_u ints;
coreyharris 0:0bd4103885bf 94 if(rc == 0)
coreyharris 0:0bd4103885bf 95 {
coreyharris 0:0bd4103885bf 96 ints.all = 0;
coreyharris 1:471e2b722d24 97 ints.bits.die_temp = 1; // Enable FIFO almost full interrupt
coreyharris 1:471e2b722d24 98 ints.bits.a_full = 1; // Enable internal die temp. interrupt
coreyharris 0:0bd4103885bf 99 rc = op_sensor.enableInterrupts(ints);
coreyharris 0:0bd4103885bf 100 }
coreyharris 0:0bd4103885bf 101
coreyharris 0:0bd4103885bf 102 //configure FIFO
coreyharris 0:0bd4103885bf 103 MAX30101::FIFO_Configuration_u fifoConfig;
coreyharris 0:0bd4103885bf 104 if(rc == 0)
coreyharris 0:0bd4103885bf 105 {
coreyharris 0:0bd4103885bf 106 fifoConfig.all = 0;
coreyharris 1:471e2b722d24 107 fifoConfig.bits.fifo_a_full = 15; // Max level of 15 samples
coreyharris 2:54182d6a168f 108 fifoConfig.bits.sample_average = MAX30101::AveragedSamples_8; // Average 4 samples
coreyharris 0:0bd4103885bf 109 rc = op_sensor.setFIFOConfiguration(fifoConfig);
coreyharris 0:0bd4103885bf 110 }
coreyharris 0:0bd4103885bf 111
coreyharris 0:0bd4103885bf 112 MAX30101::SpO2Configuration_u spo2Config;
coreyharris 0:0bd4103885bf 113 if(rc == 0)
coreyharris 0:0bd4103885bf 114 {
coreyharris 1:471e2b722d24 115 spo2Config.all = 0;
coreyharris 1:471e2b722d24 116 spo2Config.bits.spo2_sr = MAX30101::SR_400_Hz; // SpO2 SR = 400Hz
coreyharris 2:54182d6a168f 117 spo2Config.bits.led_pw = 1;
coreyharris 0:0bd4103885bf 118 rc = op_sensor.setSpO2Configuration(spo2Config);
coreyharris 0:0bd4103885bf 119 }
coreyharris 0:0bd4103885bf 120
coreyharris 0:0bd4103885bf 121 //Set LED1 drive current
coreyharris 0:0bd4103885bf 122 if(rc == 0)
coreyharris 0:0bd4103885bf 123 {
coreyharris 0:0bd4103885bf 124 //Heart Rate only
coreyharris 2:54182d6a168f 125 rc = op_sensor.setLEDPulseAmplitude(MAX30101::LED1_PA, 0x1F);
coreyharris 0:0bd4103885bf 126 }
coreyharris 0:0bd4103885bf 127
coreyharris 0:0bd4103885bf 128 //Set operating mode
coreyharris 0:0bd4103885bf 129 modeConfig.all = 0;
coreyharris 0:0bd4103885bf 130 if(rc == 0)
coreyharris 0:0bd4103885bf 131 {
coreyharris 1:471e2b722d24 132 modeConfig.bits.mode = MAX30101::HeartRateMode; // Heart-rate only
coreyharris 0:0bd4103885bf 133 rc = op_sensor.setModeConfiguration(modeConfig);
coreyharris 0:0bd4103885bf 134 }
coreyharris 0:0bd4103885bf 135
coreyharris 0:0bd4103885bf 136
coreyharris 0:0bd4103885bf 137 return rc;
coreyharris 0:0bd4103885bf 138 }