Xi Han / Mbed OS HeartRate

Dependencies:   MAX30101

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mbed_events.h"
00003 #include "MAX30101.h"
00004 
00005 #define FIFO_DATA_MAX 288
00006 
00007 DigitalOut pwr1v8(PTA29);
00008 DigitalOut pwr3v3b(PTC13);
00009 DigitalOut pwr15v(PTB12);
00010 I2C i2c0(PTB1, PTB0);
00011 InterruptIn maximInterrupt(PTB18);
00012 Serial pc(USBTX, USBRX);
00013 
00014 EventQueue evqueue(32 * EVENTS_EVENT_SIZE);
00015 Thread t;
00016 
00017 MAX30101 hr(i2c0);
00018 
00019 int mask_ppg = 0;
00020 uint32_t count = 0;
00021 void interruptHandlerQueued() {
00022     
00023     MAX30101::InterruptBitField_u interruptStatus;
00024     hr.getInterruptStatus(interruptStatus);
00025 //    printf("Interrupt Status: 0x%02x\r\n", interruptStatus.all);
00026     
00027     if (interruptStatus.bits.pwr_rdy == 0x1) {
00028 //        printf("Powered on\r\n");
00029         
00030         // Soft reset
00031         MAX30101::ModeConfiguration_u modeConf;
00032         modeConf.all = 0;
00033         modeConf.bits.reset = 1;
00034         hr.setModeConfiguration(modeConf);
00035         wait(0.01);
00036         
00037         // Configure FIFO
00038         MAX30101::FIFO_Configuration_u fifoConf;
00039         hr.getFIFOConfiguration(fifoConf);
00040 //        pc.printf("FIFO Configuration: 0x%02x\r\n", fifoConf.all);
00041           
00042         // Set LED power
00043         hr.setLEDPulseAmplitude(MAX30101::LED1_PA, 0x0C);
00044         hr.setLEDPulseAmplitude(MAX30101::ProxModeLED_PA, 0x19);
00045 //        pc.printf("LED set\r\n");
00046         
00047         MAX30101::SpO2Configuration_u spo2Conf;
00048         hr.getSpO2Configuration(spo2Conf);
00049         spo2Conf.bits.led_pw = MAX30101::PW_1;
00050         spo2Conf.bits.spo2_sr = MAX30101::SR_100_Hz;
00051         hr.setSpO2Configuration(spo2Conf);
00052         hr.getSpO2Configuration(spo2Conf);
00053 //        pc.printf("SpO2 Configuration: 0x%02x\r\n", spo2Conf.all);
00054         
00055         // Proximity settings
00056         hr.setProxIntThreshold(0x14);
00057         
00058         // Enable HR mode
00059         modeConf.all = 0;
00060         modeConf.bits.mode = MAX30101::HeartRateMode;
00061         hr.setModeConfiguration(modeConf);
00062 //        printf("Mode set\r\n");
00063     }
00064     
00065     if (interruptStatus.bits.prox_int == 0x1) {
00066 //        printf("Proximity Triggered, entered HR Mode.");
00067     }
00068     
00069     if (interruptStatus.bits.ppg_rdy == 0x1) {
00070 //        printf("PPG Ready.\r\n");
00071         mask_ppg = 1;
00072     }
00073     
00074     if (interruptStatus.bits.a_full == 0x1) {
00075 //        printf("FIFO Almost Full.\r\n");
00076         uint8_t data[FIFO_DATA_MAX];
00077         uint16_t readBytes = 0;
00078         hr.readFIFO(MAX30101::OneLedChannel, data, readBytes);
00079         
00080         for (uint16_t i = 0; i < readBytes; i += 3) {
00081             uint8_t sample[4] = {0};
00082             sample[0] = data[i + 2];
00083             sample[1] = data[i + 1];
00084             sample[2] = data[i];
00085             
00086             printf("%u\r\n", *(uint32_t *) sample);
00087         }
00088     }
00089     
00090     interruptStatus.all = 0xFF;
00091     if (mask_ppg == 1) {
00092         interruptStatus.bits.ppg_rdy = 0;
00093     }
00094     hr.enableInterrupts(interruptStatus);
00095 }
00096 
00097 void interruptHandler() {
00098     evqueue.call(interruptHandlerQueued);
00099 }
00100 
00101 // main() runs in its own thread in the OS
00102 int main() {
00103 //    printf("Hello world.\r\n");
00104     
00105     t.start(callback(&evqueue, &EventQueue::dispatch_forever));
00106      
00107     pwr1v8 = 1;
00108     pwr3v3b = 1;
00109     pwr15v = 0;
00110     
00111     maximInterrupt.fall(interruptHandler);
00112     maximInterrupt.enable_irq();
00113     
00114     MAX30101::InterruptBitField_u interruptStatus;
00115     interruptStatus.all = 0xFF;
00116     hr.enableInterrupts(interruptStatus);
00117     
00118     return 0;
00119 }
00120