Make noise with a piezo buzzer. Use a pwm pin. IO expander ints done.
Fork of beep by
beep.h@6:936ba3699e47, 2016-09-23 (annotated)
- Committer:
- jurica238814
- Date:
- Fri Sep 23 12:12:16 2016 +0000
- Revision:
- 6:936ba3699e47
- Parent:
- 5:49c961e79a12
IO expander ints done.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 0:18e4a9c978ec | 1 | #ifndef MBED_BEEP_H |
dreschpe | 0:18e4a9c978ec | 2 | #define MBED_BEEP_H |
dreschpe | 0:18e4a9c978ec | 3 | |
dreschpe | 0:18e4a9c978ec | 4 | #include "mbed.h" |
dreschpe | 0:18e4a9c978ec | 5 | |
jurica238814 | 5:49c961e79a12 | 6 | #define isdigit(n) (n >= '0' && n <= '9') |
jurica238814 | 5:49c961e79a12 | 7 | #define OCTAVE_OFFSET 0 |
jurica238814 | 5:49c961e79a12 | 8 | |
dreschpe | 1:ddccf0a4a414 | 9 | /** class to make sound with a buzzer, based on a PwmOut |
dreschpe | 3:5a8242af60ba | 10 | * The class use a timeout to switch off the sound - it is not blocking while making noise |
dreschpe | 3:5a8242af60ba | 11 | * |
dreschpe | 3:5a8242af60ba | 12 | * Example: |
dreschpe | 3:5a8242af60ba | 13 | * @code |
dreschpe | 3:5a8242af60ba | 14 | * // Beep with 1Khz for 0.5 seconds |
dreschpe | 3:5a8242af60ba | 15 | * #include "mbed.h" |
dreschpe | 3:5a8242af60ba | 16 | * #include "beep.h" |
dreschpe | 3:5a8242af60ba | 17 | * |
dreschpe | 3:5a8242af60ba | 18 | * Beep buzzer(p21); |
dreschpe | 3:5a8242af60ba | 19 | * |
dreschpe | 3:5a8242af60ba | 20 | * int main() { |
dreschpe | 3:5a8242af60ba | 21 | * ... |
dreschpe | 3:5a8242af60ba | 22 | * buzzer.beep(1000,0.5); |
dreschpe | 3:5a8242af60ba | 23 | * ... |
dreschpe | 3:5a8242af60ba | 24 | * } |
dreschpe | 3:5a8242af60ba | 25 | * @endcode |
dreschpe | 3:5a8242af60ba | 26 | */ |
dreschpe | 1:ddccf0a4a414 | 27 | |
dreschpe | 1:ddccf0a4a414 | 28 | |
dreschpe | 0:18e4a9c978ec | 29 | namespace mbed { |
dreschpe | 0:18e4a9c978ec | 30 | |
dreschpe | 0:18e4a9c978ec | 31 | /* Class: Beep |
dreschpe | 0:18e4a9c978ec | 32 | * A class witch uses pwm to controle a beeper to generate sounds. |
dreschpe | 0:18e4a9c978ec | 33 | */ |
dreschpe | 0:18e4a9c978ec | 34 | class Beep { |
dreschpe | 0:18e4a9c978ec | 35 | |
dreschpe | 0:18e4a9c978ec | 36 | public: |
dreschpe | 0:18e4a9c978ec | 37 | |
dreschpe | 3:5a8242af60ba | 38 | /** Create a Beep object connected to the specified PwmOut pin |
dreschpe | 3:5a8242af60ba | 39 | * |
dreschpe | 3:5a8242af60ba | 40 | * @param pin PwmOut pin to connect to |
dreschpe | 3:5a8242af60ba | 41 | */ |
dreschpe | 0:18e4a9c978ec | 42 | Beep (PinName pin); |
dreschpe | 0:18e4a9c978ec | 43 | |
dreschpe | 3:5a8242af60ba | 44 | /** Beep with given frequency and duration. |
dreschpe | 3:5a8242af60ba | 45 | * |
dreschpe | 3:5a8242af60ba | 46 | * @param frequency - the frequency of the tone in Hz |
dreschpe | 3:5a8242af60ba | 47 | * @param time - the duration of the tone in seconds |
dreschpe | 3:5a8242af60ba | 48 | */ |
dreschpe | 0:18e4a9c978ec | 49 | void beep (float frequency, float time); |
jurica238814 | 5:49c961e79a12 | 50 | void playRttl(char *p); |
dreschpe | 0:18e4a9c978ec | 51 | |
dreschpe | 3:5a8242af60ba | 52 | /** stop the beep instantaneous |
dreschpe | 3:5a8242af60ba | 53 | * usually not used |
dreschpe | 3:5a8242af60ba | 54 | */ |
dreschpe | 0:18e4a9c978ec | 55 | void nobeep(); |
dreschpe | 0:18e4a9c978ec | 56 | |
dreschpe | 0:18e4a9c978ec | 57 | private : |
dreschpe | 0:18e4a9c978ec | 58 | PwmOut _pwm; |
dreschpe | 0:18e4a9c978ec | 59 | Timeout toff; |
dreschpe | 0:18e4a9c978ec | 60 | }; |
dreschpe | 0:18e4a9c978ec | 61 | |
dreschpe | 0:18e4a9c978ec | 62 | } |
dreschpe | 0:18e4a9c978ec | 63 | #endif |