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

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

Fork of BH1790GLC by Hideki Tanaka

Committer:
MikkoZ
Date:
Mon Jun 19 20:13:11 2017 +0000
Revision:
2:4446bf1de21b
Parent:
1:32bb2624bed8
Cleaned register definitions.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikkoZ 1:32bb2624bed8 1 /*
MikkoZ 1:32bb2624bed8 2 The MIT License (MIT)
MikkoZ 1:32bb2624bed8 3 Copyright (c) 2017 Rohm Semiconductor
MikkoZ 1:32bb2624bed8 4
MikkoZ 1:32bb2624bed8 5 Permission is hereby granted, free of charge, to any person obtaining a
MikkoZ 1:32bb2624bed8 6 copy of this software and associated documentation files (the
MikkoZ 1:32bb2624bed8 7 "Software"), to deal in the Software without restriction, including
MikkoZ 1:32bb2624bed8 8 without limitation the rights to use, copy, modify, merge, publish,
MikkoZ 1:32bb2624bed8 9 distribute, sublicense, and/or sell copies of the Software, and to
MikkoZ 1:32bb2624bed8 10 permit persons to whom the Software is furnished to do so, subject to
MikkoZ 1:32bb2624bed8 11 the following conditions:
MikkoZ 1:32bb2624bed8 12
MikkoZ 1:32bb2624bed8 13 The above copyright notice and this permission notice shall be included
MikkoZ 1:32bb2624bed8 14 in all copies or substantial portions of the Software.
MikkoZ 1:32bb2624bed8 15
MikkoZ 1:32bb2624bed8 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
MikkoZ 1:32bb2624bed8 17 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MikkoZ 1:32bb2624bed8 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
MikkoZ 1:32bb2624bed8 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
MikkoZ 1:32bb2624bed8 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
MikkoZ 1:32bb2624bed8 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
MikkoZ 1:32bb2624bed8 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
MikkoZ 1:32bb2624bed8 23 */
MikkoZ 1:32bb2624bed8 24
MikkoZ 1:32bb2624bed8 25
MikkoZ 1:32bb2624bed8 26 #include "RegisterWriter/RegisterWriter/rohm_hal2.h"
MikkoZ 1:32bb2624bed8 27 #include "RegisterWriter/RegisterWriter/RegisterWriter.h"
MikkoZ 1:32bb2624bed8 28
MikkoZ 1:32bb2624bed8 29 #include "rohm-bh1790glc-driver/bh1790glc_registers.h"
MikkoZ 1:32bb2624bed8 30 #include "rohm-bh1790glc-driver/bh1790glc.h"
MikkoZ 1:32bb2624bed8 31
MikkoZ 1:32bb2624bed8 32 BH1790GLC::BH1790GLC(RegisterWriter &i2c_obj, uint8_t sad, uint8_t wai) : i2c_rw(i2c_obj) {
MikkoZ 1:32bb2624bed8 33 /* Note that bh1790glc writes need to be single write command, not two separate. */
MikkoZ 1:32bb2624bed8 34 _sad = sad;
MikkoZ 1:32bb2624bed8 35 _wai = wai;
MikkoZ 1:32bb2624bed8 36 }
MikkoZ 1:32bb2624bed8 37
MikkoZ 1:32bb2624bed8 38 BH1790GLC::~BH1790GLC()
MikkoZ 1:32bb2624bed8 39 {
MikkoZ 1:32bb2624bed8 40 }
MikkoZ 1:32bb2624bed8 41
MikkoZ 1:32bb2624bed8 42 /* Check if sensor is found and setup default configuration for default measurements
MikkoZ 1:32bb2624bed8 43 *
MikkoZ 1:32bb2624bed8 44 * @return error true/false
MikkoZ 1:32bb2624bed8 45 */
MikkoZ 1:32bb2624bed8 46 int BH1790GLC::set_default_on(void)
MikkoZ 1:32bb2624bed8 47 {
MikkoZ 1:32bb2624bed8 48 int error;
MikkoZ 1:32bb2624bed8 49 uint8_t id;
MikkoZ 1:32bb2624bed8 50 uint8_t part_id;
MikkoZ 1:32bb2624bed8 51 uint8_t setup[3];
MikkoZ 1:32bb2624bed8 52
MikkoZ 1:32bb2624bed8 53 error = i2c_rw.read_register(_sad, BH1790GLC_PART_ID, &part_id, 1);
MikkoZ 1:32bb2624bed8 54 error = error || i2c_rw.read_register(_sad, BH1790GLC_MANUFACTURER_ID, &id, 1);
MikkoZ 1:32bb2624bed8 55 if (error) {
MikkoZ 1:32bb2624bed8 56 DEBUG_printf("read error.\r\n");
MikkoZ 1:32bb2624bed8 57 return (error);
MikkoZ 1:32bb2624bed8 58 }
MikkoZ 1:32bb2624bed8 59
MikkoZ 1:32bb2624bed8 60 if (part_id != BH1790GLC_PART_ID_WIA_ID) {
MikkoZ 1:32bb2624bed8 61 DEBUG_printf("PartID [%02X] doesn't match BH1790GLC.\r\n", part_id);
MikkoZ 1:32bb2624bed8 62 } else {
MikkoZ 1:32bb2624bed8 63 DEBUG_printf("PartID [%02X] OK.\r\n", part_id);
MikkoZ 1:32bb2624bed8 64 }
MikkoZ 1:32bb2624bed8 65
MikkoZ 1:32bb2624bed8 66 if (id != BH1790GLC_MID_VAL) {
MikkoZ 1:32bb2624bed8 67 DEBUG_printf("Manufacturer id [%02X] doesn't match!\r\n", id);
MikkoZ 1:32bb2624bed8 68 } else {
MikkoZ 1:32bb2624bed8 69 DEBUG_printf("Manufacturer id [%02X] OK.\r\n", id);
MikkoZ 1:32bb2624bed8 70 }
MikkoZ 1:32bb2624bed8 71
MikkoZ 1:32bb2624bed8 72 setup[0] = (BH1790GLC_MEAS_CONTROL1_RDY_ENABLE | BH1790GLC_MEAS_CONTROL1_LED_LIGHTING_FREQ_128HZ | BH1790GLC_MEAS_CONTROL1_RCYCLE_32HZ);
MikkoZ 1:32bb2624bed8 73 setup[1] = (BH1790GLC_MEAS_CONTROL2_LED1_EN_PULSED | BH1790GLC_MEAS_CONTROL2_LED_ON_TIME_216T_OSC | BH1790GLC_MEAS_CONTROL2_LED_CURRENT_10MA);
MikkoZ 1:32bb2624bed8 74 setup[2] = BH1790GLC_MEAS_START_MEAS_ST_START;
MikkoZ 1:32bb2624bed8 75 error = i2c_rw.write_register(_sad, BH1790GLC_MEAS_CONTROL1, setup, (uint8_t)sizeof(setup));
MikkoZ 1:32bb2624bed8 76 if (error) {
MikkoZ 1:32bb2624bed8 77 DEBUG_printf("BH1790GLC_MEAS_CONTROL1 write failed.\r\n");
MikkoZ 1:32bb2624bed8 78 }
MikkoZ 1:32bb2624bed8 79
MikkoZ 1:32bb2624bed8 80 return (error);
MikkoZ 1:32bb2624bed8 81 }
MikkoZ 1:32bb2624bed8 82 /*
MikkoZ 1:32bb2624bed8 83 * @return error true/false
MikkoZ 1:32bb2624bed8 84 */
MikkoZ 1:32bb2624bed8 85 int BH1790GLC::getresults_raw(uint8_t *data)
MikkoZ 1:32bb2624bed8 86 {
MikkoZ 1:32bb2624bed8 87 return i2c_rw.read_register(_sad, BH1790GLC_DATAOUT_LEDOFF_L, data, 4);
MikkoZ 1:32bb2624bed8 88 }
MikkoZ 1:32bb2624bed8 89
MikkoZ 1:32bb2624bed8 90 /*
MikkoZ 1:32bb2624bed8 91 * @return error true/false
MikkoZ 1:32bb2624bed8 92 */
MikkoZ 1:32bb2624bed8 93 int BH1790GLC::getresults(uint16_t *data)
MikkoZ 1:32bb2624bed8 94 {
MikkoZ 1:32bb2624bed8 95 int error;
MikkoZ 1:32bb2624bed8 96 uint8_t val[4];
MikkoZ 1:32bb2624bed8 97
MikkoZ 1:32bb2624bed8 98 error = getresults_raw(val);
MikkoZ 1:32bb2624bed8 99 if (error) {
MikkoZ 1:32bb2624bed8 100 return error;
MikkoZ 1:32bb2624bed8 101 }
MikkoZ 1:32bb2624bed8 102 data[0] = ((unsigned short)val[1] << 8) | (val[0]);
MikkoZ 1:32bb2624bed8 103 data[1] = ((unsigned short)val[3] << 8) | (val[2]);
MikkoZ 1:32bb2624bed8 104
MikkoZ 1:32bb2624bed8 105 return false;
MikkoZ 1:32bb2624bed8 106 }
MikkoZ 1:32bb2624bed8 107
MikkoZ 1:32bb2624bed8 108 //int BH1790GLC::write(char memory_address, char *data, int size)
MikkoZ 1:32bb2624bed8 109 //{
MikkoZ 1:32bb2624bed8 110 // int rc;
MikkoZ 1:32bb2624bed8 111 // rc = i2c_rw.write_register(_sad, memory_address, data, size );
MikkoZ 1:32bb2624bed8 112 // return !rc;
MikkoZ 1:32bb2624bed8 113 //}
MikkoZ 1:32bb2624bed8 114 //
MikkoZ 1:32bb2624bed8 115 //int BH1790GLC::read(uint8_t memory_address, uint8_t *data, uint8_t size)
MikkoZ 1:32bb2624bed8 116 //{
MikkoZ 1:32bb2624bed8 117 // int rc;
MikkoZ 1:32bb2624bed8 118 // rc = i2c_rw.read_register(_sad, memory_address, data, size);
MikkoZ 1:32bb2624bed8 119 //
MikkoZ 1:32bb2624bed8 120 // return !rc;
MikkoZ 1:32bb2624bed8 121 //}