John Greene / Mbed OS MAX30101_HR_SPO2

Dependencies:   MAX30101 max32630fthr

Fork of MAX30101_Demo_Plot_Algorithm by John Greene

Files at this revision

API Documentation at this revision

Comitter:
johnGreeneMaxim
Date:
Mon Nov 20 16:36:55 2017 +0000
Parent:
9:affd4e6372a0
Commit message:
SPO2 and HR calculations

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
max32630fthr.lib Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Oct 15 19:22:36 2017 +0000
+++ b/main.cpp	Mon Nov 20 16:36:55 2017 +0000
@@ -37,10 +37,7 @@
 #include "MAX30101.h"
 #include "algorithm.h"
 
-//equals 3*number of LEDS used
-#define DIV_SAMPLE 6 
-//sets the number of samples sent to the heart rate and SPO2 calculation 
-#define CALC_SAMPLE 500
+
 
 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
 
@@ -54,6 +51,11 @@
     op_sensorIntFlag = 1;
 }
 
+//declare large variables outside of main 
+uint32_t redData[500];//set array to max fifo size 
+uint32_t irData[500];//set array to max fifo size 
+    
+    
 int main()
 {
     Serial pc(USBTX, USBRX);            // Use USB debug probe for serial link
@@ -81,132 +83,83 @@
     uint16_t idx, readBytes;
     int32_t opSample;
     uint32_t sample;
-    uint32_t redData[500];//set array to max fifo size 
-    uint32_t irData[500];//set array to max fifo size 
+ 
     int r=0; //counter for redData position 
     int ir=0; //counter for irData position
     int c=0; //counter to print values
-    int i =0; //second counter 
     int32_t spo2 =0;
     int8_t spo2Valid = 0;
     int32_t heartRate = 0;
     int8_t heartRateValid = 0;
-    int32_t numSamples = 0;
-    uint32_t fifoIR[MAX30101::MAX_FIFO_BYTES];
-    uint32_t fifoRED[MAX30101::MAX_FIFO_BYTES];
-    
+
     
+    pc.printf("Starting Program...Please wait a few seconds while data is being collected.\r\n");
     while(1) 
-    {
-       
-        
+    {   
         if( rc == 0 ) 
         {
             
             // Check if op_sensor interrupt asserted
             if(op_sensorIntFlag) 
             {
-                pc.printf("Entered op_sensorIntFlag check\r\n");
+                //pc.printf("Entered op_sensorIntFlag check\r\n");
                 op_sensorIntFlag = 0;                       // Lower interrupt flag
                 rc = op_sensor.getInterruptStatus(ints);    // Read interrupt status 
                 
                 // Check if FIFO almost full interrupt asserted
                 if((rc == 0) && (ints.bits.a_full)) 
                 {
-                    pc.printf("about to read fifo\r\n");
                     // Read FIFO 
                     rc = op_sensor.readFIFO(MAX30101::TwoLedChannels, fifoData, readBytes);     
-                    
-                    numSamples = readBytes/DIV_SAMPLE; //calcualtes number of smaples read 
-                    
+                
                     if(rc == 0) 
                     {
                         
                         // Convert read bytes into samples
-                        for(idx = 0, c=0, i=0; idx < readBytes; idx+=3) 
+                        for(idx = 0, c=0; idx < readBytes; idx+=3) 
                         {
-                            //pc.printf("In sample adjustment \r\n");
+
                             sample = (fifoData[idx]<<16) | (fifoData[idx+1]<<8) | (fifoData[idx+2]);
-                            opSample = sample << 14;           // Sign extends sample 
-                            opSample = opSample >> 14;
+                            opSample = sample;
+                            opSample&=0x03FFFF;  //Mask MSB [23:18]
+                            
                             if(idx%2==0)
                             {
-                                //pc.printf("end red allocation\r\n");
                                 redData[r] = opSample;//saves to buff for calculations 
-                                fifoRED[i] = opSample;//saves to buff to print values just obtained 
                                 r++;
-                                i++;
                                 
                             }
                             else
                             {
-                                //pc.printf("end ir allocation\r\n");
+                               
                                 irData[ir] = opSample; //saves to buff for calculations 
-                                fifoIR[c] = opSample;//saves to buff to print values just obtained 
                                 ir++;
-                                c++;
                                 
                             }
-                            pc.printf("Red count = %i\r\n IR count = %i\r\n",r,ir);
-                            //pc.printf("idx = %i and readbytes = %i\r\n", idx,readBytes);
                             
                         }
                         
-                        if(r>=200 & ir>=200)//checks to make sure there are 200 samples in data buffers 
+                        if(r>=450 & ir>=450)//checks to make sure there are 450 
+                                            //samples in data buffers 
                         {
-                            pc.printf("In Calculation Function\r\n");
+                            
                             //calculate heart rate and spo2
-                            maxim_heart_rate_and_oxygen_saturation( irData, CALC_SAMPLE, 
+                            maxim_heart_rate_and_oxygen_saturation( irData, ir, 
                             redData, &spo2, &spo2Valid, &heartRate, &heartRateValid);
+                            pc.printf("SPO2 = %i\r\n",spo2);
+                            //heart rate is typically twice what it should be, thus divide by 2
+                            pc.printf("Heart Rate = %i\r\n",heartRate/2); 
                             
-                             for(c=100;c<200;c++)//dump first hundred samples after calculations 
+                             for(c=100;c<450;c++)//dump first hundred samples 
+                                                 //after calculations 
                             {
                                 redData[c-100]=redData[c];
                                 irData[c-100]=irData[c];
 
                             }
-                            r=50;
-                            ir=50;
-                            
-                            
-                        }
-
-                        
-                        
-                        pc.printf("Read Samples %i\r\n",numSamples);
-                        //prints the Red LED data 
-                        pc.printf("RED LED DATA\r\n");
-                        for(c=0; c<numSamples; c++)
-                        {
-                            pc.printf("%i\r\n",fifoRED[c]);  
-                        }
-                        
-                        //prints the IR LED data 
-                        pc.printf("IR LED DATA\r\n");
-                        for(c=0; c<numSamples; c++)
-                        {
-                            pc.printf("%i\r\n",fifoIR[c]);  
-                        }
-                        
-                        if(spo2Valid==1)
-                        {
-                            pc.printf("SPO2 = %i\r\n",spo2);
-                            spo2Valid=0;
-                        }
-                        else
-                        {
-                           pc.printf("SPO2 calculation waiting for enough samples\r\n");
-                        }
-                        if(heartRateValid==1)
-                        {
-                            pc.printf("Heart Rate = %i\r\n",heartRate);
-                            heartRateValid=0;
-                        }else
-                        {
-                            pc.printf("Heart rate calculation waiting for enough samples\r\n");
-                        }
-                        
-                        
+                            r=350;
+                            ir=350;    
+                        }  
                     }
                 }
             }
@@ -254,8 +207,8 @@
     if(rc == 0)
     {
         fifoConfig.all = 0;
-        fifoConfig.bits.fifo_a_full = 15;                               // Max level of 15 samples  
-        fifoConfig.bits.sample_average = MAX30101::AveragedSamples_8;   // Average 8 samples
+        fifoConfig.bits.fifo_a_full = 15;                            // Max level of 15 samples  
+        fifoConfig.bits.sample_average = MAX30101::AveragedSamples_8;// Average 8 samples
         rc = op_sensor.setFIFOConfiguration(fifoConfig);
     }
     
--- a/max32630fthr.lib	Sun Oct 15 19:22:36 2017 +0000
+++ b/max32630fthr.lib	Mon Nov 20 16:36:55 2017 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/MaximIntegrated/code/max32630fthr/#02d6b69121e5
+http://developer.mbed.org/teams/MaximIntegrated/code/max32630fthr/#9eb360063579