Arun Raj / Mbed OS MAXREFDES101_SOURCE

Dependencies:   max32630fthr Adafruit_FeatherOLED USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX30205.cpp Source File

MAX30205.cpp

00001 /*******************************************************************************
00002  * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  * Except as contained in this notice, the name of Maxim Integrated
00023  * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024  * Products, Inc. Branding Policy.
00025  *
00026  * The mere transfer of this software does not imply any licenses
00027  * of trade secrets, proprietary technology, copyrights, patents,
00028  * trademarks, maskwork rights, or any other form of intellectual
00029  * property whatsoever. Maxim Integrated Products, Inc. retains all
00030  * ownership rights.
00031  *******************************************************************************
00032  */
00033  
00034  
00035 #include "MAX30205.h"
00036 
00037 
00038 //******************************************************************************
00039 MAX30205::MAX30205(I2C *i2c, uint8_t slaveAddress):
00040 m_i2c(i2c), m_writeAddress(slaveAddress << 1), 
00041 m_readAddress((slaveAddress << 1) | 1)
00042 {
00043 
00044 }
00045 
00046 
00047 //******************************************************************************
00048 MAX30205::~MAX30205(void) 
00049 {
00050   //empty block
00051 }
00052 
00053 
00054 
00055 
00056 //******************************************************************************
00057 int32_t MAX30205::readTemperature(uint16_t &value) 
00058 {
00059   return readRegister(MAX30205::Temperature, value);
00060 }
00061 
00062 
00063 //******************************************************************************
00064 int32_t MAX30205::readConfiguration(Configuration_u &config)
00065 {
00066     uint16_t data;
00067     
00068     int32_t result = readRegister(MAX30205::Configuration, data);
00069     if(result == 0)
00070     {
00071         config.all = (0x00FF & data);
00072     }
00073     
00074     return result;
00075     
00076 }
00077 
00078 
00079 //******************************************************************************    
00080 int32_t MAX30205::writeConfiguration(const Configuration_u config)
00081 {
00082     uint16_t local_config = (0x00FF & config.all);
00083     
00084     return writeRegister(MAX30205::Configuration, local_config);
00085 }
00086 
00087 
00088 //******************************************************************************
00089 int32_t MAX30205::readTHYST(uint16_t &value) 
00090 {
00091   return readRegister(MAX30205::THYST, value);
00092 }
00093 
00094 
00095 //******************************************************************************
00096 int32_t MAX30205::writeTHYST(uint16_t value) 
00097 {
00098   return writeRegister(MAX30205::THYST, value);
00099 }
00100 
00101 
00102 //******************************************************************************
00103 int32_t MAX30205::readTOS(uint16_t &value)
00104 {
00105     return readRegister(MAX30205::TOS, value);
00106 }
00107 
00108 
00109 //******************************************************************************
00110 int32_t MAX30205::writeTOS(const uint16_t value)
00111 {
00112     return writeRegister(MAX30205::TOS, value);
00113 }
00114 
00115 
00116 //******************************************************************************
00117 float MAX30205::toCelsius(uint32_t rawTemp) 
00118 {
00119   uint8_t val1, val2;
00120   float result;
00121   
00122   val1 = (rawTemp >> 8);
00123   val2 = (rawTemp & 0xFF);
00124   
00125   result = static_cast<float>(val1 + (val2/ 256.0F));
00126   
00127   return result;
00128 }
00129 
00130 
00131 //******************************************************************************
00132 float MAX30205::toFahrenheit(float temperatureC) 
00133 {
00134   return((temperatureC * 1.8F) + 32.0f);
00135 }
00136 
00137 
00138 //******************************************************************************
00139 int32_t MAX30205::writeRegister(Registers_e reg, uint16_t value) 
00140 {
00141   int32_t result;
00142   
00143   uint8_t hi = ((value >> 8) & 0xFF);
00144   uint8_t lo = (value & 0xFF);
00145   char cmdData[3] = {reg, hi, lo};
00146   
00147   result = m_i2c->write(m_writeAddress, cmdData, 3);
00148   
00149   return result;
00150 }
00151 
00152 
00153 //******************************************************************************
00154 int32_t MAX30205::readRegister(Registers_e reg, uint16_t &value) 
00155 {
00156   int32_t result;
00157   
00158   char data[2];
00159   char cmdData[1] = {reg};
00160   
00161   result = m_i2c->write(m_writeAddress, cmdData, 1);
00162   if(result == 0)
00163   {
00164       result = m_i2c->read(m_readAddress, data, 2);
00165       if (result == 0)
00166       {
00167           value = (data[0] << 8) + data[1];
00168       }
00169   }
00170   
00171   return result;
00172 }
00173 
00174 int MAX30205::dump_registers(addr_val_pair *reg_vals){
00175     int i;
00176     int ret = 0;
00177     uint16_t val;
00178     for (i = 0x00; i <= 0x03; i++) {
00179         reg_vals[i].addr = i;
00180         ret |= readRegister(static_cast<Registers_e>(i), val);
00181         reg_vals[i].val = val;
00182     }
00183 
00184     return ret;
00185 }
00186 
00187 const char *MAX30205::get_sensor_part_name()
00188 {
00189     return "max30205";
00190 }
00191 
00192 const char *MAX30205::get_sensor_algo_ver()
00193 {
00194     return "dummy_algo_ver";
00195 }
00196 
00197 
00198 int MAX30205::sensor_enable(int enable){
00199     int32_t ret;
00200     Configuration_u config;
00201 
00202 
00203     ret = readConfiguration(config);
00204     if(ret != 0)
00205         return ret;
00206 
00207     if(enable){
00208         config.bits.shutdown = 0;
00209     }else{
00210         config.bits.shutdown = 1;
00211     }
00212 
00213     return writeConfiguration(config);
00214 }
00215 
00216 /**
00217 * @brief    Get sensor ID.
00218 *
00219 * @returns  Sensor ID number.
00220 */
00221 unsigned char MAX30205::get_sensor_id() {
00222 
00223     return( SENSOR_ID_MAX30205 );
00224 
00225 }