John Greene / Mbed OS MAX30101_HR_SPO2

Dependencies:   MAX30101 max32630fthr

Fork of MAX30101_Demo_Plot_Algorithm by John Greene

Committer:
coreyharris
Date:
Thu Aug 17 15:53:16 2017 +0000
Revision:
0:0bd4103885bf
Child:
1:471e2b722d24
Functional MAX30101 Demo;

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 0:0bd4103885bf 10 volatile bool op_sensorIntFlag = false;
coreyharris 0:0bd4103885bf 11 void op_sensor_callback()
coreyharris 0:0bd4103885bf 12 {
coreyharris 0:0bd4103885bf 13 op_sensorIntFlag = true;
coreyharris 0:0bd4103885bf 14 }
coreyharris 0:0bd4103885bf 15
coreyharris 0:0bd4103885bf 16 int main()
coreyharris 0:0bd4103885bf 17 {
coreyharris 0:0bd4103885bf 18 Serial pc(USBTX, USBRX);
coreyharris 0:0bd4103885bf 19 pc.baud(115200);
coreyharris 0:0bd4103885bf 20
coreyharris 0:0bd4103885bf 21 DigitalOut rLed(LED1, LED_OFF);
coreyharris 0:0bd4103885bf 22 DigitalOut gLed(LED2, LED_OFF);
coreyharris 0:0bd4103885bf 23 DigitalOut bLed(LED3, LED_OFF);
coreyharris 0:0bd4103885bf 24
coreyharris 0:0bd4103885bf 25 InterruptIn op_sensor_int(P3_2);
coreyharris 0:0bd4103885bf 26
coreyharris 0:0bd4103885bf 27 I2C i2cBus(I2C1_SDA, I2C1_SCL); // I2C bus
coreyharris 0:0bd4103885bf 28
coreyharris 0:0bd4103885bf 29 MAX30101 * op_sensor;
coreyharris 0:0bd4103885bf 30 op_sensor = new MAX30101(i2cBus);
coreyharris 0:0bd4103885bf 31 int rc = op_sensor_config(*op_sensor);
coreyharris 0:0bd4103885bf 32
coreyharris 0:0bd4103885bf 33 op_sensor_int.fall(op_sensor_callback);
coreyharris 0:0bd4103885bf 34
coreyharris 0:0bd4103885bf 35 MAX30101::InterruptBitField_u ints;
coreyharris 0:0bd4103885bf 36 uint8_t fifoData[MAX30101::MAX_FIFO_BYTES];
coreyharris 0:0bd4103885bf 37 uint16_t idx, readBytes;
coreyharris 0:0bd4103885bf 38 while(1) {
coreyharris 0:0bd4103885bf 39
coreyharris 0:0bd4103885bf 40 if( rc == 0 )
coreyharris 0:0bd4103885bf 41 {
coreyharris 0:0bd4103885bf 42
coreyharris 0:0bd4103885bf 43 if(op_sensorIntFlag)
coreyharris 0:0bd4103885bf 44 {
coreyharris 0:0bd4103885bf 45
coreyharris 0:0bd4103885bf 46 pc.printf("Interrupt seen...\r\n");
coreyharris 0:0bd4103885bf 47 op_sensorIntFlag = false;
coreyharris 0:0bd4103885bf 48 rc = op_sensor->getInterruptStatus(ints);
coreyharris 0:0bd4103885bf 49
coreyharris 0:0bd4103885bf 50 if((rc == 0) && (ints.bits.a_full))
coreyharris 0:0bd4103885bf 51 {
coreyharris 0:0bd4103885bf 52
coreyharris 0:0bd4103885bf 53 rc = op_sensor->readFIFO(MAX30101::OneLedChannel, fifoData, readBytes);
coreyharris 0:0bd4103885bf 54
coreyharris 0:0bd4103885bf 55 if(rc == 0)
coreyharris 0:0bd4103885bf 56 {
coreyharris 0:0bd4103885bf 57
coreyharris 0:0bd4103885bf 58 pc.printf("FIFO Read, received %d bytes\r\n", readBytes);
coreyharris 0:0bd4103885bf 59
coreyharris 0:0bd4103885bf 60 for(idx = 0; idx < readBytes; idx++)
coreyharris 0:0bd4103885bf 61 {
coreyharris 0:0bd4103885bf 62 pc.printf("fifoData[0x%04x] = 0x%02x\r\n", idx, fifoData[idx]);
coreyharris 0:0bd4103885bf 63 }
coreyharris 0:0bd4103885bf 64 pc.printf("\r\n");
coreyharris 0:0bd4103885bf 65 }
coreyharris 0:0bd4103885bf 66 }
coreyharris 0:0bd4103885bf 67 }
coreyharris 0:0bd4103885bf 68
coreyharris 0:0bd4103885bf 69 } else {
coreyharris 0:0bd4103885bf 70
coreyharris 0:0bd4103885bf 71 pc.printf("Something went wrong, check the I2C bus or power connections... \r\n");
coreyharris 0:0bd4103885bf 72 bLed = LED_OFF;
coreyharris 0:0bd4103885bf 73 gLed = LED_OFF;
coreyharris 0:0bd4103885bf 74
coreyharris 0:0bd4103885bf 75 while(1)
coreyharris 0:0bd4103885bf 76 {
coreyharris 0:0bd4103885bf 77 rLed = !rLed;
coreyharris 0:0bd4103885bf 78 wait(0.5);
coreyharris 0:0bd4103885bf 79 }
coreyharris 0:0bd4103885bf 80
coreyharris 0:0bd4103885bf 81 }
coreyharris 0:0bd4103885bf 82
coreyharris 0:0bd4103885bf 83 }
coreyharris 0:0bd4103885bf 84
coreyharris 0:0bd4103885bf 85 }
coreyharris 0:0bd4103885bf 86
coreyharris 0:0bd4103885bf 87
coreyharris 0:0bd4103885bf 88 bool op_sensor_config(MAX30101 &op_sensor) {
coreyharris 0:0bd4103885bf 89
coreyharris 0:0bd4103885bf 90 //Reset Device
coreyharris 0:0bd4103885bf 91 MAX30101::ModeConfiguration_u modeConfig;
coreyharris 0:0bd4103885bf 92 modeConfig.all = 0;
coreyharris 0:0bd4103885bf 93 modeConfig.bits.reset = 1;
coreyharris 0:0bd4103885bf 94 int32_t rc = op_sensor.setModeConfiguration(modeConfig);
coreyharris 0:0bd4103885bf 95
coreyharris 0:0bd4103885bf 96
coreyharris 0:0bd4103885bf 97 //enable MAX30101 interrupts
coreyharris 0:0bd4103885bf 98 MAX30101::InterruptBitField_u ints;
coreyharris 0:0bd4103885bf 99 if(rc == 0)
coreyharris 0:0bd4103885bf 100 {
coreyharris 0:0bd4103885bf 101 ints.all = 0;
coreyharris 0:0bd4103885bf 102 ints.bits.die_temp = 1;
coreyharris 0:0bd4103885bf 103 ints.bits.a_full = 1;
coreyharris 0:0bd4103885bf 104 rc = op_sensor.enableInterrupts(ints);
coreyharris 0:0bd4103885bf 105 }
coreyharris 0:0bd4103885bf 106
coreyharris 0:0bd4103885bf 107 //configure FIFO
coreyharris 0:0bd4103885bf 108 MAX30101::FIFO_Configuration_u fifoConfig;
coreyharris 0:0bd4103885bf 109 if(rc == 0)
coreyharris 0:0bd4103885bf 110 {
coreyharris 0:0bd4103885bf 111 fifoConfig.all = 0;
coreyharris 0:0bd4103885bf 112 fifoConfig.bits.fifo_a_full = 15; //Max level of 15 samples
coreyharris 0:0bd4103885bf 113 fifoConfig.bits.sample_average = MAX30101::AveragedSamples_4;
coreyharris 0:0bd4103885bf 114 rc = op_sensor.setFIFOConfiguration(fifoConfig);
coreyharris 0:0bd4103885bf 115 }
coreyharris 0:0bd4103885bf 116
coreyharris 0:0bd4103885bf 117 MAX30101::SpO2Configuration_u spo2Config;
coreyharris 0:0bd4103885bf 118 spo2Config.all = 0;
coreyharris 0:0bd4103885bf 119 spo2Config.bits.spo2_sr = MAX30101::SR_1600_Hz;
coreyharris 0:0bd4103885bf 120 if(rc == 0)
coreyharris 0:0bd4103885bf 121 {
coreyharris 0:0bd4103885bf 122 rc = op_sensor.setSpO2Configuration(spo2Config);
coreyharris 0:0bd4103885bf 123 }
coreyharris 0:0bd4103885bf 124
coreyharris 0:0bd4103885bf 125 //Set LED1 drive current
coreyharris 0:0bd4103885bf 126 if(rc == 0)
coreyharris 0:0bd4103885bf 127 {
coreyharris 0:0bd4103885bf 128 //Heart Rate only
coreyharris 0:0bd4103885bf 129 rc = op_sensor.setLEDPulseAmplitude(MAX30101::LED1_PA, 0x1F);
coreyharris 0:0bd4103885bf 130 }
coreyharris 0:0bd4103885bf 131
coreyharris 0:0bd4103885bf 132 //Set operating mode
coreyharris 0:0bd4103885bf 133 modeConfig.all = 0;
coreyharris 0:0bd4103885bf 134 modeConfig.bits.mode = MAX30101::HeartRateMode;
coreyharris 0:0bd4103885bf 135 if(rc == 0)
coreyharris 0:0bd4103885bf 136 {
coreyharris 0:0bd4103885bf 137 rc = op_sensor.setModeConfiguration(modeConfig);
coreyharris 0:0bd4103885bf 138 }
coreyharris 0:0bd4103885bf 139
coreyharris 0:0bd4103885bf 140
coreyharris 0:0bd4103885bf 141 return rc;
coreyharris 0:0bd4103885bf 142 }