Manages the sending and reading of pulses via separate trigger and echo pins.
PulseManager.h@0:67bcc374fa16, 2015-08-03 (annotated)
- Committer:
- aagnone3
- Date:
- Mon Aug 03 18:26:05 2015 +0000
- Revision:
- 0:67bcc374fa16
Initial revision.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aagnone3 | 0:67bcc374fa16 | 1 | #ifndef MBED_PULSEMANAGER_H |
aagnone3 | 0:67bcc374fa16 | 2 | #define MBED_PULSEMANAGER_H |
aagnone3 | 0:67bcc374fa16 | 3 | |
aagnone3 | 0:67bcc374fa16 | 4 | #include "mbed.h" |
aagnone3 | 0:67bcc374fa16 | 5 | |
aagnone3 | 0:67bcc374fa16 | 6 | class PulseManager { |
aagnone3 | 0:67bcc374fa16 | 7 | public: |
aagnone3 | 0:67bcc374fa16 | 8 | /** |
aagnone3 | 0:67bcc374fa16 | 9 | * Create a PulseManager object connected to the specified pin |
aagnone3 | 0:67bcc374fa16 | 10 | * @param triggerPin output pin to use for generating a trigger signal |
aagnone3 | 0:67bcc374fa16 | 11 | * @param echoPin input pin to use for measuring an echo signal |
aagnone3 | 0:67bcc374fa16 | 12 | */ |
aagnone3 | 0:67bcc374fa16 | 13 | PulseManager(PinName triggerPin, PinName echoPin); |
aagnone3 | 0:67bcc374fa16 | 14 | |
aagnone3 | 0:67bcc374fa16 | 15 | /** |
aagnone3 | 0:67bcc374fa16 | 16 | * Destructor to free allocated memory |
aagnone3 | 0:67bcc374fa16 | 17 | */ |
aagnone3 | 0:67bcc374fa16 | 18 | ~PulseManager(); |
aagnone3 | 0:67bcc374fa16 | 19 | |
aagnone3 | 0:67bcc374fa16 | 20 | /** |
aagnone3 | 0:67bcc374fa16 | 21 | * Set the value of the trigger pin |
aagnone3 | 0:67bcc374fa16 | 22 | * @param val Value to set, 0 for LOW, otherwise HIGH |
aagnone3 | 0:67bcc374fa16 | 23 | */ |
aagnone3 | 0:67bcc374fa16 | 24 | void write(int val); |
aagnone3 | 0:67bcc374fa16 | 25 | |
aagnone3 | 0:67bcc374fa16 | 26 | /** |
aagnone3 | 0:67bcc374fa16 | 27 | * Send a pulse of a given value for a specified time |
aagnone3 | 0:67bcc374fa16 | 28 | * @param val Value to set, 0 for LOW, otherwise HIGH |
aagnone3 | 0:67bcc374fa16 | 29 | * @param time Length of pulse in microseconds |
aagnone3 | 0:67bcc374fa16 | 30 | */ |
aagnone3 | 0:67bcc374fa16 | 31 | void write_us(int val, int time); |
aagnone3 | 0:67bcc374fa16 | 32 | |
aagnone3 | 0:67bcc374fa16 | 33 | /** |
aagnone3 | 0:67bcc374fa16 | 34 | * Return the length of the next HIGH pulse in microsconds |
aagnone3 | 0:67bcc374fa16 | 35 | */ |
aagnone3 | 0:67bcc374fa16 | 36 | int read_high_us(); |
aagnone3 | 0:67bcc374fa16 | 37 | |
aagnone3 | 0:67bcc374fa16 | 38 | /** |
aagnone3 | 0:67bcc374fa16 | 39 | * Return the length of the next HIGH pulse in microseconds or -1 if longer than timeout |
aagnone3 | 0:67bcc374fa16 | 40 | * @param timeout Time before pulse reading aborts and returns -1, in microseconds |
aagnone3 | 0:67bcc374fa16 | 41 | */ |
aagnone3 | 0:67bcc374fa16 | 42 | int read_high_us(int timeout); |
aagnone3 | 0:67bcc374fa16 | 43 | |
aagnone3 | 0:67bcc374fa16 | 44 | /** |
aagnone3 | 0:67bcc374fa16 | 45 | * Return the length of the next LOW pulse in microsconds |
aagnone3 | 0:67bcc374fa16 | 46 | */ |
aagnone3 | 0:67bcc374fa16 | 47 | int read_low_us(); |
aagnone3 | 0:67bcc374fa16 | 48 | |
aagnone3 | 0:67bcc374fa16 | 49 | /** |
aagnone3 | 0:67bcc374fa16 | 50 | * Return the length of the next LOW pulse in microseconds or -1 if longer than timeout |
aagnone3 | 0:67bcc374fa16 | 51 | * @param timeout Time before pulse reading aborts and returns -1, in microseconds |
aagnone3 | 0:67bcc374fa16 | 52 | */ |
aagnone3 | 0:67bcc374fa16 | 53 | int read_low_us(int timeout); |
aagnone3 | 0:67bcc374fa16 | 54 | |
aagnone3 | 0:67bcc374fa16 | 55 | /** |
aagnone3 | 0:67bcc374fa16 | 56 | * Return the length of the next pulse in microsconds |
aagnone3 | 0:67bcc374fa16 | 57 | */ |
aagnone3 | 0:67bcc374fa16 | 58 | int read_us(); |
aagnone3 | 0:67bcc374fa16 | 59 | |
aagnone3 | 0:67bcc374fa16 | 60 | /** |
aagnone3 | 0:67bcc374fa16 | 61 | * Return the length of the next pulse in microseconds or -1 if longer than timeout |
aagnone3 | 0:67bcc374fa16 | 62 | * @param timeout Time before pulse reading aborts and returns -1, in microseconds |
aagnone3 | 0:67bcc374fa16 | 63 | */ |
aagnone3 | 0:67bcc374fa16 | 64 | int read_us(int timeout); |
aagnone3 | 0:67bcc374fa16 | 65 | |
aagnone3 | 0:67bcc374fa16 | 66 | private: |
aagnone3 | 0:67bcc374fa16 | 67 | |
aagnone3 | 0:67bcc374fa16 | 68 | /** Start value for echo pin to use for detecting a change */ |
aagnone3 | 0:67bcc374fa16 | 69 | int startval; |
aagnone3 | 0:67bcc374fa16 | 70 | /** Timer for measuring pulse width */ |
aagnone3 | 0:67bcc374fa16 | 71 | Timer pulsetime; |
aagnone3 | 0:67bcc374fa16 | 72 | /** Timer for detecting a timeout */ |
aagnone3 | 0:67bcc374fa16 | 73 | Timer runtime; |
aagnone3 | 0:67bcc374fa16 | 74 | /** Pin handle for trigger pin */ |
aagnone3 | 0:67bcc374fa16 | 75 | DigitalOut trigger; |
aagnone3 | 0:67bcc374fa16 | 76 | /** Pin handle for echo pin */ |
aagnone3 | 0:67bcc374fa16 | 77 | DigitalIn echo; |
aagnone3 | 0:67bcc374fa16 | 78 | }; |
aagnone3 | 0:67bcc374fa16 | 79 | |
aagnone3 | 0:67bcc374fa16 | 80 | #endif |