Fork of Maxim's human body temp sensor library that works with mbed-os 5
Fork of MAX30205 by
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_address(slaveAddress) 00041 { 00042 } 00043 00044 00045 //****************************************************************************** 00046 MAX30205::~MAX30205(void) 00047 { 00048 //empty block 00049 } 00050 00051 00052 //****************************************************************************** 00053 int32_t MAX30205::readTemperature(uint16_t &value) 00054 { 00055 return readRegister(MAX30205::Temperature, value); 00056 } 00057 00058 00059 //****************************************************************************** 00060 int32_t MAX30205::readConfiguration(Configuration_u &config) 00061 { 00062 uint16_t data; 00063 00064 int32_t result = readRegister(MAX30205::Configuration, data); 00065 if(result == 0) 00066 { 00067 config.all = (0x00FF & data); 00068 } 00069 00070 return result; 00071 00072 } 00073 00074 00075 //****************************************************************************** 00076 int32_t MAX30205::writeConfiguration(const Configuration_u config) 00077 { 00078 uint16_t local_config = (0x00FF & config.all); 00079 00080 return writeRegister(MAX30205::Configuration, local_config); 00081 } 00082 00083 00084 //****************************************************************************** 00085 int32_t MAX30205::readTHYST(uint16_t &value) 00086 { 00087 return readRegister(MAX30205::THYST, value); 00088 } 00089 00090 00091 //****************************************************************************** 00092 int32_t MAX30205::writeTHYST(uint16_t value) 00093 { 00094 return writeRegister(MAX30205::THYST, value); 00095 } 00096 00097 00098 //****************************************************************************** 00099 int32_t MAX30205::readTOS(uint16_t &value) 00100 { 00101 return readRegister(MAX30205::TOS, value); 00102 } 00103 00104 00105 //****************************************************************************** 00106 int32_t MAX30205::writeTOS(const uint16_t value) 00107 { 00108 return writeRegister(MAX30205::TOS, value); 00109 } 00110 00111 00112 //****************************************************************************** 00113 float MAX30205::toCelsius(uint32_t rawTemp) 00114 { 00115 uint8_t val1, val2; 00116 float result; 00117 00118 val1 = (rawTemp >> 8); 00119 val2 = (rawTemp & 0xFF); 00120 00121 result = static_cast<float>(val1 + (val2/ 256.0F)); 00122 00123 return result; 00124 } 00125 00126 00127 //****************************************************************************** 00128 float MAX30205::toFahrenheit(float temperatureC) 00129 { 00130 return((temperatureC * 1.8F) + 32.0f); 00131 } 00132 00133 00134 //****************************************************************************** 00135 int32_t MAX30205::writeRegister(Registers_e reg, uint16_t value) 00136 { 00137 int32_t result; 00138 00139 uint8_t hi = ((value >> 8) & 0xFF); 00140 uint8_t lo = (value & 0xFF); 00141 char cmdData[3] = {reg, hi, lo}; 00142 00143 result = m_i2c.write(m_address, cmdData, 3); 00144 00145 return result; 00146 } 00147 00148 00149 //****************************************************************************** 00150 int32_t MAX30205::readRegister(Registers_e reg, uint16_t &value) 00151 { 00152 int32_t result; 00153 00154 char data[2]; 00155 char cmdData[1] = {reg}; 00156 00157 result = m_i2c.write(m_address, cmdData, 1); 00158 if(result == 0) 00159 { 00160 result = m_i2c.read(m_address, data, 2); 00161 if (result == 0) 00162 { 00163 value = (data[0] << 8) + data[1]; 00164 } 00165 } 00166 00167 return result; 00168 }
Generated on Thu Jul 14 2022 10:01:50 by
