John Greene / Mbed OS MAX30101_HR_SPO2

Dependencies:   MAX30101 max32630fthr

Fork of MAX30101_Demo_Plot_Algorithm by John Greene

Committer:
coreyharris
Date:
Tue Aug 22 14:09:19 2017 +0000
Revision:
3:a580414c44ce
Parent:
2:54182d6a168f
Child:
4:14d1b87cd3c4
Added comments and fixed print statement to display samples instead of 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 3:a580414c44ce 64
coreyharris 3:a580414c44ce 65 } else {
coreyharris 0:0bd4103885bf 66
coreyharris 0:0bd4103885bf 67 pc.printf("Something went wrong, check the I2C bus or power connections... \r\n");
coreyharris 0:0bd4103885bf 68 bLed = LED_OFF;
coreyharris 0:0bd4103885bf 69 gLed = LED_OFF;
coreyharris 0:0bd4103885bf 70
coreyharris 0:0bd4103885bf 71 while(1)
coreyharris 0:0bd4103885bf 72 {
coreyharris 0:0bd4103885bf 73 rLed = !rLed;
coreyharris 0:0bd4103885bf 74 wait(0.5);
coreyharris 3:a580414c44ce 75 }
coreyharris 3:a580414c44ce 76 }
coreyharris 0:0bd4103885bf 77
coreyharris 3:a580414c44ce 78 }
coreyharris 0:0bd4103885bf 79 }
coreyharris 0:0bd4103885bf 80
coreyharris 0:0bd4103885bf 81
coreyharris 0:0bd4103885bf 82 bool op_sensor_config(MAX30101 &op_sensor) {
coreyharris 0:0bd4103885bf 83
coreyharris 0:0bd4103885bf 84 //Reset Device
coreyharris 0:0bd4103885bf 85 MAX30101::ModeConfiguration_u modeConfig;
coreyharris 0:0bd4103885bf 86 modeConfig.all = 0;
coreyharris 0:0bd4103885bf 87 modeConfig.bits.reset = 1;
coreyharris 0:0bd4103885bf 88 int32_t rc = op_sensor.setModeConfiguration(modeConfig);
coreyharris 0:0bd4103885bf 89
coreyharris 0:0bd4103885bf 90
coreyharris 0:0bd4103885bf 91 //enable MAX30101 interrupts
coreyharris 0:0bd4103885bf 92 MAX30101::InterruptBitField_u ints;
coreyharris 0:0bd4103885bf 93 if(rc == 0)
coreyharris 0:0bd4103885bf 94 {
coreyharris 0:0bd4103885bf 95 ints.all = 0;
coreyharris 1:471e2b722d24 96 ints.bits.die_temp = 1; // Enable FIFO almost full interrupt
coreyharris 1:471e2b722d24 97 ints.bits.a_full = 1; // Enable internal die temp. interrupt
coreyharris 0:0bd4103885bf 98 rc = op_sensor.enableInterrupts(ints);
coreyharris 0:0bd4103885bf 99 }
coreyharris 0:0bd4103885bf 100
coreyharris 0:0bd4103885bf 101 //configure FIFO
coreyharris 0:0bd4103885bf 102 MAX30101::FIFO_Configuration_u fifoConfig;
coreyharris 0:0bd4103885bf 103 if(rc == 0)
coreyharris 0:0bd4103885bf 104 {
coreyharris 0:0bd4103885bf 105 fifoConfig.all = 0;
coreyharris 1:471e2b722d24 106 fifoConfig.bits.fifo_a_full = 15; // Max level of 15 samples
coreyharris 2:54182d6a168f 107 fifoConfig.bits.sample_average = MAX30101::AveragedSamples_8; // Average 4 samples
coreyharris 0:0bd4103885bf 108 rc = op_sensor.setFIFOConfiguration(fifoConfig);
coreyharris 0:0bd4103885bf 109 }
coreyharris 0:0bd4103885bf 110
coreyharris 0:0bd4103885bf 111 MAX30101::SpO2Configuration_u spo2Config;
coreyharris 0:0bd4103885bf 112 if(rc == 0)
coreyharris 0:0bd4103885bf 113 {
coreyharris 1:471e2b722d24 114 spo2Config.all = 0;
coreyharris 1:471e2b722d24 115 spo2Config.bits.spo2_sr = MAX30101::SR_400_Hz; // SpO2 SR = 400Hz
coreyharris 2:54182d6a168f 116 spo2Config.bits.led_pw = 1;
coreyharris 0:0bd4103885bf 117 rc = op_sensor.setSpO2Configuration(spo2Config);
coreyharris 0:0bd4103885bf 118 }
coreyharris 0:0bd4103885bf 119
coreyharris 0:0bd4103885bf 120 //Set LED1 drive current
coreyharris 0:0bd4103885bf 121 if(rc == 0)
coreyharris 0:0bd4103885bf 122 {
coreyharris 0:0bd4103885bf 123 //Heart Rate only
coreyharris 2:54182d6a168f 124 rc = op_sensor.setLEDPulseAmplitude(MAX30101::LED1_PA, 0x1F);
coreyharris 0:0bd4103885bf 125 }
coreyharris 0:0bd4103885bf 126
coreyharris 0:0bd4103885bf 127 //Set operating mode
coreyharris 0:0bd4103885bf 128 modeConfig.all = 0;
coreyharris 0:0bd4103885bf 129 if(rc == 0)
coreyharris 0:0bd4103885bf 130 {
coreyharris 1:471e2b722d24 131 modeConfig.bits.mode = MAX30101::HeartRateMode; // Heart-rate only
coreyharris 0:0bd4103885bf 132 rc = op_sensor.setModeConfiguration(modeConfig);
coreyharris 0:0bd4103885bf 133 }
coreyharris 0:0bd4103885bf 134
coreyharris 0:0bd4103885bf 135
coreyharris 0:0bd4103885bf 136 return rc;
coreyharris 0:0bd4103885bf 137 }