prog
hcsr04.h@0:568f3374c9a7, 2019-03-26 (annotated)
- Committer:
- CiroSamu
- Date:
- Tue Mar 26 12:54:18 2019 +0000
- Revision:
- 0:568f3374c9a7
prog
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CiroSamu | 0:568f3374c9a7 | 1 | /* Copyright (c) 2013 Prabhu Desai |
CiroSamu | 0:568f3374c9a7 | 2 | * pdtechworld@gmail.com |
CiroSamu | 0:568f3374c9a7 | 3 | * |
CiroSamu | 0:568f3374c9a7 | 4 | * |
CiroSamu | 0:568f3374c9a7 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
CiroSamu | 0:568f3374c9a7 | 6 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
CiroSamu | 0:568f3374c9a7 | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
CiroSamu | 0:568f3374c9a7 | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
CiroSamu | 0:568f3374c9a7 | 9 | * furnished to do so, subject to the following conditions: |
CiroSamu | 0:568f3374c9a7 | 10 | * |
CiroSamu | 0:568f3374c9a7 | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
CiroSamu | 0:568f3374c9a7 | 12 | * substantial portions of the Software. |
CiroSamu | 0:568f3374c9a7 | 13 | * |
CiroSamu | 0:568f3374c9a7 | 14 | * For more details on the sensor : |
CiroSamu | 0:568f3374c9a7 | 15 | * http://www.elecfreaks.com/store/hcsr04-ultrasonic-sensor-distance-measuring-module-p-91.html?zenid=pgm8pgnvaodbe36dibq5s1soi3 |
CiroSamu | 0:568f3374c9a7 | 16 | * |
CiroSamu | 0:568f3374c9a7 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
CiroSamu | 0:568f3374c9a7 | 18 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
CiroSamu | 0:568f3374c9a7 | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
CiroSamu | 0:568f3374c9a7 | 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
CiroSamu | 0:568f3374c9a7 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
CiroSamu | 0:568f3374c9a7 | 22 | */ |
CiroSamu | 0:568f3374c9a7 | 23 | |
CiroSamu | 0:568f3374c9a7 | 24 | #ifndef MBED_HCSR04_H |
CiroSamu | 0:568f3374c9a7 | 25 | #define MBED_HCSR04_H |
CiroSamu | 0:568f3374c9a7 | 26 | |
CiroSamu | 0:568f3374c9a7 | 27 | #include "mbed.h" |
CiroSamu | 0:568f3374c9a7 | 28 | |
CiroSamu | 0:568f3374c9a7 | 29 | /** HCSR04 Class(es) |
CiroSamu | 0:568f3374c9a7 | 30 | */ |
CiroSamu | 0:568f3374c9a7 | 31 | |
CiroSamu | 0:568f3374c9a7 | 32 | class HCSR04 |
CiroSamu | 0:568f3374c9a7 | 33 | { |
CiroSamu | 0:568f3374c9a7 | 34 | public: |
CiroSamu | 0:568f3374c9a7 | 35 | /** Create a HCSR04 object connected to the specified pin |
CiroSamu | 0:568f3374c9a7 | 36 | * @param pin i/o pin to connect to |
CiroSamu | 0:568f3374c9a7 | 37 | */ |
CiroSamu | 0:568f3374c9a7 | 38 | HCSR04(PinName TrigPin,PinName EchoPin); |
CiroSamu | 0:568f3374c9a7 | 39 | ~HCSR04(); |
CiroSamu | 0:568f3374c9a7 | 40 | |
CiroSamu | 0:568f3374c9a7 | 41 | /** Return the distance from obstacle in cm |
CiroSamu | 0:568f3374c9a7 | 42 | * @param distance in cms and returns -1, in case of failure |
CiroSamu | 0:568f3374c9a7 | 43 | */ |
CiroSamu | 0:568f3374c9a7 | 44 | unsigned int get_dist_cm(void); |
CiroSamu | 0:568f3374c9a7 | 45 | /** Return the pulse duration equal to sonic waves travelling to obstacle and back to receiver. |
CiroSamu | 0:568f3374c9a7 | 46 | * @param pulse duration in microseconds. |
CiroSamu | 0:568f3374c9a7 | 47 | */ |
CiroSamu | 0:568f3374c9a7 | 48 | unsigned int get_pulse_us(void); |
CiroSamu | 0:568f3374c9a7 | 49 | /** Generates the trigger pulse of 10us on the trigger PIN. |
CiroSamu | 0:568f3374c9a7 | 50 | */ |
CiroSamu | 0:568f3374c9a7 | 51 | void start(void ); |
CiroSamu | 0:568f3374c9a7 | 52 | void isr_rise(void); |
CiroSamu | 0:568f3374c9a7 | 53 | void isr_fall(void); |
CiroSamu | 0:568f3374c9a7 | 54 | void fall (void (*fptr)(void)); |
CiroSamu | 0:568f3374c9a7 | 55 | void rise (void (*fptr)(void)); |
CiroSamu | 0:568f3374c9a7 | 56 | float filter (int* measure, int n); |
CiroSamu | 0:568f3374c9a7 | 57 | |
CiroSamu | 0:568f3374c9a7 | 58 | private: |
CiroSamu | 0:568f3374c9a7 | 59 | |
CiroSamu | 0:568f3374c9a7 | 60 | Timer pulsetime; |
CiroSamu | 0:568f3374c9a7 | 61 | DigitalOut trigger; |
CiroSamu | 0:568f3374c9a7 | 62 | InterruptIn echo; |
CiroSamu | 0:568f3374c9a7 | 63 | unsigned int pulsedur; |
CiroSamu | 0:568f3374c9a7 | 64 | unsigned int distance; |
CiroSamu | 0:568f3374c9a7 | 65 | float value; |
CiroSamu | 0:568f3374c9a7 | 66 | |
CiroSamu | 0:568f3374c9a7 | 67 | }; |
CiroSamu | 0:568f3374c9a7 | 68 | |
CiroSamu | 0:568f3374c9a7 | 69 | #endif |