ddd

Dependencies:   USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX30205.cpp Source File

MAX30205.cpp

00001 /*******************************************************************************
00002  * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  * Except as contained in this notice, the name of Maxim Integrated
00023  * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024  * Products, Inc. Branding Policy.
00025  *
00026  * The mere transfer of this software does not imply any licenses
00027  * of trade secrets, proprietary technology, copyrights, patents,
00028  * trademarks, maskwork rights, or any other form of intellectual
00029  * property whatsoever. Maxim Integrated Products, Inc. retains all
00030  * ownership rights.
00031  *******************************************************************************
00032  */
00033 #include "MAX30205.h"
00034 
00035 //******************************************************************************
00036 MAX30205::MAX30205(PinName sda, PinName scl, int slaveAddress) : 
00037           slaveAddress(slaveAddress) {
00038   i2c = new I2C(sda, scl);
00039   isOwner = true;
00040   i2c->frequency(100000);
00041 }
00042 
00043 //******************************************************************************
00044 MAX30205::MAX30205(I2C *i2c, int slaveAddress) : slaveAddress(slaveAddress) {
00045   this->i2c = i2c;
00046   i2c->frequency(100000);
00047   isOwner = false;
00048 }
00049 
00050 //******************************************************************************
00051 MAX30205::~MAX30205(void) {
00052   if (isOwner == true) {
00053     delete i2c;
00054   }
00055 }
00056 
00057 //******************************************************************************
00058 int MAX30205::reg_write(char reg, char value) {
00059   int result;
00060   char cmdData[2] = {(char)reg, value};
00061   result = i2c->write(slaveAddress, cmdData, 2);
00062   if (result != 0) return -1;
00063   return 0;
00064 }
00065 
00066 //******************************************************************************
00067 int MAX30205::reg_write16(char reg, uint16_t value) {
00068   int result;
00069   char hi = (value >> 8) & 0xFF;
00070   char lo = value & 0xFF;
00071   char cmdData[3] = {reg, hi, lo};
00072   result = i2c->write(slaveAddress, cmdData, 3);
00073   if (result != 0) return -1;
00074   return 0;
00075 }
00076 
00077 //******************************************************************************
00078 int MAX30205::reg_read(char reg, char *value) {
00079   int result;
00080   char cmdData[1] = {reg};
00081 
00082   result = i2c->write(slaveAddress, cmdData, 1);
00083   if (result != 0) return -1;
00084   result = i2c->read(slaveAddress, value, 1);
00085   if (result != 0) return -1;
00086   return 0;
00087 }
00088 
00089 //******************************************************************************
00090 int MAX30205::reg_read16(char reg, uint16_t *value) {
00091   int result;
00092   char data[2];
00093   char cmdData[1] = {reg};
00094   result = i2c->write(slaveAddress, cmdData, 1);
00095   if (result != 0) return -1;
00096   result = i2c->read(slaveAddress, data, 2);
00097   if (result != 0) return -1;
00098   *value = (data[0] << 8) + data[1];
00099   return 0;
00100 }
00101 
00102 //******************************************************************************
00103 int MAX30205::readTemperature(uint16_t *value) {
00104   uint8_t data[2];
00105   int status;
00106   status = reg_read16(MAX30205_Temperature, (uint16_t *)&data);
00107   *value = (data[0] << 8) + data[1];
00108   return status;
00109 }
00110 
00111 //******************************************************************************
00112 float MAX30205::toCelsius(unsigned int rawTemp) {
00113   float val;
00114   float val1, val2;
00115   val1 = (float)(rawTemp >> 8);
00116   val2 = (float)(rawTemp & 0xFF);
00117   val = val2 + (val1 / 256.0f);
00118   return val;
00119 }
00120 
00121 //******************************************************************************
00122 float MAX30205::toFahrenheit(float temperatureC) {
00123   return temperatureC * 9.0f / 5.0f + 32.0f;
00124 }
00125 
00126 //******************************************************************************
00127 int MAX30205::reg_THYST_Read(uint16_t *value) {
00128   return reg_read16(MAX30205_THYST, value);
00129 }
00130 
00131 //******************************************************************************
00132 int MAX30205::reg_THYST_Write(uint16_t value) {
00133   return reg_write16(MAX30205_THYST, value);
00134 }