SRF02 Ultrasonic Ranger
Dependents: DISCO-F746NG_SRF02_2
SRF02.cpp
- Committer:
- yangcq88517
- Date:
- 2014-07-31
- Revision:
- 0:adf5a063372d
File content as of revision 0:adf5a063372d:
#include "SRF02.h" SRF02::SRF02(uint8_t I2C_address,PinName sda, PinName scl): i2c_bus(sda,scl) { deviceAddess = I2C_address; } int SRF02::GetValue(uint8_t command, uint8_t mode) { char w[2]= {COMMAND_REGISTER,command}; if (i2c_bus.write(deviceAddess,w ,2)>=0) wait(DefaultWait); else return -1; char r[1] = {mode}; if (i2c_bus.write(deviceAddess,r ,1)>=0) if (i2c_bus.read(deviceAddess, w, 2)>=0) return (w[0] << 8) | w[1]; return -1; } int SRF02::GetMinimumRange() { return GetValue(REAL_RANGING_CENTIMETERS,Autotune); } int SRF02::GetCentimeters() { return GetValue(REAL_RANGING_CENTIMETERS,Range); } int SRF02::GetInches() { return GetValue(REAL_RANGING_INCHES,Range); } int SRF02::GetMicroSeconds() { return GetValue(REAL_RANGING_SECONDS,Range); } bool SRF02::ChangeAddress(uint8_t newAddress) { char w[2]= {COMMAND_REGISTER,0xA0}; if (i2c_bus.write(deviceAddess,w ,2)>=0) w[1] = 0xAA; else return false; if (i2c_bus.write(deviceAddess,w ,2)>=0) w[1] = 0xA5; else return false; if (i2c_bus.write(deviceAddess,w ,2)>=0) w[1] = newAddress; else return false; if (i2c_bus.write(deviceAddess,w ,2)>=0) { deviceAddess = newAddress; return true; } return false; }