DP
Dependencies: FastAnalogIn mbed-rtos mbed
Fork of dipl_prace_v10 by
Diff: ultrasonic.cpp
- Revision:
- 0:f3b355df6f26
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ultrasonic.cpp Sun Apr 26 13:14:02 2015 +0000 @@ -0,0 +1,49 @@ +#include "ultrasonic.h" +#include "threads.h" + + +/* +* Constructor +*/ +cUltrasonic::cUltrasonic(PinName pinEcho, PinName pinTrig) : echo(pinEcho), trig(pinTrig) { + echo.rise(this,&cUltrasonic::riseEdge); + echo.fall(this,&cUltrasonic::fallEdge); +} + + +/* +* This method set trigger +*/ +void cUltrasonic::setTrig(void) { + trig.write(0); + wait_us(5); + trig.write(1); + wait_us(10); + trig.write(0); +} + +/* +* ISR of rising edge of received pulse +* in this thread the timer starts +*/ +void cUltrasonic::riseEdge(void) { + timer.start(); +} + +/* +* ISR of falling edge of received pulse +* in this thread is saved width of pulse and signal set to collect thread +*/ +void cUltrasonic::fallEdge(void) { + timer.stop(); + pulseWidth = timer.read_us(); + timer.reset(); + thread->signal_set(0x01); +} + +/* +* This method returns width of pulse +*/ +int cUltrasonic::getPulseWidth(void) { + return pulseWidth; +} \ No newline at end of file