Rohm BH1726 ALS sensor driver.

Dependents:   rohm-bh1726-hello rohm-tileshield-6sensor-demo

Fork of rohm-bh1745 by Rohm

Files at this revision

API Documentation at this revision

Comitter:
MikkoZ
Date:
Wed Sep 14 07:14:36 2016 +0000
Parent:
1:64629eee9eab
Child:
3:6eb91c7c6560
Commit message:
Rohm BH1726 dual channel ALS sensor driver for 400-700nm wavelength.

Changed in this revision

rohm-bh1726/bh1726.h Show annotated file Show diff for this revision Revisions of this file
rohm-bh1726/bh1726_driver.h Show annotated file Show diff for this revision Revisions of this file
rohm-bh1745/bh1745.h Show diff for this revision Revisions of this file
rohm-bh1745/bh1745_driver.h Show diff for this revision Revisions of this file
source/bh1726_driver.cpp Show annotated file Show diff for this revision Revisions of this file
source/bh1745_driver.cpp Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rohm-bh1726/bh1726.h	Wed Sep 14 07:14:36 2016 +0000
@@ -0,0 +1,103 @@
+/*   Copyright 2016 Rohm Semiconductor
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+#ifndef BH1726_H
+#define BH1726_H
+
+/* registers */
+#define BH1726_REGISTER_DUMP_START 0x80
+#define BH1726_CONTROL 0x80
+// ADC Light integration time. Cycle = 256-itime; integration time= cycle x 2.7ms; measurement time = 2ms + integration time
+#define BH1726_TIMING 0x81
+#define BH1726_INTERRUPT 0x82
+#define BH1726_TH_LOW_LSBS 0x83
+#define BH1726_TH_LOW_MSBS 0x84
+#define BH1726_TH_UP_LSBS 0x85
+#define BH1726_TH_UP_MSBS 0x86
+#define BH1726_GAIN 0x87
+#define BH1726_ID 0x92
+#define BH1726_DATA0_LSBS 0x94
+#define BH1726_DATA0_MSBS 0x95
+#define BH1726_DATA1_LSBS 0x96
+#define BH1726_DATA1_MSBS 0x97
+#define BH1726_WAIT 0x98
+// special command, no data
+#define BH1726_INTERRUPT_OUTPUT_RESET 0xE1
+// special command, no data
+#define BH1726_SOFTWARE_RESET 0xE4
+#define BH1726_REGISTER_DUMP_END 0x98
+/* registers bits */
+#define BH1726_CONTROL_ADC_INTR_INACTIVE (0x00 << 5)
+#define BH1726_CONTROL_ADC_INTR_ACTIVE (0x01 << 5)
+#define BH1726_CONTROL_ADC_VALID_FALSE (0x00 << 4)
+// Updated since last reading
+#define BH1726_CONTROL_ADC_VALID_TRUE (0x01 << 4)
+#define BH1726_CONTROL_ADC_EN_FALSE (0x00 << 1)
+#define BH1726_CONTROL_ADC_EN_TRUE (0x01 << 1)
+#define BH1726_CONTROL_POWER_OFF (0x00 << 0)
+#define BH1726_CONTROL_POWER_ON (0x01 << 0)
+// 691.2ms
+#define BH1726_TIMING_ITIME_256CYCLE (0x00 << 0)
+// 399.6ms
+#define BH1726_TIMING_ITIME_148CYCLE (0x6C << 0)
+// 199.8ms
+#define BH1726_TIMING_ITIME_74CYCLE (0xB6 << 0)
+// 135ms
+#define BH1726_TIMING_ITIME_50CYCLE (0xCE << 0)
+// 102.6ms
+#define BH1726_TIMING_ITIME_38CYCLE (0xDA << 0)
+// 51.3ms
+#define BH1726_TIMING_ITIME_19CYCLE (0xED << 0)
+// 5.4ms
+#define BH1726_TIMING_ITIME_2CYCLE (0xFE << 0)
+// 2.7ms
+#define BH1726_TIMING_ITIME_1CYCLE (0xFF << 0)
+#define BH1726_INTERRUPT_INT_LATCH_YES (0x00 << 5)
+#define BH1726_INTERRUPT_INT_LATCH_NO (0x01 << 5)
+#define BH1726_INTERRUPT_INT_EN_FALSE (0x00 << 4)
+#define BH1726_INTERRUPT_INT_EN_TRUE (0x01 << 4)
+#define BH1726_INTERRUPT_PERSIST_DRDY (0x00 << 0)
+#define BH1726_INTERRUPT_PERSIST_AFTER_1 (0x01 << 0)
+#define BH1726_INTERRUPT_PERSIST_AFTER_2 (0x02 << 0)
+#define BH1726_INTERRUPT_PERSIST_AFTER_3 (0x03 << 0)
+#define BH1726_INTERRUPT_PERSIST_AFTER_4 (0x04 << 0)
+#define BH1726_GAIN_GAIN0_X1 (0x00 << 2)
+#define BH1726_GAIN_GAIN0_X2 (0x01 << 2)
+#define BH1726_GAIN_GAIN0_X64 (0x02 << 2)
+#define BH1726_GAIN_GAIN0_X128 (0x03 << 2)
+#define BH1726_GAIN_GAIN1_X1 (0x00 << 0)
+#define BH1726_GAIN_GAIN1_X2 (0x01 << 0)
+#define BH1726_GAIN_GAIN1_X64 (0x02 << 0)
+#define BH1726_GAIN_GAIN1_X128 (0x03 << 0)
+#define BH1726_ID_NUMBER_FOR_PART (0x72 << 0)
+#define BH1726_WAIT_WAIT_NO (0x00 << 0)
+// after each measurement (low current consumption mode)
+#define BH1726_WAIT_WAIT_300MS (0x01 << 0)
+ /*registers bit masks */
+#define BH1726_CONTROL_ADC_INTR_MASK 0x20
+#define BH1726_CONTROL_ADC_VALID_MASK 0x10
+
+#define BH1726_CONTROL_ADC_EN_MASK 0x02
+#define BH1726_CONTROL_POWER_MASK 0x01
+#define BH1726_TIMING_ITIME_MASK 0xFF
+
+#define BH1726_INTERRUPT_INT_LATCH_MASK 0x20
+#define BH1726_INTERRUPT_INT_EN_MASK 0x10
+#define BH1726_INTERRUPT_PERSIST_MASK 0x0F
+#define BH1726_GAIN_GAIN0_MASK 0x0C
+#define BH1726_GAIN_GAIN1_MASK 0x03
+#define BH1726_ID_NUMBER_MASK 0xFF
+#define BH1726_WAIT_WAIT_MASK 0x01
+
+#endif /* BH1726_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rohm-bh1726/bh1726_driver.h	Wed Sep 14 07:14:36 2016 +0000
@@ -0,0 +1,30 @@
+/*   Copyright 2016 Rohm Semiconductor
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+#ifndef bh1726_driver_h
+#define bh1726_driver_h
+
+#include "../../rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h"         //types
+
+/* bh1726 driver*/
+uint8_t bh1726_readId();
+void bh1726_wait_until_found();
+void bh1726_soft_reset();
+bool bh1726_read_data(uint16_t* data16);
+void bh1726_initial_setup();
+void bh1726_clear_interrupt();
+void bh1726_dumpregs(uint8_t dumpstart, uint8_t dumpend);
+
+#endif
+
--- a/rohm-bh1745/bh1745.h	Mon Sep 12 12:31:40 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,133 +0,0 @@
-/*   Copyright 2016 Rohm Semiconductor
-
-   Licensed under the Apache License, Version 2.0 (the "License");
-   you may not use this file except in compliance with the License.
-   You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-*/
-#ifndef BH1745_H
-#define BH1745_H
-
-/* registers */
-#define BH1745_REGISTER_DUMP_START 0x40
-#define BH1745_SYSTEM_CONTROL 0x40
-// Writing MODE_CONTROL1/2/3 restarts measurement if any.
-#define BH1745_MODE_CONTROL1 0x41
-#define BH1745_MODE_CONTROL2 0x42
-// Always write 02h
-#define BH1745_MODE_CONTROL3 0x44
-// Least significant byte of uint16 RED measurement value
-#define BH1745_RED_DATA_LSBS 0x50
-// Most significant byte of uint16 RED measurement value
-#define BH1745_RED_DATA_MSBS 0x51
-#define BH1745_GREEN_DATA_LSBS 0x52
-#define BH1745_GREEN_DATA_MSBS 0x53
-#define BH1745_BLUE_DATA_LSBS 0x54
-#define BH1745_BLUE_DATA_MSBS 0x55
-#define BH1745_CLEAR_DATA_LSBS 0x56
-#define BH1745_CLEAR_DATA_MSBS 0x57
-// DINT data is used for internal calculation of BH1745NUC.
-#define BH1745_DINT_DATA_LSBS 0x58
-// DINT registers are used for IC test only.
-#define BH1745_DINT_DATA_MSBS 0x59
-#define BH1745_INTERRUPT 0x60
-#define BH1745_PERSISTENCE 0x61
-#define BH1745_TH_LSBS 0x62
-#define BH1745_TH_MSBS 0x63
-#define BH1745_TL_LSBS 0x64
-#define BH1745_TL_MSBS 0x65
-#define BH1745_ID_REG 0x92
-#define BH1745_REGISTER_DUMP_END 0x92
-/* registers bits */
-// Initial reset is not started
-#define BH1745_SYSTEM_CONTROL_SW_RESET_NOT_STARTED (0x00 << 7)
-// Initial reset is started
-#define BH1745_SYSTEM_CONTROL_SW_RESET_START (0x01 << 7)
-// In specification "not_initialized"
-#define BH1745_SYSTEM_CONTROL_INT_PIN_ACTIVE (0x00 << 6)
-// high impedance
-#define BH1745_SYSTEM_CONTROL_INT_PIN_INACTIVE (0x01 << 6)
-#define BH1745_SYSTEM_CONTROL_PART_ID (0x0B << 0)
-#define BH1745_MODE_CONTROL1_RESERVED_WRITE00000 (0x00 << 3)
-#define BH1745_MODE_CONTROL1_MEASUREMENT_TIME_160MSEC (0x00 << 0)
-#define BH1745_MODE_CONTROL1_MEASUREMENT_TIME_320MSEC (0x01 << 0)
-#define BH1745_MODE_CONTROL1_MEASUREMENT_TIME_640MSEC (0x02 << 0)
-#define BH1745_MODE_CONTROL1_MEASUREMENT_TIME_1280MSEC (0x03 << 0)
-#define BH1745_MODE_CONTROL1_MEASUREMENT_TIME_2560MSEC (0x04 << 0)
-#define BH1745_MODE_CONTROL1_MEASUREMENT_TIME_5120MSEC (0x05 << 0)
-#define BH1745_MODE_CONTROL1_MEASUREMENT_TIME_FORBIDDEN6 (0x06 << 0)
-#define BH1745_MODE_CONTROL1_MEASUREMENT_TIME_FORBIDDEN7 (0x07 << 0)
-#define BH1745_MODE_CONTROL2_DATA_UPDATED_NO (0x00 << 7)
-#define BH1745_MODE_CONTROL2_DATA_UPDATED_YES (0x01 << 7)
-#define BH1745_MODE_CONTROL2_RESERVED65_WRITE00 (0x00 << 5)
-#define BH1745_MODE_CONTROL2_RGBC_MEASUREMENT_INACTIVE (0x00 << 4)
-#define BH1745_MODE_CONTROL2_RGBC_MEASUREMENT_ACTIVE (0x01 << 4)
-#define BH1745_MODE_CONTROL2_RESERVED32_WRITE00 (0x00 << 2)
-#define BH1745_MODE_CONTROL2_ADC_GAIN_1X (0x00 << 0)
-#define BH1745_MODE_CONTROL2_ADC_GAIN_2X (0x01 << 0)
-#define BH1745_MODE_CONTROL2_ADC_GAIN_16X (0x02 << 0)
-#define BH1745_MODE_CONTROL2_ADC_GAIN_FORBIDDEN3 (0x03 << 0)
-#define BH1745_MODE_CONTROL3_ALWAYS_02H (0x02 << 0)
-#define BH1745_INTERRUPT_STATUS_INACTIVE (0x00 << 7)
-#define BH1745_INTERRUPT_STATUS_ACTIVE (0x01 << 7)
-#define BH1745_INTERRUPT_RESERVED65_WRITE00 (0x00 << 5)
-// INT pin is latched until INTERRUPT register is read or initialized
-#define BH1745_INTERRUPT_LATCH_ENABLE (0x00 << 4)
-// INT pin is updated after each measurement
-#define BH1745_INTERRUPT_LATCH_DISABLE (0x01 << 4)
-// red channel
-#define BH1745_INTERRUPT_SOURCE_SELECT_RED (0x00 << 2)
-// green channel
-#define BH1745_INTERRUPT_SOURCE_SELECT_GREEN (0x01 << 2)
-// blue channel
-#define BH1745_INTERRUPT_SOURCE_SELECT_BLUE (0x02 << 2)
-// clear channel
-#define BH1745_INTERRUPT_SOURCE_SELECT_CLEAR (0x03 << 2)
-#define BH1745_INTERRUPT_RESERVED1_WRITE0 (0x00 << 1)
-#define BH1745_INTERRUPT_PIN_DISABLE (0x00 << 0)
-#define BH1745_INTERRUPT_PIN_ENABLE (0x01 << 0)
-#define BH1745_PERSISTENCE_RESERVED72_WRITE000001 (0x01 << 2)
-// Interrupt status is toggled at each measurement end.
-#define BH1745_PERSISTENCE_OF_INTERRUPT_STATUS_TOGGLE_AFTER_MEASUREMENT (0x00 << 0)
-// Interrupt status is updated at each measurement end.
-#define BH1745_PERSISTENCE_OF_INTERRUPT_STATUS_UPDATE_AFTER_MEASUREMENT (0x01 << 0)
-// Interrupt status is updated if 4 consecutive threshold judgements are the same
-#define BH1745_PERSISTENCE_OF_INTERRUPT_STATUS_UPDATE_AFTER_4_SAME (0x02 << 0)
-// Interrupt status is updated if 8 consecutive threshold judgements are the same
-#define BH1745_PERSISTENCE_OF_INTERRUPT_STATUS_UPDATE_AFTER_8_SAME (0x03 << 0)
-#define BH1745_ID_REG_MANUFACTURER_ID (0xE0 << 0)
- /*registers bit masks */
-#define BH1745_SYSTEM_CONTROL_SW_RESET_MASK 0x80
-
-#define BH1745_SYSTEM_CONTROL_INT_PIN_MASK 0x40
-
-#define BH1745_SYSTEM_CONTROL_PART_MASK 0x3F
-#define BH1745_MODE_CONTROL1_RESERVED_MASK 0xF8
-#define BH1745_MODE_CONTROL1_MEASUREMENT_TIME_MASK 0x07
-#define BH1745_MODE_CONTROL2_DATA_UPDATED_MASK 0x80
-#define BH1745_MODE_CONTROL2_RESERVED65_MASK 0x60
-#define BH1745_MODE_CONTROL2_RGBC_MEASUREMENT_MASK 0x10
-#define BH1745_MODE_CONTROL2_RESERVED32_MASK 0x0C
-#define BH1745_MODE_CONTROL2_ADC_GAIN_MASK 0x03
-#define BH1745_MODE_CONTROL3_ALWAYS_MASK 0xFF
-#define BH1745_INTERRUPT_STATUS_MASK 0x80
-#define BH1745_INTERRUPT_RESERVED65_MASK 0x60
-#define BH1745_INTERRUPT_LATCH_MASK 0x10
-
-#define BH1745_INTERRUPT_SOURCE_MASK 0x0C
-// Write 0
-#define BH1745_INTERRUPT_RESERVED1_MASK 0x02
-#define BH1745_INTERRUPT_PIN_MASK 0x01
-#define BH1745_PERSISTENCE_RESERVED72_MASK 0xFC
-#define BH1745_PERSISTENCE_OF_INTERRUPT_MASK 0x03
-
-#define BH1745_ID_REG_MANUFACTURER_MASK 0xFF
-
-#endif /* BH1745_H */
--- a/rohm-bh1745/bh1745_driver.h	Mon Sep 12 12:31:40 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,29 +0,0 @@
-/*   Copyright 2016 Rohm Semiconductor
-
-   Licensed under the Apache License, Version 2.0 (the "License");
-   you may not use this file except in compliance with the License.
-   You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-*/
-#ifndef bh1745_driver_h
-#define bh1745_driver_h
-
-#include "../../rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h"         //types
-
-/* bh1745 driver*/
-uint8_t bh1745_readId();
-void bh1745_wait_until_found();
-void bh1745_soft_reset();
-bool bh1745_read_data(uint16_t* data16);
-void bh1745_initial_setup();
-void bh1745_clear_interrupt();
-
-#endif
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/bh1726_driver.cpp	Wed Sep 14 07:14:36 2016 +0000
@@ -0,0 +1,114 @@
+/*   Copyright 2016 Rohm Semiconductor
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+#include "../../rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h"         //types, DEBUG_print*
+#include "../../rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h"        //read_register, write_register, change_bits
+
+#include "../rohm-bh1726/bh1726.h"      //bh1726_* register definitions
+#include "../rohm-bh1726/bh1726_driver.h"
+//Choose SAD according to setup
+//#define SAD  0x29
+#define SAD 0x39
+
+/* bh1726 driver*/
+uint8_t bh1726_readId(){
+    uint8_t partid;
+    uint8_t read_bytes;
+
+    read_bytes = read_register(SAD, BH1726_ID, &partid, 1);
+    if ( read_bytes > 0 ){
+        DEBUG_printf("Part ID: %u\n\r", partid);
+        return(partid);
+        }
+    else{
+        DEBUG_print("Part ID read failed.\n\r");
+        return 255;
+        }
+}
+
+void bh1726_dumpregs(uint8_t dumpstart, uint8_t dumpend){
+    uint8_t i;
+    uint8_t value;
+    uint8_t read_bytes;
+    
+    for(i = dumpstart;i < dumpend;i++){
+        read_bytes = read_register(SAD, i, &value, 1);
+        if ( read_bytes > 0 ){
+            DEBUG_printf("Register [%3u]=[%3u]\n\r", i, value);
+            }
+        }
+    return;
+}
+
+void bh1726_wait_until_found(){
+  uint8_t id;
+
+  DEBUG_printf("Hoping to find part ID: %u\n\r", BH1726_ID_NUMBER_FOR_PART);
+  id = bh1726_readId();
+  while (id != BH1726_ID_NUMBER_FOR_PART){
+    wait_ms(100);
+    id = bh1726_readId();
+    }
+  return;
+  }
+
+void bh1726_soft_reset(){
+    write_register(SAD, BH1726_SOFTWARE_RESET, 0);
+}
+
+void bh1726_clear_interrupt(){
+    write_register(SAD, BH1726_INTERRUPT_OUTPUT_RESET, 0);
+}
+
+void bh1726_initial_setup(){
+    write_register(SAD, BH1726_TIMING,
+        (BH1726_TIMING_ITIME_38CYCLE)   //105ms measurement time
+        );
+    write_register(SAD, BH1726_INTERRUPT,
+        (BH1726_INTERRUPT_INT_LATCH_YES |
+        BH1726_INTERRUPT_INT_EN_FALSE |
+        BH1726_INTERRUPT_PERSIST_DRDY)
+        );
+    write_register(SAD, BH1726_GAIN,
+        (BH1726_GAIN_GAIN0_X1 |
+         BH1726_GAIN_GAIN1_X1)
+        );
+    write_register(SAD, BH1726_WAIT,
+        (BH1726_WAIT_WAIT_NO)
+        );
+    write_register(SAD, BH1726_CONTROL,
+        (BH1726_CONTROL_ADC_EN_TRUE |
+        BH1726_CONTROL_POWER_ON)
+        );
+}
+
+/* input param: data16, pointer to 4*16bit memory 
+   return: error, true/false */
+bool bh1726_read_data(uint16_t* data16){
+    #define BH1726_DATA_LEN 4
+    uint8_t data[BH1726_DATA_LEN];
+    uint8_t read_bytes;
+
+    read_bytes = read_register(SAD, BH1726_DATA0_LSBS, &data[0], BH1726_DATA_LEN);
+    if (read_bytes == BH1726_DATA_LEN){
+        data16[0] = (data[0]) | (data[1] << 8); //data0
+        data16[1] = (data[2]) | (data[3] << 8); //data1
+        return false;
+        }
+    else{
+        DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes);
+        return true;
+        }
+   
+    }
--- a/source/bh1745_driver.cpp	Mon Sep 12 12:31:40 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,108 +0,0 @@
-/*   Copyright 2016 Rohm Semiconductor
-
-   Licensed under the Apache License, Version 2.0 (the "License");
-   you may not use this file except in compliance with the License.
-   You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-*/
-#include "../../rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h"         //types, DEBUG_print*
-#include "../../rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h"        //read_register, write_register, change_bits
-
-#include "../rohm-bh1745/bh1745.h"      //bh1745_* register definitions
-#include "../rohm-bh1745/bh1745_driver.h"
-//Choose SAD according to setup
-//#define SAD  0x38
-#define SAD 0x39
-
-/* bh1745 driver*/
-uint8_t bh1745_readId(){
-    uint8_t id;
-    uint8_t read_bytes;
-
-    read_bytes = read_register(SAD, BH1745_ID_REG, &id, 1);
-    if ( read_bytes > 0 ){
-        uint8_t partid;
-        
-        DEBUG_printf("Manufacturer: %u\n\r", id);
-        read_bytes = read_register(SAD, BH1745_SYSTEM_CONTROL, &partid, 1);
-        if ( read_bytes > 0 ){
-            DEBUG_printf("Part ID: %u\n\r", (partid & 0b00111111) );
-        return(partid);
-        }
-        else{
-            DEBUG_print("Part ID read failed.\n\r");
-            return 255;
-        }
-    }
-    else{
-        DEBUG_print("Manufacturer read failed.\n\r");
-        return 255;
-    }
-}
-
-void bh1745_wait_until_found(){
-  uint8_t id;
-
-  id = bh1745_readId();
-  while (id == 255){
-    wait(100);
-    id = bh1745_readId();
-    }
-  return;
-  }
-
-void bh1745_soft_reset(){
-    change_bits(SAD, BH1745_MODE_CONTROL1, BH1745_SYSTEM_CONTROL_SW_RESET_MASK, BH1745_SYSTEM_CONTROL_SW_RESET_START);
-}
-
-void bh1745_clear_interrupt(){
-    uint8_t tmp;
-    read_register(SAD, BH1745_INTERRUPT, &tmp, 1);
-}
-
-void bh1745_initial_setup(){
-    write_register(SAD, BH1745_INTERRUPT,
-        (BH1745_INTERRUPT_STATUS_INACTIVE |
-        BH1745_INTERRUPT_LATCH_ENABLE |
-        BH1745_INTERRUPT_PIN_DISABLE)
-        );
-    write_register(SAD, BH1745_PERSISTENCE,
-        (BH1745_PERSISTENCE_OF_INTERRUPT_STATUS_TOGGLE_AFTER_MEASUREMENT)
-        );
-    write_register(SAD, BH1745_MODE_CONTROL1,
-        (BH1745_MODE_CONTROL1_MEASUREMENT_TIME_160MSEC)
-        );
-    write_register(SAD, BH1745_MODE_CONTROL2,
-        (BH1745_MODE_CONTROL2_RGBC_MEASUREMENT_ACTIVE |
-         BH1745_MODE_CONTROL2_ADC_GAIN_1X)
-        );
-}
-
-/* input param: data16, pointer to 4*16bit memory 
-   return: error, true/false */
-bool bh1745_read_data(uint16_t* data16){
-    #define BH1745_DATA_LEN 8
-    uint8_t data[BH1745_DATA_LEN];
-    uint8_t read_bytes;
-
-    read_bytes = read_register(SAD, BH1745_RED_DATA_LSBS, &data[0], BH1745_DATA_LEN);
-    if (read_bytes == BH1745_DATA_LEN){
-        data16[0] = (data[0]) | (data[1] << 8); //red
-        data16[1] = (data[2]) | (data[3] << 8); //green
-        data16[2] = (data[4]) | (data[5] << 8); //blue
-        data16[3] = (data[6]) | (data[7] << 8); //clear
-        return false;
-        }
-    else{
-        DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes);
-        return true;
-        }
-   
-    }