Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Nucleo_UltrasonicHelloWorld xxx_Sonar-HC-SR04_Lsg
ultrasonic.h@5:6fd0e9c7ead4, 2021-06-10 (annotated)
- Committer:
- JohnnyK
- Date:
- Thu Jun 10 18:45:59 2021 +0000
- Revision:
- 5:6fd0e9c7ead4
- Parent:
- 2:cc1143d36567
- Child:
- 6:9808e2c61247
Update to MbedOS 6+
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ejteb | 0:6aa04a8c8d4c | 1 | #ifndef MBED_ULTRASONIC_H |
ejteb | 0:6aa04a8c8d4c | 2 | #define MBED_ULTRASONIC_H |
ejteb | 0:6aa04a8c8d4c | 3 | |
ejteb | 0:6aa04a8c8d4c | 4 | #include "mbed.h" |
ejteb | 0:6aa04a8c8d4c | 5 | |
ejteb | 0:6aa04a8c8d4c | 6 | class ultrasonic |
ejteb | 0:6aa04a8c8d4c | 7 | { |
ejteb | 0:6aa04a8c8d4c | 8 | public: |
ejteb | 2:cc1143d36567 | 9 | /**iniates the class with the specified trigger pin, echo pin, update speed and timeout**/ |
JohnnyK | 5:6fd0e9c7ead4 | 10 | ultrasonic(PinName trigPin, PinName echoPin, Kernel::Clock::duration_u32 updateSpeed, Kernel::Clock::duration_u32 timeout); |
ejteb | 2:cc1143d36567 | 11 | /**iniates the class with the specified trigger pin, echo pin, update speed, timeout and method to call when the distance changes**/ |
JohnnyK | 5:6fd0e9c7ead4 | 12 | ultrasonic(PinName trigPin, PinName echoPin, Kernel::Clock::duration_u32 updateSpeed, Kernel::Clock::duration_u32 timeout, void onUpdate(int)); |
ejteb | 2:cc1143d36567 | 13 | /** returns the last measured distance**/ |
ejteb | 0:6aa04a8c8d4c | 14 | int getCurrentDistance(void); |
ejteb | 2:cc1143d36567 | 15 | /**pauses measuring the distance**/ |
ejteb | 0:6aa04a8c8d4c | 16 | void pauseUpdates(void); |
ejteb | 2:cc1143d36567 | 17 | /**starts mesuring the distance**/ |
ejteb | 0:6aa04a8c8d4c | 18 | void startUpdates(void); |
ejteb | 2:cc1143d36567 | 19 | /**attachs the method to be called when the distances changes**/ |
ejteb | 0:6aa04a8c8d4c | 20 | void attachOnUpdate(void method(int)); |
ejteb | 2:cc1143d36567 | 21 | /**changes the speed at which updates are made**/ |
JohnnyK | 5:6fd0e9c7ead4 | 22 | void changeUpdateSpeed(Kernel::Clock::duration_u32 updateSpeed); |
ejteb | 2:cc1143d36567 | 23 | /**gets whether the distance has been changed since the last call of isUpdated() or checkDistance()**/ |
ejteb | 0:6aa04a8c8d4c | 24 | int isUpdated(void); |
ejteb | 2:cc1143d36567 | 25 | /**gets the speed at which updates are made**/ |
JohnnyK | 5:6fd0e9c7ead4 | 26 | Kernel::Clock::duration_u32 getUpdateSpeed(void); |
ejteb | 2:cc1143d36567 | 27 | /**call this as often as possible in your code, eg. at the end of a while(1) loop, |
ejteb | 2:cc1143d36567 | 28 | and it will check whether the method you have attached needs to be called**/ |
ejteb | 2:cc1143d36567 | 29 | void checkDistance(void); |
ejteb | 0:6aa04a8c8d4c | 30 | private: |
ejteb | 0:6aa04a8c8d4c | 31 | DigitalOut _trig; |
ejteb | 0:6aa04a8c8d4c | 32 | InterruptIn _echo; |
ejteb | 0:6aa04a8c8d4c | 33 | Timer _t; |
ejteb | 0:6aa04a8c8d4c | 34 | Timeout _tout; |
ejteb | 0:6aa04a8c8d4c | 35 | int _distance; |
JohnnyK | 5:6fd0e9c7ead4 | 36 | Kernel::Clock::duration_u32 _updateSpeed; |
ejteb | 0:6aa04a8c8d4c | 37 | int start; |
ejteb | 0:6aa04a8c8d4c | 38 | int end; |
ejteb | 0:6aa04a8c8d4c | 39 | volatile int done; |
ejteb | 0:6aa04a8c8d4c | 40 | void (*_onUpdateMethod)(int); |
ejteb | 0:6aa04a8c8d4c | 41 | void _startT(void); |
ejteb | 0:6aa04a8c8d4c | 42 | void _updateDist(void); |
ejteb | 0:6aa04a8c8d4c | 43 | void _startTrig(void); |
JohnnyK | 5:6fd0e9c7ead4 | 44 | Kernel::Clock::duration_u32 _timeout; |
ejteb | 0:6aa04a8c8d4c | 45 | int d; |
ejteb | 0:6aa04a8c8d4c | 46 | }; |
ejteb | 0:6aa04a8c8d4c | 47 | #endif |