![](/media/cache/profiles/0738069b244a1c43c83112b735140a16.50x50_q85.jpg)
SEEED STUDIO SEN136B5B test (Using PING library in local)
PING.h@0:02a23e3031ee, 2012-09-24 (annotated)
- Committer:
- mio
- Date:
- Mon Sep 24 15:52:56 2012 +0000
- Revision:
- 0:02a23e3031ee
Import PING code to local.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mio | 0:02a23e3031ee | 1 | #ifndef MBED_PING_H |
mio | 0:02a23e3031ee | 2 | #define MBED_PING_H |
mio | 0:02a23e3031ee | 3 | #include "mbed.h" |
mio | 0:02a23e3031ee | 4 | |
mio | 0:02a23e3031ee | 5 | /* Class: PING |
mio | 0:02a23e3031ee | 6 | * Abstraction for the PING Ultrasonic range finder |
mio | 0:02a23e3031ee | 7 | * |
mio | 0:02a23e3031ee | 8 | * Example: |
mio | 0:02a23e3031ee | 9 | * > // Print measured distance |
mio | 0:02a23e3031ee | 10 | * > |
mio | 0:02a23e3031ee | 11 | * > #include "mbed.h" |
mio | 0:02a23e3031ee | 12 | * > #include "PING.h" |
mio | 0:02a23e3031ee | 13 | * > |
mio | 0:02a23e3031ee | 14 | * > PING ping(p9); |
mio | 0:02a23e3031ee | 15 | * > |
mio | 0:02a23e3031ee | 16 | * > int main() { |
mio | 0:02a23e3031ee | 17 | * > while(1) { |
mio | 0:02a23e3031ee | 18 | * > printf("Measured : %.1f\n", ping.read()); |
mio | 0:02a23e3031ee | 19 | * > wait(0.2); |
mio | 0:02a23e3031ee | 20 | * > } |
mio | 0:02a23e3031ee | 21 | * > } |
mio | 0:02a23e3031ee | 22 | */ |
mio | 0:02a23e3031ee | 23 | class PING |
mio | 0:02a23e3031ee | 24 | { |
mio | 0:02a23e3031ee | 25 | // Public functions |
mio | 0:02a23e3031ee | 26 | public: |
mio | 0:02a23e3031ee | 27 | /* Constructor: PING |
mio | 0:02a23e3031ee | 28 | * Create a PING object, connected to the specified pins |
mio | 0:02a23e3031ee | 29 | * |
mio | 0:02a23e3031ee | 30 | * Variables: |
mio | 0:02a23e3031ee | 31 | * trigger - Output to trigger the PING |
mio | 0:02a23e3031ee | 32 | */ |
mio | 0:02a23e3031ee | 33 | PING(PinName trigger); |
mio | 0:02a23e3031ee | 34 | |
mio | 0:02a23e3031ee | 35 | /* Function: read |
mio | 0:02a23e3031ee | 36 | * A non-blocking function that will return the last measurement |
mio | 0:02a23e3031ee | 37 | * |
mio | 0:02a23e3031ee | 38 | * Variables: |
mio | 0:02a23e3031ee | 39 | * returns - floating point representation in cm. |
mio | 0:02a23e3031ee | 40 | */ |
mio | 0:02a23e3031ee | 41 | float read(void); |
mio | 0:02a23e3031ee | 42 | |
mio | 0:02a23e3031ee | 43 | /* Function: read_in |
mio | 0:02a23e3031ee | 44 | * A non-blocking function that will return the last measurement |
mio | 0:02a23e3031ee | 45 | * |
mio | 0:02a23e3031ee | 46 | * Variables: |
mio | 0:02a23e3031ee | 47 | * returns - floating point representation in in. |
mio | 0:02a23e3031ee | 48 | */ |
mio | 0:02a23e3031ee | 49 | float read_in(void); |
mio | 0:02a23e3031ee | 50 | |
mio | 0:02a23e3031ee | 51 | /* Function: operator float |
mio | 0:02a23e3031ee | 52 | * A short hand way of using the read function |
mio | 0:02a23e3031ee | 53 | * |
mio | 0:02a23e3031ee | 54 | * Example: |
mio | 0:02a23e3031ee | 55 | * > float range = ping.read(); |
mio | 0:02a23e3031ee | 56 | * > float range = ping; |
mio | 0:02a23e3031ee | 57 | * > |
mio | 0:02a23e3031ee | 58 | * > if(ping.read() > 0.25) { ... } |
mio | 0:02a23e3031ee | 59 | * > if(ping > 0.25) { ... } |
mio | 0:02a23e3031ee | 60 | */ |
mio | 0:02a23e3031ee | 61 | operator float(); |
mio | 0:02a23e3031ee | 62 | |
mio | 0:02a23e3031ee | 63 | private : |
mio | 0:02a23e3031ee | 64 | DigitalInOut _trigger; |
mio | 0:02a23e3031ee | 65 | Timer _timer; |
mio | 0:02a23e3031ee | 66 | Ticker _ticker; |
mio | 0:02a23e3031ee | 67 | void _startRange(void); |
mio | 0:02a23e3031ee | 68 | float _dist; |
mio | 0:02a23e3031ee | 69 | int _invalidctr; |
mio | 0:02a23e3031ee | 70 | }; |
mio | 0:02a23e3031ee | 71 | |
mio | 0:02a23e3031ee | 72 | #endif |