Rohm BH1745 red-green-blue-clear -color sensor driver
Dependents: rohm-bh1745-hello rohm-tileshield-6sensor-demo GR-PEACH_IoT_Platform_HTTP_sample
Fork of rohm-rpr0521 by
bh1745_driver.cpp
00001 /* Copyright 2016 Rohm Semiconductor 00002 00003 Licensed under the Apache License, Version 2.0 (the "License"); 00004 you may not use this file except in compliance with the License. 00005 You may obtain a copy of the License at 00006 00007 http://www.apache.org/licenses/LICENSE-2.0 00008 00009 Unless required by applicable law or agreed to in writing, software 00010 distributed under the License is distributed on an "AS IS" BASIS, 00011 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 See the License for the specific language governing permissions and 00013 limitations under the License. 00014 */ 00015 #include "../../rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h" //types, DEBUG_print* 00016 #include "../../rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h" //read_register, write_register, change_bits 00017 00018 #include "../rohm-bh1745/bh1745.h" //bh1745_* register definitions 00019 #include "../rohm-bh1745/bh1745_driver.h" 00020 //Choose SAD according to setup 00021 //#define SAD 0x38 00022 #define SAD 0x39 00023 00024 /* bh1745 driver*/ 00025 uint8_t bh1745_readId(){ 00026 uint8_t id; 00027 uint8_t read_bytes; 00028 00029 read_bytes = read_register(SAD, BH1745_ID_REG, &id, 1); 00030 if ( read_bytes > 0 ){ 00031 uint8_t partid; 00032 00033 DEBUG_printf("Manufacturer: %u\n\r", id); 00034 if (id == 0xe0){ 00035 read_bytes = read_register(SAD, BH1745_SYSTEM_CONTROL, &partid, 1); 00036 partid = partid & 0b00111111; 00037 if ( read_bytes > 0 ){ 00038 DEBUG_printf("Part ID: %u\n\r", partid); 00039 if (partid == 0x0b){//OK 00040 return(partid); 00041 } else { 00042 DEBUG_print("Wrong part ID for this driver.\n\r"); 00043 return 255; 00044 } 00045 } else { 00046 DEBUG_print("Part ID read failed.\n\r"); 00047 return 255; 00048 } 00049 } else { 00050 DEBUG_print("Wrong manufacturer Id for this driver.\n\r"); 00051 return 255; 00052 } 00053 } else { 00054 DEBUG_print("Manufacturer read failed.\n\r"); 00055 return 255; 00056 } 00057 } 00058 00059 void bh1745_wait_until_found(){ 00060 uint8_t id; 00061 00062 id = bh1745_readId(); 00063 while (id == 255){ 00064 wait(100); 00065 id = bh1745_readId(); 00066 } 00067 return; 00068 } 00069 00070 void bh1745_soft_reset(){ 00071 change_bits(SAD, BH1745_MODE_CONTROL1, BH1745_SYSTEM_CONTROL_SW_RESET_MASK, BH1745_SYSTEM_CONTROL_SW_RESET_START); 00072 } 00073 00074 void bh1745_clear_interrupt(){ 00075 uint8_t tmp; 00076 read_register(SAD, BH1745_INTERRUPT, &tmp, 1); 00077 } 00078 00079 void bh1745_initial_setup(){ 00080 write_register(SAD, BH1745_INTERRUPT, 00081 (BH1745_INTERRUPT_STATUS_INACTIVE | 00082 BH1745_INTERRUPT_LATCH_ENABLE | 00083 BH1745_INTERRUPT_PIN_DISABLE) 00084 ); 00085 write_register(SAD, BH1745_PERSISTENCE, 00086 (BH1745_PERSISTENCE_OF_INTERRUPT_STATUS_TOGGLE_AFTER_MEASUREMENT) 00087 ); 00088 write_register(SAD, BH1745_MODE_CONTROL1, 00089 (BH1745_MODE_CONTROL1_MEASUREMENT_TIME_160MSEC) 00090 ); 00091 write_register(SAD, BH1745_MODE_CONTROL2, 00092 (BH1745_MODE_CONTROL2_RGBC_MEASUREMENT_ACTIVE | 00093 BH1745_MODE_CONTROL2_ADC_GAIN_1X) 00094 ); 00095 } 00096 00097 /* input param: data16, pointer to 4*16bit memory 00098 return: error, true/false */ 00099 bool bh1745_read_data(uint16_t* data16){ 00100 #define BH1745_DATA_LEN 8 00101 uint8_t data[BH1745_DATA_LEN]; 00102 uint8_t read_bytes; 00103 00104 read_bytes = read_register(SAD, BH1745_RED_DATA_LSBS, &data[0], BH1745_DATA_LEN); 00105 if (read_bytes == BH1745_DATA_LEN){ 00106 data16[0] = (data[0]) | (data[1] << 8); //red 00107 data16[1] = (data[2]) | (data[3] << 8); //green 00108 data16[2] = (data[4]) | (data[5] << 8); //blue 00109 data16[3] = (data[6]) | (data[7] << 8); //clear 00110 return false; 00111 } else { 00112 DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes); 00113 return true; 00114 } 00115 00116 }
Generated on Tue Jul 12 2022 15:42:41 by 1.7.2