mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1
elessair 0:f269e3021894 2 /*
elessair 0:f269e3021894 3 Copyright (c) 2010 Chris Styles ( chris dot styles at mbed dot org )
elessair 0:f269e3021894 4
elessair 0:f269e3021894 5 Permission is hereby granted, free of charge, to any person obtaining a copy
elessair 0:f269e3021894 6 of this software and associated documentation files (the "Software"), to deal
elessair 0:f269e3021894 7 in the Software without restriction, including without limitation the rights
elessair 0:f269e3021894 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
elessair 0:f269e3021894 9 copies of the Software, and to permit persons to whom the Software is
elessair 0:f269e3021894 10 furnished to do so, subject to the following conditions:
elessair 0:f269e3021894 11
elessair 0:f269e3021894 12 The above copyright notice and this permission notice shall be included in
elessair 0:f269e3021894 13 all copies or substantial portions of the Software.
elessair 0:f269e3021894 14
elessair 0:f269e3021894 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
elessair 0:f269e3021894 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
elessair 0:f269e3021894 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
elessair 0:f269e3021894 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
elessair 0:f269e3021894 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
elessair 0:f269e3021894 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
elessair 0:f269e3021894 21 THE SOFTWARE.
elessair 0:f269e3021894 22 */
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 #include "SRF08.h"
elessair 0:f269e3021894 25
elessair 0:f269e3021894 26
elessair 0:f269e3021894 27 SRF08::SRF08(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
elessair 0:f269e3021894 28 char cmd[2];
elessair 0:f269e3021894 29
elessair 0:f269e3021894 30 // Set up SRF08 max range and receiver sensitivity over I2C bus
elessair 0:f269e3021894 31 cmd[0] = 0x02; // Range register
elessair 0:f269e3021894 32 cmd[1] = 0x1C; // Set max range about 100cm
elessair 0:f269e3021894 33 m_i2c.write(m_addr, cmd, 2);
elessair 0:f269e3021894 34 cmd[0] = 0x01; // Receiver gain register
elessair 0:f269e3021894 35 cmd[1] = 0x1B; // Set max receiver gain
elessair 0:f269e3021894 36 m_i2c.write(m_addr, cmd, 2);
elessair 0:f269e3021894 37
elessair 0:f269e3021894 38 }
elessair 0:f269e3021894 39
elessair 0:f269e3021894 40 SRF08::~SRF08() {
elessair 0:f269e3021894 41
elessair 0:f269e3021894 42 }
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44 float SRF08::read() {
elessair 0:f269e3021894 45
elessair 0:f269e3021894 46 char cmd[2];
elessair 0:f269e3021894 47 char echo[2];
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49
elessair 0:f269e3021894 50 // Get range data from SRF08
elessair 0:f269e3021894 51 // Send Tx burst command over I2C bus
elessair 0:f269e3021894 52 cmd[0] = 0x00; // Command register
elessair 0:f269e3021894 53 cmd[1] = 0x51; // Ranging results in cm
elessair 0:f269e3021894 54 m_i2c.write(m_addr, cmd, 2); // Send ranging burst
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 wait(0.07); // Wait for return echo
elessair 0:f269e3021894 57
elessair 0:f269e3021894 58 // Read back range over I2C bus
elessair 0:f269e3021894 59 cmd[0] = 0x02; // Address of first echo
elessair 0:f269e3021894 60 m_i2c.write(m_addr, cmd, 1, 1); // Send address of first echo
elessair 0:f269e3021894 61 m_i2c.read(m_addr, echo, 2); // Read two-byte echo result
elessair 0:f269e3021894 62
elessair 0:f269e3021894 63 // Generate PWM mark/space ratio from range data
elessair 0:f269e3021894 64 float range = (echo[0]<<8)+echo[1];
elessair 0:f269e3021894 65
elessair 0:f269e3021894 66 return range;
elessair 0:f269e3021894 67 }