OB1203 basic mbed driver

Dependents:   OB1203_IDT

Revision:
1:99ca9c464503
Parent:
0:2da1f69b38fd
Child:
2:3cba0fe6db60
--- a/OB1203.cpp	Wed Apr 25 05:26:34 2018 +0000
+++ b/OB1203.cpp	Thu May 03 02:20:50 2018 +0000
@@ -28,7 +28,7 @@
 
 void OB1203::writeBlock(int addr, char startReg, char *data, char numBytes) {
 /*writes data from an array beginning at the startReg*/
-    pc.printf("entering writeBlock with data %02x %02x\r\n",data[0],data[1]);
+//    pc.printf("entering writeBlock with data %02x %02x\r\n",data[0],data[1]);
     char writeData[numBytes+1];
     writeData[0]=startReg;
     for(int n=1;n<numBytes+1;n++) {
@@ -46,7 +46,6 @@
     i2c->read(addr,data,numBytes);
 }
 
-
 int OB1203::get_status()
 {
     char writeData[1]; //declare array of size 1
@@ -57,6 +56,12 @@
     return (data[0]<<8 | data[1]);
 }
 
+bool OB1203::dataIsNew()
+{
+    int status_reg_vals;
+    status_reg_vals = get_status();
+    return ((status_reg_vals & 0x10)>>8) || (status_reg_vals & 0x01);
+}
 
 void OB1203::setMainConfig()
 {
@@ -82,10 +87,10 @@
     char writeData[6];
     writeData[0] = (char) (ls_thres_hi & 0x000000FF);
     writeData[1] = (char) ((ls_thres_hi & 0x0000FF00)>>8);
-    writeData[2] = (char) (ls_thres_hi & 0x00FF0000)>>16;
+    writeData[2] = (char) ((ls_thres_hi & 0x00FF0000)>>16);
     writeData[3] = (char) (ls_thres_lo & 0x000000FF);
     writeData[4] = (char) ((ls_thres_lo & 0x0000FF00)>>8);
-    writeData[5] = (char) (ls_thres_lo & 0x00FF0000)>>16;
+    writeData[5] = (char) ((ls_thres_lo & 0x00FF0000)>>16);
     writeBlock(OB1203_ADDR,REG_LS_THRES_HI,writeData,6); //default  
 }
 
@@ -260,7 +265,7 @@
 
 uint32_t OB1203::bytes2uint32(char *data, int start_byte) 
 {
-    //coverts a string of 3 bytes with LSB first into unsigned long
+    //coverts a string of 3 bytes with LSB first into unsigned long MSB last
     return uint32_t(data[start_byte+2])<<16 | int32_t(data[start_byte+1])<<8 | int32_t(data[start_byte]) ;
 }
 
@@ -278,7 +283,8 @@
         byte_data[16],byte_data[17],byte_data[18]);
     #endif
      
-    //byte_data[0] is status
+
+    //byte_data[0] is ps (not populated)
     data[1] = bytes2uint32(byte_data,4); //w
     data[2] = bytes2uint32(byte_data,7); //g
     data[3] = bytes2uint32(byte_data,10); //b
@@ -295,9 +301,8 @@
     #ifdef DEBUG
         pc.printf( "%02x %02x %02x %02x\r\n", byte_data[0], byte_data[1], byte_data[2], byte_data[3] );
     #endif
-     
-    //byte_data[0] is status
-    data[0] = ((uint32_t)byte_data[3])<<8 | ((uint32_t)byte_data[2]);
+
+    data[0] = ((uint32_t)byte_data[3])<<8 | ((uint32_t)byte_data[2]); //ps data
     return ( (byte_data[1] & PS_NEW_DATA) == 0x01 ? 1 : 0); //return 1 if new data or 0 if old data
 }
 
@@ -313,8 +318,7 @@
         byte_data[15],byte_data[16],byte_data[17],byte_data[18] );
     #endif
      
-    //byte_data[0] is status
-    data[0] = ((uint32_t)byte_data[3])<<8 | ((uint32_t)byte_data[2]);
+    data[0] = ((uint32_t)byte_data[3])<<8 | ((uint32_t)byte_data[2]); //ps
     data[1] = bytes2uint32(byte_data,4); //w
     data[2] = bytes2uint32(byte_data,7); //g
     data[3] = bytes2uint32(byte_data,10); //b