Thundersoft / Mbed OS BH1790GLC_for_TTMxx
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 "bh1790glc.h"
00027 
00028 BH1790GLC::BH1790GLC(RegisterWriter &i2c_obj, uint8_t sad, uint8_t wai) : i2c_rw(i2c_obj) {
00029     /* Note that bh1790glc writes need to be single write command, not two separate. */
00030     _sad = sad;
00031     _wai = wai;        
00032 }
00033 
00034 BH1790GLC::~BH1790GLC()
00035 {
00036 }
00037 
00038 uint8_t BH1790GLC::getDeviceID()
00039 {
00040     uint8_t device_id;
00041     i2c_rw.read_register(_sad, BH1790GLC_PART_ID, &device_id, 1);
00042     return device_id;
00043 }
00044 
00045 /* Check if sensor is found and setup default configuration for default measurements
00046  *
00047  * @return error true/false
00048  */
00049 int BH1790GLC::set_default_on(void)
00050 {
00051     int error;
00052     uint8_t id;
00053     uint8_t part_id;
00054     uint8_t setup[3];
00055 
00056     error = i2c_rw.read_register(_sad, BH1790GLC_PART_ID, &part_id, 1);
00057     error = error || i2c_rw.read_register(_sad, BH1790GLC_MANUFACTURER_ID, &id, 1);
00058     if (error) {
00059         DEBUG_printf("read error.\r\n");
00060         return (error);
00061     }
00062 
00063     if (part_id != BH1790GLC_PART_ID_WIA_ID) {
00064         DEBUG_printf("PartID [%02X] doesn't match BH1790GLC.\r\n", part_id);
00065     } else {
00066         DEBUG_printf("PartID [%02X] OK.\r\n", part_id);
00067     }
00068 
00069     if (id != BH1790GLC_MID_VAL) {
00070         DEBUG_printf("Manufacturer id [%02X] doesn't match!\r\n", id);
00071     } else {
00072         DEBUG_printf("Manufacturer id [%02X] OK.\r\n", id);
00073     }
00074 
00075     setup[0] = (BH1790GLC_MEAS_CONTROL1_RDY_ENABLE | BH1790GLC_MEAS_CONTROL1_LED_LIGHTING_FREQ_128HZ | BH1790GLC_MEAS_CONTROL1_RCYCLE_32HZ);
00076     setup[1] = (BH1790GLC_MEAS_CONTROL2_LED1_EN_PULSED | BH1790GLC_MEAS_CONTROL2_LED_ON_TIME_216T_OSC | BH1790GLC_MEAS_CONTROL2_LED_CURRENT_10MA);
00077     setup[2] = BH1790GLC_MEAS_START_MEAS_ST_START;
00078     error = i2c_rw.write_register(_sad, BH1790GLC_MEAS_CONTROL1, setup, (uint8_t)sizeof(setup));
00079     if (error) {
00080         DEBUG_printf("BH1790GLC_MEAS_CONTROL1 write failed.\r\n");
00081     }
00082 
00083     return (error);
00084 }
00085 /*
00086  * @return error true/false
00087  */
00088 int BH1790GLC::getresults_raw(uint8_t *data)
00089 {
00090     return i2c_rw.read_register(_sad, BH1790GLC_DATAOUT_LEDOFF_L, data, 4);
00091 }
00092 
00093 /*
00094  * @return error true/false
00095  */
00096 int BH1790GLC::getresults(uint16_t *data)
00097 {
00098     int error;
00099     uint8_t val[4];
00100 
00101     error = getresults_raw(val);
00102     if (error) {
00103         return error;
00104     }
00105     data[0] = ((unsigned short)val[1] << 8) | (val[0]);
00106     data[1] = ((unsigned short)val[3] << 8) | (val[2]);
00107 
00108     return false;
00109 }
00110 
00111 //int BH1790GLC::write(char memory_address, char *data, int size)
00112 //{
00113 //   int rc;
00114 //    rc = i2c_rw.write_register(_sad, memory_address, data, size );
00115 //    return !rc;
00116 //}
00117 //
00118 //int BH1790GLC::read(uint8_t memory_address, uint8_t *data, uint8_t size)
00119 //{
00120 //    int rc;
00121 //    rc = i2c_rw.read_register(_sad, memory_address, data, size);
00122 //    
00123 //    return !rc;
00124 //}