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.
Buzzer.cpp
00001 #include "mbed.h" 00002 #include "Buzzer.h" 00003 00004 const int tab_A[]= {NOTE_A3,NOTE_A4,NOTE_A5,NOTE_A6,NOTE_A7}; 00005 00006 const int tab_AS[]= {NOTE_AS3,NOTE_AS4,NOTE_AS5,NOTE_AS6,NOTE_AS7}; 00007 00008 const int tab_B[]= {NOTE_B3,NOTE_B4,NOTE_B5,NOTE_B6,NOTE_B7}; 00009 00010 const int tab_BS[]= {NOTE_C3,NOTE_C4,NOTE_C5,NOTE_C6,NOTE_C7}; 00011 00012 const int tab_C[]= {NOTE_C3,NOTE_C4,NOTE_C5,NOTE_C6,NOTE_C7}; 00013 00014 const int tab_CS[]= {NOTE_CS3,NOTE_CS4,NOTE_CS5,NOTE_CS6,NOTE_CS7}; 00015 00016 const int tab_D[]= {NOTE_D3,NOTE_D4,NOTE_D5,NOTE_D6,NOTE_D7}; 00017 00018 const int tab_DS[]= {NOTE_DS3,NOTE_DS4,NOTE_DS5,NOTE_DS6,NOTE_DS7}; 00019 00020 const int tab_E[]= {NOTE_E3,NOTE_E4,NOTE_E5,NOTE_E6,NOTE_E7}; 00021 00022 const int tab_ES[]= {NOTE_F3,NOTE_F4,NOTE_F5,NOTE_F6,NOTE_F7}; 00023 00024 const int tab_F[]= {NOTE_F3,NOTE_F4,NOTE_F5,NOTE_F6,NOTE_F7}; 00025 00026 const int tab_FS[]= {NOTE_FS3,NOTE_FS4,NOTE_FS5,NOTE_FS6,NOTE_FS7}; 00027 00028 const int tab_G[]= {NOTE_G3,NOTE_G4,NOTE_G5,NOTE_G6,NOTE_G7}; 00029 00030 const int tab_GS[]= {NOTE_GS3,NOTE_GS4,NOTE_GS5,NOTE_GS6,NOTE_GS7}; 00031 00032 #define isDigit(n) (n>='0' && n <= '9') 00033 00034 //------------------------------ 00035 Note::Note(char _note,bool _isSharp,char _octave,int _duration): 00036 myNoteChar(_note),myIsSharp(_isSharp),myOctave(_octave),myDuration(_duration) 00037 { 00038 myPeriod_us=calculatePeriod_us(); 00039 } 00040 //-------------------------------- 00041 Note::Note(char* str,int _duration):myDuration(_duration) 00042 { 00043 parse(str); 00044 myPeriod_us=calculatePeriod_us(); 00045 } 00046 //-------------------------------- 00047 void Note::parse(const char* _str) 00048 { 00049 int i=0; 00050 00051 for(i=0; i<3; i++) { 00052 switch(_str[i]) { 00053 // 00054 case 'R': 00055 myNoteChar='R'; 00056 break; 00057 00058 case 'r': 00059 myNoteChar='R'; 00060 break; 00061 00062 // 00063 case 'A': 00064 myNoteChar='A'; 00065 break; 00066 case 'a': 00067 myNoteChar='A'; 00068 break; 00069 // 00070 // 00071 case 'B': 00072 myNoteChar='B'; 00073 break; 00074 // 00075 case 'b': 00076 myNoteChar='B'; 00077 break; 00078 // 00079 case 'C': 00080 myNoteChar='C'; 00081 break; 00082 case 'c': 00083 myNoteChar='C'; 00084 break; 00085 // 00086 case 'D': 00087 myNoteChar='D'; 00088 break; 00089 case 'd': 00090 myNoteChar='D'; 00091 break; 00092 // 00093 // 00094 case 'E': 00095 myNoteChar='E'; 00096 break; 00097 // 00098 case 'e': 00099 myNoteChar='E'; 00100 break; 00101 // 00102 case 'F': 00103 myNoteChar='F'; 00104 break; 00105 // 00106 case 'f': 00107 myNoteChar='F'; 00108 break; 00109 // 00110 case 'G': 00111 myNoteChar='G'; 00112 break; 00113 // 00114 case 'g': 00115 myNoteChar='G'; 00116 break; 00117 // 00118 case '%': 00119 myIsSharp=false; 00120 break; 00121 // 00122 // 00123 case '#': 00124 myIsSharp=true; 00125 break; 00126 // 00127 // 00128 case '3': 00129 myOctave=3; 00130 break; 00131 // 00132 // 00133 case '4': 00134 myOctave=4; 00135 break; 00136 // 00137 // 00138 case '5': 00139 myOctave=5; 00140 break; 00141 // 00142 // 00143 case '6': 00144 myOctave=6; 00145 break; 00146 // 00147 // 00148 case '7': 00149 myOctave=7; 00150 break; 00151 00152 default: 00153 myNoteChar='R'; 00154 break; 00155 } 00156 // 00157 } 00158 // 00159 00160 } 00161 //-------------------------------- 00162 int Note::calculatePeriod_us(void) 00163 { 00164 switch(myNoteChar) { 00165 // 00166 case 'p': 00167 return NOTE_R; 00168 // 00169 case 'R': 00170 return NOTE_R; 00171 // 00172 case 'r': 00173 return NOTE_R; 00174 // 00175 case 'A': 00176 // 00177 if(myOctave >=3 && myOctave <=7) { 00178 if(myIsSharp==false) { 00179 return tab_A[myOctave-3]; 00180 } else { 00181 return tab_AS[myOctave-3]; 00182 } 00183 } 00184 break; 00185 // 00186 case 'a': 00187 // 00188 if(myOctave >=3 && myOctave <=7) { 00189 if(myIsSharp==false) { 00190 return tab_A[myOctave-3]; 00191 } else { 00192 return tab_AS[myOctave-3]; 00193 } 00194 } 00195 break; 00196 // 00197 case 'B': 00198 // 00199 if(myOctave >=3 && myOctave <=7) { 00200 if(myIsSharp==false) { 00201 return tab_B[myOctave-3]; 00202 } else { 00203 return tab_BS[myOctave-3]; 00204 } 00205 } 00206 break; 00207 // 00208 case 'b': 00209 // 00210 if(myOctave >=3 && myOctave <=7) { 00211 if(myIsSharp==false) { 00212 return tab_B[myOctave-3]; 00213 } else { 00214 return tab_BS[myOctave-3]; 00215 } 00216 } 00217 break; 00218 // 00219 case 'C': 00220 // 00221 if(myOctave >=3 && myOctave <=7) { 00222 if(myIsSharp==false) { 00223 return tab_C[myOctave-3]; 00224 } else { 00225 return tab_CS[myOctave-3]; 00226 } 00227 } 00228 break; 00229 // 00230 case 'c': 00231 // 00232 if(myOctave >=3 && myOctave <=7) { 00233 if(myIsSharp==false) { 00234 return tab_C[myOctave-3]; 00235 } else { 00236 return tab_CS[myOctave-3]; 00237 } 00238 } 00239 break; 00240 // 00241 // 00242 case 'D': 00243 // 00244 if(myOctave >=3 && myOctave <=7) { 00245 if(myIsSharp==false) { 00246 return tab_D[myOctave-3]; 00247 } else { 00248 return tab_DS[myOctave-3]; 00249 } 00250 } 00251 break; 00252 // 00253 case 'd': 00254 // 00255 if(myOctave >=3 && myOctave <=7) { 00256 if(myIsSharp==false) { 00257 return tab_D[myOctave-3]; 00258 } else { 00259 return tab_DS[myOctave-3]; 00260 } 00261 } 00262 break; 00263 // 00264 case 'E': 00265 // 00266 if(myOctave >=3 && myOctave <=7) { 00267 if(myIsSharp==false) { 00268 return tab_E[myOctave-3]; 00269 } else { 00270 return tab_ES[myOctave-3]; 00271 } 00272 } 00273 break; 00274 // 00275 case 'e': 00276 // 00277 if(myOctave >=3 && myOctave <=7) { 00278 if(myIsSharp==false) { 00279 return tab_E[myOctave-3]; 00280 } else { 00281 return tab_ES[myOctave-3]; 00282 } 00283 } 00284 break; 00285 // 00286 case 'F': 00287 // 00288 if(myOctave >=3 && myOctave <=7) { 00289 if(myIsSharp==false) { 00290 return tab_F[myOctave-3]; 00291 } else { 00292 return tab_FS[myOctave-3]; 00293 } 00294 } 00295 break; 00296 // 00297 case 'f': 00298 // 00299 if(myOctave >=3 && myOctave <=7) { 00300 if(myIsSharp==false) { 00301 return tab_F[myOctave-3]; 00302 } else { 00303 return tab_FS[myOctave-3]; 00304 } 00305 } 00306 break; 00307 // 00308 case 'G': 00309 // 00310 if(myOctave >=3 && myOctave <=7) { 00311 if(myIsSharp==false) { 00312 return tab_G[myOctave-3]; 00313 } else { 00314 return tab_GS[myOctave-3]; 00315 } 00316 } 00317 break; 00318 // 00319 case 'g': 00320 // 00321 if(myOctave >=3 && myOctave <=7) { 00322 if(myIsSharp==false) { 00323 return tab_G[myOctave-3]; 00324 } else { 00325 return tab_GS[myOctave-3]; 00326 } 00327 } 00328 break; 00329 // 00330 default: 00331 break; 00332 } 00333 00334 return 0; 00335 } 00336 //-------------------------------- 00337 int Note::getNotePeriod_us(void) const 00338 { 00339 return this->myPeriod_us; 00340 } 00341 //--------------------------------- 00342 bool Note::getIsSharp(void) const 00343 { 00344 return this->myIsSharp; 00345 } 00346 //---------------------------------- 00347 char Note::getNoteChar(void) const 00348 { 00349 return this-> myNoteChar; 00350 } 00351 //---------------------------------- 00352 char Note::getOctave(void) const 00353 { 00354 return this->myOctave; 00355 } 00356 //--------------------------------- 00357 int Note::getDuration(void) const 00358 { 00359 return this->myDuration; 00360 } 00361 //---------------------------------- 00362 void Note::toString(char* buffer) 00363 { 00364 00365 buffer[0]= myNoteChar; 00366 if(myIsSharp) 00367 buffer[1]='#'; 00368 else 00369 buffer[1]='%'; 00370 00371 buffer[2]=myOctave+'0'; 00372 00373 buffer[3]='\0'; 00374 00375 } 00376 //----------------------------------- 00377 00378 00379 Buzzer::Buzzer(PinName _pwmOut):PwmOut(_pwmOut) 00380 { 00381 this->period_us(1); 00382 this->pulsewidth_us(0); 00383 } 00384 //----------------------------------- 00385 void Buzzer::tone(const Note* _note) 00386 { 00387 int mperiod = _note->getNotePeriod_us(); 00388 00389 int dure=_note->getDuration(); 00390 00391 this->period_us(mperiod); 00392 this->pulsewidth_us(mperiod/2); 00393 00394 wait_ms(dure); 00395 this->pulsewidth_us(0); 00396 00397 00398 wait_ms(dure); 00399 00400 } 00401 //------------------------------------- 00402 Buzzer::~Buzzer() 00403 { 00404 } 00405 //-------------------------------------- 00406 Music::Music(const char* p) 00407 { 00408 char defautDuree=4; 00409 char defautOctave=6; 00410 int bmp=63; 00411 00412 int ronde=0; 00413 int dura=0; 00414 int num=0; 00415 00416 char _note; 00417 bool _issharp=false; 00418 char _octave=0; 00419 00420 char newOctave=0; 00421 // 00422 while(*p != ':') 00423 p++; //le titre 00424 00425 p++; //skip : 00426 00427 //defaut duration 00428 if(*p=='d') { 00429 p++; 00430 p++; //skip d= 00431 00432 00433 while(isDigit(*p)) { 00434 num=(num*10)+(*p++-'0'); 00435 } 00436 // 00437 if(num >0) 00438 defautDuree=num; 00439 p++; //skip , 00440 } 00441 // 00442 if(*p=='o') { 00443 p++; 00444 p++; //skip o= 00445 00446 num=*p++ -'0'; 00447 00448 if(num >=3 && num <=7) 00449 defautOctave=num; 00450 00451 p++; //skip , 00452 00453 } 00454 // 00455 if(*p =='b') { 00456 p++; 00457 p++; //skip b= 00458 num=0; 00459 00460 while(isDigit(*p)) { 00461 num=num*10+(*p++ -'0'); 00462 } 00463 bmp=num; 00464 p++; //skip : 00465 } 00466 // 00467 ronde=(60*500/bmp)*4; // in ms 00468 00469 //les notes 00470 while(*p) { 00471 //ge t duration if available 00472 num=0; 00473 while(isDigit(*p)) { 00474 num=(num*10)+(*p++ -'0'); 00475 } 00476 if(num) 00477 dura=ronde/num; 00478 else 00479 dura=ronde/defautDuree; 00480 //la note 00481 _note=*p; 00482 00483 p++; 00484 //issharp 00485 if(*p=='#') { 00486 _issharp=true; 00487 p++; 00488 } else { 00489 _issharp=false; 00490 00491 } 00492 //un point ? 00493 if(*p=='.') { 00494 dura+=dura/2; 00495 p++; 00496 } 00497 // 00498 //nouvelle octave 00499 if(isDigit(*p)) { 00500 newOctave =*p - '0'; 00501 p++; 00502 } 00503 00504 if(newOctave >0) 00505 _octave=newOctave; 00506 00507 else { 00508 _octave=defautOctave; 00509 } 00510 00511 // 00512 if(*p==',') 00513 { 00514 p++; 00515 myTabNotes.push_back(new Note(_note,_issharp,_octave,dura)); 00516 } 00517 00518 00519 00520 00521 } 00522 } 00523 //-------------------------------------- 00524 int Music::getNumbersNotes(void) const 00525 { 00526 return this->myTabNotes.size(); 00527 } 00528 00529 //-------------------------------------- 00530 void Music::play(Buzzer* _buzzer) 00531 { 00532 /* 00533 char buffer[10]; 00534 int duration=0; 00535 00536 for(int i=0; i< myTabNotes.size(); i++) { 00537 myTabNotes[i]->toString(buffer); 00538 duration=myTabNotes[i]->getDuration(); 00539 00540 pc.printf(" %s:%d \n\r",buffer,duration); 00541 } 00542 */ 00543 00544 for(int i=0; i< myTabNotes.size(); i++) { 00545 _buzzer->tone(myTabNotes[i]); 00546 } 00547 00548 00549 } 00550 //-------------------------------------- 00551 Music::~Music() 00552 { 00553 for(int i=0; i< myTabNotes.size(); i++) { 00554 delete(myTabNotes[i]); 00555 myTabNotes[i]=0; 00556 } 00557 } 00558 //-------------------------------------- 00559 00560 00561
Generated on Fri Jul 22 2022 18:19:14 by
1.7.2