Interface library for the Devantech SRF02/SRF08 ultrasonic i2c rangers. Depends on I2cRtosDriver lib!

Files at this revision

API Documentation at this revision

Comitter:
humlet
Date:
Thu Feb 21 23:40:36 2013 +0000
Child:
1:32c4dd194228
Commit message:
first running

Changed in this revision

SRF02_IF.cpp Show annotated file Show diff for this revision Revisions of this file
SRF02_IF.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SRF02_IF.cpp	Thu Feb 21 23:40:36 2013 +0000
@@ -0,0 +1,64 @@
+#include "SRF02_IF.h"
+
+SRF02_IF::SRF02_IF(int adr, I2C& i2c) : m_adr(adr), m_i2c(i2c)
+{}
+
+int SRF02_IF::triggerRanging()
+{
+    const char regNcmd[2]= {0x00,0x52};
+    return m_i2c.write(m_adr, regNcmd, 2);
+}
+
+int SRF02_IF::triggerPing()
+{
+    const char regNcmd[2]= {0x00,0x5C};
+    return m_i2c.write(m_adr, regNcmd, 2);
+}
+
+int SRF02_IF::triggerEchoMeasurement()
+{
+    const char regNcmd[2]= {0x00,0x58};
+    return m_i2c.write(m_adr, regNcmd, 2);
+}
+
+int SRF02_IF::readTransitTime_us()
+{
+    char chk[1]= {0xff};
+    while(chk[0] == 0xff) {
+        m_i2c.write(m_adr, 0x00, 1, 1);
+        m_i2c.read(m_adr, chk, 1);
+    }
+/*   //m_i2c.write(m_adr, 0x00, 1, 1);
+    char eco;
+    do {
+        eco=m_i2c.read(1);
+    } while(eco == 0xff);
+*/
+    const char reg[1] = {0x02};
+    char result[2];
+    m_i2c.write(m_adr, reg, 1, 1);
+    m_i2c.read(m_adr, result, 2);
+
+    return (static_cast<int>(result[0])<<8)| static_cast<int>(result[1]);
+}
+
+void SRF02_IF::resetI2CAdress(int newAddress)
+{
+    char regNcmd[2];
+
+    regNcmd[0]=0x00;
+
+    regNcmd[1]=0xA0;
+    m_i2c.write(m_adr,regNcmd,2);
+
+    regNcmd[1]=0xAA;
+    m_i2c.write(m_adr,regNcmd,2);
+
+    regNcmd[1]=0xA5;
+    m_i2c.write(m_adr,regNcmd,2);
+
+    regNcmd[1]=newAddress;
+    m_i2c.write(m_adr,regNcmd,2);
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SRF02_IF.h	Thu Feb 21 23:40:36 2013 +0000
@@ -0,0 +1,24 @@
+#ifndef SRF02_IF_H
+#define SRF02_IF_H
+
+#include "mbed.h"
+
+class SRF02_IF
+{
+    int m_adr;
+    I2C& m_i2c;
+
+public:
+
+    static const int sonicSpeed = 343;
+
+    SRF02_IF(int adr, I2C& i2c);
+
+    int triggerRanging();
+    int triggerPing();
+    int triggerEchoMeasurement();
+    int readTransitTime_us();
+
+    void resetI2CAdress(int newAddress);
+};
+#endif
\ No newline at end of file