Works with interrupts

Fork of HC_SR04_Ultrasonic_Library by EJ Teb

Committer:
maskas
Date:
Tue Dec 26 09:28:16 2017 +0000
Revision:
5:36fcbbb149d8
Parent:
4:e0f9c9fb4cf3
add missing "start" method to the first constructor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ejteb 0:6aa04a8c8d4c 1 #include "ultrasonic.h"
ejteb 0:6aa04a8c8d4c 2
ejteb 0:6aa04a8c8d4c 3 ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout):_trig(trigPin), _echo(echoPin)
ejteb 0:6aa04a8c8d4c 4 {
ejteb 0:6aa04a8c8d4c 5 _updateSpeed = updateSpeed;
ejteb 0:6aa04a8c8d4c 6 _timeout = timeout;
maskas 5:36fcbbb149d8 7 _t.start ();
ejteb 0:6aa04a8c8d4c 8 }
ejteb 0:6aa04a8c8d4c 9
ejteb 2:cc1143d36567 10 ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int))
ejteb 0:6aa04a8c8d4c 11 :_trig(trigPin), _echo(echoPin)
ejteb 0:6aa04a8c8d4c 12 {
ejteb 0:6aa04a8c8d4c 13 _onUpdateMethod=onUpdate;
ejteb 0:6aa04a8c8d4c 14 _updateSpeed = updateSpeed;
ejteb 0:6aa04a8c8d4c 15 _timeout = timeout;
ejteb 0:6aa04a8c8d4c 16 _t.start ();
ejteb 0:6aa04a8c8d4c 17 }
ejteb 2:cc1143d36567 18 void ultrasonic::_startT()
ejteb 2:cc1143d36567 19 {
maskas 5:36fcbbb149d8 20 //printf("Start2: %d\n", _t.read());
ejteb 2:cc1143d36567 21 if(_t.read()>600)
ejteb 2:cc1143d36567 22 {
ejteb 2:cc1143d36567 23 _t.reset ();
ejteb 2:cc1143d36567 24 }
ejteb 0:6aa04a8c8d4c 25 start = _t.read_us ();
maskas 5:36fcbbb149d8 26 //printf("Start: %d\n", start);
ejteb 0:6aa04a8c8d4c 27 }
ejteb 0:6aa04a8c8d4c 28
ejteb 0:6aa04a8c8d4c 29 void ultrasonic::_updateDist()
ejteb 0:6aa04a8c8d4c 30 {
ejteb 0:6aa04a8c8d4c 31 end = _t.read_us ();
ejteb 0:6aa04a8c8d4c 32 done = 1;
ejteb 0:6aa04a8c8d4c 33 _distance = (end - start)/6;
ejteb 0:6aa04a8c8d4c 34 _tout.detach();
ejteb 2:cc1143d36567 35 _tout.attach(this,&ultrasonic::_startTrig, _updateSpeed);
ejteb 0:6aa04a8c8d4c 36 }
ejteb 0:6aa04a8c8d4c 37 void ultrasonic::_startTrig(void)
ejteb 0:6aa04a8c8d4c 38 {
ejteb 0:6aa04a8c8d4c 39 _tout.detach();
ejteb 0:6aa04a8c8d4c 40 _trig=1;
ejteb 0:6aa04a8c8d4c 41 wait_us(10);
ejteb 0:6aa04a8c8d4c 42 done = 0;
ejteb 0:6aa04a8c8d4c 43 _echo.rise(this,&ultrasonic::_startT);
ejteb 0:6aa04a8c8d4c 44 _echo.fall(this,&ultrasonic::_updateDist);
ejteb 0:6aa04a8c8d4c 45 _echo.enable_irq ();
ejteb 0:6aa04a8c8d4c 46 _tout.attach(this,&ultrasonic::_startTrig,_timeout);
ejteb 2:cc1143d36567 47 _trig=0;
ejteb 0:6aa04a8c8d4c 48 }
ejteb 0:6aa04a8c8d4c 49
ejteb 0:6aa04a8c8d4c 50 int ultrasonic::getCurrentDistance(void)
ejteb 0:6aa04a8c8d4c 51 {
ejteb 0:6aa04a8c8d4c 52 return _distance;
ejteb 0:6aa04a8c8d4c 53 }
ejteb 0:6aa04a8c8d4c 54 void ultrasonic::pauseUpdates(void)
ejteb 0:6aa04a8c8d4c 55 {
ejteb 0:6aa04a8c8d4c 56 _tout.detach();
ejteb 0:6aa04a8c8d4c 57 _echo.rise(NULL);
ejteb 0:6aa04a8c8d4c 58 _echo.fall(NULL);
ejteb 0:6aa04a8c8d4c 59 }
ejteb 0:6aa04a8c8d4c 60 void ultrasonic::startUpdates(void)
ejteb 0:6aa04a8c8d4c 61 {
ejteb 0:6aa04a8c8d4c 62 _startTrig();
ejteb 0:6aa04a8c8d4c 63 }
ejteb 0:6aa04a8c8d4c 64 void ultrasonic::attachOnUpdate(void method(int))
ejteb 0:6aa04a8c8d4c 65 {
ejteb 0:6aa04a8c8d4c 66 _onUpdateMethod = method;
ejteb 0:6aa04a8c8d4c 67 }
ejteb 0:6aa04a8c8d4c 68 void ultrasonic::changeUpdateSpeed(float updateSpeed)
ejteb 0:6aa04a8c8d4c 69 {
ejteb 0:6aa04a8c8d4c 70 _updateSpeed = updateSpeed;
ejteb 0:6aa04a8c8d4c 71 }
ejteb 0:6aa04a8c8d4c 72 float ultrasonic::getUpdateSpeed()
ejteb 0:6aa04a8c8d4c 73 {
ejteb 0:6aa04a8c8d4c 74 return _updateSpeed;
ejteb 0:6aa04a8c8d4c 75 }
ejteb 0:6aa04a8c8d4c 76 int ultrasonic::isUpdated(void)
ejteb 0:6aa04a8c8d4c 77 {
ejteb 4:e0f9c9fb4cf3 78 //printf("%d", done);
ejteb 0:6aa04a8c8d4c 79 d=done;
ejteb 0:6aa04a8c8d4c 80 done = 0;
ejteb 0:6aa04a8c8d4c 81 return d;
ejteb 0:6aa04a8c8d4c 82 }
ejteb 2:cc1143d36567 83 void ultrasonic::checkDistance(void)
ejteb 2:cc1143d36567 84 {
ejteb 2:cc1143d36567 85 if(isUpdated())
ejteb 2:cc1143d36567 86 {
ejteb 2:cc1143d36567 87 (*_onUpdateMethod)(_distance);
ejteb 2:cc1143d36567 88 }
ejteb 2:cc1143d36567 89 }