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: N5110 PinDetect PowerControl mbed
Sound.h
00001 #ifndef SOUND_H 00002 #define SOUND_H 00003 00004 /// @file Sound.h 00005 00006 #include "mbed.h" 00007 #include "Resources.h" 00008 00009 /// A note is given by its frequency and beat. 00010 struct Note 00011 { 00012 Note(float f, float b = 1.0) : frequency(f), beat(b) {} 00013 float frequency; /// Frequency of note 00014 float beat; /// Normal beat last for 1 cycle 00015 }; 00016 00017 /// Sound uses a piezo buzzer connect to a PWMOut to output sound 00018 class Sound 00019 { 00020 public: 00021 /* Creates a new sound object. 00022 * @param pin The pin of the pwm output the piezo buzzer is connected to. 00023 */ 00024 Sound(PinName buzzerPin); 00025 00026 /// Destructor 00027 ~Sound(); 00028 00029 /// Plays the given note. @param note The note to be played. 00030 void playNote(Note ¬e); 00031 00032 private: 00033 void start() {*buzzer = 0.5;} // Set duty cycle to 50% 00034 void stop() {*buzzer = 0.0;} // Turn off pwm out 00035 00036 // Variables 00037 private: 00038 PwmOut *buzzer; // Piezo buzzer. 00039 Timeout ticker; // Used for stopping sound after the given beat. 00040 }; 00041 00042 /// Sound effects. Commonly used notes. 00043 namespace SFX 00044 { 00045 extern Note PLAYER_DEAD; 00046 extern Note ENEMY_DEAD; 00047 extern Note BULLET_FIRED; 00048 extern Note PLAYER_JUMP; 00049 extern Note RESTART; 00050 } 00051 00052 #endif
Generated on Tue Jul 12 2022 21:59:47 by
