Integrated Music Player (Basic Drums & Piano)

Dependencies:   4DGL-uLCD-SE mbed

Fork of MPR121_Demo by jim hamblen

Revision:
1:2af7b5d46614
Parent:
0:e09703934ff4
--- a/main.cpp	Wed Mar 16 01:49:13 2011 +0000
+++ b/main.cpp	Sun Mar 23 07:18:02 2014 +0000
@@ -23,102 +23,200 @@
 #include <mbed.h>
 #include <string>
 #include <list>
-
+#include "uLCD_4DGL.h"
 #include <mpr121.h>
 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
-
-// Create the interrupt receiver object on pin 26
 InterruptIn interrupt(p26);
-
-// Setup the Serial to the PC for debugging
+InterruptIn interrupt1(p25);
+uLCD_4DGL uLCD(p13,p14,p11); 
+I2C i2c(p9, p10);
+I2C i2c1(p28, p27);
+Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
+Mpr121 mpr121_1(&i2c1, Mpr121::ADD_VSS);
+PwmOut speaker(p21);
+PwmOut speaker1(p22);
 Serial pc(USBTX, USBRX);
 
-// Setup the i2c bus on pins 28 and 27
-I2C i2c(p9, p10);
-
-// Setup the Mpr121:
-// constructor(i2c object, i2c address of the mpr121)
-Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
-
-void fallInterrupt() {
-    int key_code=0;
+int key_code=0;
+int key_code1=0;
+float beats = 0;
+float beats_var = 0;
+void pianoInterrupt() {
+    
     int i=0;
     int value=mpr121.read(0x00);
     value +=mpr121.read(0x01)<<8;
-    // LED demo mod by J. Hamblen
-    //pc.printf("MPR value: %x \r\n", value);
+
     i=0;
-    // puts key number out to LEDs for demo
+ 
     for (i=0; i<12; i++) {
         if (((value>>i)&0x01)==1) key_code=i+1;
         }
-    led4=key_code & 0x01;
-    led3=(key_code>>1) & 0x01;
-    led2=(key_code>>2) & 0x01;
-    led1=(key_code>>3) & 0x01;
 }
-int main() {
-
-    pc.printf("\nHello from the mbed & mpr121\n\r");
-
-    unsigned char dataArray[2];
-    int key;
-    int count = 0;
 
-    pc.printf("Test 1: read a value: \r\n");
-    dataArray[0] = mpr121.read(AFE_CFG);
-    pc.printf("Read value=%x\r\n\n",dataArray[0]);
-
-    pc.printf("Test 2: read a value: \r\n");
-    dataArray[0] = mpr121.read(0x5d);
-    pc.printf("Read value=%x\r\n\n",dataArray[0]);
+void drumsInterrupt() {
+    
+    int i=0;
+    int value=mpr121_1.read(0x00);
+    value +=mpr121_1.read(0x01)<<8;
+     i=0;
+    
+    
+    for (i=0; i<12; i++) {
+        if (((value>>i)&0x01)==1) key_code1=i+1;
+        }
+  
+}
 
-    pc.printf("Test 3: write & read a value: \r\n");
-    mpr121.read(ELE0_T);
-    mpr121.write(ELE0_T,0x22);
-    dataArray[0] = mpr121.read(ELE0_T);
-    pc.printf("Read value=%x\r\n\n",dataArray[0]);
+void piano() {
+      speaker.period(1.0/ (float (key_code*75.1) ) );
+        speaker = float(key_code*2)/50.0;
+        wait(.5);
+}
 
-    pc.printf("Test 4: Write many values: \r\n");
-    unsigned char data[] = {0x1,0x3,0x5,0x9,0x15,0x25,0x41};
-    mpr121.writeMany(0x42,data,7);
+void drums() {
 
-    // Now read them back ..
-    key = 0x42;
-    count = 0;
-    while (count < 7) {
-        char result = mpr121.read(key);
-        key++;
-        count++;
-        pc.printf("Read value: '%x'=%x\n\r",key,result);
-    }
+          /*speaker1.period(1.0/ (float (key_code1*5.1) ) );
+        speaker1 = float(key_code1*7)/ 30.0;*/
+        wait(.5);
+        
+        switch(key_code1) {
+        case 1:  
+               beats = 1.0/ ( float (10.1) );
+               speaker1.period( beats );
+               speaker1.pulsewidth( beats );
+        speaker1 = 0.009;
+        break;
+        case 2:  
+        beats = 1.0/ ( float (15.1) );
+        speaker1.period( beats );
+         
+        speaker1 = 0.9;
+        break;
+        case 3:    
+        beats = 1.0/ ( float (20.1) );
+        speaker1.period( beats );
+        speaker1 = 0.9;
+        break;
+        case 4: 
+        beats = 1.0/ ( float (25.1) );
+        speaker1.period( beats );
+        speaker1 = float(key_code1*2)/50.0;
+        break;
+        case 5:
+        beats = 1.0/ ( float (30.1) );
+           speaker1.period( beats );
+           speaker1 = float(key_code1*2)/50.0;
+        break;
+        case 6: 
+           beats = 1.0/ ( float (33.1) );
+           speaker1.period( beats );
+           speaker1 = float(key_code1*2)/50.0;
+        break;
+        case 7: 
+        beats = 1.0/ ( float (40.1) );
+        speaker1.period( beats );
+        
+           speaker1 = float(key_code1*2)/50.0;
+        break;
+        case 8: 
+        beats = 1.0/ ( float (45.1) );
+        speaker1.period( beats );
+           speaker1 = 0.9;
+        break;
+        case 9: 
+        beats = 1.0/ ( float (8.1) );
+        speaker1.period( beats );
+           speaker1 = float(key_code1*2)/50.0;
+        break;
+        case 10: 
+        beats = 1.0/ ( float (5.1) );
+        speaker1.period( beats );
+        
+           speaker1 = float(key_code1*2)/50.0;
+        break;
+        case 11: 
+        beats = 1.0/ ( float (30.1) );
+        speaker1.period( beats );
+         speaker1.pulsewidth( 1.0/ ( float (10.1) ) );
+         speaker1 = float(key_code1*2)/50.0;
+        break;
+        case 12:
+        beats = 1.0/ ( float (10.1) );
+        speaker1.period( beats );
+                speaker1.pulsewidth( 0.9 );
+        speaker1 = float(key_code1*2)/50.0;
+        break;
+        
+        default:
+        break;
+        }
+       
+}
+int d;
+void LCD_Display() {
+    uLCD.cls();
+    uLCD.locate(0,0);
+    
+   
+    
+    
+    uLCD.color(GREEN);
+    uLCD.text_bold(ON);
+    
+    uLCD.printf("MUSIC ");
+    uLCD.text_bold(ON);
+    uLCD.printf("PLAYER" );
+    uLCD.printf("\n\n" );
+    
+    uLCD.text_bold(OFF);
+    uLCD.color(WHITE);
+    uLCD.textbackground_color(BLUE);
+    uLCD.printf("\nPiano\nKey Pressed %D", key_code );
+    uLCD.color(GREEN);
+    uLCD.textbackground_color(BLACK);
+    uLCD.printf("\n\n" );
+    uLCD.printf("Timbre: %0.2f", (float) speaker );
+    uLCD.printf("\nPitch: %.2f", (float (key_code*75.1))  );
+    uLCD.printf("\n" );
+    uLCD.color(WHITE);
+    uLCD.textbackground_color(BLUE);
+    uLCD.printf("\nDrums\nKey Pressed %D", key_code1 );
+    uLCD.color(GREEN);
+    uLCD.textbackground_color(BLACK);
+    uLCD.printf("\n\n" );
+    uLCD.printf("Loudness: %0.2f", (float) speaker1 );
+    beats_var = (beats == 0) ? 0 : (1.0 / (float)beats );
+    d = (int) beats_var;
+    uLCD.printf("\nBeats/Sec: %d", d  );
+    uLCD.printf("\n" );
+    uLCD.printf("\n\n" );
+   
+}
 
-    pc.printf("Test 5: Read Electrodes:\r\n");
-    key = ELE0_T;
-    count = 0;
-    while (count < 24) {
-        char result = mpr121.read(key);
-        pc.printf("Read key:%x value:%x\n\r",key,result);
-        key++;
-        count++;
-    }
-    pc.printf("--------- \r\n\n");
+int main() {
+  
+   
+  interrupt.fall(&pianoInterrupt);
+  interrupt.mode(PullUp);
+      interrupt1.fall(&drumsInterrupt);
+    interrupt1.mode(PullUp);
 
-    // mpr121.setProximityMode(true);
-
-    pc.printf("ELE_CFG=%x", mpr121.read(ELE_CFG));
+    uLCD.background_color(BLACK);
+    uLCD.cls();
+    uLCD.color(WHITE);
+        uLCD.textbackground_color(BLUE);
+    uLCD.set_font(FONT_7X8);
+    //uLCD.text_mode(OPAQUE);
+    uLCD.text_mode(TRANSPARENT);
+  while (1) {
+       
+      piano();
+      drums();
+      LCD_Display();
+   }
 
-    interrupt.fall(&fallInterrupt);
-    interrupt.mode(PullUp);
 
-    while (1) {
-        wait(5);
-        pc.printf(".");
-    }
 }