This fork adds bulk reading for faster transactions. It also removes the I2C construction from the constructor.

Dependents:   ColorDetector ColorDetectorV2 offline_sync_k64f

Fork of GroveColourSensor by Brian Daniels

Files at this revision

API Documentation at this revision

Comitter:
marcuschang
Date:
Wed Jul 15 10:25:43 2015 +0000
Parent:
4:f0e8304db2a3
Commit message:
Fixed bug. uint16_t truncated to uint8_t.

Changed in this revision

GroveColourSensor.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r f0e8304db2a3 -r f6a136b99533 GroveColourSensor.cpp
--- a/GroveColourSensor.cpp	Tue Apr 28 16:12:57 2015 +0000
+++ b/GroveColourSensor.cpp	Wed Jul 15 10:25:43 2015 +0000
@@ -57,10 +57,10 @@
     char tmpColours[8] = {0};
     
     if (!i2c->read((SEVEN_BIT_ADDRESS << 1), tmpColours, sizeof(tmpColours))) {        
-        sample->ch.green = bytesTo16bit(tmpColours[0], tmpColours[1] << 8);
-        sample->ch.red = bytesTo16bit(tmpColours[2], tmpColours[3] << 8);
-        sample->ch.blue = bytesTo16bit(tmpColours[4], tmpColours[5] << 8);
-        sample->ch.clear = bytesTo16bit(tmpColours[6], tmpColours[7] << 8);
+        sample->ch.green = bytesTo16bit(tmpColours[0], tmpColours[1]);
+        sample->ch.red = bytesTo16bit(tmpColours[2], tmpColours[3]);
+        sample->ch.blue = bytesTo16bit(tmpColours[4], tmpColours[5]);
+        sample->ch.clear = bytesTo16bit(tmpColours[6], tmpColours[7]);
     } else {
         printf("I2C Read error\r\n");
     }
@@ -75,8 +75,7 @@
 }
 
 uint16_t GroveColourSensor::bytesTo16bit(char lowByte, char highByte) {
-    uint16_t res = 0;
-    res |= lowByte;
-    res |= highByte << 8;
+    uint16_t res = highByte;
+    res = (res << 8) | lowByte;
     return res;
 }