Simple tool for PWM piezo speaker
Piezo.h@2:fa6d9a0b66fc, 2016-10-01 (annotated)
- Committer:
- TeaPack_CZ
- Date:
- Sat Oct 01 21:15:45 2016 +0000
- Revision:
- 2:fa6d9a0b66fc
- Parent:
- 1:220d39711dc6
- Child:
- 3:df1e38c2c57b
Add demo code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TeaPack_CZ | 0:39de01daa2de | 1 | #ifndef PIEZO |
TeaPack_CZ | 0:39de01daa2de | 2 | #define PIEZO |
TeaPack_CZ | 0:39de01daa2de | 3 | |
TeaPack_CZ | 0:39de01daa2de | 4 | #include "mbed.h" |
TeaPack_CZ | 0:39de01daa2de | 5 | |
TeaPack_CZ | 2:fa6d9a0b66fc | 6 | /** Class for the PWM Piezo speaker. |
TeaPack_CZ | 2:fa6d9a0b66fc | 7 | * This class is using PWM to make sounds with piezo speaker. |
TeaPack_CZ | 2:fa6d9a0b66fc | 8 | * Currently tested with LPC1768.\n\n |
TeaPack_CZ | 2:fa6d9a0b66fc | 9 | * |
TeaPack_CZ | 2:fa6d9a0b66fc | 10 | * |
TeaPack_CZ | 2:fa6d9a0b66fc | 11 | * Writen by: Jan Crha (TeaPack_CZ), 2016. |
TeaPack_CZ | 2:fa6d9a0b66fc | 12 | * |
TeaPack_CZ | 2:fa6d9a0b66fc | 13 | * Last modified: 2016-10-01 |
TeaPack_CZ | 2:fa6d9a0b66fc | 14 | * |
TeaPack_CZ | 2:fa6d9a0b66fc | 15 | * Example: |
TeaPack_CZ | 2:fa6d9a0b66fc | 16 | * @code |
TeaPack_CZ | 2:fa6d9a0b66fc | 17 | * #include "mbed.h" |
TeaPack_CZ | 2:fa6d9a0b66fc | 18 | * #include "Piezo.h" |
TeaPack_CZ | 2:fa6d9a0b66fc | 19 | * |
TeaPack_CZ | 2:fa6d9a0b66fc | 20 | * Piezo PS(p21); |
TeaPack_CZ | 2:fa6d9a0b66fc | 21 | * |
TeaPack_CZ | 2:fa6d9a0b66fc | 22 | * int main(){ |
TeaPack_CZ | 2:fa6d9a0b66fc | 23 | * int playTime = 120; |
TeaPack_CZ | 2:fa6d9a0b66fc | 24 | * |
TeaPack_CZ | 2:fa6d9a0b66fc | 25 | * for(int i=0; i<2; i++) |
TeaPack_CZ | 2:fa6d9a0b66fc | 26 | * { |
TeaPack_CZ | 2:fa6d9a0b66fc | 27 | * PS.play(466.16,playTime); |
TeaPack_CZ | 2:fa6d9a0b66fc | 28 | * wait_ms(playTime); |
TeaPack_CZ | 2:fa6d9a0b66fc | 29 | * PS.play(415.30,playTime); |
TeaPack_CZ | 2:fa6d9a0b66fc | 30 | * wait_ms(playTime); |
TeaPack_CZ | 2:fa6d9a0b66fc | 31 | * |
TeaPack_CZ | 2:fa6d9a0b66fc | 32 | * PS.play(466.16,playTime); |
TeaPack_CZ | 2:fa6d9a0b66fc | 33 | * wait_ms(playTime); |
TeaPack_CZ | 2:fa6d9a0b66fc | 34 | * PS.play(349.23,playTime); |
TeaPack_CZ | 2:fa6d9a0b66fc | 35 | * wait_ms(playTime); |
TeaPack_CZ | 2:fa6d9a0b66fc | 36 | * |
TeaPack_CZ | 2:fa6d9a0b66fc | 37 | * PS.play(277.18,playTime/2); |
TeaPack_CZ | 2:fa6d9a0b66fc | 38 | * wait_ms(playTime/2); |
TeaPack_CZ | 2:fa6d9a0b66fc | 39 | * PS.play(349.23,playTime/2); |
TeaPack_CZ | 2:fa6d9a0b66fc | 40 | * wait_ms(playTime/2); |
TeaPack_CZ | 2:fa6d9a0b66fc | 41 | * wait_ms(playTime); |
TeaPack_CZ | 2:fa6d9a0b66fc | 42 | * PS.play(233.08,playTime/2); |
TeaPack_CZ | 2:fa6d9a0b66fc | 43 | * wait_ms(playTime/2); |
TeaPack_CZ | 2:fa6d9a0b66fc | 44 | * |
TeaPack_CZ | 2:fa6d9a0b66fc | 45 | * wait_ms(playTime*4); |
TeaPack_CZ | 2:fa6d9a0b66fc | 46 | * } |
TeaPack_CZ | 2:fa6d9a0b66fc | 47 | * } |
TeaPack_CZ | 2:fa6d9a0b66fc | 48 | * @endcode |
TeaPack_CZ | 2:fa6d9a0b66fc | 49 | */ |
TeaPack_CZ | 2:fa6d9a0b66fc | 50 | |
TeaPack_CZ | 2:fa6d9a0b66fc | 51 | class Piezo |
TeaPack_CZ | 2:fa6d9a0b66fc | 52 | { |
TeaPack_CZ | 2:fa6d9a0b66fc | 53 | |
TeaPack_CZ | 0:39de01daa2de | 54 | public: |
TeaPack_CZ | 0:39de01daa2de | 55 | Piezo(PinName Pwmout); |
TeaPack_CZ | 2:fa6d9a0b66fc | 56 | |
TeaPack_CZ | 0:39de01daa2de | 57 | void play(float, int); |
TeaPack_CZ | 0:39de01daa2de | 58 | void playAsync(float, int); |
TeaPack_CZ | 2:fa6d9a0b66fc | 59 | |
TeaPack_CZ | 0:39de01daa2de | 60 | void stop(); |
TeaPack_CZ | 0:39de01daa2de | 61 | |
TeaPack_CZ | 0:39de01daa2de | 62 | private: |
TeaPack_CZ | 0:39de01daa2de | 63 | PwmOut _pwm; |
TeaPack_CZ | 0:39de01daa2de | 64 | Ticker _timer; |
TeaPack_CZ | 2:fa6d9a0b66fc | 65 | |
TeaPack_CZ | 0:39de01daa2de | 66 | float Period; |
TeaPack_CZ | 2:fa6d9a0b66fc | 67 | |
TeaPack_CZ | 0:39de01daa2de | 68 | float min_freq; |
TeaPack_CZ | 0:39de01daa2de | 69 | float max_freq; |
TeaPack_CZ | 0:39de01daa2de | 70 | }; |
TeaPack_CZ | 0:39de01daa2de | 71 | |
TeaPack_CZ | 0:39de01daa2de | 72 | #endif |