RFSRF05.cpp

Committer:
madcowswe
Date:
2012-03-19
Revision:
0:525cb1ac9f1f

File content as of revision 0:525cb1ac9f1f:

/* mbed SRF05 Ultrasonic Rangefiner Library
 * Copyright (c) 2007-2010, cstyles, sford
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "RFSRF05.h"
#include "mbed.h"

RFSRF05::RFSRF05(PinName trigger,
                 PinName echo0,
                 PinName echo1,
                 PinName echo2,
                 PinName echo3,
                 PinName echo4,
                 PinName echo5,
                 PinName SDI,
                 PinName SDO,
                 PinName SCK,
                 PinName NCS,
                 PinName NIRQ)
        : _trigger(trigger),
        _echo0(echo0),
        _echo1(echo1),
        _echo2(echo2),
        _echo3(echo3),
        _echo4(echo4),
        _echo5(echo5),
        _rf(SDI,SDO,SCK,NCS,NIRQ) {


    // initialises codes
    _code[0] = CODE0;
    _code[1] = CODE1;
    _code[2] = CODE2;

    //set callback execute to true
    ValidPulse = true;

    // Attach interrupts
    _echo0.rise(this, &RFSRF05::_rising);
    _echo0.fall(this, &RFSRF05::_falling);
    _echo1.fall(this, &RFSRF05::_falling);
    _echo2.fall(this, &RFSRF05::_falling);
    _echo3.fall(this, &RFSRF05::_falling);
    _echo4.fall(this, &RFSRF05::_falling);
    _echo5.fall(this, &RFSRF05::_falling);

    //init callabck function
    callbackfunc = NULL;

    // innitialises beacon counter
    _beacon_counter = 0;

    //Interrupts every 50ms
    _ticker.attach(this, &RFSRF05::_startRange, 0.05);
}

void RFSRF05::_startRange() {

    // increments counter
    _beacon_counter = (_beacon_counter + 1) % 3;

    // writes code to RF port
    _rf.write(_code[_beacon_counter]);

    // send a trigger pulse, 10uS long
    ValidPulse = false;
    
    _trigger = 1;
    wait_us (10);
    _trigger = 0;
}

// Clear and start the timer at the begining of the echo pulse
void RFSRF05::_rising(void) {

    _timer.reset();
    _timer.start();
    
    //Set callback execute to ture
    ValidPulse = true;
}

// Stop and read the timer at the end of the pulse
void RFSRF05::_falling(void) {
    _timer.stop();

    if (ValidPulse) {
        ValidPulse = false;
    
        //Calucate distance
        _dist[_beacon_counter] =  _timer.read_us()/29.0;
    
        if (callbackfunc)
            (*callbackfunc)(_beacon_counter, _dist[_beacon_counter]);

    }
    
}

float RFSRF05::read0() {
    // returns distance
    return (_dist[0]);
}

float RFSRF05::read1() {
    // returns distance
    return (_dist[1]);
}

float RFSRF05::read2() {
    // returns distance
    return (_dist[2]);
}

//SRF05::operator float() {
//    return read();
//}