diff options
author | neoraider <devnull@localhost> | 2007-04-16 00:01:04 +0200 |
---|---|---|
committer | neoraider <devnull@localhost> | 2007-04-16 00:01:04 +0200 |
commit | d02fbc84105ff2da74f03fd658ace8919e3e9437 (patch) | |
tree | a9ab5298ca9584e7eabc0bcb6d8b34ca1ad55726 /Srf10.cpp | |
parent | ccc0183cee7c410d668a0b1a9153b061a4785e42 (diff) | |
download | rc2007-rescue-d02fbc84105ff2da74f03fd658ace8919e3e9437.tar rc2007-rescue-d02fbc84105ff2da74f03fd658ace8919e3e9437.zip |
LineSensorArray + Srf10 added.
Diffstat (limited to 'Srf10.cpp')
-rw-r--r-- | Srf10.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Srf10.cpp b/Srf10.cpp new file mode 100644 index 0000000..329fc8a --- /dev/null +++ b/Srf10.cpp @@ -0,0 +1,60 @@ +#include "Srf10.h" + +#include "i2c.h" + + +Srf10::Srf10(uint8_t id) +{ + this->id = id; + + gain = 16; + range = 255; + + unit = Inches; + + distance = 0; + + firmware = readFirmware(); +} + +int Srf10::readFirmware() { + uint8_t byte; + + if(!I2CSendByte(id, 0)) return -1; + if(!I2CRecvByte(id, &byte)) return -1; + + return byte; +} + +bool Srf10::setGain(uint8_t gain) { + uint8_t data[2] = {1, gain}; + + if(!I2CSend(id, data, 2)) return false; + + this->gain = gain; + return true; +} + +bool Srf10::setRange(uint8_t range) { + uint8_t data[2] = {2, range}; + + if(!I2CSend(id, data, 2)) return false; + + this->range = range; + return true; +} + +long Srf10::updateDistance() { + uint8_t data[2] = {0, unit}; + + if(!I2CSend(id, data, 2)) return -1; + + while(readFirmware() != 0xFF); + + if(!I2CSendByte(id, 2)) return -1; + if(!I2CRecv(id, data, 2)) return -1; + + distance = (((uint16_t)data[0]) << 8) | data[1]; + + return distance; +} |