CatPot 2015-2016 / Ping

Dependents:   CatPot_2v00_T_Sub CatPot_2v10_T_Sub CatPot_2v20_T_Sub CatPot_2v30_T_Sub

Fork of Ping by Joel Rosiene

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ping.cpp Source File

Ping.cpp

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