Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of g3_waterplay by
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(3); 00036 _cmd.write(1); 00037 wait_us(3); 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); 00052 else 00053 return -1; 00054 }
Generated on Tue Jul 12 2022 21:07:58 by
1.7.2
