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