Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HC_SR04_Ultrasonic_Library by
ultrasonic.cpp
- Committer:
- ejteb
- Date:
- 2014-11-24
- Revision:
- 3:3459c57331e2
- Parent:
- 2:cc1143d36567
- Child:
- 4:e0f9c9fb4cf3
File content as of revision 3:3459c57331e2:
 #include "ultrasonic.h"
    
    ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout):_trig(trigPin), _echo(echoPin)
    {
        _updateSpeed = updateSpeed;
        _timeout = timeout;
    }
    
    ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int))
    :_trig(trigPin), _echo(echoPin)
    {
        _onUpdateMethod=onUpdate;
        _updateSpeed = updateSpeed;
        _timeout = timeout;
        _t.start ();
    }
    void ultrasonic::_startT()
    { 
        if(_t.read()>600)
        {
            _t.reset ();
        }
        start = _t.read_us ();
    }
        
    void ultrasonic::_updateDist()
    {
        end = _t.read_us ();
        done = 1;
        _distance = (end - start)/6;       
        _tout.detach();
        _tout.attach(this,&ultrasonic::_startTrig, _updateSpeed);   
    }
    void ultrasonic::_startTrig(void)
    {
            _tout.detach();
            _trig=1;             
            wait_us(10);        
            done = 0;            
            _echo.rise(this,&ultrasonic::_startT);   
            _echo.fall(this,&ultrasonic::_updateDist);
            _echo.enable_irq ();
            _tout.attach(this,&ultrasonic::_startTrig,_timeout);
            _trig=0;                     
    }
    
    int ultrasonic::getCurrentDistance(void)
    {
        return _distance;
    }
    void ultrasonic::pauseUpdates(void)
    {
        _tout.detach();
        _echo.rise(NULL);
        _echo.fall(NULL);
    }
    void ultrasonic::startUpdates(void)
    {
        _startTrig();
    }
    void ultrasonic::attachOnUpdate(void method(int))
    {
        _onUpdateMethod = method;
    }
    void ultrasonic::changeUpdateSpeed(float updateSpeed)
    {
        _updateSpeed = updateSpeed;
    }
    float ultrasonic::getUpdateSpeed()
    {
        return _updateSpeed;
    }
    int ultrasonic::isUpdated(void)
    {
        printf("%d", done);
        d=done;
        done = 0;
        return d;
    }
    void ultrasonic::checkDistance(void)
    {
        if(isUpdated())
        {
            (*_onUpdateMethod)(_distance);
        }
    }
            
    