-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPing.cpp
More file actions
54 lines (46 loc) · 1.11 KB
/
Ping.cpp
File metadata and controls
54 lines (46 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "Ping.h"
#include "mbed.h"
Ping::Ping(PinName PING_PIN)
: _event(PING_PIN)
, _cmd(PING_PIN)
, _timer()
{
_event.rise(this,&Ping::_Starts);
_event.fall(this,&Ping::_Stops);
_SPEED_OF_SOUND_CM = 33;
}
void Ping::_Starts(void)
{
_Valid = false; // start the timere, and invalidate the current time.
_Busy = true;
_timer.start();
_Time = _timer.read_us();
}
void Ping::_Stops(void)
{
_Valid = true; // When it stops, update the time
_Busy = false;
_Time = _timer.read_us()-_Time;
}
void Ping::Send()
{
_cmd.output();
_cmd.write(0); // see the ping documentation http://www.parallax.com/Portals/0/Downloads/docs/prod/acc/28015-PING-v1.6.pdf
wait_us(3);
_cmd.write(1);
wait_us(3);
_cmd.write(0);
_cmd.input();
}
void Ping::Set_Speed_of_Sound(int SoS_ms )
{
_SPEED_OF_SOUND_CM = SoS_ms;
}
int Ping::Read_cm()
// -1 means not valid.
{
if(_Valid && ~_Busy)
return ((_Time*_SPEED_OF_SOUND_CM)/1000);
else
return -1;
}