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:
2:ca82f89f415d
Parent:
1:76fb116fa28d
Child:
4:a11bd4ea3c18
--- a/SRF08.h	Tue Jul 10 08:48:02 2012 +0000
+++ b/SRF08.h	Wed Jul 11 07:53:50 2012 +0000
@@ -1,6 +1,5 @@
-
 /*
-Copyright (c) 2010 Chris Styles (chris dot styles at mbed dot org)
+Copyright (c) 2012 Brent Dekker
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
@@ -26,108 +25,90 @@
 
 #include "mbed.h"
 
-#define RANGEBUFSIZE 5
-
-/*
- * The SRF08 is an Ultrasonic range finder, with an I2C interface that allows the measurement to be read directly in centimetres
+/**
+ * The SRF08 is an ultrasonic range finder with an I2C interface that allows 
+ * the measurement to be read directly in centimetres. More information can be
+ * found on this website: http://www.robot-electronics.co.uk/htm/srf08tech.shtml
  */
 class SRF08 {
 
 public:
 
-    /*
-     * Constructor: SRF08
-     * Args:        PinName sda: Data pin of I2C bus to which module is connected
-     *              PinName scl: Clock pin of I2C bus to which module is connected
-     *              int addr:    address of module on the I2C bus
-     * Returns:     void
-     * Description: Creates an instance of the SRF08 to communicate with a sRF08 module
+    /**
+     * Create a SRF08 object connected to the specified I2C pins and address
+     *
+     * @param SDA I2C SDA pin to connect to
+     * @param SCL I2C SCL pin to connect to
+     * @param i2cAddress Address of WSRF08 on I2C bus
      */
-    SRF08(PinName sda, PinName scl, int addr);
-
-    /*
-     * Destructor:  ~SRF08
-     * Args:        void
-     * Returns:     void
-     * Description: Destroys instance of SRF08 class
-     */
-    ~SRF08();
+    SRF08(PinName SDA, PinName SCL, int i2cAddress);
     
-    /* 
-     * Function:    startRanging
-     * Args:        void
-     * Returns:     void
-     * Description: Sends command to module to start ranging.
+    /**
+     * Send the "Start ranging in cm" command via I2C
      */
     void startRanging();
     
-    /* 
-     * Function:    rangingFinished
-     * Args:        void
-     * Returns:     Bool: whether ranging is finished
-     * Description: Checks if the ranging process on the module is finished
+    /**
+     * Checks if the module has finished ranging
+     *
+     * @param returns Boolean stating module is finished or not
      */
     bool rangingFinished();
     
-    /* 
-     * 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();
-    
-    /*
-     * Function:    readRange
-     * Args:        void
-     * Returns:     int range
-     * Description: Reads the range register and converts it to a usable value
+    /**
+     * Gets the measured range from the module
+     *
+     * @param returns Integer range in centimetre
      */
-    int readRange();
+    int getRange();
     
-    /*
-     * Function:    readBufRange
-     * Args:        void
-     * Returns:     int range
-     * Description: Reads the range register, adds it to an array and converts it to a filtered value
+    /**
+     * Gets the measured light intensity from the module
+     *
+     * @param returns A normalised number 0-255 representing dark to light
      */
-    //int readBufRange();
-    
-    /*
-     * Function:    readLightIntensity
-     * Args:        void
-     * Returns:     int lightIntensity
-     * Description: Reads the lightIntensity from the module
+     int getLightIntensity();
+     
+    /**
+     * Sets the range register of the SRF08 for faster ranging.
+     * 
+     * The max range is ((rangeVal x 43mm) + 43mm). The sensors maximum range
+     *  is about six metres
+     *
+     * @param rangeVal The value written to the range register of the SRF08
      */
-     int readLightIntensity();
+    void setRangeRegister(unsigned char rangeVal);
      
-    /*
-     * Function:    setRangeRegister
-     * Args:        rangeVal
-     * Returns:     void
-     * Description: Sets the maximum range for which the module waits for an echo
-     *              The range is ((rangeVal x 43mm) + 43mm)
-     *              The max range is about six meters
+    /**
+     * Sets the max gain register of the SRF08.
+     * 
+     * @param gainVal The value written to the max gain register of the SRF08
      */
-    void setRangeRegister(char rangeVal);
+    void setMaxGainRegister(unsigned char gainVal);
     
-    /*
-     * Function:    setAddress
-     * Args:        address
-     * Returns:     void
-     * Description: Sets the address of the module on the I2C bus. The factory default address is 0x0E (224)
-     *              The address can have the following values:
-     *                 E0 | E2 | E4 | E6 ... FC | FE
+    /**
+     * Changes the I2C address of the SRF08.
+     *
+     * The factory default address is 0x0E (224)
+     *  The address can have the following values:
+     *   E0 | E2 | E4 | E6 ... FC | FE
+     * 
+     * @param i2cAddress The new I2C address for the SRF08. 
      */
-    void setAddress(char address);
+    void setAddress(int i2cAddress);
      
+protected:
+
+    I2C i2cMod;
+    unsigned char i2cAddress;
+    Timeout rangeTimeout;
+    bool rangingBusy;
+    
+    void setRangingFinished();
+    
 private:
-    I2C m_i2c;
-    int m_addr;
-    int rangeBuf[RANGEBUFSIZE];
-    int rangeBufIndex;
-    
-    int getAverageBufRange(void);
+
+    Serial debugPC;
 };
 
-#endif
+#endif
\ No newline at end of file