TEST

Dependencies:   max32630fthr Adafruit_FeatherOLED USBDevice

Committer:
wwwarunraj
Date:
Sun Apr 19 11:19:57 2020 +0000
Revision:
4:291477e8690d
Parent:
1:f60eafbf009a
19/04

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gmehmet 1:f60eafbf009a 1 /*******************************************************************************
gmehmet 1:f60eafbf009a 2 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
gmehmet 1:f60eafbf009a 3 *
gmehmet 1:f60eafbf009a 4 * Permission is hereby granted, free of charge, to any person obtaining a
gmehmet 1:f60eafbf009a 5 * copy of this software and associated documentation files (the "Software"),
gmehmet 1:f60eafbf009a 6 * to deal in the Software without restriction, including without limitation
gmehmet 1:f60eafbf009a 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
gmehmet 1:f60eafbf009a 8 * and/or sell copies of the Software, and to permit persons to whom the
gmehmet 1:f60eafbf009a 9 * Software is furnished to do so, subject to the following conditions:
gmehmet 1:f60eafbf009a 10 *
gmehmet 1:f60eafbf009a 11 * The above copyright notice and this permission notice shall be included
gmehmet 1:f60eafbf009a 12 * in all copies or substantial portions of the Software.
gmehmet 1:f60eafbf009a 13 *
gmehmet 1:f60eafbf009a 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
gmehmet 1:f60eafbf009a 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
gmehmet 1:f60eafbf009a 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
gmehmet 1:f60eafbf009a 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
gmehmet 1:f60eafbf009a 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
gmehmet 1:f60eafbf009a 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
gmehmet 1:f60eafbf009a 20 * OTHER DEALINGS IN THE SOFTWARE.
gmehmet 1:f60eafbf009a 21 *
gmehmet 1:f60eafbf009a 22 * Except as contained in this notice, the name of Maxim Integrated
gmehmet 1:f60eafbf009a 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
gmehmet 1:f60eafbf009a 24 * Products, Inc. Branding Policy.
gmehmet 1:f60eafbf009a 25 *
gmehmet 1:f60eafbf009a 26 * The mere transfer of this software does not imply any licenses
gmehmet 1:f60eafbf009a 27 * of trade secrets, proprietary technology, copyrights, patents,
gmehmet 1:f60eafbf009a 28 * trademarks, maskwork rights, or any other form of intellectual
gmehmet 1:f60eafbf009a 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
gmehmet 1:f60eafbf009a 30 * ownership rights.
gmehmet 1:f60eafbf009a 31 *******************************************************************************
gmehmet 1:f60eafbf009a 32 */
gmehmet 1:f60eafbf009a 33
gmehmet 1:f60eafbf009a 34
gmehmet 1:f60eafbf009a 35 #include "MAX30205.h"
gmehmet 1:f60eafbf009a 36
gmehmet 1:f60eafbf009a 37
gmehmet 1:f60eafbf009a 38 //******************************************************************************
gmehmet 1:f60eafbf009a 39 MAX30205::MAX30205(I2C *i2c, uint8_t slaveAddress):
gmehmet 1:f60eafbf009a 40 m_i2c(i2c), m_writeAddress(slaveAddress << 1),
gmehmet 1:f60eafbf009a 41 m_readAddress((slaveAddress << 1) | 1)
gmehmet 1:f60eafbf009a 42 {
gmehmet 1:f60eafbf009a 43
gmehmet 1:f60eafbf009a 44 }
gmehmet 1:f60eafbf009a 45
gmehmet 1:f60eafbf009a 46
gmehmet 1:f60eafbf009a 47 //******************************************************************************
gmehmet 1:f60eafbf009a 48 MAX30205::~MAX30205(void)
gmehmet 1:f60eafbf009a 49 {
gmehmet 1:f60eafbf009a 50 //empty block
gmehmet 1:f60eafbf009a 51 }
gmehmet 1:f60eafbf009a 52
gmehmet 1:f60eafbf009a 53
gmehmet 1:f60eafbf009a 54
gmehmet 1:f60eafbf009a 55
gmehmet 1:f60eafbf009a 56 //******************************************************************************
gmehmet 1:f60eafbf009a 57 int32_t MAX30205::readTemperature(uint16_t &value)
gmehmet 1:f60eafbf009a 58 {
gmehmet 1:f60eafbf009a 59 return readRegister(MAX30205::Temperature, value);
gmehmet 1:f60eafbf009a 60 }
gmehmet 1:f60eafbf009a 61
gmehmet 1:f60eafbf009a 62
gmehmet 1:f60eafbf009a 63 //******************************************************************************
gmehmet 1:f60eafbf009a 64 int32_t MAX30205::readConfiguration(Configuration_u &config)
gmehmet 1:f60eafbf009a 65 {
gmehmet 1:f60eafbf009a 66 uint16_t data;
gmehmet 1:f60eafbf009a 67
gmehmet 1:f60eafbf009a 68 int32_t result = readRegister(MAX30205::Configuration, data);
gmehmet 1:f60eafbf009a 69 if(result == 0)
gmehmet 1:f60eafbf009a 70 {
gmehmet 1:f60eafbf009a 71 config.all = (0x00FF & data);
gmehmet 1:f60eafbf009a 72 }
gmehmet 1:f60eafbf009a 73
gmehmet 1:f60eafbf009a 74 return result;
gmehmet 1:f60eafbf009a 75
gmehmet 1:f60eafbf009a 76 }
gmehmet 1:f60eafbf009a 77
gmehmet 1:f60eafbf009a 78
gmehmet 1:f60eafbf009a 79 //******************************************************************************
gmehmet 1:f60eafbf009a 80 int32_t MAX30205::writeConfiguration(const Configuration_u config)
gmehmet 1:f60eafbf009a 81 {
gmehmet 1:f60eafbf009a 82 uint16_t local_config = (0x00FF & config.all);
gmehmet 1:f60eafbf009a 83
gmehmet 1:f60eafbf009a 84 return writeRegister(MAX30205::Configuration, local_config);
gmehmet 1:f60eafbf009a 85 }
gmehmet 1:f60eafbf009a 86
gmehmet 1:f60eafbf009a 87
gmehmet 1:f60eafbf009a 88 //******************************************************************************
gmehmet 1:f60eafbf009a 89 int32_t MAX30205::readTHYST(uint16_t &value)
gmehmet 1:f60eafbf009a 90 {
gmehmet 1:f60eafbf009a 91 return readRegister(MAX30205::THYST, value);
gmehmet 1:f60eafbf009a 92 }
gmehmet 1:f60eafbf009a 93
gmehmet 1:f60eafbf009a 94
gmehmet 1:f60eafbf009a 95 //******************************************************************************
gmehmet 1:f60eafbf009a 96 int32_t MAX30205::writeTHYST(uint16_t value)
gmehmet 1:f60eafbf009a 97 {
gmehmet 1:f60eafbf009a 98 return writeRegister(MAX30205::THYST, value);
gmehmet 1:f60eafbf009a 99 }
gmehmet 1:f60eafbf009a 100
gmehmet 1:f60eafbf009a 101
gmehmet 1:f60eafbf009a 102 //******************************************************************************
gmehmet 1:f60eafbf009a 103 int32_t MAX30205::readTOS(uint16_t &value)
gmehmet 1:f60eafbf009a 104 {
gmehmet 1:f60eafbf009a 105 return readRegister(MAX30205::TOS, value);
gmehmet 1:f60eafbf009a 106 }
gmehmet 1:f60eafbf009a 107
gmehmet 1:f60eafbf009a 108
gmehmet 1:f60eafbf009a 109 //******************************************************************************
gmehmet 1:f60eafbf009a 110 int32_t MAX30205::writeTOS(const uint16_t value)
gmehmet 1:f60eafbf009a 111 {
gmehmet 1:f60eafbf009a 112 return writeRegister(MAX30205::TOS, value);
gmehmet 1:f60eafbf009a 113 }
gmehmet 1:f60eafbf009a 114
gmehmet 1:f60eafbf009a 115
gmehmet 1:f60eafbf009a 116 //******************************************************************************
gmehmet 1:f60eafbf009a 117 float MAX30205::toCelsius(uint32_t rawTemp)
gmehmet 1:f60eafbf009a 118 {
gmehmet 1:f60eafbf009a 119 uint8_t val1, val2;
gmehmet 1:f60eafbf009a 120 float result;
gmehmet 1:f60eafbf009a 121
gmehmet 1:f60eafbf009a 122 val1 = (rawTemp >> 8);
gmehmet 1:f60eafbf009a 123 val2 = (rawTemp & 0xFF);
gmehmet 1:f60eafbf009a 124
gmehmet 1:f60eafbf009a 125 result = static_cast<float>(val1 + (val2/ 256.0F));
gmehmet 1:f60eafbf009a 126
gmehmet 1:f60eafbf009a 127 return result;
gmehmet 1:f60eafbf009a 128 }
gmehmet 1:f60eafbf009a 129
gmehmet 1:f60eafbf009a 130
gmehmet 1:f60eafbf009a 131 //******************************************************************************
gmehmet 1:f60eafbf009a 132 float MAX30205::toFahrenheit(float temperatureC)
gmehmet 1:f60eafbf009a 133 {
gmehmet 1:f60eafbf009a 134 return((temperatureC * 1.8F) + 32.0f);
gmehmet 1:f60eafbf009a 135 }
gmehmet 1:f60eafbf009a 136
gmehmet 1:f60eafbf009a 137
gmehmet 1:f60eafbf009a 138 //******************************************************************************
gmehmet 1:f60eafbf009a 139 int32_t MAX30205::writeRegister(Registers_e reg, uint16_t value)
gmehmet 1:f60eafbf009a 140 {
gmehmet 1:f60eafbf009a 141 int32_t result;
gmehmet 1:f60eafbf009a 142
gmehmet 1:f60eafbf009a 143 uint8_t hi = ((value >> 8) & 0xFF);
gmehmet 1:f60eafbf009a 144 uint8_t lo = (value & 0xFF);
gmehmet 1:f60eafbf009a 145 char cmdData[3] = {reg, hi, lo};
gmehmet 1:f60eafbf009a 146
gmehmet 1:f60eafbf009a 147 result = m_i2c->write(m_writeAddress, cmdData, 3);
gmehmet 1:f60eafbf009a 148
gmehmet 1:f60eafbf009a 149 return result;
gmehmet 1:f60eafbf009a 150 }
gmehmet 1:f60eafbf009a 151
gmehmet 1:f60eafbf009a 152
gmehmet 1:f60eafbf009a 153 //******************************************************************************
gmehmet 1:f60eafbf009a 154 int32_t MAX30205::readRegister(Registers_e reg, uint16_t &value)
gmehmet 1:f60eafbf009a 155 {
gmehmet 1:f60eafbf009a 156 int32_t result;
gmehmet 1:f60eafbf009a 157
gmehmet 1:f60eafbf009a 158 char data[2];
gmehmet 1:f60eafbf009a 159 char cmdData[1] = {reg};
gmehmet 1:f60eafbf009a 160
gmehmet 1:f60eafbf009a 161 result = m_i2c->write(m_writeAddress, cmdData, 1);
gmehmet 1:f60eafbf009a 162 if(result == 0)
gmehmet 1:f60eafbf009a 163 {
gmehmet 1:f60eafbf009a 164 result = m_i2c->read(m_readAddress, data, 2);
gmehmet 1:f60eafbf009a 165 if (result == 0)
gmehmet 1:f60eafbf009a 166 {
gmehmet 1:f60eafbf009a 167 value = (data[0] << 8) + data[1];
gmehmet 1:f60eafbf009a 168 }
gmehmet 1:f60eafbf009a 169 }
gmehmet 1:f60eafbf009a 170
gmehmet 1:f60eafbf009a 171 return result;
gmehmet 1:f60eafbf009a 172 }
gmehmet 1:f60eafbf009a 173
gmehmet 1:f60eafbf009a 174 int MAX30205::dump_registers(addr_val_pair *reg_vals){
gmehmet 1:f60eafbf009a 175 int i;
gmehmet 1:f60eafbf009a 176 int ret = 0;
gmehmet 1:f60eafbf009a 177 uint16_t val;
gmehmet 1:f60eafbf009a 178 for (i = 0x00; i <= 0x03; i++) {
gmehmet 1:f60eafbf009a 179 reg_vals[i].addr = i;
gmehmet 1:f60eafbf009a 180 ret |= readRegister(static_cast<Registers_e>(i), val);
gmehmet 1:f60eafbf009a 181 reg_vals[i].val = val;
gmehmet 1:f60eafbf009a 182 }
gmehmet 1:f60eafbf009a 183
gmehmet 1:f60eafbf009a 184 return ret;
gmehmet 1:f60eafbf009a 185 }
gmehmet 1:f60eafbf009a 186
gmehmet 1:f60eafbf009a 187 const char *MAX30205::get_sensor_part_name()
gmehmet 1:f60eafbf009a 188 {
gmehmet 1:f60eafbf009a 189 return "max30205";
gmehmet 1:f60eafbf009a 190 }
gmehmet 1:f60eafbf009a 191
gmehmet 1:f60eafbf009a 192 const char *MAX30205::get_sensor_algo_ver()
gmehmet 1:f60eafbf009a 193 {
gmehmet 1:f60eafbf009a 194 return "dummy_algo_ver";
gmehmet 1:f60eafbf009a 195 }
gmehmet 1:f60eafbf009a 196
gmehmet 1:f60eafbf009a 197
gmehmet 1:f60eafbf009a 198 int MAX30205::sensor_enable(int enable){
gmehmet 1:f60eafbf009a 199 int32_t ret;
gmehmet 1:f60eafbf009a 200 Configuration_u config;
gmehmet 1:f60eafbf009a 201
gmehmet 1:f60eafbf009a 202
gmehmet 1:f60eafbf009a 203 ret = readConfiguration(config);
gmehmet 1:f60eafbf009a 204 if(ret != 0)
gmehmet 1:f60eafbf009a 205 return ret;
gmehmet 1:f60eafbf009a 206
gmehmet 1:f60eafbf009a 207 if(enable){
gmehmet 1:f60eafbf009a 208 config.bits.shutdown = 0;
gmehmet 1:f60eafbf009a 209 }else{
gmehmet 1:f60eafbf009a 210 config.bits.shutdown = 1;
gmehmet 1:f60eafbf009a 211 }
gmehmet 1:f60eafbf009a 212
gmehmet 1:f60eafbf009a 213 return writeConfiguration(config);
gmehmet 1:f60eafbf009a 214 }
gmehmet 1:f60eafbf009a 215
gmehmet 1:f60eafbf009a 216 /**
gmehmet 1:f60eafbf009a 217 * @brief Get sensor ID.
gmehmet 1:f60eafbf009a 218 *
gmehmet 1:f60eafbf009a 219 * @returns Sensor ID number.
gmehmet 1:f60eafbf009a 220 */
gmehmet 1:f60eafbf009a 221 unsigned char MAX30205::get_sensor_id() {
gmehmet 1:f60eafbf009a 222
gmehmet 1:f60eafbf009a 223 return( SENSOR_ID_MAX30205 );
gmehmet 1:f60eafbf009a 224
gmehmet 1:f60eafbf009a 225 }