A Library to control I2C SRF02 sensor

Dependents:   LPC1768BagSensor

Revision:
0:8ac34d529a3b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SRF02.cpp	Fri Dec 16 14:00:06 2011 +0000
@@ -0,0 +1,34 @@
+#include "SRF02.h"
+
+/* Creates an instance of class. Setting the pin used for I2C, the address of device and the measure range type. */
+SRF02::SRF02(PinName sda, PinName scl, int addr, char type) : _i2c(sda, scl), _addr(addr)
+{
+    _typem = type;
+}
+
+/* Destroyer of class instance. */
+SRF02::~SRF02() 
+{
+
+}
+
+/* Start and return the range measure. */
+float SRF02::read() 
+{
+    char command[2];
+    char result[2];
+
+    command[0] = 0x00;                          // Set the command register
+    command[1] = _typem;                        // Ranging results in type indicated in a costructor
+    _i2c.write(_addr, command, 2);              // Send the command to start a ranging burst
+
+    wait_ms(70);                                // Wait 70mS for complete the measure
+
+    command[0] = 0x02;                          // The start address of measure result
+    _i2c.write(_addr, command, 1, 1);           // Send address to read a measure
+    _i2c.read(_addr, result, 2);                // Read two byte of measure
+
+    float range = (result[0]<<8)+result[1];     // Convert the two byte in a float value
+
+    return range;
+}