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: 4DGL-uLCD-SE SDFileSystem Speaker mbed wave_player
Fork of Beat_Demo_full_full by
main.cpp
00001 #include <mbed.h> 00002 #include <mpr121.h> 00003 #include "uLCD_4DGL.h" 00004 #include "Speaker.h" 00005 #include "SDFileSystem.h" 00006 #include "wave_player.h" 00007 #include "wavfile.c" 00008 #include "wavfile.h" 00009 00010 #include <stdio.h> 00011 #include <stdlib.h> 00012 #include <string.h> 00013 #include "wave_player.h" 00014 00015 SDFileSystem sd(p5, p6, p7, p8,"sd"); //SD card 00016 00017 uLCD_4DGL uLCD(p13,p14,p11); 00018 00019 // Create the interrupt receiver object on pin 26 00020 InterruptIn interrupt1(p26); 00021 InterruptIn interrupt2(p25); 00022 // Setup the i2c bus on pins 9 and 10 00023 I2C i2c1(p9, p10); 00024 I2C i2c2(p28, p27); 00025 // Setup the Mpr121: 00026 // constructor(i2c object, i2c address of the mpr121) 00027 Mpr121 tones1pad(&i2c1, Mpr121::ADD_VSS); 00028 Mpr121 tones2pad(&i2c2, Mpr121::ADD_VSS); 00029 00030 Speaker mySpeaker(p21); 00031 InterruptIn pb1(p12); 00032 InterruptIn pb2(p29); 00033 00034 AnalogOut DACout(p18); 00035 wave_player waver(&DACout); 00036 00037 int key_code [2] = {88,88}; 00038 int touches; 00039 int music [1000] = { }; 00040 int music2 [1000] = { }; 00041 00042 const int NUM_SAMPLES = (WAVFILE_SAMPLES_PER_SECOND*0.2); 00043 int flagPb; 00044 bool flagPb2; 00045 bool flagInterrupt; 00046 00047 // Key hit/release interrupt routine for 1st touchpad 00048 void fallInterrupt1() 00049 { 00050 key_code[0]=88; 00051 key_code[1]=88; 00052 flagInterrupt = true; 00053 00054 int value=tones1pad.read(0x00); 00055 value +=tones1pad.read(0x01)<<8; 00056 00057 int k = 0; 00058 for(int i=0; i<12; i++) 00059 { 00060 if(((value>>i)&0x01) == 1) 00061 { 00062 key_code[k] = i; 00063 if (k < 1){ 00064 k=k+1; 00065 } 00066 } 00067 } 00068 uLCD.locate(1,2); 00069 //uLCD.printf("%2D%2D",key_code[0],key_code[1]); 00070 touches = touches + 1; 00071 } 00072 00073 //sets up interrupts for 2nd toucpad 00074 void fallInterrupt2() { 00075 key_code[0]=88; 00076 key_code[1]=88; 00077 flagInterrupt = true; 00078 00079 int value2=tones2pad.read(0x00); 00080 value2 +=tones2pad.read(0x01)<<8; 00081 00082 // puts key number out to LEDs for demo 00083 int k = 0; 00084 for(int j=0; j<12; j++) 00085 { 00086 if(((value2>>j)&0x01) == 1) 00087 { 00088 key_code [k] = j +12; 00089 if (k < 1){ 00090 k=k+1; 00091 } 00092 } 00093 } 00094 00095 uLCD.locate(1,3); 00096 //uLCD.printf("%2D%2D",key_code[0],key_code[1]); 00097 touches = touches + 1; 00098 } 00099 00100 void pb1_hit_interrupt (void) 00101 { 00102 if (flagPb == 0) 00103 { 00104 touches = 0; 00105 00106 uLCD.locate(1,1); 00107 uLCD.printf("Start Recording"); 00108 uLCD.locate(1,5); 00109 uLCD.printf("Waiting"); 00110 flagPb = 1; 00111 00112 } 00113 else if (flagPb == 1) 00114 { 00115 FILE * f = wavfile_open("/sd/Example14.wav"); 00116 uLCD.locate(1,1); 00117 uLCD.printf("Recording ended"); 00118 uLCD.locate(1,4); 00119 uLCD.printf("touches = %d",touches); 00120 uLCD.locate(1,6); 00121 /* 00122 for (int k = 0; k<=touches; k++) 00123 { 00124 uLCD.printf("%d",music[k]); 00125 } 00126 */ 00127 for (int k = 0; k<=touches; k++){ 00128 short waveform[NUM_SAMPLES]; 00129 double frequency = music[k]; //880.0; 00130 double frequency2 = music2[k]; 00131 int volume = 32000; 00132 int length = NUM_SAMPLES; 00133 int i; 00134 for(i=0;i<length;i++) { 00135 double t = (double) i / WAVFILE_SAMPLES_PER_SECOND; 00136 if (frequency2 == 0) 00137 { 00138 waveform[i] = volume*sin(frequency*t*2*3.1415926); 00139 } 00140 else if (frequency2 != 0) 00141 { 00142 waveform[i] = volume*(sin(frequency*t*2*3.1415926)+sin(frequency2*t*2*3.1415926))/2; 00143 } 00144 } 00145 if (music[k] == 0) 00146 { 00147 int length_short = length*2/9; 00148 fwrite(waveform,sizeof(short),length_short,f); 00149 } 00150 else if (music[k] != 0) 00151 { 00152 fwrite(waveform,sizeof(short),length,f); 00153 } 00154 } 00155 wavfile_close(f); 00156 00157 uLCD.locate(1,5); 00158 uLCD.printf("Done "); 00159 flagPb = 0; 00160 } 00161 } 00162 00163 void pb2_hit_interrupt (void) 00164 { 00165 flagPb2 = true; 00166 uLCD.cls(); 00167 uLCD.locate(1,1); 00168 uLCD.printf("Let the fun begin\n"); 00169 } 00170 00171 int main() 00172 { 00173 touches = -1; 00174 flagPb = 0; 00175 flagInterrupt = false; 00176 00177 key_code[0] = 88; 00178 key_code[1] = 88; 00179 00180 pb1.fall(&pb1_hit_interrupt); 00181 wait(.2); 00182 pb1.mode(PullUp); 00183 wait(.2); 00184 00185 pb2.mode(PullUp); 00186 wait(.2); 00187 pb2.fall(&pb2_hit_interrupt); 00188 wait(.2); 00189 00190 interrupt2.fall(&fallInterrupt2); 00191 interrupt2.mode(PullUp); 00192 interrupt1.fall(&fallInterrupt1); 00193 interrupt1.mode(PullUp); 00194 00195 while(1) 00196 { 00197 00198 if ( (flagPb == 1) && (flagInterrupt == false )) 00199 { 00200 00201 touches = touches + 1; 00202 music[touches] = 0; 00203 music2[touches] = 0; 00204 wait(0.05); 00205 //flagInterrupt = false; 00206 } 00207 00208 if (flagPb2 == true) 00209 { 00210 FILE *wave_file; 00211 uLCD.locate(1,6); 00212 uLCD.printf("Playing"); 00213 wave_file=fopen("/sd/Example14.wav","r"); 00214 waver.play(wave_file); 00215 fclose(wave_file); 00216 flagPb2 = false; 00217 uLCD.locate(1,7); 00218 uLCD.printf("Playing Ended"); 00219 } 00220 00221 if (key_code[1] == 88) 00222 { 00223 music2[touches] = 0; 00224 switch(key_code[0]) //uses a case for each individual touch sensor 00225 { 00226 case 0: 00227 mySpeaker.PlayNote(262,0,0.2,1); 00228 music[touches] = 262; 00229 flagInterrupt = false; 00230 mySpeaker.StopPlaying(); 00231 break; 00232 case 1: 00233 mySpeaker.PlayNote(277,0,0.2,1); 00234 music[touches] = 277; 00235 flagInterrupt = false; 00236 mySpeaker.StopPlaying(); 00237 break; 00238 case 2: 00239 mySpeaker.PlayNote(294,0,0.2,1); 00240 music[touches] = 294; 00241 flagInterrupt = false; 00242 mySpeaker.StopPlaying(); 00243 break; 00244 case 3: 00245 mySpeaker.PlayNote(311,0,0.2,1); 00246 music[touches] = 311; 00247 flagInterrupt = false; 00248 mySpeaker.StopPlaying(); 00249 break; 00250 case 4: 00251 mySpeaker.PlayNote(330,0,0.2,1); 00252 music[touches] = 330; 00253 flagInterrupt = false; 00254 mySpeaker.StopPlaying(); 00255 break; 00256 case 5: 00257 mySpeaker.PlayNote(349,0,0.2,1); 00258 music[touches] = 349; 00259 flagInterrupt = false; 00260 mySpeaker.StopPlaying(); 00261 break; 00262 case 6: 00263 mySpeaker.PlayNote(370,0,0.2,1); 00264 music[touches] = 370; 00265 flagInterrupt = false; 00266 mySpeaker.StopPlaying(); 00267 break; 00268 case 7: 00269 mySpeaker.PlayNote(392,0,0.2,1); 00270 music[touches] = 392; 00271 flagInterrupt = false; 00272 mySpeaker.StopPlaying(); 00273 break; 00274 case 8: 00275 mySpeaker.PlayNote(415,0,0.2,1); 00276 music[touches] = 415; 00277 flagInterrupt = false; 00278 mySpeaker.StopPlaying(); 00279 break; 00280 case 9: 00281 mySpeaker.PlayNote(440,0,0.2,1); 00282 music[touches] = 440; 00283 flagInterrupt = false; 00284 mySpeaker.StopPlaying(); 00285 break; 00286 case 10: 00287 mySpeaker.PlayNote(466,0,0.2,1); 00288 music[touches] = 466; 00289 flagInterrupt = false; 00290 mySpeaker.StopPlaying(); 00291 break; 00292 case 11: 00293 mySpeaker.PlayNote(494,0,0.2,1); 00294 music[touches] = 494; 00295 flagInterrupt = false; 00296 mySpeaker.StopPlaying(); 00297 break; 00298 case 12: 00299 mySpeaker.PlayNote(523,0,0.2,1); 00300 music[touches] = 523; 00301 flagInterrupt = false; 00302 mySpeaker.StopPlaying(); 00303 break; 00304 case 13: 00305 mySpeaker.PlayNote(554,0,0.2,1); 00306 music[touches] = 554; 00307 flagInterrupt = false; 00308 mySpeaker.StopPlaying(); 00309 break; 00310 case 14: 00311 mySpeaker.PlayNote(587,0,0.2,1); 00312 music[touches] = 587; 00313 flagInterrupt = false; 00314 mySpeaker.StopPlaying(); 00315 break; 00316 case 15: 00317 mySpeaker.PlayNote(622,0,0.2,1); 00318 music[touches] = 622; 00319 flagInterrupt = false; 00320 mySpeaker.StopPlaying(); 00321 break; 00322 case 16: 00323 mySpeaker.PlayNote(659,0,0.2,1); 00324 music[touches] = 659; 00325 flagInterrupt = false; 00326 mySpeaker.StopPlaying(); 00327 break; 00328 case 17: 00329 mySpeaker.PlayNote(698,0,0.2,1); 00330 music[touches] = 698; 00331 flagInterrupt = false; 00332 mySpeaker.StopPlaying(); 00333 break; 00334 case 18: 00335 mySpeaker.PlayNote(740,0,0.2,1); 00336 music[touches] = 740; 00337 flagInterrupt = false; 00338 mySpeaker.StopPlaying(); 00339 break; 00340 case 19: 00341 mySpeaker.PlayNote(784,0,0.2,1); 00342 music[touches] = 784; 00343 flagInterrupt = false; 00344 mySpeaker.StopPlaying(); 00345 break; 00346 case 20: 00347 mySpeaker.PlayNote(830,0,0.2,1); 00348 music[touches] = 830; 00349 flagInterrupt = false; 00350 mySpeaker.StopPlaying(); 00351 break; 00352 case 21: 00353 mySpeaker.PlayNote(880,0,0.2,1); 00354 music[touches] = 830; 00355 flagInterrupt = false; 00356 mySpeaker.StopPlaying(); 00357 break; 00358 case 22: 00359 mySpeaker.PlayNote(932,0,0.2,1); 00360 music[touches] = 932; 00361 flagInterrupt = false; 00362 mySpeaker.StopPlaying(); 00363 break; 00364 case 23: 00365 mySpeaker.PlayNote(988,0,0.2,1); 00366 music[touches] = 988; 00367 flagInterrupt = false; 00368 mySpeaker.StopPlaying(); 00369 break; 00370 00371 default: 00372 music[touches] = 0; 00373 flagInterrupt = false; 00374 00375 } 00376 } 00377 00378 if (key_code[1] != 88) //Overtunes 00379 { 00380 switch(key_code[0]) //uses a case for each individual touch sensor 00381 { 00382 case 0: 00383 music[touches] = 262; 00384 break; 00385 case 1: 00386 music[touches] = 277; 00387 break; 00388 case 2: 00389 music[touches] = 294; 00390 break; 00391 case 3: 00392 music[touches] = 311; 00393 break; 00394 case 4: 00395 music[touches] = 330; 00396 break; 00397 case 5: 00398 music[touches] = 349; 00399 break; 00400 case 6: 00401 music[touches] = 370; 00402 break; 00403 case 7: 00404 music[touches] = 392; 00405 break; 00406 case 8: 00407 music[touches] = 415; 00408 break; 00409 case 9: 00410 music[touches] = 440; 00411 break; 00412 case 10: 00413 music[touches] = 466; 00414 break; 00415 case 11: 00416 music[touches] = 494; 00417 break; 00418 case 12: 00419 music[touches] = 523; 00420 break; 00421 case 13: 00422 music[touches] = 554; 00423 break; 00424 case 14: 00425 music[touches] = 587; 00426 break; 00427 case 15: 00428 music[touches] = 622; 00429 break; 00430 case 16: 00431 music[touches] = 659; 00432 break; 00433 case 17: 00434 music[touches] = 698; 00435 break; 00436 case 18: 00437 music[touches] = 740; 00438 break; 00439 case 19: 00440 music[touches] = 784; 00441 break; 00442 case 20: 00443 music[touches] = 830; 00444 break; 00445 case 21: 00446 music[touches] = 830; 00447 break; 00448 case 22: 00449 music[touches] = 932; 00450 break; 00451 case 23: 00452 music[touches] = 988; 00453 break; 00454 00455 default: 00456 music[touches] = 0; 00457 00458 } 00459 00460 switch(key_code[1]) //uses a case for each individual touch sensor 00461 { 00462 case 0: 00463 mySpeaker.PlayNote(music[touches],262,0.2,1); 00464 music2[touches] = 262; 00465 flagInterrupt = false; 00466 mySpeaker.StopPlaying(); 00467 break; 00468 case 1: 00469 mySpeaker.PlayNote(music[touches],277,0.2,1); 00470 music2[touches] = 277; 00471 flagInterrupt = false; 00472 mySpeaker.StopPlaying(); 00473 break; 00474 case 2: 00475 mySpeaker.PlayNote(music[touches],294,0.2,1); 00476 music2[touches] = 294; 00477 flagInterrupt = false; 00478 mySpeaker.StopPlaying(); 00479 break; 00480 case 3: 00481 mySpeaker.PlayNote(music[touches],311,0.2,1); 00482 music2[touches] = 311; 00483 flagInterrupt = false; 00484 mySpeaker.StopPlaying(); 00485 break; 00486 case 4: 00487 mySpeaker.PlayNote(music[touches],330,0.2,1); 00488 music2[touches] = 330; 00489 flagInterrupt = false; 00490 mySpeaker.StopPlaying(); 00491 break; 00492 case 5: 00493 mySpeaker.PlayNote(music[touches],349,0.2,1); 00494 music2[touches] = 349; 00495 flagInterrupt = false; 00496 mySpeaker.StopPlaying(); 00497 break; 00498 case 6: 00499 mySpeaker.PlayNote(music[touches],370,0.2,1); 00500 music2[touches] = 370; 00501 flagInterrupt = false; 00502 mySpeaker.StopPlaying(); 00503 break; 00504 case 7: 00505 mySpeaker.PlayNote(music[touches],392,0.2,1); 00506 music2[touches] = 392; 00507 flagInterrupt = false; 00508 mySpeaker.StopPlaying(); 00509 break; 00510 case 8: 00511 mySpeaker.PlayNote(music[touches],415,0.2,1); 00512 music2[touches] = 415; 00513 flagInterrupt = false; 00514 mySpeaker.StopPlaying(); 00515 break; 00516 case 9: 00517 mySpeaker.PlayNote(music[touches],440,0.2,1); 00518 music2[touches] = 440; 00519 flagInterrupt = false; 00520 mySpeaker.StopPlaying(); 00521 break; 00522 case 10: 00523 mySpeaker.PlayNote(music[touches],466,0.2,1); 00524 music2[touches] = 466; 00525 flagInterrupt = false; 00526 mySpeaker.StopPlaying(); 00527 break; 00528 case 11: 00529 mySpeaker.PlayNote(music[touches],494,0.2,1); 00530 music2[touches] = 494; 00531 flagInterrupt = false; 00532 mySpeaker.StopPlaying(); 00533 break; 00534 case 12: 00535 mySpeaker.PlayNote(music[touches],523,0.2,1); 00536 music2[touches] = 523; 00537 flagInterrupt = false; 00538 mySpeaker.StopPlaying(); 00539 break; 00540 case 13: 00541 mySpeaker.PlayNote(music[touches],554,0.2,1); 00542 music2[touches] = 554; 00543 flagInterrupt = false; 00544 mySpeaker.StopPlaying(); 00545 break; 00546 case 14: 00547 mySpeaker.PlayNote(music[touches],587,0.2,1); 00548 music2[touches] = 587; 00549 flagInterrupt = false; 00550 mySpeaker.StopPlaying(); 00551 break; 00552 case 15: 00553 mySpeaker.PlayNote(music[touches],622,0.2,1); 00554 music2[touches] = 622; 00555 flagInterrupt = false; 00556 mySpeaker.StopPlaying(); 00557 break; 00558 case 16: 00559 mySpeaker.PlayNote(music[touches],659,0.2,1); 00560 music2[touches] = 659; 00561 flagInterrupt = false; 00562 mySpeaker.StopPlaying(); 00563 break; 00564 case 17: 00565 mySpeaker.PlayNote(music[touches],698,0.2,1); 00566 music2[touches] = 698; 00567 flagInterrupt = false; 00568 mySpeaker.StopPlaying(); 00569 break; 00570 case 18: 00571 mySpeaker.PlayNote(music[touches],740,0.2,1); 00572 music2[touches] = 740; 00573 flagInterrupt = false; 00574 mySpeaker.StopPlaying(); 00575 break; 00576 case 19: 00577 mySpeaker.PlayNote(music[touches],784,0.2,1); 00578 music2[touches] = 784; 00579 flagInterrupt = false; 00580 mySpeaker.StopPlaying(); 00581 break; 00582 case 20: 00583 mySpeaker.PlayNote(music[touches],830,0.2,1); 00584 music2[touches] = 830; 00585 flagInterrupt = false; 00586 mySpeaker.StopPlaying(); 00587 break; 00588 case 21: 00589 mySpeaker.PlayNote(music[touches],880,0.2,1); 00590 music2[touches] = 830; 00591 flagInterrupt = false; 00592 mySpeaker.StopPlaying(); 00593 break; 00594 case 22: 00595 mySpeaker.PlayNote(music[touches],932,0.2,1); 00596 music2[touches] = 932; 00597 flagInterrupt = false; 00598 mySpeaker.StopPlaying(); 00599 break; 00600 case 23: 00601 mySpeaker.PlayNote(music[touches],988,0.2,1); 00602 music2[touches] = 988; 00603 flagInterrupt = false; 00604 mySpeaker.StopPlaying(); 00605 break; 00606 00607 default: 00608 music2[touches] = 0; 00609 flagInterrupt = false; 00610 00611 } 00612 } 00613 } 00614 } 00615
Generated on Tue Aug 23 2022 10:31:42 by
1.7.2
