Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SRF08.cpp
00001 /* 00002 Copyright (c) 2012 Brent Dekker 00003 00004 Permission is hereby granted, free of charge, to any person obtaining a copy 00005 of this software and associated documentation files (the "Software"), to deal 00006 in the Software without restriction, including without limitation the rights 00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 copies of the Software, and to permit persons to whom the Software is 00009 furnished to do so, subject to the following conditions: 00010 00011 The above copyright notice and this permission notice shall be included in 00012 all copies or substantial portions of the Software. 00013 00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 THE SOFTWARE. 00021 */ 00022 00023 #include "SRF08.h" 00024 00025 /* 00026 * Constructor: SRF08 00027 * Args: 00028 * Returns: void 00029 * Description: Creates an instance of the SRF08 to communicate with a sRF08 module 00030 */ 00031 SRF08::SRF08(PinName SDA, PinName SCL, int i2cAddress) : 00032 i2cMod(SDA, SCL), 00033 i2cAddress(i2cAddress), 00034 rangingBusy(false) { 00035 00036 } 00037 00038 /* 00039 * Function: startRanging 00040 * Args: void 00041 * Returns: void 00042 * Description: Sends command to module to start ranging. 00043 */ 00044 void SRF08::startRanging() { 00045 //Create a two byte command. The first first byte is the register address 00046 // on the SRF08 to write to. The second byte is the command which is written 00047 // to that address ("Start ranging in cm" in this case). 00048 const char command[] = {0x00, 0x51}; 00049 i2cMod.write(i2cAddress, command, 2); 00050 this->rangingBusy = true; 00051 Thread::wait(70); 00052 setRangingFinished(); 00053 } 00054 00055 /* 00056 * Function: rangingFinished 00057 * Args: void 00058 * Returns: Bool: whether ranging is finished 00059 * Description: Checks if the ranging process on the module is finished 00060 */ 00061 bool SRF08::rangingFinished() { 00062 if(this->rangingBusy) return false; 00063 return true; 00064 } 00065 00066 /* 00067 * Function: getRange 00068 * Args: void 00069 * Returns: int range 00070 * Description: Range in cm. This function should only be called when ranging is finished, otherwise previous value is returned 00071 */ 00072 int SRF08::getRange() { 00073 while (!rangingFinished() ) 00074 Thread::wait(10); //Wait until ranging is finished 00075 const char command[] = {0x02}; //Address of range register 00076 char response[] = {0x00, 0x00}; 00077 i2cMod.write(i2cAddress, command, 1, 1); //Send command 00078 i2cMod.read(i2cAddress, response, 2); //Read 16bits result 00079 int range = (response[0]<<8)+response[1]; //Shift two bytes into int 00080 return range; //Return int range 00081 } 00082 00083 /* 00084 * Function: readLightIntensity 00085 * Args: void 00086 * Returns: int lightIntensity 00087 * Description: Reads the lightIntensity from the module 00088 * The light intensity is updated if a range command is sent, so don't use 00089 * this function only 00090 */ 00091 int SRF08::getLightIntensity() { 00092 const char command[] = {0x01}; //Light intensity register 00093 char response[] = {0x00}; 00094 i2cMod.write(i2cAddress, command, 1, 1); //Send command 00095 i2cMod.read(i2cAddress, response, 1); //Read response 00096 int lightIntensity = response[0]; 00097 return lightIntensity; 00098 } 00099 00100 /* 00101 * Function: setRangeRegister 00102 * Args: rangeVal 00103 * Returns: void 00104 * Description: Sets the maximum range for which the module waits for an echo 00105 * The range is ((rangeVal x 43mm) + 43mm) 00106 * The max range is about six meters 00107 */ 00108 void SRF08::setRangeRegister(unsigned char rangeVal) { 00109 while (!rangingFinished() ) 00110 Thread::wait(10); //Wait until ranging is finished 00111 char command[] = {0x02, rangeVal}; //Range register 00112 i2cMod.write(i2cAddress, command, 2); //Send command 00113 } 00114 00115 00116 //Function setMaxGainRegister 00117 void SRF08::setMaxGainRegister(unsigned char gainVal) { 00118 while (!rangingFinished() ) 00119 Thread::wait(10); //Wait until ranging is finished 00120 char command[] = {0x01, gainVal}; //Max gain register 00121 i2cMod.write(i2cAddress, command, 2); //Send command 00122 } 00123 00124 /* 00125 * Function: setAddress 00126 * Args: address 00127 * Returns: void 00128 * Description: Sets the address of the module on the I2C bus. The factory default address is 0x0E (224) 00129 * The address can have the following values: 00130 * E0 | E2 | E4 | E6 ... FC | FE 00131 */ 00132 void SRF08::setAddress(int address) { 00133 //Send address change sequence 00134 char command[] = {0x00, 0xA0}; 00135 i2cMod.write(i2cAddress, command, 2); 00136 command[1] = 0xAA; 00137 i2cMod.write(i2cAddress, command, 2); 00138 command[1] = 0xA5; 00139 i2cMod.write(i2cAddress, command, 2); 00140 command[1] = address; 00141 i2cMod.write(i2cAddress, command, 2); 00142 //Save the updated address 00143 i2cAddress = address; 00144 } 00145 00146 //Small helper function for Timeout object 00147 void SRF08::setRangingFinished() { 00148 this->rangingBusy = false; 00149 } 00150
Generated on Mon Jul 18 2022 05:41:29 by
