an I2C bus optical sensor for heart rate monitor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BH1790GLC.cpp Source File

BH1790GLC.cpp

00001 /*****************************************************************************
00002   BH1790GLC.cpp
00003 
00004  Copyright (c) 2016 ROHM Co.,Ltd.
00005 
00006  Permission is hereby granted, free of charge, to any person obtaining a copy
00007  of this software and associated documentation files (the "Software"), to deal
00008  in the Software without restriction, including without limitation the rights
00009  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  copies of the Software, and to permit persons to whom the Software is
00011  furnished to do so, subject to the following conditions:
00012 
00013  The above copyright notice and this permission notice shall be included in
00014  all copies or substantial portions of the Software.
00015 
00016  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  THE SOFTWARE.
00023 ******************************************************************************/
00024 
00025 
00026 #include "BH1790GLC.h"
00027 
00028 BH1790GLC::BH1790GLC( PinName sda, PinName scl, char address )
00029     : i2c_p( new I2C( sda, scl ) ), i2c( *i2c_p ), adr( address )
00030 {
00031     init();
00032 }
00033 
00034 BH1790GLC::BH1790GLC( I2C &i2c_obj, char address )
00035     : i2c_p( NULL ), i2c( i2c_obj ), adr( address )
00036 {
00037     init();
00038 }
00039 
00040 BH1790GLC::~BH1790GLC()
00041 {
00042     if ( NULL != i2c_p )
00043         delete i2c_p;
00044 }
00045 
00046 void BH1790GLC::init(void)
00047 {
00048 }
00049 
00050 int BH1790GLC::meas_start(void)
00051 {
00052     int rc;
00053     char val[3];
00054     val[0] = BH1790GLC_MEAS_CONTROL1_VAL;
00055     val[1] = BH1790GLC_MEAS_CONTROL2_VAL;
00056     val[2] = BH1790GLC_MEAS_START_VAL;
00057     rc = write(BH1790GLC_MEAS_CONTROL1, val, sizeof(val));
00058     return (rc);
00059 }
00060 
00061 int BH1790GLC::get_rawval(char *data)
00062 {
00063     int rc;
00064     rc = read(BH1790GLC_DATAOUT_LEDOFF, data, 4);
00065     return (rc);
00066 }
00067 
00068 int BH1790GLC::get_val(unsigned short *data)
00069 {
00070     int rc;
00071     char val[4];
00072     rc = get_rawval(val);
00073     data[0] = ((unsigned short)val[1] << 8) | (val[0]);
00074     data[1] = ((unsigned short)val[3] << 8) | (val[2]);
00075     return (rc);
00076 }
00077 
00078 int BH1790GLC::write(char memory_address, char *data, int size)
00079 {
00080     int rc;
00081     char cmd[size];
00082     cmd[0] = memory_address;
00083     for (int i=0; i<size; i++) {
00084         cmd[i+1] = data[i];
00085     }
00086     rc = i2c.write( adr, cmd, size+1,true);
00087     return (rc);
00088 }
00089 
00090 int BH1790GLC::read(char memory_address, char *data, int size)
00091 {
00092     int rc;
00093     char cmd[size];
00094     cmd[0] = memory_address;
00095     rc = i2c.write( adr, cmd, 1, false);
00096     if (rc !=0){
00097         return (rc);
00098     }
00099     rc = i2c.read ( adr, data, size);
00100     return (rc);
00101 }