Works with interrupts

Dependents:   Nucleo_UltrasonicHelloWorld Proj Nucleo_UltrasonicHelloWorld mbedTurtleCar ... more

Revision:
2:cc1143d36567
Parent:
0:6aa04a8c8d4c
Child:
3:3459c57331e2
diff -r 114e0698a5d2 -r cc1143d36567 ultrasonic.cpp
--- a/ultrasonic.cpp	Sun Nov 23 20:34:26 2014 +0000
+++ b/ultrasonic.cpp	Mon Nov 24 17:10:44 2014 +0000
@@ -2,54 +2,38 @@
     
     ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout):_trig(trigPin), _echo(echoPin)
     {
-        printf("ultrasonic (wo methods)\r\n");
         _updateSpeed = updateSpeed;
         _timeout = timeout;
     }
     
-    ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int), void onTrig(void),void onStartTimer(void)/*, Timer *t*/ )
+    ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int))
     :_trig(trigPin), _echo(echoPin)
     {
-        printf("ultrasonic(with methods)\r\n");
         _onUpdateMethod=onUpdate;
-        _onTriggerMethod=onTrig;
-        _onStartTimerMethod=onStartTimer;
         _updateSpeed = updateSpeed;
         _timeout = timeout;
         _t.start ();
-        //_ticker.attach_us(this,&ultrasonic::ticktock, 10);
-        //_t = t;
     }
-    void ultrasonic::_startT(){ 
-        //_t.reset ();
+    void ultrasonic::_startT()
+    { 
+        if(_t.read()>600)
+        {
+            _t.reset ();
+        }
         start = _t.read_us ();
-//        _t.start();  
-        (*_onStartTimerMethod)();            
-       // printf("_startT %d\r\n", tickcount);
-
-        return;
     }
         
     void ultrasonic::_updateDist()
     {
         end = _t.read_us ();
         done = 1;
-        //_t.stop();
-       // printf("_t.read_us()=%d\r\n", _t.read_us());
-       // printf("_t.read() = %f\r\n", _t.read());
-      //  printf("_t.read_us()=%d\r\n", _t.read_us());
-      //  printf("_t.read() = %f\r\n", _t.read());
         _distance = (end - start)/6;       
-         
-        //_t.reset();
         _tout.detach();
-        _tout.attach(this,&ultrasonic::_startTrig, _updateSpeed);
-        (*_onUpdateMethod)(_distance);          
+        _tout.attach(this,&ultrasonic::_startTrig, _updateSpeed);   
     }
     void ultrasonic::_startTrig(void)
     {
             _tout.detach();
-            //printf("_startTrig\r\n");
             _trig=1;             
             wait_us(10);        
             done = 0;            
@@ -57,50 +41,27 @@
             _echo.fall(this,&ultrasonic::_updateDist);
             _echo.enable_irq ();
             _tout.attach(this,&ultrasonic::_startTrig,_timeout);
-            _trig=0;   
-
-                      
+            _trig=0;                     
     }
     
     int ultrasonic::getCurrentDistance(void)
     {
-        //printf("getCurrDistance\r\n");
         return _distance;
     }
     void ultrasonic::pauseUpdates(void)
     {
-        //printf("pauseUpdates\r\n");
         _tout.detach();
         _echo.rise(NULL);
         _echo.fall(NULL);
     }
     void ultrasonic::startUpdates(void)
     {
-        //printf("startUpdates\r\n");
         _startTrig();
- /*       while (1)
-        {
-        
-                    while (done == 0)
-            {
-            }
- //       printf("_updateDist- t=%f, %d\r\n", _t.read(), start);
- //       printf("_updateDist- t=%d, %d\r\n", end, start);
-        printf("_updateDist- dist=%d\r\n", _distance);
-        }*/
     }
     void ultrasonic::attachOnUpdate(void method(int))
     {
         _onUpdateMethod = method;
     }
-    void ultrasonic::attachOnTrigger(void method(void))
-    {
-        _onTriggerMethod = method;
-    }
-    void ultrasonic::attachOnStartTimer(void method(void))
-    {
-        _onStartTimerMethod = method;
-    }
     void ultrasonic::changeUpdateSpeed(float updateSpeed)
     {
         _updateSpeed = updateSpeed;
@@ -109,14 +70,16 @@
     {
         return _updateSpeed;
     }
-    
-    /*void ultrasonic::ticktock(void)
-    {
-        tickcount += 1;
-    }*/
     int ultrasonic::isUpdated(void)
     {
         d=done;
         done = 0;
         return d;
     }
+    void ultrasonic::checkDistance(void)
+    {
+        if(isUpdated())
+        {
+            (*_onUpdateMethod)(_distance);
+        }
+    }