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 Rohm

Files at this revision

API Documentation at this revision

Comitter:
MikkoZ
Date:
Wed Sep 14 07:04:40 2016 +0000
Parent:
1:64629eee9eab
Commit message:
Added stricter part id checks to prevent SAD collision go unnoticed. Same SADs are used in RPR0521(0x38) and BH1726(0x39).

Changed in this revision

source/bh1745_driver.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 64629eee9eab -r 2b4a1b49998d source/bh1745_driver.cpp
--- a/source/bh1745_driver.cpp	Mon Sep 12 12:31:40 2016 +0000
+++ b/source/bh1745_driver.cpp	Wed Sep 14 07:04:40 2016 +0000
@@ -31,32 +31,41 @@
         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");
+        if (id == 0xe0){
+            read_bytes = read_register(SAD, BH1745_SYSTEM_CONTROL, &partid, 1);
+            partid = partid & 0b00111111;
+            if ( read_bytes > 0 ){
+                DEBUG_printf("Part ID: %u\n\r", partid);
+                if (partid == 0x0b){//OK
+                    return(partid);
+                } else {
+                    DEBUG_print("Wrong part ID for this driver.\n\r");
+                    return 255;
+                }
+            } else {
+                DEBUG_print("Part ID read failed.\n\r");
+                return 255;
+            }
+        } else {
+            DEBUG_print("Wrong manufacturer Id for this driver.\n\r");
             return 255;
         }
-    }
-    else{
+    } else {
         DEBUG_print("Manufacturer read failed.\n\r");
         return 255;
     }
 }
 
 void bh1745_wait_until_found(){
-  uint8_t id;
+    uint8_t id;
 
-  id = bh1745_readId();
-  while (id == 255){
-    wait(100);
     id = bh1745_readId();
+    while (id == 255){
+        wait(100);
+        id = bh1745_readId();
     }
-  return;
-  }
+    return;
+}
 
 void bh1745_soft_reset(){
     change_bits(SAD, BH1745_MODE_CONTROL1, BH1745_SYSTEM_CONTROL_SW_RESET_MASK, BH1745_SYSTEM_CONTROL_SW_RESET_START);
@@ -99,10 +108,9 @@
         data16[2] = (data[4]) | (data[5] << 8); //blue
         data16[3] = (data[6]) | (data[7] << 8); //clear
         return false;
-        }
-    else{
+    } else {
         DEBUG_printf("Read error. Read %d bytes\n\r", read_bytes);
         return true;
-        }
+    }
    
-    }
+}