MAX32620HSP (MAXREFDES100) RPC Example for Graphical User Interface

Dependencies:   USBDevice

Fork of HSP_Release by Jerry Bradshaw

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){
00063     return -1;
00064   }
00065   return 0;
00066 }
00067 
00068 //******************************************************************************
00069 int MAX30205::reg_write16(char reg, uint16_t value) {
00070   int result;
00071   char hi = (value >> 8) & 0xFF;
00072   char lo = value & 0xFF;
00073   char cmdData[3] = {reg, hi, lo};
00074   result = i2c->write(slaveAddress, cmdData, 3);
00075   if (result != 0) {
00076     return -1;
00077   }
00078   return 0;
00079 }
00080 
00081 //******************************************************************************
00082 int MAX30205::reg_read(char reg, char *value) {
00083   int result;
00084   char cmdData[1] = {reg};
00085 
00086   result = i2c->write(slaveAddress, cmdData, 1);
00087   if (result != 0) {
00088     return -1;
00089   }
00090   result = i2c->read(slaveAddress, value, 1);
00091   if (result != 0){
00092     return -1;
00093   }
00094   return 0;
00095 }
00096 
00097 //******************************************************************************
00098 int MAX30205::reg_read16(char reg, uint16_t *value) {
00099   int result;
00100   char data[2];
00101   char cmdData[1] = {reg};
00102   result = i2c->write(slaveAddress, cmdData, 1);
00103   if (result != 0) {
00104     return -1;
00105   }
00106   result = i2c->read(slaveAddress, data, 2);
00107   if (result != 0) {
00108     return -1;
00109   }
00110   *value = (data[0] << 8) + data[1];
00111   return 0;
00112 }
00113 
00114 //******************************************************************************
00115 int MAX30205::readTemperature(uint16_t *value) {
00116   uint8_t data[2];
00117   int status;
00118   status = reg_read16(MAX30205_Temperature, (uint16_t *)&data);
00119   *value = (data[0] << 8) + data[1];
00120   return status;
00121 }
00122 
00123 //******************************************************************************
00124 float MAX30205::toCelsius(unsigned int rawTemp) {
00125   float val;
00126   float val1, val2;
00127   val1 = (float)(rawTemp >> 8);
00128   val2 = (float)(rawTemp & 0xFF);
00129   val = val2 + (val1 / 256.0f);
00130   return val;
00131 }
00132 
00133 //******************************************************************************
00134 float MAX30205::toFahrenheit(float temperatureC) {
00135   return temperatureC * 9.0f / 5.0f + 32.0f;
00136 }
00137 
00138 //******************************************************************************
00139 int MAX30205::reg_THYST_Read(uint16_t *value) {
00140   return reg_read16(MAX30205_THYST, value);
00141 }
00142 
00143 //******************************************************************************
00144 int MAX30205::reg_THYST_Write(uint16_t value) {
00145   return reg_write16(MAX30205_THYST, value);
00146 }