Library for MAX30208 Temperature Sensor

Dependents:   LP_Receiver_Wakeup

Committer:
tlyp
Date:
Fri Sep 04 18:09:49 2020 +0000
Revision:
0:468c23ed35b6
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tlyp 0:468c23ed35b6 1 /*******************************************************************************
tlyp 0:468c23ed35b6 2 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
tlyp 0:468c23ed35b6 3 *
tlyp 0:468c23ed35b6 4 * Permission is hereby granted, free of charge, to any person obtaining a
tlyp 0:468c23ed35b6 5 * copy of this software and associated documentation files (the "Software"),
tlyp 0:468c23ed35b6 6 * to deal in the Software without restriction, including without limitation
tlyp 0:468c23ed35b6 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
tlyp 0:468c23ed35b6 8 * and/or sell copies of the Software, and to permit persons to whom the
tlyp 0:468c23ed35b6 9 * Software is furnished to do so, subject to the following conditions:
tlyp 0:468c23ed35b6 10 *
tlyp 0:468c23ed35b6 11 * The above copyright notice and this permission notice shall be included
tlyp 0:468c23ed35b6 12 * in all copies or substantial portions of the Software.
tlyp 0:468c23ed35b6 13 *
tlyp 0:468c23ed35b6 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
tlyp 0:468c23ed35b6 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
tlyp 0:468c23ed35b6 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
tlyp 0:468c23ed35b6 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
tlyp 0:468c23ed35b6 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
tlyp 0:468c23ed35b6 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
tlyp 0:468c23ed35b6 20 * OTHER DEALINGS IN THE SOFTWARE.
tlyp 0:468c23ed35b6 21 *
tlyp 0:468c23ed35b6 22 * Except as contained in this notice, the name of Maxim Integrated
tlyp 0:468c23ed35b6 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
tlyp 0:468c23ed35b6 24 * Products, Inc. Branding Policy.
tlyp 0:468c23ed35b6 25 *
tlyp 0:468c23ed35b6 26 * The mere transfer of this software does not imply any licenses
tlyp 0:468c23ed35b6 27 * of trade secrets, proprietary technology, copyrights, patents,
tlyp 0:468c23ed35b6 28 * trademarks, maskwork rights, or any other form of intellectual
tlyp 0:468c23ed35b6 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
tlyp 0:468c23ed35b6 30 * ownership rights.
tlyp 0:468c23ed35b6 31 *******************************************************************************
tlyp 0:468c23ed35b6 32 * @file MAX30208.cpp
tlyp 0:468c23ed35b6 33 * @brief This is the C++ file used for the MAX30208 human body temperature sensor library.
tlyp 0:468c23ed35b6 34 * @version 1.0
tlyp 0:468c23ed35b6 35 * @notes This file needs to be imported along with MAX30208.h for the program to work properly. This is library containing basic functions to be used in conjunction with the MAX30208. This library does not support any other devices. This is an MBed tested library.
tlyp 0:468c23ed35b6 36 *****************************************************************************/
tlyp 0:468c23ed35b6 37
tlyp 0:468c23ed35b6 38
tlyp 0:468c23ed35b6 39 #include "MAX30208.h"
tlyp 0:468c23ed35b6 40
tlyp 0:468c23ed35b6 41
tlyp 0:468c23ed35b6 42 //******************************************************************************
tlyp 0:468c23ed35b6 43 MAX30208::MAX30208(I2C &i2c, uint8_t slaveAddress):
tlyp 0:468c23ed35b6 44 m_i2c(i2c), m_writeAddress(slaveAddress << 1),
tlyp 0:468c23ed35b6 45 m_readAddress((slaveAddress << 1) | 1)
tlyp 0:468c23ed35b6 46 {
tlyp 0:468c23ed35b6 47 }
tlyp 0:468c23ed35b6 48
tlyp 0:468c23ed35b6 49
tlyp 0:468c23ed35b6 50 //******************************************************************************
tlyp 0:468c23ed35b6 51 MAX30208::~MAX30208(void) {
tlyp 0:468c23ed35b6 52 //empty block
tlyp 0:468c23ed35b6 53 }
tlyp 0:468c23ed35b6 54
tlyp 0:468c23ed35b6 55 //******************************************************************************
tlyp 0:468c23ed35b6 56 int32_t MAX30208::writeInterruptRegister(Configuration_InterruptEnable config) {
tlyp 0:468c23ed35b6 57 return(writeRegister(MAX30208::Interrupt_Enable,(config.all << 8),2));
tlyp 0:468c23ed35b6 58 }
tlyp 0:468c23ed35b6 59
tlyp 0:468c23ed35b6 60 //******************************************************************************
tlyp 0:468c23ed35b6 61 int32_t MAX30208::readInterruptRegister(Configuration_InterruptEnable &config) {
tlyp 0:468c23ed35b6 62 uint16_t data;
tlyp 0:468c23ed35b6 63 int32_t status;
tlyp 0:468c23ed35b6 64 status = readRegister(MAX30208::Interrupt_Enable, data, 1);
tlyp 0:468c23ed35b6 65 if(status == 0) {
tlyp 0:468c23ed35b6 66 config.all = data;
tlyp 0:468c23ed35b6 67 }
tlyp 0:468c23ed35b6 68 return(status);
tlyp 0:468c23ed35b6 69 }
tlyp 0:468c23ed35b6 70
tlyp 0:468c23ed35b6 71 //******************************************************************************
tlyp 0:468c23ed35b6 72 int32_t MAX30208::readStatus(uint16_t &value) {
tlyp 0:468c23ed35b6 73 return(readRegister(MAX30208::Status,value, 1));
tlyp 0:468c23ed35b6 74 }
tlyp 0:468c23ed35b6 75
tlyp 0:468c23ed35b6 76 //******************************************************************************
tlyp 0:468c23ed35b6 77 int32_t MAX30208::readWritePointer(uint16_t &value) {
tlyp 0:468c23ed35b6 78 return (readRegister(MAX30208::FIFO_Write_Pointer,value, 1));
tlyp 0:468c23ed35b6 79 }
tlyp 0:468c23ed35b6 80
tlyp 0:468c23ed35b6 81 //******************************************************************************
tlyp 0:468c23ed35b6 82 int32_t MAX30208::readReadPointer(uint16_t &value) {
tlyp 0:468c23ed35b6 83 return (readRegister(MAX30208::FIFO_Read_Pointer,value,1));
tlyp 0:468c23ed35b6 84 }
tlyp 0:468c23ed35b6 85
tlyp 0:468c23ed35b6 86 //******************************************************************************
tlyp 0:468c23ed35b6 87 int32_t MAX30208::writeReadPointer(uint8_t config) {
tlyp 0:468c23ed35b6 88 return(writeRegister(MAX30208::FIFO_Read_Pointer, (config << 8),2));
tlyp 0:468c23ed35b6 89 }
tlyp 0:468c23ed35b6 90
tlyp 0:468c23ed35b6 91 //******************************************************************************
tlyp 0:468c23ed35b6 92 int32_t MAX30208::readOverflow(uint16_t &value) {
tlyp 0:468c23ed35b6 93 return(readRegister(MAX30208::FIFO_Overflow_Counter,value, 1));
tlyp 0:468c23ed35b6 94 }
tlyp 0:468c23ed35b6 95
tlyp 0:468c23ed35b6 96 //******************************************************************************
tlyp 0:468c23ed35b6 97 int32_t MAX30208::readDataCounter(uint16_t &value) {
tlyp 0:468c23ed35b6 98 return(readRegister(MAX30208::FIFO_Data_Counter,value, 1));
tlyp 0:468c23ed35b6 99 }
tlyp 0:468c23ed35b6 100
tlyp 0:468c23ed35b6 101 //******************************************************************************
tlyp 0:468c23ed35b6 102 int32_t MAX30208::readData(uint16_t &value) {
tlyp 0:468c23ed35b6 103 return(readRegister(MAX30208::FIFO_Data,value, 2));
tlyp 0:468c23ed35b6 104 }
tlyp 0:468c23ed35b6 105
tlyp 0:468c23ed35b6 106 //******************************************************************************
tlyp 0:468c23ed35b6 107 int32_t MAX30208::takeDataMeasurment() {
tlyp 0:468c23ed35b6 108 return(writeRegister(MAX30208::Temp_Sensor_Setup,0xFF00,2));
tlyp 0:468c23ed35b6 109 }
tlyp 0:468c23ed35b6 110
tlyp 0:468c23ed35b6 111 //******************************************************************************
tlyp 0:468c23ed35b6 112 int32_t MAX30208::readFIFOConfig1(uint16_t &value) {
tlyp 0:468c23ed35b6 113 return (readRegister(MAX30208::FIFO_Config1,value, 1));
tlyp 0:468c23ed35b6 114 }
tlyp 0:468c23ed35b6 115
tlyp 0:468c23ed35b6 116 //******************************************************************************
tlyp 0:468c23ed35b6 117 int32_t MAX30208::writeFIFOConfig1(uint8_t config) {
tlyp 0:468c23ed35b6 118 return(writeRegister(MAX30208::FIFO_Config1,(config << 8),2));
tlyp 0:468c23ed35b6 119 }
tlyp 0:468c23ed35b6 120
tlyp 0:468c23ed35b6 121 //******************************************************************************
tlyp 0:468c23ed35b6 122 int32_t MAX30208::readFIFOConfig2(Configuration_FIFOConfig2 &config) {
tlyp 0:468c23ed35b6 123 uint16_t data;
tlyp 0:468c23ed35b6 124 int32_t status;
tlyp 0:468c23ed35b6 125 status = readRegister(MAX30208::FIFO_Config2, data, 1);
tlyp 0:468c23ed35b6 126 if(status == 0){
tlyp 0:468c23ed35b6 127 config.all = data;
tlyp 0:468c23ed35b6 128 }
tlyp 0:468c23ed35b6 129 return(status);
tlyp 0:468c23ed35b6 130 }
tlyp 0:468c23ed35b6 131
tlyp 0:468c23ed35b6 132 //******************************************************************************
tlyp 0:468c23ed35b6 133 int32_t MAX30208::writeFIFOConfig2(Configuration_FIFOConfig2 config) {
tlyp 0:468c23ed35b6 134 return(writeRegister(MAX30208::FIFO_Config2,(config.all << 8),2));
tlyp 0:468c23ed35b6 135 }
tlyp 0:468c23ed35b6 136
tlyp 0:468c23ed35b6 137 //******************************************************************************
tlyp 0:468c23ed35b6 138 int32_t MAX30208::resetDevice() {
tlyp 0:468c23ed35b6 139 return(writeRegister(MAX30208::System_Control,(0x01 << 8),2));
tlyp 0:468c23ed35b6 140 }
tlyp 0:468c23ed35b6 141
tlyp 0:468c23ed35b6 142 //******************************************************************************
tlyp 0:468c23ed35b6 143 int32_t MAX30208::readAlarmHigh(uint16_t &temp) {
tlyp 0:468c23ed35b6 144 return readRegister(MAX30208::Alarm_High_MSB,temp, 2);
tlyp 0:468c23ed35b6 145 }
tlyp 0:468c23ed35b6 146
tlyp 0:468c23ed35b6 147 //******************************************************************************
tlyp 0:468c23ed35b6 148 int32_t MAX30208::writeAlarmHigh(uint16_t temp) {
tlyp 0:468c23ed35b6 149 return(writeRegister(MAX30208::Alarm_High_MSB, temp, 3));
tlyp 0:468c23ed35b6 150 }
tlyp 0:468c23ed35b6 151
tlyp 0:468c23ed35b6 152 //******************************************************************************
tlyp 0:468c23ed35b6 153 int32_t MAX30208::readAlarmLow(uint16_t &value) {
tlyp 0:468c23ed35b6 154 return (readRegister(MAX30208::Alarm_Low_MSB,value, 2));
tlyp 0:468c23ed35b6 155 }
tlyp 0:468c23ed35b6 156
tlyp 0:468c23ed35b6 157 //******************************************************************************
tlyp 0:468c23ed35b6 158 int32_t MAX30208::writeAlarmLow(uint16_t temp) {
tlyp 0:468c23ed35b6 159 return(writeRegister(MAX30208::Alarm_Low_MSB,temp,3));
tlyp 0:468c23ed35b6 160 }
tlyp 0:468c23ed35b6 161
tlyp 0:468c23ed35b6 162 //******************************************************************************
tlyp 0:468c23ed35b6 163 int32_t MAX30208::readGPIOSetup(Configuration_GPIOSetup &config) {
tlyp 0:468c23ed35b6 164 uint16_t data;
tlyp 0:468c23ed35b6 165 int32_t status;
tlyp 0:468c23ed35b6 166 status = readRegister(MAX30208::GPIO_Setup, data, 1);
tlyp 0:468c23ed35b6 167 if(status == 0) {
tlyp 0:468c23ed35b6 168 config.all = data;
tlyp 0:468c23ed35b6 169 }
tlyp 0:468c23ed35b6 170 return(status);
tlyp 0:468c23ed35b6 171 }
tlyp 0:468c23ed35b6 172
tlyp 0:468c23ed35b6 173 //******************************************************************************
tlyp 0:468c23ed35b6 174 int32_t MAX30208::writeGPIOSetup(Configuration_GPIOSetup config) {
tlyp 0:468c23ed35b6 175 return(writeRegister(MAX30208::GPIO_Setup,(config.all << 8),2));
tlyp 0:468c23ed35b6 176 }
tlyp 0:468c23ed35b6 177
tlyp 0:468c23ed35b6 178 //******************************************************************************
tlyp 0:468c23ed35b6 179 int32_t MAX30208::readGPIOControl(Configuration_GPIOControl &config) {
tlyp 0:468c23ed35b6 180 uint16_t data;
tlyp 0:468c23ed35b6 181 int32_t status;
tlyp 0:468c23ed35b6 182 status = readRegister(MAX30208::GPIO_Control, data, 1);
tlyp 0:468c23ed35b6 183 if(status == 0) {
tlyp 0:468c23ed35b6 184 config.all = data;
tlyp 0:468c23ed35b6 185 }
tlyp 0:468c23ed35b6 186 return(status);
tlyp 0:468c23ed35b6 187 }
tlyp 0:468c23ed35b6 188
tlyp 0:468c23ed35b6 189 //******************************************************************************
tlyp 0:468c23ed35b6 190 int32_t MAX30208::writeGPIOControl(Configuration_GPIOControl config) {
tlyp 0:468c23ed35b6 191 return(writeRegister(MAX30208::GPIO_Control,(config.all << 8),2));
tlyp 0:468c23ed35b6 192 }
tlyp 0:468c23ed35b6 193
tlyp 0:468c23ed35b6 194 //******************************************************************************
tlyp 0:468c23ed35b6 195 float MAX30208::toCelsius(uint16_t rawTemp) {
tlyp 0:468c23ed35b6 196 float celsius;
tlyp 0:468c23ed35b6 197 celsius = 0.005*rawTemp;
tlyp 0:468c23ed35b6 198 return celsius;
tlyp 0:468c23ed35b6 199 }
tlyp 0:468c23ed35b6 200
tlyp 0:468c23ed35b6 201 //******************************************************************************
tlyp 0:468c23ed35b6 202 float MAX30208::toFahrenheit(float temperatureC) {
tlyp 0:468c23ed35b6 203 float temperatureF;
tlyp 0:468c23ed35b6 204 temperatureF = (temperatureC * 1.8F) + 32.0f;
tlyp 0:468c23ed35b6 205 return temperatureF;
tlyp 0:468c23ed35b6 206 }
tlyp 0:468c23ed35b6 207
tlyp 0:468c23ed35b6 208 //******************************************************************************
tlyp 0:468c23ed35b6 209 int32_t MAX30208::writeRegister(Registers_e reg, uint16_t value, int bytesWritten) {
tlyp 0:468c23ed35b6 210
tlyp 0:468c23ed35b6 211 int32_t ret;
tlyp 0:468c23ed35b6 212
tlyp 0:468c23ed35b6 213 uint8_t hi = ((value >> 8) & 0xFF);
tlyp 0:468c23ed35b6 214 uint8_t lo = (value & 0xFF);
tlyp 0:468c23ed35b6 215 char val[3] = {reg, hi, lo};
tlyp 0:468c23ed35b6 216
tlyp 0:468c23ed35b6 217 ret = m_i2c.write(m_writeAddress, val, bytesWritten, false);
tlyp 0:468c23ed35b6 218 return (ret);
tlyp 0:468c23ed35b6 219 }
tlyp 0:468c23ed35b6 220
tlyp 0:468c23ed35b6 221 //******************************************************************************
tlyp 0:468c23ed35b6 222 int32_t MAX30208::readRegister(Registers_e reg, uint16_t &value, int bytesRead) {
tlyp 0:468c23ed35b6 223
tlyp 0:468c23ed35b6 224 int32_t ret;
tlyp 0:468c23ed35b6 225 char cmdata[1] = {reg};
tlyp 0:468c23ed35b6 226 char dataRead[2];
tlyp 0:468c23ed35b6 227
tlyp 0:468c23ed35b6 228 ret = m_i2c.write(m_readAddress,cmdata,1,true);
tlyp 0:468c23ed35b6 229 if (ret == 0) {
tlyp 0:468c23ed35b6 230 ret = m_i2c.read(m_readAddress,dataRead,bytesRead,false);
tlyp 0:468c23ed35b6 231 if(ret == 0 && bytesRead == 2){
tlyp 0:468c23ed35b6 232 value = ((dataRead[0]<<8)+ dataRead[1]);
tlyp 0:468c23ed35b6 233 }
tlyp 0:468c23ed35b6 234 else if (ret == 0 && bytesRead == 1){
tlyp 0:468c23ed35b6 235 value = dataRead[0];
tlyp 0:468c23ed35b6 236 }
tlyp 0:468c23ed35b6 237 }
tlyp 0:468c23ed35b6 238 return(ret);
tlyp 0:468c23ed35b6 239 }