Library for MAX30101 SpO2 and heart rate sensor

Dependents:   HeartRate HeartRate proj final_project_ee119 ... more

Files at this revision

API Documentation at this revision

Comitter:
j3
Date:
Mon May 01 21:00:34 2017 +0000
Parent:
1:9e2a3b4a24c7
Child:
3:a2effad05c99
Commit message:
Added mbr fx implementations

Changed in this revision

MAX30101.cpp Show annotated file Show diff for this revision Revisions of this file
MAX30101.h Show annotated file Show diff for this revision Revisions of this file
--- a/MAX30101.cpp	Thu Apr 13 23:46:22 2017 +0000
+++ b/MAX30101.cpp	Mon May 01 21:00:34 2017 +0000
@@ -90,105 +90,172 @@
 //*****************************************************************************
 int32_t MAX30101::setFIFOConfiguration(const FIFO_Configuration_u config)
 {
-    return -1;
+    return writeRegister(FIFO_Configuration, config.all);
 }
     
 
 //*****************************************************************************
 int32_t MAX30101::getFIFOConfiguration(FIFO_Configuration_u &config)
 {
-    return -1;
+    return readRegister(FIFO_Configuration, config.all);
 }
     
     
 //*****************************************************************************
 int32_t MAX30101::setModeConfiguration(const ModeConfiguration_u config)
 {
-    return -1;
+    return writeRegister(ModeConfiguration, (config.all & 0xC7));
 }
     
 
 //*****************************************************************************    
 int32_t MAX30101::getModeConfiguration(ModeConfiguration_u &config)
 {
-    return -1;
+    return readRegister(ModeConfiguration, config.all);
 }   
     
 
 //*****************************************************************************    
 int32_t MAX30101::setSpO2Configuration(const SpO2Configuration_u config)
 {
-    return -1;
+    return writeRegister(SpO2Configuration, (config.all & 0x7F));
 }   
 
 
 //*****************************************************************************    
 int32_t MAX30101::getSpO2Configuration(SpO2Configuration_u &config)
 {
-    return -1;
+    return readRegister(SpO2Configuration, config.all);
 }
 
 
 //*****************************************************************************  
 int32_t MAX30101::setLEDPulseAmplitude(Registers_e reg, const uint8_t amp)
 {
-    return -1;
+    return writeRegister(reg, amp);
 }
 
 
 //*****************************************************************************  
 int32_t MAX30101::getLEDPulseAmplitude(Registers_e reg, uint8_t &amp)
 {
-    return -1;
+    return readRegister(reg, amp);
 }
 
 
 //*****************************************************************************  
 int32_t MAX30101::setMultiLEDModeControl(Registers_e reg, const ModeControlReg_u data)
 {
-    return -1;
+    return writeRegister(reg, data.all);
 }
  
     
 //*****************************************************************************  
 int32_t MAX30101::getMultiLEDModeControl(Registers_e reg, ModeControlReg_u &data)
 {
-    return -1;
+    return readRegister(reg, data.all);
 }
 
     
 //*****************************************************************************  
 int32_t MAX30101::getDieTemperature(uint16_t &data)
 {
-    return -1;
+    int32_t result = -1;
+    
+    //Die temp conversion time is 30ms
+    //ATTEMPTS > than 30ms at 100KHz SCL for getInterruptStatus call
+    const uint32_t ATTEMPTS = 100; 
+    uint32_t num_reads = 0;
+    
+    //local i2c transaction buffer
+    char local_data[2];
+    local_data[0] = DieTempConfig;
+    local_data[1] = 1;
+    
+    //initiate die temp conversion
+    result = m_i2cBus.write(I2C_W_ADRS, local_data, 2, true);
+    if(result == 0)
+    {
+        //object for holding status registers data
+        InterruptBitField_u status;
+        status.all = 0;
+        
+        //poll status registers until temp ready, or read fails
+        do
+        {
+            result = getInterruptStatus(status);
+            num_reads++;
+        }
+        while(!status.bits.die_temp && (result == 0) && (num_reads < ATTEMPTS));
+        
+        if(status.bits.die_temp)
+        {
+            //set pointer to temperature integer register
+            local_data[0] = DieTempInt;
+            result = m_i2cBus.write(I2C_W_ADRS, local_data, 1, true);
+            if(result == 0)
+            {
+                //read two bytes
+                result = m_i2cBus.read(I2C_R_ADRS, local_data, 2);
+                if(result == 0)
+                {
+                    //stuff data
+                    data = ( (local_data[0] << 8) | (local_data[1] << 4) );
+                    data = (data >> 4);
+                }
+            }
+        }
+        else
+        {
+            //if result equals 0 because num_reads exceeded ATTEMPTS,
+            //change result to -1.  Otherwise keep result's error code.
+            result = (result == 0) ? -1 : result;
+        }
+    }
+    return result;
 }
 
     
 //*****************************************************************************  
 int32_t MAX30101::getDieTemperatureC(float &data)
 {
-    return -1;
+    uint16_t raw_temp;
+    
+    int32_t result = getDieTemperature(raw_temp);
+    if(result == 0)
+    {
+        if(raw_temp & 0x0800)
+        {
+            data = ((0xFFFFF000 | raw_temp)/16.0F);
+        }
+        else
+        {
+            data = (raw_temp/16.0F);
+        }
+    }
+    
+    return result;
 }
 
 
 //*****************************************************************************  
 float MAX30101::celsius2fahrenheit(float c) 
 {
-    return 0.0F;
+    return ((1.8F * c) + 32.0F);
 }
     
 
 //*****************************************************************************     
 int32_t MAX30101::setProxIntThreshold(const uint8_t data)
 {
-    return -1;
+    return writeRegister(ProxIntThreshold, data);
 }
     
 
 //*****************************************************************************     
 int32_t MAX30101::getProxIntThreshold(uint8_t &data)
 {
-    return -1;
+    return readRegister(ProxIntThreshold, data);
 }
 
 
--- a/MAX30101.h	Thu Apr 13 23:46:22 2017 +0000
+++ b/MAX30101.h	Mon May 01 21:00:34 2017 +0000
@@ -282,7 +282,7 @@
     int32_t getMultiLEDModeControl(Registers_e reg, ModeControlReg_u &data);
     
     /**
-    * @brief Gets raw die temperature
+    * @brief Gets raw die temperature, interrupt must be enabled
     *
     * @param[out] data - Raw die temperature on success
     *
@@ -291,7 +291,7 @@
     int32_t getDieTemperature(uint16_t &data);
     
     /**
-    * @brief Gets die temperature in celsius
+    * @brief Gets die temperature in celsius, interrupt must be enabled
     *
     * @param[out] data - Die temperature in celsius on success
     *