Hugo Hu / Mbed 2 deprecated BRAVEHEART

Dependencies:   mbed N5110 ShiftReg PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Music.h Source File

Music.h

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