Library for using HC-SR04 with some API Documentation added

Fork of HC_SR04_Ultrasonic_Library by EJ Teb

Files at this revision

API Documentation at this revision

Comitter:
ejteb
Date:
Mon Nov 24 17:10:44 2014 +0000
Parent:
1:114e0698a5d2
Child:
3:3459c57331e2
Commit message:
Library working with hello world and documentation and tidy code

Changed in this revision

ultrasonic.cpp Show annotated file Show diff for this revision Revisions of this file
ultrasonic.h Show annotated file Show diff for this revision Revisions of this file
--- 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);
+        }
+    }
--- a/ultrasonic.h	Sun Nov 23 20:34:26 2014 +0000
+++ b/ultrasonic.h	Mon Nov 24 17:10:44 2014 +0000
@@ -6,18 +6,27 @@
 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);
-        ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int), void onTrig(void),void onStartTimer(void)/*, Timer *t*/ );
+        /**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));
-        void attachOnTrigger(void method(void));
-        void attachOnStartTimer(void method(void));
+        /**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);
-        float getUpdateSpeed();
+        /**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;
@@ -29,16 +38,10 @@
         int end;
         volatile int done;
         void (*_onUpdateMethod)(int);
-        void (*_onTriggerMethod)(void);
-        void (*_onStartTimerMethod)(void);
         void _startT(void);
         void _updateDist(void);
         void _startTrig(void);
         float _timeout;
-
         int d;
-        /*unsigned int tickcount;
-        Ticker _ticker;
-        void ticktock(void);*/
 };
 #endif
\ No newline at end of file