Wireless project using Xbee
Fork of HC_SR04_Ultrasonic_Library by
Revision 5:c23850f8fb5a, committed 2017-12-06
- Comitter:
- Niranjan_ravi
- Date:
- Wed Dec 06 19:27:12 2017 +0000
- Parent:
- 4:e0f9c9fb4cf3
- Commit message:
- commit changes to the revision history
Changed in this revision
ultrasonic.cpp | Show diff for this revision Revisions of this file |
ultrasonic.h | Show diff for this revision Revisions of this file |
diff -r e0f9c9fb4cf3 -r c23850f8fb5a ultrasonic.cpp --- a/ultrasonic.cpp Mon Nov 24 20:23:07 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,86 +0,0 @@ - #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); - } - }
diff -r e0f9c9fb4cf3 -r c23850f8fb5a ultrasonic.h --- a/ultrasonic.h Mon Nov 24 20:23:07 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,47 +0,0 @@ -#ifndef MBED_ULTRASONIC_H -#define MBED_ULTRASONIC_H - -#include "mbed.h" - -class ultrasonic -{ - public: - /**iniates the class with the specified trigger pin, echo pin, update speed and timeout**/ - ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout); - /**iniates the class with the specified trigger pin, echo pin, update speed, timeout and method to call when the distance changes**/ - ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int)); - /** returns the last measured distance**/ - int getCurrentDistance(void); - /**pauses measuring the distance**/ - void pauseUpdates(void); - /**starts mesuring the distance**/ - void startUpdates(void); - /**attachs the method to be called when the distances changes**/ - void attachOnUpdate(void method(int)); - /**changes the speed at which updates are made**/ - void changeUpdateSpeed(float updateSpeed); - /**gets whether the distance has been changed since the last call of isUpdated() or checkDistance()**/ - int isUpdated(void); - /**gets the speed at which updates are made**/ - float getUpdateSpeed(void); - /**call this as often as possible in your code, eg. at the end of a while(1) loop, - and it will check whether the method you have attached needs to be called**/ - void checkDistance(void); - private: - DigitalOut _trig; - InterruptIn _echo; - Timer _t; - Timeout _tout; - int _distance; - float _updateSpeed; - int start; - int end; - volatile int done; - void (*_onUpdateMethod)(int); - void _startT(void); - void _updateDist(void); - void _startTrig(void); - float _timeout; - int d; -}; -#endif \ No newline at end of file