DP

Dependencies:   FastAnalogIn mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ultrasonic.cpp Source File

ultrasonic.cpp

00001 #include "ultrasonic.h"
00002 #include "threads.h"
00003 
00004 
00005 /*
00006 * Constructor 
00007 */
00008 cUltrasonic::cUltrasonic(PinName pinEcho, PinName pinTrig) : echo(pinEcho), trig(pinTrig) {
00009     echo.rise(this,&cUltrasonic::riseEdge);
00010     echo.fall(this,&cUltrasonic::fallEdge);
00011 }
00012 
00013 
00014 /* 
00015 * This method set trigger
00016 */
00017 void cUltrasonic::setTrig(void) {
00018         trig.write(0);
00019         wait_us(5);
00020         trig.write(1); 
00021         wait_us(10);
00022         trig.write(0);
00023 }
00024 
00025 /* 
00026 * ISR of rising edge of received pulse 
00027 * in this thread the timer starts
00028 */
00029 void cUltrasonic::riseEdge(void) {
00030     timer.start();  
00031 }
00032 
00033 /*
00034 * ISR of falling edge of received pulse
00035 * in this thread is saved width of pulse and signal set to collect thread
00036 */
00037 void cUltrasonic::fallEdge(void) {
00038     timer.stop();
00039     pulseWidth = timer.read_us();
00040     timer.reset();
00041     thread->signal_set(0x01);
00042 }
00043 
00044 /*
00045 * This method returns width  of pulse
00046 */
00047 int cUltrasonic::getPulseWidth(void) {
00048     return pulseWidth;  
00049 }