Mike Spadaru / physcom
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ping.cpp Source File

ping.cpp

00001 #include "ping.h"
00002 #include "mbed.h"
00003 
00004 namespace physcom {
00005     
00006 Ping::Ping(PinName PING_PIN)
00007         : _event(PING_PIN)
00008         , _cmd(PING_PIN)
00009         , _timer()
00010         {
00011             _event.rise(this,&Ping::_Starts);
00012             _event.fall(this,&Ping::_Stops);
00013             _SPEED_OF_SOUND_CM = 33;
00014         } 
00015         
00016 void Ping::_Starts(void)
00017 {
00018       _Valid = false;  // start the timere, and invalidate the current time.
00019       _Busy = true;
00020       _timer.start();
00021       _Time = _timer.read_us();      
00022 }
00023 
00024 void Ping::_Stops(void)
00025 {
00026       _Valid = true;  // When it stops, update the time
00027       _Busy = false;
00028       _Time = _timer.read_us()-_Time;
00029 }
00030 
00031 void Ping::Send()
00032 {
00033      
00034      _cmd.output();
00035      _cmd.write(0);  // see the ping documentation http://www.parallax.com/Portals/0/Downloads/docs/prod/acc/28015-PING-v1.6.pdf
00036      wait_us(3);
00037      _cmd.write(1);
00038      wait_us(3);
00039      _cmd.write(0);
00040      _cmd.input();
00041      
00042 }
00043 void Ping::Set_Speed_of_Sound(int SoS_ms )
00044 {
00045      _SPEED_OF_SOUND_CM = SoS_ms;
00046 }
00047 
00048 int Ping::Read_cm()
00049 // -1 means not valid.
00050 {
00051     if(_Valid && ~_Busy) 
00052         return ((_Time*_SPEED_OF_SOUND_CM)/1000);
00053     else 
00054         return -1;
00055 }
00056 
00057 } // Namespace physcom