MAX30208 Example Program

Dependencies:   max32630fthr USBDevice

Committer:
tlyp
Date:
Fri Sep 04 18:05:40 2020 +0000
Revision:
5:5dd9f657a9f9
Parent:
3:27e027ab268b
initial commit

Who changed what in which revision?

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