Lucas Lim / Mbed 2 deprecated HSP_Temperature_Barometer_CS3237

Dependencies:   mbed

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 //MAX30205::~MAX30205(void) 
00048 //{
00049 //  //empty block
00050 //}
00051 //
00052 //
00053 ////******************************************************************************
00054 //int32_t MAX30205::readTemperature(uint16_t &value) 
00055 //{
00056 //  return readRegister(MAX30205::Temperature, value);
00057 //
00058 //}
00059 //
00060 //
00061 ////******************************************************************************
00062 //int32_t MAX30205::readConfiguration(Configuration_u &config)
00063 //{
00064 //    uint16_t data;
00065 //    
00066 //    int32_t result = readRegister(MAX30205::Configuration, data);
00067 //    if(result == 0)
00068 //    {
00069 //        config.all = (0x00FF & data);
00070 //    }
00071 //    
00072 //    return result;
00073 //    
00074 //}
00075 //
00076 //
00077 ////******************************************************************************    
00078 //int32_t MAX30205::writeConfiguration(const Configuration_u config)
00079 //{
00080 //    uint16_t local_config = (0x00FF & config.all);
00081 //    
00082 //    return writeRegister(MAX30205::Configuration, local_config);
00083 //}
00084 //
00085 //
00086 ////******************************************************************************
00087 //int32_t MAX30205::readTHYST(uint16_t &value) 
00088 //{
00089 //  return readRegister(MAX30205::THYST, value);
00090 //}
00091 //
00092 //
00093 ////******************************************************************************
00094 //int32_t MAX30205::writeTHYST(uint16_t value) 
00095 //{
00096 //  return writeRegister(MAX30205::THYST, value);
00097 //}
00098 //
00099 //
00100 ////******************************************************************************
00101 //int32_t MAX30205::readTOS(uint16_t &value)
00102 //{
00103 //    return readRegister(MAX30205::TOS, value);
00104 //}
00105 //
00106 //
00107 ////******************************************************************************
00108 //int32_t MAX30205::writeTOS(const uint16_t value)
00109 //{
00110 //    return writeRegister(MAX30205::TOS, value);
00111 //}
00112 //
00113 //
00114 ////******************************************************************************
00115 //float MAX30205::toCelsius(uint32_t rawTemp) 
00116 //{
00117 //  uint8_t val1, val2;
00118 //  float result;
00119 //  
00120 //  val1 = (rawTemp >> 8);
00121 //  val2 = (rawTemp & 0xFF);
00122 //  
00123 //  result = static_cast<float>(val1 + (val2/ 256.0F));
00124 //  
00125 //  return result;
00126 //}
00127 //
00128 //
00129 ////******************************************************************************
00130 //float MAX30205::toFahrenheit(float temperatureC) 
00131 //{
00132 //  return((temperatureC * 1.8F) + 32.0f);
00133 //}
00134 //
00135 //
00136 ////******************************************************************************
00137 //int32_t MAX30205::writeRegister(Registers_e reg, uint16_t value) 
00138 //{
00139 //  int32_t result;
00140 //  
00141 //  uint8_t hi = ((value >> 8) & 0xFF);
00142 //  uint8_t lo = (value & 0xFF);
00143 //  char cmdData[3] = {reg, hi, lo};
00144 //  
00145 //  result = m_i2c.write(m_writeAddress, cmdData, 3);
00146 //  
00147 //  return result;
00148 //}
00149 //
00150 //
00151 ////******************************************************************************
00152 //int32_t MAX30205::readRegister(Registers_e reg, uint16_t &value) 
00153 //{
00154 //  int32_t result;
00155 //  
00156 //  char data[2];
00157 //  char cmdData[1] = {reg};
00158 //  
00159 //  result = m_i2c.write(m_writeAddress, cmdData, 1);
00160 //  if(result == 0)
00161 //  {
00162 //      result = m_i2c.read(m_readAddress, data, 2);
00163 //      if (result == 0)
00164 //      {
00165 //          value = (data[0] << 8) + data[1];
00166 //      }
00167 //  }
00168 //  
00169 //  return result;
00170 //}
00171 
00172 
00173 /*******************************************************************************
00174  * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00175  *
00176  * Permission is hereby granted, free of charge, to any person obtaining a
00177  * copy of this software and associated documentation files (the "Software"),
00178  * to deal in the Software without restriction, including without limitation
00179  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00180  * and/or sell copies of the Software, and to permit persons to whom the
00181  * Software is furnished to do so, subject to the following conditions:
00182  *
00183  * The above copyright notice and this permission notice shall be included
00184  * in all copies or substantial portions of the Software.
00185  *
00186  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00187  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00188  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00189  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00190  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00191  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00192  * OTHER DEALINGS IN THE SOFTWARE.
00193  *
00194  * Except as contained in this notice, the name of Maxim Integrated
00195  * Products, Inc. shall not be used except as stated in the Maxim Integrated
00196  * Products, Inc. Branding Policy.
00197  *
00198  * The mere transfer of this software does not imply any licenses
00199  * of trade secrets, proprietary technology, copyrights, patents,
00200  * trademarks, maskwork rights, or any other form of intellectual
00201  * property whatsoever. Maxim Integrated Products, Inc. retains all
00202  * ownership rights.
00203  *******************************************************************************
00204  */
00205 #include "MAX30205.h"
00206 
00207 //******************************************************************************
00208 MAX30205::MAX30205(PinName sda, PinName scl, int slaveAddress) : 
00209           slaveAddress(slaveAddress) {
00210   i2c = new I2C(sda, scl);
00211   isOwner = true;
00212   i2c->frequency(100000);
00213 }
00214 
00215 //******************************************************************************
00216 MAX30205::MAX30205(I2C *i2c, int slaveAddress) : slaveAddress(slaveAddress) {
00217   this->i2c = i2c;
00218   i2c->frequency(100000);
00219   isOwner = false;
00220 }
00221 
00222 //******************************************************************************
00223 MAX30205::~MAX30205(void) {
00224   if (isOwner == true) {
00225     delete i2c;
00226   }
00227 }
00228 
00229 //******************************************************************************
00230 int MAX30205::reg_write(char reg, char value) {
00231   int result;
00232   char cmdData[2] = {(char)reg, value};
00233   result = i2c->write(slaveAddress, cmdData, 2);
00234   if (result != 0){
00235     return -1;
00236   }
00237   return 0;
00238 }
00239 
00240 //******************************************************************************
00241 int MAX30205::reg_write16(char reg, uint16_t value) {
00242   int result;
00243   char hi = (value >> 8) & 0xFF;
00244   char lo = value & 0xFF;
00245   char cmdData[3] = {reg, hi, lo};
00246   result = i2c->write(slaveAddress, cmdData, 3);
00247   if (result != 0) {
00248     return -1;
00249   }
00250   return 0;
00251 }
00252 
00253 //******************************************************************************
00254 int MAX30205::reg_read(char reg, char *value) {
00255   int result;
00256   char cmdData[1] = {reg};
00257 
00258   result = i2c->write(slaveAddress, cmdData, 1);
00259   if (result != 0) {
00260     return -1;
00261   }
00262   result = i2c->read(slaveAddress, value, 1);
00263   if (result != 0){
00264     return -1;
00265   }
00266   return 0;
00267 }
00268 
00269 //******************************************************************************
00270 int MAX30205::reg_read16(char reg, uint16_t *value) {
00271   int result;
00272   char data[2];
00273   char cmdData[1] = {reg};
00274   result = i2c->write(slaveAddress, cmdData, 1);
00275   if (result != 0) {
00276     return -1;
00277   }
00278   result = i2c->read(slaveAddress, data, 2);
00279   if (result != 0) {
00280     return -1;
00281   }
00282   *value = (data[0] << 8) + data[1];
00283   return 0;
00284 }
00285 
00286 //******************************************************************************
00287 int MAX30205::readTemperature(uint16_t *value) {
00288   uint8_t data[2];
00289   int status;
00290   status = reg_read16(MAX30205_Temperature, (uint16_t *)&data);
00291   *value = (data[0] << 8) + data[1];
00292   return status;
00293 }
00294 
00295 //******************************************************************************
00296 float MAX30205::toCelsius(unsigned int rawTemp) {
00297   float val;
00298   float val1, val2;
00299   val1 = (float)(rawTemp >> 8);
00300   val2 = (float)(rawTemp & 0xFF);
00301   val = val2 + (val1 / 256.0f);
00302   return val;
00303 }
00304 
00305 //******************************************************************************
00306 float MAX30205::toFahrenheit(float temperatureC) {
00307   return temperatureC * 9.0f / 5.0f + 32.0f;
00308 }
00309 
00310 //******************************************************************************
00311 int MAX30205::reg_THYST_Read(uint16_t *value) {
00312   return reg_read16(MAX30205_THYST, value);
00313 }
00314 
00315 //******************************************************************************
00316 int MAX30205::reg_THYST_Write(uint16_t value) {
00317   return reg_write16(MAX30205_THYST, value);
00318 }