srf08 library working with RTOS from BenderII

Committer:
zatakon
Date:
Tue Aug 11 13:01:45 2015 +0000
Revision:
0:70df4930eee9
???????; ???????; ????????; ???????; ???????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zatakon 0:70df4930eee9 1 /*
zatakon 0:70df4930eee9 2 Copyright (c) 2012 Brent Dekker
zatakon 0:70df4930eee9 3
zatakon 0:70df4930eee9 4 Permission is hereby granted, free of charge, to any person obtaining a copy
zatakon 0:70df4930eee9 5 of this software and associated documentation files (the "Software"), to deal
zatakon 0:70df4930eee9 6 in the Software without restriction, including without limitation the rights
zatakon 0:70df4930eee9 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
zatakon 0:70df4930eee9 8 copies of the Software, and to permit persons to whom the Software is
zatakon 0:70df4930eee9 9 furnished to do so, subject to the following conditions:
zatakon 0:70df4930eee9 10
zatakon 0:70df4930eee9 11 The above copyright notice and this permission notice shall be included in
zatakon 0:70df4930eee9 12 all copies or substantial portions of the Software.
zatakon 0:70df4930eee9 13
zatakon 0:70df4930eee9 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
zatakon 0:70df4930eee9 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
zatakon 0:70df4930eee9 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
zatakon 0:70df4930eee9 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
zatakon 0:70df4930eee9 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
zatakon 0:70df4930eee9 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
zatakon 0:70df4930eee9 20 THE SOFTWARE.
zatakon 0:70df4930eee9 21 */
zatakon 0:70df4930eee9 22
zatakon 0:70df4930eee9 23 #include "SRF08.h"
zatakon 0:70df4930eee9 24
zatakon 0:70df4930eee9 25 /*
zatakon 0:70df4930eee9 26 * Constructor: SRF08
zatakon 0:70df4930eee9 27 * Args:
zatakon 0:70df4930eee9 28 * Returns: void
zatakon 0:70df4930eee9 29 * Description: Creates an instance of the SRF08 to communicate with a sRF08 module
zatakon 0:70df4930eee9 30 */
zatakon 0:70df4930eee9 31 SRF08::SRF08(PinName SDA, PinName SCL, int i2cAddress) :
zatakon 0:70df4930eee9 32 i2cMod(SDA, SCL),
zatakon 0:70df4930eee9 33 i2cAddress(i2cAddress),
zatakon 0:70df4930eee9 34 rangingBusy(false) {
zatakon 0:70df4930eee9 35
zatakon 0:70df4930eee9 36 }
zatakon 0:70df4930eee9 37
zatakon 0:70df4930eee9 38 /*
zatakon 0:70df4930eee9 39 * Function: startRanging
zatakon 0:70df4930eee9 40 * Args: void
zatakon 0:70df4930eee9 41 * Returns: void
zatakon 0:70df4930eee9 42 * Description: Sends command to module to start ranging.
zatakon 0:70df4930eee9 43 */
zatakon 0:70df4930eee9 44 void SRF08::startRanging() {
zatakon 0:70df4930eee9 45 //Create a two byte command. The first first byte is the register address
zatakon 0:70df4930eee9 46 // on the SRF08 to write to. The second byte is the command which is written
zatakon 0:70df4930eee9 47 // to that address ("Start ranging in cm" in this case).
zatakon 0:70df4930eee9 48 const char command[] = {0x00, 0x51};
zatakon 0:70df4930eee9 49 i2cMod.write(i2cAddress, command, 2);
zatakon 0:70df4930eee9 50 this->rangingBusy = true;
zatakon 0:70df4930eee9 51 Thread::wait(70);
zatakon 0:70df4930eee9 52 setRangingFinished();
zatakon 0:70df4930eee9 53 }
zatakon 0:70df4930eee9 54
zatakon 0:70df4930eee9 55 /*
zatakon 0:70df4930eee9 56 * Function: rangingFinished
zatakon 0:70df4930eee9 57 * Args: void
zatakon 0:70df4930eee9 58 * Returns: Bool: whether ranging is finished
zatakon 0:70df4930eee9 59 * Description: Checks if the ranging process on the module is finished
zatakon 0:70df4930eee9 60 */
zatakon 0:70df4930eee9 61 bool SRF08::rangingFinished() {
zatakon 0:70df4930eee9 62 if(this->rangingBusy) return false;
zatakon 0:70df4930eee9 63 return true;
zatakon 0:70df4930eee9 64 }
zatakon 0:70df4930eee9 65
zatakon 0:70df4930eee9 66 /*
zatakon 0:70df4930eee9 67 * Function: getRange
zatakon 0:70df4930eee9 68 * Args: void
zatakon 0:70df4930eee9 69 * Returns: int range
zatakon 0:70df4930eee9 70 * Description: Range in cm. This function should only be called when ranging is finished, otherwise previous value is returned
zatakon 0:70df4930eee9 71 */
zatakon 0:70df4930eee9 72 int SRF08::getRange() {
zatakon 0:70df4930eee9 73 while (!rangingFinished() )
zatakon 0:70df4930eee9 74 Thread::wait(10); //Wait until ranging is finished
zatakon 0:70df4930eee9 75 const char command[] = {0x02}; //Address of range register
zatakon 0:70df4930eee9 76 char response[] = {0x00, 0x00};
zatakon 0:70df4930eee9 77 i2cMod.write(i2cAddress, command, 1, 1); //Send command
zatakon 0:70df4930eee9 78 i2cMod.read(i2cAddress, response, 2); //Read 16bits result
zatakon 0:70df4930eee9 79 int range = (response[0]<<8)+response[1]; //Shift two bytes into int
zatakon 0:70df4930eee9 80 return range; //Return int range
zatakon 0:70df4930eee9 81 }
zatakon 0:70df4930eee9 82
zatakon 0:70df4930eee9 83 /*
zatakon 0:70df4930eee9 84 * Function: readLightIntensity
zatakon 0:70df4930eee9 85 * Args: void
zatakon 0:70df4930eee9 86 * Returns: int lightIntensity
zatakon 0:70df4930eee9 87 * Description: Reads the lightIntensity from the module
zatakon 0:70df4930eee9 88 * The light intensity is updated if a range command is sent, so don't use
zatakon 0:70df4930eee9 89 * this function only
zatakon 0:70df4930eee9 90 */
zatakon 0:70df4930eee9 91 int SRF08::getLightIntensity() {
zatakon 0:70df4930eee9 92 const char command[] = {0x01}; //Light intensity register
zatakon 0:70df4930eee9 93 char response[] = {0x00};
zatakon 0:70df4930eee9 94 i2cMod.write(i2cAddress, command, 1, 1); //Send command
zatakon 0:70df4930eee9 95 i2cMod.read(i2cAddress, response, 1); //Read response
zatakon 0:70df4930eee9 96 int lightIntensity = response[0];
zatakon 0:70df4930eee9 97 return lightIntensity;
zatakon 0:70df4930eee9 98 }
zatakon 0:70df4930eee9 99
zatakon 0:70df4930eee9 100 /*
zatakon 0:70df4930eee9 101 * Function: setRangeRegister
zatakon 0:70df4930eee9 102 * Args: rangeVal
zatakon 0:70df4930eee9 103 * Returns: void
zatakon 0:70df4930eee9 104 * Description: Sets the maximum range for which the module waits for an echo
zatakon 0:70df4930eee9 105 * The range is ((rangeVal x 43mm) + 43mm)
zatakon 0:70df4930eee9 106 * The max range is about six meters
zatakon 0:70df4930eee9 107 */
zatakon 0:70df4930eee9 108 void SRF08::setRangeRegister(unsigned char rangeVal) {
zatakon 0:70df4930eee9 109 while (!rangingFinished() )
zatakon 0:70df4930eee9 110 Thread::wait(10); //Wait until ranging is finished
zatakon 0:70df4930eee9 111 char command[] = {0x02, rangeVal}; //Range register
zatakon 0:70df4930eee9 112 i2cMod.write(i2cAddress, command, 2); //Send command
zatakon 0:70df4930eee9 113 }
zatakon 0:70df4930eee9 114
zatakon 0:70df4930eee9 115
zatakon 0:70df4930eee9 116 //Function setMaxGainRegister
zatakon 0:70df4930eee9 117 void SRF08::setMaxGainRegister(unsigned char gainVal) {
zatakon 0:70df4930eee9 118 while (!rangingFinished() )
zatakon 0:70df4930eee9 119 Thread::wait(10); //Wait until ranging is finished
zatakon 0:70df4930eee9 120 char command[] = {0x01, gainVal}; //Max gain register
zatakon 0:70df4930eee9 121 i2cMod.write(i2cAddress, command, 2); //Send command
zatakon 0:70df4930eee9 122 }
zatakon 0:70df4930eee9 123
zatakon 0:70df4930eee9 124 /*
zatakon 0:70df4930eee9 125 * Function: setAddress
zatakon 0:70df4930eee9 126 * Args: address
zatakon 0:70df4930eee9 127 * Returns: void
zatakon 0:70df4930eee9 128 * Description: Sets the address of the module on the I2C bus. The factory default address is 0x0E (224)
zatakon 0:70df4930eee9 129 * The address can have the following values:
zatakon 0:70df4930eee9 130 * E0 | E2 | E4 | E6 ... FC | FE
zatakon 0:70df4930eee9 131 */
zatakon 0:70df4930eee9 132 void SRF08::setAddress(int address) {
zatakon 0:70df4930eee9 133 //Send address change sequence
zatakon 0:70df4930eee9 134 char command[] = {0x00, 0xA0};
zatakon 0:70df4930eee9 135 i2cMod.write(i2cAddress, command, 2);
zatakon 0:70df4930eee9 136 command[1] = 0xAA;
zatakon 0:70df4930eee9 137 i2cMod.write(i2cAddress, command, 2);
zatakon 0:70df4930eee9 138 command[1] = 0xA5;
zatakon 0:70df4930eee9 139 i2cMod.write(i2cAddress, command, 2);
zatakon 0:70df4930eee9 140 command[1] = address;
zatakon 0:70df4930eee9 141 i2cMod.write(i2cAddress, command, 2);
zatakon 0:70df4930eee9 142 //Save the updated address
zatakon 0:70df4930eee9 143 i2cAddress = address;
zatakon 0:70df4930eee9 144 }
zatakon 0:70df4930eee9 145
zatakon 0:70df4930eee9 146 //Small helper function for Timeout object
zatakon 0:70df4930eee9 147 void SRF08::setRangingFinished() {
zatakon 0:70df4930eee9 148 this->rangingBusy = false;
zatakon 0:70df4930eee9 149 }
zatakon 0:70df4930eee9 150