commands for SRF02 sensor

Fork of SRF02 by Craig Evans

Revision:
3:d571cee468e9
Parent:
2:284c0564fc75
--- a/SRF02.cpp	Thu Apr 30 10:34:19 2015 +0000
+++ b/SRF02.cpp	Fri May 08 15:41:19 2015 +0000
@@ -53,8 +53,8 @@
         wait(0.1);    
     }    
 }
-/*
-int SRF02::getDistanceIN()
+
+int SRF02::getDistanceIn()
 {
     char data[2];
     
@@ -81,6 +81,34 @@
 
     // high byte is first, then low byte, so combine into 16-bit value
     return (data[0] << 8) | data[1];
-}*/
+}
 
+int SRF02::getDistanceUs()
+{
+    char data[2];
+    
+    // need to send CM command to command register
+    data[0] = CMD_REG;
+    data[1] = US_CMD;
+    int ack = i2c->write(SRF02_W_ADD,data,2);  
+    if (ack)
+        error();  // if we don't receive acknowledgement, flash error message
+        
+    // this will start the sensor ranging, the datasheet suggests a delay of at least 65 ms before reading the result
+    wait_ms(70);
+    
+    // we can now read the result - tell the sensor we want the high byte
+    char reg = RANGE_H_REG;   
+    ack = i2c->write(SRF02_W_ADD,&reg,1);  
+    if (ack)
+        error();  // if we don't receive acknowledgement, flash error message
+    
+    // if we read two bytes, the register is automatically incremented (H and L)
+    ack = i2c->read(SRF02_R_ADD,data,2); 
+    if (ack)
+        error();  // if we don't receive acknowledgement, flash error message
 
+    // high byte is first, then low byte, so combine into 16-bit value
+    return (data[0] << 8) | data[1];
+}
+