Optical pulse wave sensor BH1790GLC driver. The original code contributed by Hideki Tanaka.

Dependents:   rohm-bh1790glc-hello rohm-SensorShield-example ECE568_Project2_Final

Fork of BH1790GLC by Hideki Tanaka

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bh1790glc.cpp Source File

bh1790glc.cpp

00001 /*
00002 The MIT License (MIT)
00003 Copyright (c) 2017 Rohm Semiconductor
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a
00006 copy of this software and associated documentation files (the
00007 "Software"), to deal in the Software without restriction, including
00008 without limitation the rights to use, copy, modify, merge, publish,
00009 distribute, sublicense, and/or sell copies of the Software, and to
00010 permit persons to whom the Software is furnished to do so, subject to
00011 the following conditions:
00012 
00013 The above copyright notice and this permission notice shall be included
00014 in all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00017 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00018 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00019 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
00020 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00021 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00022 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 */
00024 
00025 
00026 #include "RegisterWriter/RegisterWriter/rohm_hal2.h"
00027 #include "RegisterWriter/RegisterWriter/RegisterWriter.h"
00028 
00029 #include "rohm-bh1790glc-driver/bh1790glc_registers.h"
00030 #include "rohm-bh1790glc-driver/bh1790glc.h"
00031 
00032 BH1790GLC::BH1790GLC(RegisterWriter &i2c_obj, uint8_t sad, uint8_t wai) : i2c_rw(i2c_obj) {
00033     /* Note that bh1790glc writes need to be single write command, not two separate. */
00034     _sad = sad;
00035     _wai = wai;        
00036 }
00037 
00038 BH1790GLC::~BH1790GLC()
00039 {
00040 }
00041 
00042 /* Check if sensor is found and setup default configuration for default measurements
00043  *
00044  * @return error true/false
00045  */
00046 int BH1790GLC::set_default_on(void)
00047 {
00048     int error;
00049     uint8_t id;
00050     uint8_t part_id;
00051     uint8_t setup[3];
00052 
00053     error = i2c_rw.read_register(_sad, BH1790GLC_PART_ID, &part_id, 1);
00054     error = error || i2c_rw.read_register(_sad, BH1790GLC_MANUFACTURER_ID, &id, 1);
00055     if (error) {
00056         DEBUG_printf("read error.\r\n");
00057         return (error);
00058     }
00059 
00060     if (part_id != BH1790GLC_PART_ID_WIA_ID) {
00061         DEBUG_printf("PartID [%02X] doesn't match BH1790GLC.\r\n", part_id);
00062     } else {
00063         DEBUG_printf("PartID [%02X] OK.\r\n", part_id);
00064     }
00065 
00066     if (id != BH1790GLC_MID_VAL) {
00067         DEBUG_printf("Manufacturer id [%02X] doesn't match!\r\n", id);
00068     } else {
00069         DEBUG_printf("Manufacturer id [%02X] OK.\r\n", id);
00070     }
00071 
00072     setup[0] = (BH1790GLC_MEAS_CONTROL1_RDY_ENABLE | BH1790GLC_MEAS_CONTROL1_LED_LIGHTING_FREQ_128HZ | BH1790GLC_MEAS_CONTROL1_RCYCLE_32HZ);
00073     setup[1] = (BH1790GLC_MEAS_CONTROL2_LED1_EN_PULSED | BH1790GLC_MEAS_CONTROL2_LED_ON_TIME_216T_OSC | BH1790GLC_MEAS_CONTROL2_LED_CURRENT_10MA);
00074     setup[2] = BH1790GLC_MEAS_START_MEAS_ST_START;
00075     error = i2c_rw.write_register(_sad, BH1790GLC_MEAS_CONTROL1, setup, (uint8_t)sizeof(setup));
00076     if (error) {
00077         DEBUG_printf("BH1790GLC_MEAS_CONTROL1 write failed.\r\n");
00078     }
00079 
00080     return (error);
00081 }
00082 /*
00083  * @return error true/false
00084  */
00085 int BH1790GLC::getresults_raw(uint8_t *data)
00086 {
00087     return i2c_rw.read_register(_sad, BH1790GLC_DATAOUT_LEDOFF_L, data, 4);
00088 }
00089 
00090 /*
00091  * @return error true/false
00092  */
00093 int BH1790GLC::getresults(uint16_t *data)
00094 {
00095     int error;
00096     uint8_t val[4];
00097 
00098     error = getresults_raw(val);
00099     if (error) {
00100         return error;
00101     }
00102     data[0] = ((unsigned short)val[1] << 8) | (val[0]);
00103     data[1] = ((unsigned short)val[3] << 8) | (val[2]);
00104 
00105     return false;
00106 }
00107 
00108 //int BH1790GLC::write(char memory_address, char *data, int size)
00109 //{
00110 //   int rc;
00111 //    rc = i2c_rw.write_register(_sad, memory_address, data, size );
00112 //    return !rc;
00113 //}
00114 //
00115 //int BH1790GLC::read(uint8_t memory_address, uint8_t *data, uint8_t size)
00116 //{
00117 //    int rc;
00118 //    rc = i2c_rw.read_register(_sad, memory_address, data, size);
00119 //    
00120 //    return !rc;
00121 //}