Mario Simaremare
/
g3_waterplay
Salinity and temperature sensors are implemented in classes.
Ping.cpp@15:e112bab9aa2f, 2016-06-28 (annotated)
- Committer:
- mariosimaremare
- Date:
- Tue Jun 28 12:30:40 2016 +0000
- Revision:
- 15:e112bab9aa2f
- Parent:
- 7:46e65aeb4df2
Using new boxes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mariosimaremare | 7:46e65aeb4df2 | 1 | #include "Ping.h" |
mariosimaremare | 7:46e65aeb4df2 | 2 | |
mariosimaremare | 7:46e65aeb4df2 | 3 | #include "mbed.h" |
mariosimaremare | 7:46e65aeb4df2 | 4 | |
mariosimaremare | 7:46e65aeb4df2 | 5 | Ping::Ping(PinName PING_PIN) |
mariosimaremare | 7:46e65aeb4df2 | 6 | : _event(PING_PIN) |
mariosimaremare | 7:46e65aeb4df2 | 7 | , _cmd(PING_PIN) |
mariosimaremare | 7:46e65aeb4df2 | 8 | , _timer() |
mariosimaremare | 7:46e65aeb4df2 | 9 | { |
mariosimaremare | 7:46e65aeb4df2 | 10 | _event.rise(this,&Ping::_Starts); |
mariosimaremare | 7:46e65aeb4df2 | 11 | _event.fall(this,&Ping::_Stops); |
mariosimaremare | 7:46e65aeb4df2 | 12 | _SPEED_OF_SOUND_CM = 33; |
mariosimaremare | 7:46e65aeb4df2 | 13 | } |
mariosimaremare | 7:46e65aeb4df2 | 14 | |
mariosimaremare | 7:46e65aeb4df2 | 15 | void Ping::_Starts(void) |
mariosimaremare | 7:46e65aeb4df2 | 16 | { |
mariosimaremare | 7:46e65aeb4df2 | 17 | _Valid = false; // start the timere, and invalidate the current time. |
mariosimaremare | 7:46e65aeb4df2 | 18 | _Busy = true; |
mariosimaremare | 7:46e65aeb4df2 | 19 | _timer.start(); |
mariosimaremare | 7:46e65aeb4df2 | 20 | _Time = _timer.read_us(); |
mariosimaremare | 7:46e65aeb4df2 | 21 | } |
mariosimaremare | 7:46e65aeb4df2 | 22 | |
mariosimaremare | 7:46e65aeb4df2 | 23 | void Ping::_Stops(void) |
mariosimaremare | 7:46e65aeb4df2 | 24 | { |
mariosimaremare | 7:46e65aeb4df2 | 25 | _Valid = true; // When it stops, update the time |
mariosimaremare | 7:46e65aeb4df2 | 26 | _Busy = false; |
mariosimaremare | 7:46e65aeb4df2 | 27 | _Time = _timer.read_us()-_Time; |
mariosimaremare | 7:46e65aeb4df2 | 28 | } |
mariosimaremare | 7:46e65aeb4df2 | 29 | |
mariosimaremare | 7:46e65aeb4df2 | 30 | void Ping::Send() |
mariosimaremare | 7:46e65aeb4df2 | 31 | { |
mariosimaremare | 7:46e65aeb4df2 | 32 | |
mariosimaremare | 7:46e65aeb4df2 | 33 | _cmd.output(); |
mariosimaremare | 7:46e65aeb4df2 | 34 | _cmd.write(0); // see the ping documentation http://www.parallax.com/Portals/0/Downloads/docs/prod/acc/28015-PING-v1.6.pdf |
mariosimaremare | 7:46e65aeb4df2 | 35 | wait_us(3); |
mariosimaremare | 7:46e65aeb4df2 | 36 | _cmd.write(1); |
mariosimaremare | 7:46e65aeb4df2 | 37 | wait_us(3); |
mariosimaremare | 7:46e65aeb4df2 | 38 | _cmd.write(0); |
mariosimaremare | 7:46e65aeb4df2 | 39 | _cmd.input(); |
mariosimaremare | 7:46e65aeb4df2 | 40 | |
mariosimaremare | 7:46e65aeb4df2 | 41 | } |
mariosimaremare | 7:46e65aeb4df2 | 42 | void Ping::Set_Speed_of_Sound(int SoS_ms ) |
mariosimaremare | 7:46e65aeb4df2 | 43 | { |
mariosimaremare | 7:46e65aeb4df2 | 44 | _SPEED_OF_SOUND_CM = SoS_ms; |
mariosimaremare | 7:46e65aeb4df2 | 45 | } |
mariosimaremare | 7:46e65aeb4df2 | 46 | |
mariosimaremare | 7:46e65aeb4df2 | 47 | int Ping::Read_cm() |
mariosimaremare | 7:46e65aeb4df2 | 48 | // -1 means not valid. |
mariosimaremare | 7:46e65aeb4df2 | 49 | { |
mariosimaremare | 7:46e65aeb4df2 | 50 | if(_Valid && ~_Busy) |
mariosimaremare | 7:46e65aeb4df2 | 51 | return ((_Time*_SPEED_OF_SOUND_CM)/1000); |
mariosimaremare | 7:46e65aeb4df2 | 52 | else |
mariosimaremare | 7:46e65aeb4df2 | 53 | return -1; |
mariosimaremare | 7:46e65aeb4df2 | 54 | } |