
Demo for new class to play a note on a speaker using a PWM output See http://mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/
Speaker.h@0:b2fdf3770282, 2013-01-20 (annotated)
- Committer:
- 4180_1
- Date:
- Sun Jan 20 03:02:37 2013 +0000
- Revision:
- 0:b2fdf3770282
- Child:
- 2:68eee0fb2026
ver 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:b2fdf3770282 | 1 | #include "mbed.h" |
4180_1 | 0:b2fdf3770282 | 2 | // new class to play a note on Speaker based on PwmOut class |
4180_1 | 0:b2fdf3770282 | 3 | class Speaker |
4180_1 | 0:b2fdf3770282 | 4 | { |
4180_1 | 0:b2fdf3770282 | 5 | public: |
4180_1 | 0:b2fdf3770282 | 6 | Speaker(PinName pin) : _pin(pin) { |
4180_1 | 0:b2fdf3770282 | 7 | // _pin(pin) means pass pin to the Speaker Constructor |
4180_1 | 0:b2fdf3770282 | 8 | } |
4180_1 | 0:b2fdf3770282 | 9 | // class method to play a note based on PwmOut class |
4180_1 | 0:b2fdf3770282 | 10 | void PlayNote(float frequency, float duration, float volume) { |
4180_1 | 0:b2fdf3770282 | 11 | _pin.period(1.0/frequency); |
4180_1 | 0:b2fdf3770282 | 12 | _pin = volume/2.0; |
4180_1 | 0:b2fdf3770282 | 13 | wait(duration); |
4180_1 | 0:b2fdf3770282 | 14 | _pin = 0.0; |
4180_1 | 0:b2fdf3770282 | 15 | } |
4180_1 | 0:b2fdf3770282 | 16 | |
4180_1 | 0:b2fdf3770282 | 17 | private: |
4180_1 | 0:b2fdf3770282 | 18 | PwmOut _pin; |
4180_1 | 0:b2fdf3770282 | 19 | }; |