decarvalho adelino / BuzzerLib

Dependents:   acd52832_buzzer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Buzzer.h Source File

Buzzer.h

00001 #ifndef BUZZER_H
00002 #define BUZZER_H
00003 #include "mbed.h"
00004 #include <vector>
00005 
00006 /** This is a simple sound library for mbed.I use the RTTTL format 
00007 http://en.wikipedia.org/wiki/Ring_Tone_Transfer_Language
00008 
00009 Example
00010 @code
00011 #include "Buzzer.h"
00012 
00013 char *song = "The Simpsons:d=4,o=5,b=160:c.6,e6,f#6,8a6,g.6,e6,c6,8a,8f#,8f#,8f#,2g,8p,8p,8f#,8f#,8f#,8g,a#.,8c6,8c6,8c6,c6";
00014 
00015 Buzzer buzzer(p21);
00016 
00017 Music* pMusic=0;    //the song
00018 Note la("A#4",50);  //the sound
00019 
00020 int main()
00021 {
00022    pMusic= new Music(song);
00023    pMusic->play(&buzzer);
00024    
00025    buzzer.tone(&la);
00026    delete(pMusic);
00027 }
00028 @endcode
00029 */
00030 
00031 // notes definition
00032 #define NOTE_R      0
00033 
00034 //octave 3
00035 #define NOTE_C3     7644
00036 #define NOTE_CS3    7215
00037 #define NOTE_D3     6810
00038 #define NOTE_DS3    6428
00039 #define NOTE_E3     6067
00040 #define NOTE_F3     5727
00041 #define NOTE_FS3    5405
00042 #define NOTE_G3     5102
00043 #define NOTE_GS3    4815
00044 #define NOTE_A3     4545
00045 #define NOTE_AS3    4290
00046 #define NOTE_B3     4049
00047 
00048 //octave 4
00049 #define NOTE_C4     3822
00050 #define NOTE_CS4    3607
00051 #define NOTE_D4     3405
00052 #define NOTE_DS4    3214
00053 #define NOTE_E4     3033
00054 #define NOTE_F4     2863
00055 #define NOTE_FS4    2702
00056 #define NOTE_G4     2551
00057 #define NOTE_GS4    2408
00058 #define NOTE_A4     2272
00059 #define NOTE_AS4    2145
00060 #define NOTE_B4     2024
00061 
00062 //octave 5
00063 #define NOTE_C5     1911
00064 #define NOTE_CS5    1804
00065 #define NOTE_D5     1702
00066 #define NOTE_DS5    1607
00067 #define NOTE_E5     1516
00068 #define NOTE_F5     1432
00069 #define NOTE_FS5    1351
00070 #define NOTE_G5     1275
00071 #define NOTE_GS5    1204
00072 #define NOTE_A5     1136
00073 #define NOTE_AS5    1072
00074 #define NOTE_B5     1012
00075 
00076 //octave 6
00077 #define NOTE_C6     956
00078 #define NOTE_CS6    903
00079 #define NOTE_D6     852
00080 #define NOTE_DS6    803
00081 #define NOTE_E6     759
00082 #define NOTE_F6     716
00083 #define NOTE_FS6    676
00084 #define NOTE_G6     638
00085 #define NOTE_GS6    602
00086 #define NOTE_A6     568
00087 #define NOTE_AS6    536
00088 #define NOTE_B6     506
00089 
00090 //octave 7
00091 #define NOTE_C7     477
00092 #define NOTE_CS7    451
00093 #define NOTE_D7     426
00094 #define NOTE_DS7    402
00095 #define NOTE_E7     379
00096 #define NOTE_F7     358
00097 #define NOTE_FS7    338
00098 #define NOTE_G7     319
00099 #define NOTE_GS7    301
00100 #define NOTE_A7     284
00101 #define NOTE_AS7    268
00102 #define NOTE_B7     253
00103 //------------------------------
00104 class Note
00105 {
00106 public:
00107      /** Construct a Note (sound) object.
00108         *
00109         * @param _note the note in range (A,a,B,b,C,c,D,d,E,e,F,f)
00110         * @param _isSharp exemple A4 or A#4
00111         * @param _duration the time duration
00112         */
00113 
00114     Note(char _note,bool _isSharp,char _octave,int _duration);
00115     
00116     /** Construct a Note (sound) object.
00117         *
00118         * @param str exemple "C%4" or "C#4"
00119         * @param _duration the time duration
00120         */
00121     
00122     Note(char* str,int _duration);// exemple "C%4" or "C#4"
00123     
00124     
00125 //setter
00126 
00127 //getter
00128      
00129     ///    @returns the note period (PwmOutput)
00130     int getNotePeriod_us(void)const ;
00131     
00132     ///    @returns is Sharp
00133     bool getIsSharp(void) const;
00134     
00135     ///    @returns the note char
00136     char getNoteChar(void) const;
00137     
00138     ///    @returns the octave
00139     char getOctave(void) const;
00140     
00141     ///    @returns the duartion in ms
00142     int getDuration(void) const;
00143 
00144     ///use in debugging
00145     void toString(char* buffer);
00146 
00147 
00148 protected:
00149 
00150     int calculatePeriod_us(void);
00151     
00152     void parse(const char* _str);
00153     
00154     char myNoteChar;
00155     bool myIsSharp;
00156     char myOctave;  // 3->5
00157     int myPeriod_us;
00158     int myDuration;
00159 
00160 };
00161 //----------------------------------
00162 
00163 
00164 class Buzzer:public PwmOut
00165 {
00166 public:
00167  /** Construct a Buzzer object.
00168         *
00169         * @param _pwmOut select the PwmOutput
00170         */
00171 
00172     Buzzer(PinName _pwmOut);
00173     //
00174     ~Buzzer();
00175     
00176      /// The Buzzer tone a simple sound
00177     void tone(const Note* _note);
00178     
00179     
00180 protected:
00181     
00182 };
00183 
00184 
00185 //----------------------------------
00186 class Music
00187 {
00188 public:
00189      /** Construct a Music  object.
00190         *
00191         * @param p the RTTTL song
00192         */
00193     Music(const char* p);
00194     
00195     ~Music();
00196     
00197     /// play the music
00198    void  play(Buzzer* _buzzer);
00199    
00200     ///    @returns the music lenght   
00201    int getNumbersNotes(void) const;
00202 
00203 protected:
00204     int nbNotes;
00205     vector<Note*> myTabNotes;
00206 
00207 
00208 };
00209 
00210 #endif