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