Library for interfacing the SRF08 ultrasonic range sensor. Most functions of the SRF08 are covered, including interrupt-based waiting for the ranging process to finish

Dependents:   Project6

Fork of SRF08 by Brent Dekker

Revision:
1:76fb116fa28d
Parent:
0:4e0a8193b92e
Child:
2:ca82f89f415d
--- a/SRF08.cpp	Thu Jul 05 09:53:18 2012 +0000
+++ b/SRF08.cpp	Tue Jul 10 08:48:02 2012 +0000
@@ -31,7 +31,8 @@
  * Returns:     void
  * Description: Creates an instance of the SRF08 to communicate with a sRF08 module
  */
-SRF08::SRF08(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
+SRF08::SRF08(PinName sda, PinName scl, int addr) :
+        m_i2c(sda, scl), m_addr(addr), rangeBuf(), rangeBufIndex(0) {
     char cmd[2];
     // Set up SRF08 max range and receiver sensitivity over I2C bus
     cmd[0] = 0x02;                          // Range register
@@ -53,12 +54,58 @@
 }
 
 /*
+ * Function:    startRanging
+ * Args:        void
+ * Returns:     void
+ * Description: Sends command to module to start ranging.
+ */
+void startRanging() {
+    // Send Tx burst command over I2C bus
+    cmd[0] = 0x00;                      // Command register
+    cmd[1] = 0x51;                      // Ranging results in cm
+    m_i2c.write(m_addr, cmd, 2);        // Send ranging burst
+}
+
+/*
+ * Function:    rangingFinished
+ * Args:        void
+ * Returns:     Bool: whether ranging is finished
+ * Description: Checks if the ranging process on the module is finished
+ */
+bool rangingFinished() {
+    char cmd = 0x00; //Software revision register
+    char echo;
+    m_i2c.write(m_addr, cmd, 1, 1); //Send address of software rev register
+    m_i2c.read(m_addr, echo, 1);    //Read 1 byte echo result
+    if (echo == 0xFF) return false;
+    return true;
+}
+
+/*
+ * Function:    getRange
+ * Args:        void
+ * Returns:     int range
+ * Description: Range in cm. This function should only be called when ranging is finished, otherwise previous value is returned
+ */
+void getRange() {
+    char cmd[2];
+    char echo[2];
+    // Read range over I2C bus
+    cmd[0] = 0x02;                      // Address of first echo
+    m_i2c.write(m_addr, cmd, 1, 1);     // Send address of first echo
+    m_i2c.read(m_addr, echo, 2);        // Read two-byte echo result
+    // Generate PWM mark/space ratio from range data
+    int range = (echo[0]<<8)+echo[1];
+    return range;
+}
+
+/*
  * Function:    readRange
  * Args:        void
  * Returns:     int range
  * Description: Reads the range register and converts it to a usable value
  */
-int SRF08::read() {
+int SRF08::readRange() {
     char cmd[2];
     char echo[2];
 
@@ -82,6 +129,24 @@
 }
 
 /*
+ * Function:    readBufRange
+ * Args:        void
+ * Returns:     int range
+ * Description: Reads the range register, adds it to an array and converts it to a filtered value
+ */
+//int SRF08::readBufRange() {
+
+//}
+
+int SRF08::getAverageBufRange() {
+    int total = 0;
+    for (int i = 0; i < RANGEBUFSIZE; i++) {
+        total += rangeBuf[i];
+    }
+    return (int)total/RANGEBUFSIZE;
+}
+
+/*
  * Function:    setRangeRegister
  * Args:        rangeVal
  * Returns:     void