Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE PinDetect mbed
Speaker.h@0:09b7e6aa75a9, 2021-11-30 (annotated)
- Committer:
- jwalker366
- Date:
- Tue Nov 30 18:58:54 2021 +0000
- Revision:
- 0:09b7e6aa75a9
for sub;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jwalker366 | 0:09b7e6aa75a9 | 1 | #ifndef SPEAKER_H |
jwalker366 | 0:09b7e6aa75a9 | 2 | #define SPEAKER_H |
jwalker366 | 0:09b7e6aa75a9 | 3 | #include "mbed.h" |
jwalker366 | 0:09b7e6aa75a9 | 4 | |
jwalker366 | 0:09b7e6aa75a9 | 5 | // new class to play a note on Speaker based on PwmOut class |
jwalker366 | 0:09b7e6aa75a9 | 6 | class Speaker |
jwalker366 | 0:09b7e6aa75a9 | 7 | { |
jwalker366 | 0:09b7e6aa75a9 | 8 | public: |
jwalker366 | 0:09b7e6aa75a9 | 9 | Speaker(PinName pin) : _pin(pin) { |
jwalker366 | 0:09b7e6aa75a9 | 10 | // _pin(pin) means pass pin to the Speaker Constructor |
jwalker366 | 0:09b7e6aa75a9 | 11 | } |
jwalker366 | 0:09b7e6aa75a9 | 12 | // class method to play a note based on PwmOut class |
jwalker366 | 0:09b7e6aa75a9 | 13 | void PlayNote(float frequency, float duration, float volume) { |
jwalker366 | 0:09b7e6aa75a9 | 14 | _pin.period(1.0/frequency); |
jwalker366 | 0:09b7e6aa75a9 | 15 | _pin = volume/2.0; |
jwalker366 | 0:09b7e6aa75a9 | 16 | wait(duration); |
jwalker366 | 0:09b7e6aa75a9 | 17 | _pin = 0.0; |
jwalker366 | 0:09b7e6aa75a9 | 18 | } |
jwalker366 | 0:09b7e6aa75a9 | 19 | |
jwalker366 | 0:09b7e6aa75a9 | 20 | private: |
jwalker366 | 0:09b7e6aa75a9 | 21 | PwmOut _pin; |
jwalker366 | 0:09b7e6aa75a9 | 22 | }; |
jwalker366 | 0:09b7e6aa75a9 | 23 | #endif |