Fork of Maxim's human body temp sensor library that works with mbed-os 5
Fork of MAX30205 by
MAX30205.cpp@6:b5ab5204d944, 2017-04-11 (annotated)
- Committer:
- j3
- Date:
- Tue Apr 11 22:07:28 2017 +0000
- Revision:
- 6:b5ab5204d944
- Parent:
- 4:68e1b36becb0
- Child:
- 8:2aeb45dd2bb9
added suffix to enum, union, struct declaration names (_e, _u, _s) for clarity
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 0:cdad7a9ef486 | 1 | /******************************************************************************* |
j3 | 1:d4271ef9f37f | 2 | * Copyright (C) 2017 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 | 1:d4271ef9f37f | 33 | |
j3 | 1:d4271ef9f37f | 34 | |
j3 | 0:cdad7a9ef486 | 35 | #include "MAX30205.h" |
j3 | 0:cdad7a9ef486 | 36 | |
j3 | 1:d4271ef9f37f | 37 | |
j3 | 0:cdad7a9ef486 | 38 | //****************************************************************************** |
j3 | 4:68e1b36becb0 | 39 | MAX30205::MAX30205(PinName sda, PinName scl, uint8_t slaveAddress): |
j3 | 1:d4271ef9f37f | 40 | m_i2c(sda, scl), m_writeAddress(slaveAddress << 1), |
j3 | 4:68e1b36becb0 | 41 | m_readAddress((slaveAddress << 1) | 1) |
j3 | 0:cdad7a9ef486 | 42 | { |
j3 | 0:cdad7a9ef486 | 43 | } |
j3 | 0:cdad7a9ef486 | 44 | |
j3 | 1:d4271ef9f37f | 45 | |
j3 | 0:cdad7a9ef486 | 46 | //****************************************************************************** |
j3 | 4:68e1b36becb0 | 47 | MAX30205::MAX30205(I2C &i2c, uint8_t slaveAddress): |
j3 | 1:d4271ef9f37f | 48 | m_i2c(i2c), m_writeAddress(slaveAddress << 1), |
j3 | 4:68e1b36becb0 | 49 | m_readAddress((slaveAddress << 1) | 1) |
j3 | 0:cdad7a9ef486 | 50 | { |
j3 | 0:cdad7a9ef486 | 51 | } |
j3 | 0:cdad7a9ef486 | 52 | |
j3 | 1:d4271ef9f37f | 53 | |
j3 | 0:cdad7a9ef486 | 54 | //****************************************************************************** |
j3 | 0:cdad7a9ef486 | 55 | MAX30205::~MAX30205(void) |
j3 | 0:cdad7a9ef486 | 56 | { |
j3 | 0:cdad7a9ef486 | 57 | //empty block |
j3 | 0:cdad7a9ef486 | 58 | } |
j3 | 0:cdad7a9ef486 | 59 | |
j3 | 1:d4271ef9f37f | 60 | |
j3 | 0:cdad7a9ef486 | 61 | //****************************************************************************** |
j3 | 1:d4271ef9f37f | 62 | int32_t MAX30205::readTemperature(uint16_t &value) |
j3 | 1:d4271ef9f37f | 63 | { |
j3 | 1:d4271ef9f37f | 64 | return readRegister(MAX30205::Temperature, value); |
j3 | 1:d4271ef9f37f | 65 | } |
j3 | 1:d4271ef9f37f | 66 | |
j3 | 1:d4271ef9f37f | 67 | |
j3 | 1:d4271ef9f37f | 68 | //****************************************************************************** |
j3 | 6:b5ab5204d944 | 69 | int32_t MAX30205::readConfiguration(Configuration_u &config) |
j3 | 0:cdad7a9ef486 | 70 | { |
j3 | 1:d4271ef9f37f | 71 | uint16_t data; |
j3 | 1:d4271ef9f37f | 72 | |
j3 | 1:d4271ef9f37f | 73 | int32_t result = readRegister(MAX30205::Configuration, data); |
j3 | 1:d4271ef9f37f | 74 | if(result == 0) |
j3 | 1:d4271ef9f37f | 75 | { |
j3 | 1:d4271ef9f37f | 76 | config.all = (0x00FF & data); |
j3 | 1:d4271ef9f37f | 77 | } |
j3 | 1:d4271ef9f37f | 78 | |
j3 | 1:d4271ef9f37f | 79 | return result; |
j3 | 1:d4271ef9f37f | 80 | |
j3 | 1:d4271ef9f37f | 81 | } |
j3 | 1:d4271ef9f37f | 82 | |
j3 | 1:d4271ef9f37f | 83 | |
j3 | 1:d4271ef9f37f | 84 | //****************************************************************************** |
j3 | 6:b5ab5204d944 | 85 | int32_t MAX30205::writeConfiguration(const Configuration_u config) |
j3 | 1:d4271ef9f37f | 86 | { |
j3 | 1:d4271ef9f37f | 87 | uint16_t local_config = (0x00FF & config.all); |
j3 | 1:d4271ef9f37f | 88 | |
j3 | 1:d4271ef9f37f | 89 | return writeRegister(MAX30205::Configuration, local_config); |
j3 | 1:d4271ef9f37f | 90 | } |
j3 | 1:d4271ef9f37f | 91 | |
j3 | 1:d4271ef9f37f | 92 | |
j3 | 1:d4271ef9f37f | 93 | //****************************************************************************** |
j3 | 1:d4271ef9f37f | 94 | int32_t MAX30205::readTHYST(uint16_t &value) |
j3 | 1:d4271ef9f37f | 95 | { |
j3 | 1:d4271ef9f37f | 96 | return readRegister(MAX30205::THYST, value); |
j3 | 0:cdad7a9ef486 | 97 | } |
j3 | 0:cdad7a9ef486 | 98 | |
j3 | 1:d4271ef9f37f | 99 | |
j3 | 0:cdad7a9ef486 | 100 | //****************************************************************************** |
j3 | 1:d4271ef9f37f | 101 | int32_t MAX30205::writeTHYST(uint16_t value) |
j3 | 1:d4271ef9f37f | 102 | { |
j3 | 1:d4271ef9f37f | 103 | return writeRegister(MAX30205::THYST, value); |
j3 | 1:d4271ef9f37f | 104 | } |
j3 | 1:d4271ef9f37f | 105 | |
j3 | 1:d4271ef9f37f | 106 | |
j3 | 1:d4271ef9f37f | 107 | //****************************************************************************** |
j3 | 1:d4271ef9f37f | 108 | int32_t MAX30205::readTOS(uint16_t &value) |
j3 | 1:d4271ef9f37f | 109 | { |
j3 | 1:d4271ef9f37f | 110 | return readRegister(MAX30205::TOS, value); |
j3 | 1:d4271ef9f37f | 111 | } |
j3 | 1:d4271ef9f37f | 112 | |
j3 | 1:d4271ef9f37f | 113 | |
j3 | 1:d4271ef9f37f | 114 | //****************************************************************************** |
j3 | 1:d4271ef9f37f | 115 | int32_t MAX30205::writeTOS(const uint16_t value) |
j3 | 1:d4271ef9f37f | 116 | { |
j3 | 1:d4271ef9f37f | 117 | return writeRegister(MAX30205::TOS, value); |
j3 | 1:d4271ef9f37f | 118 | } |
j3 | 1:d4271ef9f37f | 119 | |
j3 | 1:d4271ef9f37f | 120 | |
j3 | 1:d4271ef9f37f | 121 | //****************************************************************************** |
j3 | 1:d4271ef9f37f | 122 | float MAX30205::toCelsius(uint32_t rawTemp) |
j3 | 1:d4271ef9f37f | 123 | { |
j3 | 2:a659724f496a | 124 | uint8_t val1, val2; |
j3 | 2:a659724f496a | 125 | float result; |
j3 | 1:d4271ef9f37f | 126 | |
j3 | 2:a659724f496a | 127 | val1 = (rawTemp >> 8); |
j3 | 2:a659724f496a | 128 | val2 = (rawTemp & 0xFF); |
j3 | 1:d4271ef9f37f | 129 | |
j3 | 2:a659724f496a | 130 | result = static_cast<float>(val1 + (val2/ 256.0F)); |
j3 | 2:a659724f496a | 131 | |
j3 | 2:a659724f496a | 132 | return result; |
j3 | 1:d4271ef9f37f | 133 | } |
j3 | 1:d4271ef9f37f | 134 | |
j3 | 1:d4271ef9f37f | 135 | |
j3 | 1:d4271ef9f37f | 136 | //****************************************************************************** |
j3 | 1:d4271ef9f37f | 137 | float MAX30205::toFahrenheit(float temperatureC) |
j3 | 1:d4271ef9f37f | 138 | { |
j3 | 1:d4271ef9f37f | 139 | return((temperatureC * 1.8F) + 32.0f); |
j3 | 1:d4271ef9f37f | 140 | } |
j3 | 1:d4271ef9f37f | 141 | |
j3 | 1:d4271ef9f37f | 142 | |
j3 | 1:d4271ef9f37f | 143 | //****************************************************************************** |
j3 | 6:b5ab5204d944 | 144 | int32_t MAX30205::writeRegister(Registers_e reg, uint16_t value) |
j3 | 0:cdad7a9ef486 | 145 | { |
j3 | 0:cdad7a9ef486 | 146 | int32_t result; |
j3 | 0:cdad7a9ef486 | 147 | |
j3 | 0:cdad7a9ef486 | 148 | uint8_t hi = ((value >> 8) & 0xFF); |
j3 | 0:cdad7a9ef486 | 149 | uint8_t lo = (value & 0xFF); |
j3 | 0:cdad7a9ef486 | 150 | char cmdData[3] = {reg, hi, lo}; |
j3 | 0:cdad7a9ef486 | 151 | |
j3 | 0:cdad7a9ef486 | 152 | result = m_i2c.write(m_writeAddress, cmdData, 3); |
j3 | 0:cdad7a9ef486 | 153 | |
j3 | 0:cdad7a9ef486 | 154 | return result; |
j3 | 0:cdad7a9ef486 | 155 | } |
j3 | 0:cdad7a9ef486 | 156 | |
j3 | 0:cdad7a9ef486 | 157 | |
j3 | 0:cdad7a9ef486 | 158 | //****************************************************************************** |
j3 | 6:b5ab5204d944 | 159 | int32_t MAX30205::readRegister(Registers_e reg, uint16_t &value) |
j3 | 0:cdad7a9ef486 | 160 | { |
j3 | 0:cdad7a9ef486 | 161 | int32_t result; |
j3 | 0:cdad7a9ef486 | 162 | |
j3 | 0:cdad7a9ef486 | 163 | char data[2]; |
j3 | 0:cdad7a9ef486 | 164 | char cmdData[1] = {reg}; |
j3 | 0:cdad7a9ef486 | 165 | |
j3 | 0:cdad7a9ef486 | 166 | result = m_i2c.write(m_writeAddress, cmdData, 1); |
j3 | 0:cdad7a9ef486 | 167 | if(result == 0) |
j3 | 0:cdad7a9ef486 | 168 | { |
j3 | 0:cdad7a9ef486 | 169 | result = m_i2c.read(m_readAddress, data, 2); |
j3 | 0:cdad7a9ef486 | 170 | if (result == 0) |
j3 | 0:cdad7a9ef486 | 171 | { |
j3 | 0:cdad7a9ef486 | 172 | value = (data[0] << 8) + data[1]; |
j3 | 0:cdad7a9ef486 | 173 | } |
j3 | 0:cdad7a9ef486 | 174 | } |
j3 | 0:cdad7a9ef486 | 175 | |
j3 | 0:cdad7a9ef486 | 176 | return result; |
j3 | 0:cdad7a9ef486 | 177 | } |