Fork of Maxim's human body temp sensor library that works with mbed-os 5

Fork of MAX30205 by Maxim Integrated

Committer:
j3
Date:
Mon Apr 03 23:11:58 2017 +0000
Revision:
0:cdad7a9ef486
Child:
1:d4271ef9f37f
Init commit, stand alone lib separated from HSP.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:cdad7a9ef486 1 /*******************************************************************************
j3 0:cdad7a9ef486 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:cdad7a9ef486 3 *
j3 0:cdad7a9ef486 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:cdad7a9ef486 5 * copy of this software and associated documentation files (the "Software"),
j3 0:cdad7a9ef486 6 * to deal in the Software without restriction, including without limitation
j3 0:cdad7a9ef486 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:cdad7a9ef486 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:cdad7a9ef486 9 * Software is furnished to do so, subject to the following conditions:
j3 0:cdad7a9ef486 10 *
j3 0:cdad7a9ef486 11 * The above copyright notice and this permission notice shall be included
j3 0:cdad7a9ef486 12 * in all copies or substantial portions of the Software.
j3 0:cdad7a9ef486 13 *
j3 0:cdad7a9ef486 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:cdad7a9ef486 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:cdad7a9ef486 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:cdad7a9ef486 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:cdad7a9ef486 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:cdad7a9ef486 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:cdad7a9ef486 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:cdad7a9ef486 21 *
j3 0:cdad7a9ef486 22 * Except as contained in this notice, the name of Maxim Integrated
j3 0:cdad7a9ef486 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:cdad7a9ef486 24 * Products, Inc. Branding Policy.
j3 0:cdad7a9ef486 25 *
j3 0:cdad7a9ef486 26 * The mere transfer of this software does not imply any licenses
j3 0:cdad7a9ef486 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:cdad7a9ef486 28 * trademarks, maskwork rights, or any other form of intellectual
j3 0:cdad7a9ef486 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:cdad7a9ef486 30 * ownership rights.
j3 0:cdad7a9ef486 31 *******************************************************************************
j3 0:cdad7a9ef486 32 */
j3 0:cdad7a9ef486 33 #include "MAX30205.h"
j3 0:cdad7a9ef486 34
j3 0:cdad7a9ef486 35 //******************************************************************************
j3 0:cdad7a9ef486 36 MAX30205::MAX30205(PinName sda, PinName scl, uint8_t slaveAddress):
j3 0:cdad7a9ef486 37 m_i2c(sda, scl), m_writeAddress(slaveAddress << 1), m_readAddress((slaveAddress << 1) | 1)
j3 0:cdad7a9ef486 38 {
j3 0:cdad7a9ef486 39 }
j3 0:cdad7a9ef486 40
j3 0:cdad7a9ef486 41 //******************************************************************************
j3 0:cdad7a9ef486 42 MAX30205::MAX30205(I2C& i2c, uint8_t slaveAddress):
j3 0:cdad7a9ef486 43 m_i2c(i2c), m_writeAddress(slaveAddress << 1), m_readAddress((slaveAddress << 1) | 1)
j3 0:cdad7a9ef486 44 {
j3 0:cdad7a9ef486 45 }
j3 0:cdad7a9ef486 46
j3 0:cdad7a9ef486 47 //******************************************************************************
j3 0:cdad7a9ef486 48 MAX30205::~MAX30205(void)
j3 0:cdad7a9ef486 49 {
j3 0:cdad7a9ef486 50 //empty block
j3 0:cdad7a9ef486 51 }
j3 0:cdad7a9ef486 52
j3 0:cdad7a9ef486 53 //******************************************************************************
j3 0:cdad7a9ef486 54 int32_t MAX30205::reg_write(Registers reg, uint8_t value)
j3 0:cdad7a9ef486 55 {
j3 0:cdad7a9ef486 56 int32_t result;
j3 0:cdad7a9ef486 57
j3 0:cdad7a9ef486 58 char cmdData[2] = {reg, value};
j3 0:cdad7a9ef486 59
j3 0:cdad7a9ef486 60 result = m_i2c.write(m_writeAddress, cmdData, 2);
j3 0:cdad7a9ef486 61
j3 0:cdad7a9ef486 62 return result;
j3 0:cdad7a9ef486 63 }
j3 0:cdad7a9ef486 64
j3 0:cdad7a9ef486 65 //******************************************************************************
j3 0:cdad7a9ef486 66 int32_t MAX30205::reg_write16(Registers reg, uint16_t value)
j3 0:cdad7a9ef486 67 {
j3 0:cdad7a9ef486 68 int32_t result;
j3 0:cdad7a9ef486 69
j3 0:cdad7a9ef486 70 uint8_t hi = ((value >> 8) & 0xFF);
j3 0:cdad7a9ef486 71 uint8_t lo = (value & 0xFF);
j3 0:cdad7a9ef486 72 char cmdData[3] = {reg, hi, lo};
j3 0:cdad7a9ef486 73
j3 0:cdad7a9ef486 74 result = m_i2c.write(m_writeAddress, cmdData, 3);
j3 0:cdad7a9ef486 75
j3 0:cdad7a9ef486 76 return result;
j3 0:cdad7a9ef486 77 }
j3 0:cdad7a9ef486 78
j3 0:cdad7a9ef486 79 //******************************************************************************
j3 0:cdad7a9ef486 80 int32_t MAX30205::reg_read(Registers reg, uint8_t& value)
j3 0:cdad7a9ef486 81 {
j3 0:cdad7a9ef486 82 int32_t result;
j3 0:cdad7a9ef486 83
j3 0:cdad7a9ef486 84 char cmdData[1] = {reg};
j3 0:cdad7a9ef486 85 char readData;
j3 0:cdad7a9ef486 86
j3 0:cdad7a9ef486 87 result = m_i2c.write(m_writeAddress, cmdData, 1);
j3 0:cdad7a9ef486 88 if(result == 0)
j3 0:cdad7a9ef486 89 {
j3 0:cdad7a9ef486 90 result = m_i2c.read(m_readAddress, &readData, 1);
j3 0:cdad7a9ef486 91 if(result == 0)
j3 0:cdad7a9ef486 92 {
j3 0:cdad7a9ef486 93 value = readData;
j3 0:cdad7a9ef486 94 }
j3 0:cdad7a9ef486 95 }
j3 0:cdad7a9ef486 96
j3 0:cdad7a9ef486 97 return result;
j3 0:cdad7a9ef486 98 }
j3 0:cdad7a9ef486 99
j3 0:cdad7a9ef486 100 //******************************************************************************
j3 0:cdad7a9ef486 101 int32_t MAX30205::reg_read16(Registers reg, uint16_t& value)
j3 0:cdad7a9ef486 102 {
j3 0:cdad7a9ef486 103 int32_t result;
j3 0:cdad7a9ef486 104
j3 0:cdad7a9ef486 105 char data[2];
j3 0:cdad7a9ef486 106 char cmdData[1] = {reg};
j3 0:cdad7a9ef486 107
j3 0:cdad7a9ef486 108 result = m_i2c.write(m_writeAddress, cmdData, 1);
j3 0:cdad7a9ef486 109 if(result == 0)
j3 0:cdad7a9ef486 110 {
j3 0:cdad7a9ef486 111 result = m_i2c.read(m_readAddress, data, 2);
j3 0:cdad7a9ef486 112 if (result == 0)
j3 0:cdad7a9ef486 113 {
j3 0:cdad7a9ef486 114 value = (data[0] << 8) + data[1];
j3 0:cdad7a9ef486 115 }
j3 0:cdad7a9ef486 116 }
j3 0:cdad7a9ef486 117
j3 0:cdad7a9ef486 118 return result;
j3 0:cdad7a9ef486 119 }
j3 0:cdad7a9ef486 120
j3 0:cdad7a9ef486 121 //******************************************************************************
j3 0:cdad7a9ef486 122 int32_t MAX30205::readTemperature(uint16_t& value)
j3 0:cdad7a9ef486 123 {
j3 0:cdad7a9ef486 124 int32_t result = reg_read16(MAX30205::Temperature, value);
j3 0:cdad7a9ef486 125
j3 0:cdad7a9ef486 126 return result;
j3 0:cdad7a9ef486 127 }
j3 0:cdad7a9ef486 128
j3 0:cdad7a9ef486 129 //******************************************************************************
j3 0:cdad7a9ef486 130 float MAX30205::toCelsius(uint32_t rawTemp)
j3 0:cdad7a9ef486 131 {
j3 0:cdad7a9ef486 132 float val1, val2;
j3 0:cdad7a9ef486 133
j3 0:cdad7a9ef486 134 val1 = static_cast<float>(rawTemp >> 8);
j3 0:cdad7a9ef486 135 val2 = static_cast<float>(rawTemp & 0xFF);
j3 0:cdad7a9ef486 136
j3 0:cdad7a9ef486 137 return(val2 + (val1 / 256.0F));
j3 0:cdad7a9ef486 138 }
j3 0:cdad7a9ef486 139
j3 0:cdad7a9ef486 140 //******************************************************************************
j3 0:cdad7a9ef486 141 float MAX30205::toFahrenheit(float temperatureC)
j3 0:cdad7a9ef486 142 {
j3 0:cdad7a9ef486 143 return((temperatureC * 1.8F) + 32.0f);
j3 0:cdad7a9ef486 144 }
j3 0:cdad7a9ef486 145
j3 0:cdad7a9ef486 146 //******************************************************************************
j3 0:cdad7a9ef486 147 int32_t MAX30205::reg_THYST_Read(uint16_t& value)
j3 0:cdad7a9ef486 148 {
j3 0:cdad7a9ef486 149 return reg_read16(MAX30205::THYST, value);
j3 0:cdad7a9ef486 150 }
j3 0:cdad7a9ef486 151
j3 0:cdad7a9ef486 152 //******************************************************************************
j3 0:cdad7a9ef486 153 int32_t MAX30205::reg_THYST_Write(uint16_t value)
j3 0:cdad7a9ef486 154 {
j3 0:cdad7a9ef486 155 return reg_write16(MAX30205::THYST, value);
j3 0:cdad7a9ef486 156 }