Ajay Elango / Mbed 2 deprecated final_mini_project

Dependencies:   4DGL-uLCD-SE mbed

Fork of MPR121_Demo by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 
00023 #include <mbed.h>
00024 #include <string>
00025 #include <list>
00026 #include "uLCD_4DGL.h"
00027 #include <mpr121.h>
00028 
00029 InterruptIn interrupt(p26);
00030 InterruptIn interrupt1(p25);
00031 uLCD_4DGL uLCD(p13,p14,p11); 
00032 I2C i2c(p9, p10);
00033 I2C i2c1(p28, p27);
00034 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00035 Mpr121 mpr121_1(&i2c1, Mpr121::ADD_VSS);
00036 PwmOut speaker(p21);
00037 PwmOut speaker1(p22);
00038 Serial pc(USBTX, USBRX);
00039 
00040 int key_code=0;
00041 int key_code1=0;
00042 float beats = 0;
00043 float beats_var = 0;
00044 void pianoInterrupt() {
00045     
00046     int i=0;
00047     int value=mpr121.read(0x00);
00048     value +=mpr121.read(0x01)<<8;
00049 
00050     i=0;
00051  
00052     for (i=0; i<12; i++) {
00053         if (((value>>i)&0x01)==1) key_code=i+1;
00054         }
00055 }
00056 
00057 void drumsInterrupt() {
00058     
00059     int i=0;
00060     int value=mpr121_1.read(0x00);
00061     value +=mpr121_1.read(0x01)<<8;
00062      i=0;
00063     
00064     
00065     for (i=0; i<12; i++) {
00066         if (((value>>i)&0x01)==1) key_code1=i+1;
00067         }
00068   
00069 }
00070 
00071 void piano() {
00072       speaker.period(1.0/ (float (key_code*75.1) ) );
00073         speaker = float(key_code*2)/50.0;
00074         wait(.5);
00075 }
00076 
00077 void drums() {
00078 
00079           /*speaker1.period(1.0/ (float (key_code1*5.1) ) );
00080         speaker1 = float(key_code1*7)/ 30.0;*/
00081         wait(.5);
00082         
00083         switch(key_code1) {
00084         case 1:  
00085                beats = 1.0/ ( float (10.1) );
00086                speaker1.period( beats );
00087                speaker1.pulsewidth( beats );
00088         speaker1 = 0.009;
00089         break;
00090         case 2:  
00091         beats = 1.0/ ( float (15.1) );
00092         speaker1.period( beats );
00093          
00094         speaker1 = 0.9;
00095         break;
00096         case 3:    
00097         beats = 1.0/ ( float (20.1) );
00098         speaker1.period( beats );
00099         speaker1 = 0.9;
00100         break;
00101         case 4: 
00102         beats = 1.0/ ( float (25.1) );
00103         speaker1.period( beats );
00104         speaker1 = float(key_code1*2)/50.0;
00105         break;
00106         case 5:
00107         beats = 1.0/ ( float (30.1) );
00108            speaker1.period( beats );
00109            speaker1 = float(key_code1*2)/50.0;
00110         break;
00111         case 6: 
00112            beats = 1.0/ ( float (33.1) );
00113            speaker1.period( beats );
00114            speaker1 = float(key_code1*2)/50.0;
00115         break;
00116         case 7: 
00117         beats = 1.0/ ( float (40.1) );
00118         speaker1.period( beats );
00119         
00120            speaker1 = float(key_code1*2)/50.0;
00121         break;
00122         case 8: 
00123         beats = 1.0/ ( float (45.1) );
00124         speaker1.period( beats );
00125            speaker1 = 0.9;
00126         break;
00127         case 9: 
00128         beats = 1.0/ ( float (8.1) );
00129         speaker1.period( beats );
00130            speaker1 = float(key_code1*2)/50.0;
00131         break;
00132         case 10: 
00133         beats = 1.0/ ( float (5.1) );
00134         speaker1.period( beats );
00135         
00136            speaker1 = float(key_code1*2)/50.0;
00137         break;
00138         case 11: 
00139         beats = 1.0/ ( float (30.1) );
00140         speaker1.period( beats );
00141          speaker1.pulsewidth( 1.0/ ( float (10.1) ) );
00142          speaker1 = float(key_code1*2)/50.0;
00143         break;
00144         case 12:
00145         beats = 1.0/ ( float (10.1) );
00146         speaker1.period( beats );
00147                 speaker1.pulsewidth( 0.9 );
00148         speaker1 = float(key_code1*2)/50.0;
00149         break;
00150         
00151         default:
00152         break;
00153         }
00154        
00155 }
00156 int d;
00157 void LCD_Display() {
00158     uLCD.cls();
00159     uLCD.locate(0,0);
00160     
00161    
00162     
00163     
00164     uLCD.color(GREEN);
00165     uLCD.text_bold(ON);
00166     
00167     uLCD.printf("MUSIC ");
00168     uLCD.text_bold(ON);
00169     uLCD.printf("PLAYER" );
00170     uLCD.printf("\n\n" );
00171     
00172     uLCD.text_bold(OFF);
00173     uLCD.color(WHITE);
00174     uLCD.textbackground_color(BLUE);
00175     uLCD.printf("\nPiano\nKey Pressed %D", key_code );
00176     uLCD.color(GREEN);
00177     uLCD.textbackground_color(BLACK);
00178     uLCD.printf("\n\n" );
00179     uLCD.printf("Timbre: %0.2f", (float) speaker );
00180     uLCD.printf("\nPitch: %.2f", (float (key_code*75.1))  );
00181     uLCD.printf("\n" );
00182     uLCD.color(WHITE);
00183     uLCD.textbackground_color(BLUE);
00184     uLCD.printf("\nDrums\nKey Pressed %D", key_code1 );
00185     uLCD.color(GREEN);
00186     uLCD.textbackground_color(BLACK);
00187     uLCD.printf("\n\n" );
00188     uLCD.printf("Loudness: %0.2f", (float) speaker1 );
00189     beats_var = (beats == 0) ? 0 : (1.0 / (float)beats );
00190     d = (int) beats_var;
00191     uLCD.printf("\nBeats/Sec: %d", d  );
00192     uLCD.printf("\n" );
00193     uLCD.printf("\n\n" );
00194    
00195 }
00196 
00197 int main() {
00198   
00199    
00200   interrupt.fall(&pianoInterrupt);
00201   interrupt.mode(PullUp);
00202       interrupt1.fall(&drumsInterrupt);
00203     interrupt1.mode(PullUp);
00204 
00205     uLCD.background_color(BLACK);
00206     uLCD.cls();
00207     uLCD.color(WHITE);
00208         uLCD.textbackground_color(BLUE);
00209     uLCD.set_font(FONT_7X8);
00210     //uLCD.text_mode(OPAQUE);
00211     uLCD.text_mode(TRANSPARENT);
00212   while (1) {
00213        
00214       piano();
00215       drums();
00216       LCD_Display();
00217    }
00218 
00219 
00220 }
00221 
00222 
00223