Library for 3.2'' uLcd Picaso Display4D system Picaso Serial Environment Command Set web: http://www.4dsystems.com.au/product/20/67/Processors_Graphics/PICASO/
Buzzer.h@1:a74e42cf52b2, 2015-04-05 (annotated)
- Committer:
- adelino
- Date:
- Sun Apr 05 13:54:48 2015 +0000
- Revision:
- 1:a74e42cf52b2
PicasoLib version 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adelino | 1:a74e42cf52b2 | 1 | #ifndef BUZZER_H |
adelino | 1:a74e42cf52b2 | 2 | #define BUZZER_H |
adelino | 1:a74e42cf52b2 | 3 | #include "mbed.h" |
adelino | 1:a74e42cf52b2 | 4 | #include <vector> |
adelino | 1:a74e42cf52b2 | 5 | |
adelino | 1:a74e42cf52b2 | 6 | /** This is a simple sound library for mbed.I use the RTTTL format |
adelino | 1:a74e42cf52b2 | 7 | http://en.wikipedia.org/wiki/Ring_Tone_Transfer_Language |
adelino | 1:a74e42cf52b2 | 8 | |
adelino | 1:a74e42cf52b2 | 9 | Example |
adelino | 1:a74e42cf52b2 | 10 | @code |
adelino | 1:a74e42cf52b2 | 11 | #include "Buzzer.h" |
adelino | 1:a74e42cf52b2 | 12 | |
adelino | 1:a74e42cf52b2 | 13 | 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"; |
adelino | 1:a74e42cf52b2 | 14 | |
adelino | 1:a74e42cf52b2 | 15 | Buzzer buzzer(p21); |
adelino | 1:a74e42cf52b2 | 16 | |
adelino | 1:a74e42cf52b2 | 17 | Music* pMusic=0; //the song |
adelino | 1:a74e42cf52b2 | 18 | Note la("A#4",50); //the sound |
adelino | 1:a74e42cf52b2 | 19 | |
adelino | 1:a74e42cf52b2 | 20 | int main() |
adelino | 1:a74e42cf52b2 | 21 | { |
adelino | 1:a74e42cf52b2 | 22 | pMusic= new Music(song); |
adelino | 1:a74e42cf52b2 | 23 | pMusic->play(&buzzer); |
adelino | 1:a74e42cf52b2 | 24 | |
adelino | 1:a74e42cf52b2 | 25 | buzzer.tone(&la); |
adelino | 1:a74e42cf52b2 | 26 | delete(pMusic); |
adelino | 1:a74e42cf52b2 | 27 | } |
adelino | 1:a74e42cf52b2 | 28 | @endcode |
adelino | 1:a74e42cf52b2 | 29 | */ |
adelino | 1:a74e42cf52b2 | 30 | |
adelino | 1:a74e42cf52b2 | 31 | // notes definition |
adelino | 1:a74e42cf52b2 | 32 | #define NOTE_R 0 |
adelino | 1:a74e42cf52b2 | 33 | |
adelino | 1:a74e42cf52b2 | 34 | //octave 3 |
adelino | 1:a74e42cf52b2 | 35 | #define NOTE_C3 7644 |
adelino | 1:a74e42cf52b2 | 36 | #define NOTE_CS3 7215 |
adelino | 1:a74e42cf52b2 | 37 | #define NOTE_D3 6810 |
adelino | 1:a74e42cf52b2 | 38 | #define NOTE_DS3 6428 |
adelino | 1:a74e42cf52b2 | 39 | #define NOTE_E3 6067 |
adelino | 1:a74e42cf52b2 | 40 | #define NOTE_F3 5727 |
adelino | 1:a74e42cf52b2 | 41 | #define NOTE_FS3 5405 |
adelino | 1:a74e42cf52b2 | 42 | #define NOTE_G3 5102 |
adelino | 1:a74e42cf52b2 | 43 | #define NOTE_GS3 4815 |
adelino | 1:a74e42cf52b2 | 44 | #define NOTE_A3 4545 |
adelino | 1:a74e42cf52b2 | 45 | #define NOTE_AS3 4290 |
adelino | 1:a74e42cf52b2 | 46 | #define NOTE_B3 4049 |
adelino | 1:a74e42cf52b2 | 47 | |
adelino | 1:a74e42cf52b2 | 48 | //octave 4 |
adelino | 1:a74e42cf52b2 | 49 | #define NOTE_C4 3822 |
adelino | 1:a74e42cf52b2 | 50 | #define NOTE_CS4 3607 |
adelino | 1:a74e42cf52b2 | 51 | #define NOTE_D4 3405 |
adelino | 1:a74e42cf52b2 | 52 | #define NOTE_DS4 3214 |
adelino | 1:a74e42cf52b2 | 53 | #define NOTE_E4 3033 |
adelino | 1:a74e42cf52b2 | 54 | #define NOTE_F4 2863 |
adelino | 1:a74e42cf52b2 | 55 | #define NOTE_FS4 2702 |
adelino | 1:a74e42cf52b2 | 56 | #define NOTE_G4 2551 |
adelino | 1:a74e42cf52b2 | 57 | #define NOTE_GS4 2408 |
adelino | 1:a74e42cf52b2 | 58 | #define NOTE_A4 2272 |
adelino | 1:a74e42cf52b2 | 59 | #define NOTE_AS4 2145 |
adelino | 1:a74e42cf52b2 | 60 | #define NOTE_B4 2024 |
adelino | 1:a74e42cf52b2 | 61 | |
adelino | 1:a74e42cf52b2 | 62 | //octave 5 |
adelino | 1:a74e42cf52b2 | 63 | #define NOTE_C5 1911 |
adelino | 1:a74e42cf52b2 | 64 | #define NOTE_CS5 1804 |
adelino | 1:a74e42cf52b2 | 65 | #define NOTE_D5 1702 |
adelino | 1:a74e42cf52b2 | 66 | #define NOTE_DS5 1607 |
adelino | 1:a74e42cf52b2 | 67 | #define NOTE_E5 1516 |
adelino | 1:a74e42cf52b2 | 68 | #define NOTE_F5 1432 |
adelino | 1:a74e42cf52b2 | 69 | #define NOTE_FS5 1351 |
adelino | 1:a74e42cf52b2 | 70 | #define NOTE_G5 1275 |
adelino | 1:a74e42cf52b2 | 71 | #define NOTE_GS5 1204 |
adelino | 1:a74e42cf52b2 | 72 | #define NOTE_A5 1136 |
adelino | 1:a74e42cf52b2 | 73 | #define NOTE_AS5 1072 |
adelino | 1:a74e42cf52b2 | 74 | #define NOTE_B5 1012 |
adelino | 1:a74e42cf52b2 | 75 | |
adelino | 1:a74e42cf52b2 | 76 | //octave 6 |
adelino | 1:a74e42cf52b2 | 77 | #define NOTE_C6 956 |
adelino | 1:a74e42cf52b2 | 78 | #define NOTE_CS6 903 |
adelino | 1:a74e42cf52b2 | 79 | #define NOTE_D6 852 |
adelino | 1:a74e42cf52b2 | 80 | #define NOTE_DS6 803 |
adelino | 1:a74e42cf52b2 | 81 | #define NOTE_E6 759 |
adelino | 1:a74e42cf52b2 | 82 | #define NOTE_F6 716 |
adelino | 1:a74e42cf52b2 | 83 | #define NOTE_FS6 676 |
adelino | 1:a74e42cf52b2 | 84 | #define NOTE_G6 638 |
adelino | 1:a74e42cf52b2 | 85 | #define NOTE_GS6 602 |
adelino | 1:a74e42cf52b2 | 86 | #define NOTE_A6 568 |
adelino | 1:a74e42cf52b2 | 87 | #define NOTE_AS6 536 |
adelino | 1:a74e42cf52b2 | 88 | #define NOTE_B6 506 |
adelino | 1:a74e42cf52b2 | 89 | |
adelino | 1:a74e42cf52b2 | 90 | //octave 7 |
adelino | 1:a74e42cf52b2 | 91 | #define NOTE_C7 477 |
adelino | 1:a74e42cf52b2 | 92 | #define NOTE_CS7 451 |
adelino | 1:a74e42cf52b2 | 93 | #define NOTE_D7 426 |
adelino | 1:a74e42cf52b2 | 94 | #define NOTE_DS7 402 |
adelino | 1:a74e42cf52b2 | 95 | #define NOTE_E7 379 |
adelino | 1:a74e42cf52b2 | 96 | #define NOTE_F7 358 |
adelino | 1:a74e42cf52b2 | 97 | #define NOTE_FS7 338 |
adelino | 1:a74e42cf52b2 | 98 | #define NOTE_G7 319 |
adelino | 1:a74e42cf52b2 | 99 | #define NOTE_GS7 301 |
adelino | 1:a74e42cf52b2 | 100 | #define NOTE_A7 284 |
adelino | 1:a74e42cf52b2 | 101 | #define NOTE_AS7 268 |
adelino | 1:a74e42cf52b2 | 102 | #define NOTE_B7 253 |
adelino | 1:a74e42cf52b2 | 103 | //------------------------------ |
adelino | 1:a74e42cf52b2 | 104 | class Note |
adelino | 1:a74e42cf52b2 | 105 | { |
adelino | 1:a74e42cf52b2 | 106 | public: |
adelino | 1:a74e42cf52b2 | 107 | /** Construct a Note (sound) object. |
adelino | 1:a74e42cf52b2 | 108 | * |
adelino | 1:a74e42cf52b2 | 109 | * @param _note the note in range (A,a,B,b,C,c,D,d,E,e,F,f) |
adelino | 1:a74e42cf52b2 | 110 | * @param _isSharp exemple A4 or A#4 |
adelino | 1:a74e42cf52b2 | 111 | * @param _duration the time duration |
adelino | 1:a74e42cf52b2 | 112 | */ |
adelino | 1:a74e42cf52b2 | 113 | |
adelino | 1:a74e42cf52b2 | 114 | Note(char _note,bool _isSharp,char _octave,int _duration); |
adelino | 1:a74e42cf52b2 | 115 | |
adelino | 1:a74e42cf52b2 | 116 | /** Construct a Note (sound) object. |
adelino | 1:a74e42cf52b2 | 117 | * |
adelino | 1:a74e42cf52b2 | 118 | * @param str exemple "C%4" or "C#4" |
adelino | 1:a74e42cf52b2 | 119 | * @param _duration the time duration |
adelino | 1:a74e42cf52b2 | 120 | */ |
adelino | 1:a74e42cf52b2 | 121 | |
adelino | 1:a74e42cf52b2 | 122 | Note(char* str,int _duration);// exemple "C%4" or "C#4" |
adelino | 1:a74e42cf52b2 | 123 | |
adelino | 1:a74e42cf52b2 | 124 | |
adelino | 1:a74e42cf52b2 | 125 | //setter |
adelino | 1:a74e42cf52b2 | 126 | |
adelino | 1:a74e42cf52b2 | 127 | //getter |
adelino | 1:a74e42cf52b2 | 128 | |
adelino | 1:a74e42cf52b2 | 129 | /// @returns the note period (PwmOutput) |
adelino | 1:a74e42cf52b2 | 130 | int getNotePeriod_us(void)const ; |
adelino | 1:a74e42cf52b2 | 131 | |
adelino | 1:a74e42cf52b2 | 132 | /// @returns is Sharp |
adelino | 1:a74e42cf52b2 | 133 | bool getIsSharp(void) const; |
adelino | 1:a74e42cf52b2 | 134 | |
adelino | 1:a74e42cf52b2 | 135 | /// @returns the note char |
adelino | 1:a74e42cf52b2 | 136 | char getNoteChar(void) const; |
adelino | 1:a74e42cf52b2 | 137 | |
adelino | 1:a74e42cf52b2 | 138 | /// @returns the octave |
adelino | 1:a74e42cf52b2 | 139 | char getOctave(void) const; |
adelino | 1:a74e42cf52b2 | 140 | |
adelino | 1:a74e42cf52b2 | 141 | /// @returns the duartion in ms |
adelino | 1:a74e42cf52b2 | 142 | int getDuration(void) const; |
adelino | 1:a74e42cf52b2 | 143 | |
adelino | 1:a74e42cf52b2 | 144 | ///use in debugging |
adelino | 1:a74e42cf52b2 | 145 | void toString(char* buffer); |
adelino | 1:a74e42cf52b2 | 146 | |
adelino | 1:a74e42cf52b2 | 147 | |
adelino | 1:a74e42cf52b2 | 148 | protected: |
adelino | 1:a74e42cf52b2 | 149 | |
adelino | 1:a74e42cf52b2 | 150 | int calculatePeriod_us(void); |
adelino | 1:a74e42cf52b2 | 151 | |
adelino | 1:a74e42cf52b2 | 152 | void parse(const char* _str); |
adelino | 1:a74e42cf52b2 | 153 | |
adelino | 1:a74e42cf52b2 | 154 | char myNoteChar; |
adelino | 1:a74e42cf52b2 | 155 | bool myIsSharp; |
adelino | 1:a74e42cf52b2 | 156 | char myOctave; // 3->5 |
adelino | 1:a74e42cf52b2 | 157 | int myPeriod_us; |
adelino | 1:a74e42cf52b2 | 158 | int myDuration; |
adelino | 1:a74e42cf52b2 | 159 | |
adelino | 1:a74e42cf52b2 | 160 | }; |
adelino | 1:a74e42cf52b2 | 161 | //---------------------------------- |
adelino | 1:a74e42cf52b2 | 162 | |
adelino | 1:a74e42cf52b2 | 163 | |
adelino | 1:a74e42cf52b2 | 164 | class Buzzer:public PwmOut |
adelino | 1:a74e42cf52b2 | 165 | { |
adelino | 1:a74e42cf52b2 | 166 | public: |
adelino | 1:a74e42cf52b2 | 167 | /** Construct a Buzzer object. |
adelino | 1:a74e42cf52b2 | 168 | * |
adelino | 1:a74e42cf52b2 | 169 | * @param _pwmOut select the PwmOutput |
adelino | 1:a74e42cf52b2 | 170 | */ |
adelino | 1:a74e42cf52b2 | 171 | |
adelino | 1:a74e42cf52b2 | 172 | Buzzer(PinName _pwmOut); |
adelino | 1:a74e42cf52b2 | 173 | // |
adelino | 1:a74e42cf52b2 | 174 | ~Buzzer(); |
adelino | 1:a74e42cf52b2 | 175 | |
adelino | 1:a74e42cf52b2 | 176 | /// The Buzzer tone a simple sound |
adelino | 1:a74e42cf52b2 | 177 | void tone(const Note* _note); |
adelino | 1:a74e42cf52b2 | 178 | |
adelino | 1:a74e42cf52b2 | 179 | |
adelino | 1:a74e42cf52b2 | 180 | protected: |
adelino | 1:a74e42cf52b2 | 181 | |
adelino | 1:a74e42cf52b2 | 182 | }; |
adelino | 1:a74e42cf52b2 | 183 | |
adelino | 1:a74e42cf52b2 | 184 | |
adelino | 1:a74e42cf52b2 | 185 | //---------------------------------- |
adelino | 1:a74e42cf52b2 | 186 | class Music |
adelino | 1:a74e42cf52b2 | 187 | { |
adelino | 1:a74e42cf52b2 | 188 | public: |
adelino | 1:a74e42cf52b2 | 189 | /** Construct a Music object. |
adelino | 1:a74e42cf52b2 | 190 | * |
adelino | 1:a74e42cf52b2 | 191 | * @param p the RTTTL song |
adelino | 1:a74e42cf52b2 | 192 | */ |
adelino | 1:a74e42cf52b2 | 193 | Music(const char* p); |
adelino | 1:a74e42cf52b2 | 194 | |
adelino | 1:a74e42cf52b2 | 195 | ~Music(); |
adelino | 1:a74e42cf52b2 | 196 | |
adelino | 1:a74e42cf52b2 | 197 | /// play the music |
adelino | 1:a74e42cf52b2 | 198 | void play(Buzzer* _buzzer); |
adelino | 1:a74e42cf52b2 | 199 | |
adelino | 1:a74e42cf52b2 | 200 | /// @returns the music lenght |
adelino | 1:a74e42cf52b2 | 201 | int getNumbersNotes(void) const; |
adelino | 1:a74e42cf52b2 | 202 | |
adelino | 1:a74e42cf52b2 | 203 | protected: |
adelino | 1:a74e42cf52b2 | 204 | int nbNotes; |
adelino | 1:a74e42cf52b2 | 205 | vector<Note*> myTabNotes; |
adelino | 1:a74e42cf52b2 | 206 | |
adelino | 1:a74e42cf52b2 | 207 | |
adelino | 1:a74e42cf52b2 | 208 | }; |
adelino | 1:a74e42cf52b2 | 209 | |
adelino | 1:a74e42cf52b2 | 210 | #endif |