Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MAX30205 by
MAX30205.cpp@9:2b8357c91343, 2017-10-18 (annotated)
- Committer:
 - seppestas
 - Date:
 - Wed Oct 18 15:51:47 2017 +0000
 - Revision:
 - 9:2b8357c91343
 - Parent:
 - 8:2aeb45dd2bb9
 
Use single, 8-bit address to work with mbed I2C; ; Mbed uses 8-bit addresses, see https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/interfaces/digital/I2C/#api
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(I2C &i2c, uint8_t slaveAddress): | 
| seppestas | 9:2b8357c91343 | 40 | m_i2c(i2c), m_address(slaveAddress) | 
| j3 | 0:cdad7a9ef486 | 41 | { | 
| j3 | 0:cdad7a9ef486 | 42 | } | 
| j3 | 0:cdad7a9ef486 | 43 | |
| j3 | 1:d4271ef9f37f | 44 | |
| j3 | 0:cdad7a9ef486 | 45 | //****************************************************************************** | 
| j3 | 0:cdad7a9ef486 | 46 | MAX30205::~MAX30205(void) | 
| j3 | 0:cdad7a9ef486 | 47 | { | 
| j3 | 0:cdad7a9ef486 | 48 | //empty block | 
| j3 | 0:cdad7a9ef486 | 49 | } | 
| j3 | 0:cdad7a9ef486 | 50 | |
| j3 | 1:d4271ef9f37f | 51 | |
| j3 | 0:cdad7a9ef486 | 52 | //****************************************************************************** | 
| j3 | 1:d4271ef9f37f | 53 | int32_t MAX30205::readTemperature(uint16_t &value) | 
| j3 | 1:d4271ef9f37f | 54 | { | 
| j3 | 1:d4271ef9f37f | 55 | return readRegister(MAX30205::Temperature, value); | 
| j3 | 1:d4271ef9f37f | 56 | } | 
| j3 | 1:d4271ef9f37f | 57 | |
| j3 | 1:d4271ef9f37f | 58 | |
| j3 | 1:d4271ef9f37f | 59 | //****************************************************************************** | 
| j3 | 6:b5ab5204d944 | 60 | int32_t MAX30205::readConfiguration(Configuration_u &config) | 
| j3 | 0:cdad7a9ef486 | 61 | { | 
| j3 | 1:d4271ef9f37f | 62 | uint16_t data; | 
| j3 | 1:d4271ef9f37f | 63 | |
| j3 | 1:d4271ef9f37f | 64 | int32_t result = readRegister(MAX30205::Configuration, data); | 
| j3 | 1:d4271ef9f37f | 65 | if(result == 0) | 
| j3 | 1:d4271ef9f37f | 66 | { | 
| j3 | 1:d4271ef9f37f | 67 | config.all = (0x00FF & data); | 
| j3 | 1:d4271ef9f37f | 68 | } | 
| j3 | 1:d4271ef9f37f | 69 | |
| j3 | 1:d4271ef9f37f | 70 | return result; | 
| j3 | 1:d4271ef9f37f | 71 | |
| j3 | 1:d4271ef9f37f | 72 | } | 
| j3 | 1:d4271ef9f37f | 73 | |
| j3 | 1:d4271ef9f37f | 74 | |
| j3 | 1:d4271ef9f37f | 75 | //****************************************************************************** | 
| j3 | 6:b5ab5204d944 | 76 | int32_t MAX30205::writeConfiguration(const Configuration_u config) | 
| j3 | 1:d4271ef9f37f | 77 | { | 
| j3 | 1:d4271ef9f37f | 78 | uint16_t local_config = (0x00FF & config.all); | 
| j3 | 1:d4271ef9f37f | 79 | |
| j3 | 1:d4271ef9f37f | 80 | return writeRegister(MAX30205::Configuration, local_config); | 
| j3 | 1:d4271ef9f37f | 81 | } | 
| j3 | 1:d4271ef9f37f | 82 | |
| j3 | 1:d4271ef9f37f | 83 | |
| j3 | 1:d4271ef9f37f | 84 | //****************************************************************************** | 
| j3 | 1:d4271ef9f37f | 85 | int32_t MAX30205::readTHYST(uint16_t &value) | 
| j3 | 1:d4271ef9f37f | 86 | { | 
| j3 | 1:d4271ef9f37f | 87 | return readRegister(MAX30205::THYST, value); | 
| j3 | 0:cdad7a9ef486 | 88 | } | 
| j3 | 0:cdad7a9ef486 | 89 | |
| j3 | 1:d4271ef9f37f | 90 | |
| j3 | 0:cdad7a9ef486 | 91 | //****************************************************************************** | 
| j3 | 1:d4271ef9f37f | 92 | int32_t MAX30205::writeTHYST(uint16_t value) | 
| j3 | 1:d4271ef9f37f | 93 | { | 
| j3 | 1:d4271ef9f37f | 94 | return writeRegister(MAX30205::THYST, value); | 
| j3 | 1:d4271ef9f37f | 95 | } | 
| j3 | 1:d4271ef9f37f | 96 | |
| j3 | 1:d4271ef9f37f | 97 | |
| j3 | 1:d4271ef9f37f | 98 | //****************************************************************************** | 
| j3 | 1:d4271ef9f37f | 99 | int32_t MAX30205::readTOS(uint16_t &value) | 
| j3 | 1:d4271ef9f37f | 100 | { | 
| j3 | 1:d4271ef9f37f | 101 | return readRegister(MAX30205::TOS, value); | 
| j3 | 1:d4271ef9f37f | 102 | } | 
| j3 | 1:d4271ef9f37f | 103 | |
| j3 | 1:d4271ef9f37f | 104 | |
| j3 | 1:d4271ef9f37f | 105 | //****************************************************************************** | 
| j3 | 1:d4271ef9f37f | 106 | int32_t MAX30205::writeTOS(const uint16_t value) | 
| j3 | 1:d4271ef9f37f | 107 | { | 
| j3 | 1:d4271ef9f37f | 108 | return writeRegister(MAX30205::TOS, value); | 
| j3 | 1:d4271ef9f37f | 109 | } | 
| j3 | 1:d4271ef9f37f | 110 | |
| j3 | 1:d4271ef9f37f | 111 | |
| j3 | 1:d4271ef9f37f | 112 | //****************************************************************************** | 
| j3 | 1:d4271ef9f37f | 113 | float MAX30205::toCelsius(uint32_t rawTemp) | 
| j3 | 1:d4271ef9f37f | 114 | { | 
| j3 | 2:a659724f496a | 115 | uint8_t val1, val2; | 
| j3 | 2:a659724f496a | 116 | float result; | 
| j3 | 1:d4271ef9f37f | 117 | |
| j3 | 2:a659724f496a | 118 | val1 = (rawTemp >> 8); | 
| j3 | 2:a659724f496a | 119 | val2 = (rawTemp & 0xFF); | 
| j3 | 1:d4271ef9f37f | 120 | |
| j3 | 2:a659724f496a | 121 | result = static_cast<float>(val1 + (val2/ 256.0F)); | 
| j3 | 2:a659724f496a | 122 | |
| j3 | 2:a659724f496a | 123 | return result; | 
| j3 | 1:d4271ef9f37f | 124 | } | 
| j3 | 1:d4271ef9f37f | 125 | |
| j3 | 1:d4271ef9f37f | 126 | |
| j3 | 1:d4271ef9f37f | 127 | //****************************************************************************** | 
| j3 | 1:d4271ef9f37f | 128 | float MAX30205::toFahrenheit(float temperatureC) | 
| j3 | 1:d4271ef9f37f | 129 | { | 
| j3 | 1:d4271ef9f37f | 130 | return((temperatureC * 1.8F) + 32.0f); | 
| j3 | 1:d4271ef9f37f | 131 | } | 
| j3 | 1:d4271ef9f37f | 132 | |
| j3 | 1:d4271ef9f37f | 133 | |
| j3 | 1:d4271ef9f37f | 134 | //****************************************************************************** | 
| j3 | 6:b5ab5204d944 | 135 | int32_t MAX30205::writeRegister(Registers_e reg, uint16_t value) | 
| j3 | 0:cdad7a9ef486 | 136 | { | 
| j3 | 0:cdad7a9ef486 | 137 | int32_t result; | 
| j3 | 0:cdad7a9ef486 | 138 | |
| j3 | 0:cdad7a9ef486 | 139 | uint8_t hi = ((value >> 8) & 0xFF); | 
| j3 | 0:cdad7a9ef486 | 140 | uint8_t lo = (value & 0xFF); | 
| j3 | 0:cdad7a9ef486 | 141 | char cmdData[3] = {reg, hi, lo}; | 
| j3 | 0:cdad7a9ef486 | 142 | |
| seppestas | 9:2b8357c91343 | 143 | result = m_i2c.write(m_address, cmdData, 3); | 
| j3 | 0:cdad7a9ef486 | 144 | |
| j3 | 0:cdad7a9ef486 | 145 | return result; | 
| j3 | 0:cdad7a9ef486 | 146 | } | 
| j3 | 0:cdad7a9ef486 | 147 | |
| j3 | 0:cdad7a9ef486 | 148 | |
| j3 | 0:cdad7a9ef486 | 149 | //****************************************************************************** | 
| j3 | 6:b5ab5204d944 | 150 | int32_t MAX30205::readRegister(Registers_e reg, uint16_t &value) | 
| j3 | 0:cdad7a9ef486 | 151 | { | 
| j3 | 0:cdad7a9ef486 | 152 | int32_t result; | 
| j3 | 0:cdad7a9ef486 | 153 | |
| j3 | 0:cdad7a9ef486 | 154 | char data[2]; | 
| j3 | 0:cdad7a9ef486 | 155 | char cmdData[1] = {reg}; | 
| j3 | 0:cdad7a9ef486 | 156 | |
| seppestas | 9:2b8357c91343 | 157 | result = m_i2c.write(m_address, cmdData, 1); | 
| j3 | 0:cdad7a9ef486 | 158 | if(result == 0) | 
| j3 | 0:cdad7a9ef486 | 159 | { | 
| seppestas | 9:2b8357c91343 | 160 | result = m_i2c.read(m_address, data, 2); | 
| j3 | 0:cdad7a9ef486 | 161 | if (result == 0) | 
| j3 | 0:cdad7a9ef486 | 162 | { | 
| j3 | 0:cdad7a9ef486 | 163 | value = (data[0] << 8) + data[1]; | 
| j3 | 0:cdad7a9ef486 | 164 | } | 
| j3 | 0:cdad7a9ef486 | 165 | } | 
| j3 | 0:cdad7a9ef486 | 166 | |
| j3 | 0:cdad7a9ef486 | 167 | return result; | 
| j3 | 0:cdad7a9ef486 | 168 | } | 
