ece 4180 project 2019

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem FATFileSystem

Files at this revision

API Documentation at this revision

Comitter:
rhuang77
Date:
Sat Apr 20 21:47:57 2019 +0000
Parent:
2:270569d33ef1
Commit message:
finalizing details

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Apr 20 19:09:27 2019 +0000
+++ b/main.cpp	Sat Apr 20 21:47:57 2019 +0000
@@ -1,5 +1,6 @@
 #include <mbed.h>
 #include <mpr121.h>
+#include <math.h>
 #include "mbed.h"
 #include "uLCD_4DGL.h"
 #include "SDFileSystem.h"
@@ -88,7 +89,7 @@
 int isplay = 0;
 int islearn = 0;
 float volume = 0.25;
-int octave = 3;
+float octave = 3;
 
 
 void readblue()
@@ -114,6 +115,7 @@
                             islearn = 1;
                             isplay = 0;
                             songcode = blue.getc() - '0';
+                            pc.printf("songcode = %d",songcode);
                         }
                     }
                     //seekbar mode
@@ -124,6 +126,8 @@
                         //octave change
                         if (bnum == '0') {
                             octave = blue.getc() - '0';
+                            octave = octave - 3;
+                            octave = pow(2, octave);
                         }
                         //volume change
                         if (bnum == '1') {
@@ -138,7 +142,6 @@
                 }
             }
         }
-        pc.printf("bye\n");
         Thread::wait(200);
     }
 }
@@ -154,103 +157,290 @@
     uLCD.printf("Initializing...\n");
     wait(1.0);
     uLCD.cls();
-
-
-    int song[14] = {1, 1,8,8,10,10,8,6,6,5,5,3,3,1};
-    char note[14] = {'C','C','G','G','A','A','G','F','F','E','E','D','D','C'};
     int i= 0;
-    Thread thread;
-    //thread.start(callback(readblue, (void *)"foo"));
+    int j=0;
+    Thread thread;;
     thread.start(readblue);
 
     while (1) {
-        // check blutooth input
-        //readblue();
+        if(islearn == 1) {
+            pc.printf("songcode in learn = %d",songcode);
+            if (songcode == 0) { //twinkle
+                int song0[14] = {1, 1,8,8,10,10,8,6,6,5,5,3,3,1};
+                char note0[14] = {'C','C','G','G','A','A','G','F','F','E','E','D','D','C'};
+                j = 14;
+                uLCD.cls();
+                //uLCD.printf("starting \nTwinkle Twinkle \nLittle Star");
+                //wait(4.0);
+                while(i <j) {
+                    uLCD.text_width(3);
+                    uLCD.text_height(3);
+                    uLCD.locate(2,2);
 
-        //char bnum = 0;
-//        char bnum1 = 0;
-//        char bnum
-//        bnum = blue.getc();
-        if(false) {
-            uLCD.printf("starting \nTwinkle Twinkle \nLittle Star");
-            wait(4.0);
-            uLCD.cls();
-            while(i <14) {
-                uLCD.text_width(3);
-                uLCD.text_height(3);
-                uLCD.locate(2,2);
+                    uLCD.printf("%c",note0[i]);
 
-                uLCD.printf("%c",note[i]);
+                    if(song0[i]==key_code) {
+                        myRGBled.write(0.0,1.0,0.0 );
+                        uLCD.cls();
+                        i++;
+                    } else if (song0[i] != key_code && key_code != 0) {
+                        myRGBled.write(1.0,0.0,0.0 );
+                    } else if (key_code == 0) {
+                        myRGBled.write(0.0,0.0,0.0 );
+                        pc.printf("STAR");
+                    }
 
-                if(song[i]==key_code) {
-                    myRGBled.write(0.0,1.0,0.0 );
-                    uLCD.cls();
-                    i++;
-                } else if (song[i] != key_code && key_code != 0) {
-                    myRGBled.write(1.0,0.0,0.0 );
-                } else if (key_code == 0) {
-                    myRGBled.write(0.0,0.0,0.0 );
-                }
+                    switch(key_code) { //uses a case statement for each individual touch sensor
 
 
+                        case 1:
+                            mySpeaker.PlayNote(261.6256, 0.5, volume);
+                            break;
+                        case 2:
+                            mySpeaker.PlayNote(277.1826, 0.5, volume);
+                            //mySpeaker.PlayNote(261.6256*2, 0.5, volume);
+                            break;
+                        case 3:
+                            mySpeaker.PlayNote(293.6648, 0.5, volume);
+                            break;
+                        case 4:
+                            mySpeaker.PlayNote(311.1270, 0.5, volume);
+                            break;
+                        case 5:
+                            mySpeaker.PlayNote(329.6276, 0.5, volume);
+                            break;
+                        case 6:
+                            mySpeaker.PlayNote(349.2282, 0.5, volume);
+                            break;
+                        case 7:
+                            mySpeaker.PlayNote(369.9944, 0.5, volume);
+                            break;
+                        case 8:
+                            mySpeaker.PlayNote(391.9954, 0.5, volume);
+                            break;
+                        case 9:
+                            mySpeaker.PlayNote(415.3047, 0.5, volume);
+                            break;
+                        case 10:
+                            mySpeaker.PlayNote(440.0, 0.5, volume);
+                            break;
+                        case 11:
+                            mySpeaker.PlayNote(466.1638, 0.5, volume);
+                            break;
+                        case 12:
+                            mySpeaker.PlayNote(493.8833, 0.5, volume);
+                            break;
+                    }
+                }
+                i = 0;
+                uLCD.printf("Congratulations! \nYou learned \nTwinkle Twinkle \nLittle Star");
+            } else if (songcode == 1) { //mary had a little lamb
+                int song1[25] = {5, 3, 1, 3, 5, 5, 5, 3, 3, 3, 5, 8, 8, 5, 3, 1, 3, 5, 5, 5, 5, 3, 5, 3, 1};
+                char note1[25] = {'E', 'D', 'C', 'D', 'E','E','E', 'D','D','D','E', 'G','G','E', 'D', 'C', 'D', 'E','E','E','E','D','E','D','C'};
+                j = 25;
+                uLCD.cls();
+                //uLCD.printf("starting \nTwinkle Twinkle \nLittle Star");
+                //wait(4.0);
+                while(i <j) {
+                    uLCD.text_width(3);
+                    uLCD.text_height(3);
+                    uLCD.locate(2,2);
 
-                switch(key_code) { //uses a case statement for each individual touch sensor
+                    uLCD.printf("%c",note1[i]);
+
+                    if(song1[i]==key_code) {
+                        myRGBled.write(0.0,1.0,0.0 );
+                        uLCD.cls();
+                        i++;
+                    } else if (song1[i] != key_code && key_code != 0) {
+                        myRGBled.write(1.0,0.0,0.0 );
+                    } else if (key_code == 0) {
+                        myRGBled.write(0.0,0.0,0.0 );
+                        pc.printf("MARY");
+                    }
+
+                    switch(key_code) { //uses a case statement for each individual touch sensor
 
 
-                    case 1:
-                        mySpeaker.PlayNote(261.6256, 0.5, volume);
-                        break;
-                    case 2:
-                        mySpeaker.PlayNote(277.1826, 0.5, volume);
-                        break;
-                    case 3:
-                        mySpeaker.PlayNote(293.6648, 0.5, volume);
-                        break;
-                    case 4:
-                        mySpeaker.PlayNote(311.1270, 0.5, volume);
-                        break;
-                    case 5:
-                        mySpeaker.PlayNote(329.6276, 0.5, volume);
-                        break;
-                    case 6:
-                        mySpeaker.PlayNote(349.2282, 0.5, volume);
-                        break;
-                    case 7:
-                        mySpeaker.PlayNote(369.9944, 0.5, volume);
-                        break;
-                    case 8:
-                        mySpeaker.PlayNote(391.9954, 0.5, volume);
-                        break;
-                    case 9:
-                        mySpeaker.PlayNote(415.3047, 0.5, volume);
-                        break;
-                    case 10:
-                        mySpeaker.PlayNote(440.0, 0.5, volume);
-                        break;
-                    case 11:
-                        mySpeaker.PlayNote(466.1638, 0.5, volume);
-                        break;
-                    case 12:
-                        mySpeaker.PlayNote(493.8833, 0.5, volume);
-                        break;
+                        case 1:
+                            mySpeaker.PlayNote(261.6256, 0.5, volume);
+                            break;
+                        case 2:
+                            mySpeaker.PlayNote(277.1826, 0.5, volume);
+                            //mySpeaker.PlayNote(261.6256*2, 0.5, volume);
+                            break;
+                        case 3:
+                            mySpeaker.PlayNote(293.6648, 0.5, volume);
+                            break;
+                        case 4:
+                            mySpeaker.PlayNote(311.1270, 0.5, volume);
+                            break;
+                        case 5:
+                            mySpeaker.PlayNote(329.6276, 0.5, volume);
+                            break;
+                        case 6:
+                            mySpeaker.PlayNote(349.2282, 0.5, volume);
+                            break;
+                        case 7:
+                            mySpeaker.PlayNote(369.9944, 0.5, volume);
+                            break;
+                        case 8:
+                            mySpeaker.PlayNote(391.9954, 0.5, volume);
+                            break;
+                        case 9:
+                            mySpeaker.PlayNote(415.3047, 0.5, volume);
+                            break;
+                        case 10:
+                            mySpeaker.PlayNote(440.0, 0.5, volume);
+                            break;
+                        case 11:
+                            mySpeaker.PlayNote(466.1638, 0.5, volume);
+                            break;
+                        case 12:
+                            mySpeaker.PlayNote(493.8833, 0.5, volume);
+                            break;
+                    }
                 }
+                i = 0;
+                uLCD.printf("Congratulations! \nYou learned \nMary Had a \nLittle Lamb");
+            } else if (songcode == 2) { //hot cross buns
+                int song2[17] = {5, 3, 1, 5, 3, 1, 1, 1, 1, 1, 3, 3, 3, 3, 5, 3, 1};
+                char note2[17] = {'E','D','C','E','D','C', 'C','C','C','C','D','D','D','D','E','D','C'};
+                j = 17;
+                uLCD.cls();
+                //uLCD.printf("starting \nTwinkle Twinkle \nLittle Star");
+                //wait(4.0);
+                while(i <j) {
+                    uLCD.text_width(3);
+                    uLCD.text_height(3);
+                    uLCD.locate(2,2);
+
+                    uLCD.printf("%c",note2[i]);
+
+                    if(song2[i]==key_code) {
+                        myRGBled.write(0.0,1.0,0.0 );
+                        uLCD.cls();
+                        i++;
+                    } else if (song2[i] != key_code && key_code != 0) {
+                        myRGBled.write(1.0,0.0,0.0 );
+                    } else if (key_code == 0) {
+                        myRGBled.write(0.0,0.0,0.0 );
+                        pc.printf("BUNS");
+                    }
+
+                    switch(key_code) { //uses a case statement for each individual touch sensor
 
 
+                        case 1:
+                            mySpeaker.PlayNote(261.6256, 0.5, volume);
+                            break;
+                        case 2:
+                            mySpeaker.PlayNote(277.1826, 0.5, volume);
+                            //mySpeaker.PlayNote(261.6256*2, 0.5, volume);
+                            break;
+                        case 3:
+                            mySpeaker.PlayNote(293.6648, 0.5, volume);
+                            break;
+                        case 4:
+                            mySpeaker.PlayNote(311.1270, 0.5, volume);
+                            break;
+                        case 5:
+                            mySpeaker.PlayNote(329.6276, 0.5, volume);
+                            break;
+                        case 6:
+                            mySpeaker.PlayNote(349.2282, 0.5, volume);
+                            break;
+                        case 7:
+                            mySpeaker.PlayNote(369.9944, 0.5, volume);
+                            break;
+                        case 8:
+                            mySpeaker.PlayNote(391.9954, 0.5, volume);
+                            break;
+                        case 9:
+                            mySpeaker.PlayNote(415.3047, 0.5, volume);
+                            break;
+                        case 10:
+                            mySpeaker.PlayNote(440.0, 0.5, volume);
+                            break;
+                        case 11:
+                            mySpeaker.PlayNote(466.1638, 0.5, volume);
+                            break;
+                        case 12:
+                            mySpeaker.PlayNote(493.8833, 0.5, volume);
+                            break;
+                    }
+                }
+                i = 0;
+                uLCD.printf("Congratulations! \nYou learned \nHot Cross \nBuns");
             }
-            i = 0;
-            uLCD.printf("Congratulations! \nYou learned \nTwinkle Twinkle \nLittle Star");
-            wait(4.0);
+            //wait(4.0);
+            uLCD.cls();
+            islearn=0;
+            //Thread::wait(200);
+        } else if(isplay == 1) {
             uLCD.cls();
-        } else if(false) {
-            uLCD.printf("Playing song");
-            wait(2.0);
-            wave_file=fopen("/sd/songs/mary.wav","r");
-            waver.play(wave_file);
-            fclose(wave_file);
-//           wave_file=fopen("/sd/wavfiles/imperial_march.wav","r");
-//          waver.play(wave_file);
-//          fclose(wave_file);
-
+//            uLCD.printf("Playing song");
+//            wait(2.0);
+            if (songcode == 0) {
+                mySpeaker.PlayNote(261.6256, 0.5, volume); //C
+                mySpeaker.PlayNote(261.6256, 0.5, volume); //C
+                mySpeaker.PlayNote(391.9954, 0.5, volume); //G
+                mySpeaker.PlayNote(391.9954, 0.5, volume); //G
+                mySpeaker.PlayNote(440.0, 0.5, volume); //A
+                mySpeaker.PlayNote(440.0, 0.5, volume); //A
+                mySpeaker.PlayNote(391.9954, 1.0, volume); //G
+                mySpeaker.PlayNote(349.2282, 0.5, volume); //F
+                mySpeaker.PlayNote(349.2282, 0.5, volume); //F
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(329.6276, 0.5, volume); //E
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(261.6256, 0.5, volume); //C
+            } else if (songcode == 1) {
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(261.6256, 0.5, volume); //C
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(329.6276, 1.0, volume); // E
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(293.6648, 1.0, volume); //D
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(391.9954, 0.5, volume); //G
+                mySpeaker.PlayNote(391.9954, 1.0, volume); //G
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(261.6256, 0.5, volume); //C
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(329.6276, 1.0, volume); // E
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(261.6256, 1.0, volume); //C
+            } else if (songcode == 2) {
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(261.6256, 1.0, volume); //C
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(261.6256, 1.0, volume); //C
+                mySpeaker.PlayNote(261.6256, 0.5, volume); //C
+                mySpeaker.PlayNote(261.6256, 0.5, volume); //C
+                mySpeaker.PlayNote(261.6256, 0.5, volume); //C
+                mySpeaker.PlayNote(261.6256, 0.5, volume); //C
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(329.6276, 0.5, volume); // E
+                mySpeaker.PlayNote(293.6648, 0.5, volume); //D
+                mySpeaker.PlayNote(261.6256, 1.0, volume); //C
+            }
+            isplay = 0;
         }
 
         else {
@@ -266,13 +456,7 @@
                     //printf("%d", key_code);
 
                     myRGBled.write(0.0,0.0,1.0 );
-//                  wave_file=fopen("/sd/songs/mary.wav","r");
-//                if(wave_file==NULL) printf("file open error!\n\n\r");
-//                printf("test");
-//                  waver.play(wave_file);
-//                  fclose(wave_file);
-                    mySpeaker.PlayNote(261.6256, 0.5, volume);
-                    pc.printf("hello\n");
+                    mySpeaker.PlayNote(261.6256 * octave, 0.5, volume);
                     break;
                 case 2:
                     uLCD.cls();
@@ -280,8 +464,9 @@
                     uLCD.text_width(5);
                     uLCD.text_height(5);
                     uLCD.printf("CS");
-                    myRGBled.write(1.0,0.0,0.0 );
-                    mySpeaker.PlayNote(277.1826, 0.5, volume);
+                    myRGBled.write(0.0,0.0,1.0 );
+                    
+                    mySpeaker.PlayNote(277.1826 * octave, 0.5, volume);
                     break;
                 case 3:
                     uLCD.cls();
@@ -289,8 +474,9 @@
                     uLCD.text_width(5);
                     uLCD.text_height(5);
                     uLCD.printf("D");
-                    myRGBled.write(0.0,1.0,0.0 );
-                    mySpeaker.PlayNote(293.6648, 0.5, volume);
+                    myRGBled.write(1.0,0.0,0.0 );
+                    
+                    mySpeaker.PlayNote(293.6648 * octave, 0.5, volume);
                     break;
                 case 4:
                     uLCD.cls();
@@ -298,7 +484,8 @@
                     uLCD.text_width(5);
                     uLCD.text_height(5);
                     uLCD.printf("DS");
-                    mySpeaker.PlayNote(311.1270, 0.5, volume);
+                    myRGBled.write(1.0,0.0,0.0 );
+                    mySpeaker.PlayNote(311.1270 * octave, 0.5, volume);
                     break;
                 case 5:
                     uLCD.cls();
@@ -306,7 +493,8 @@
                     uLCD.text_width(5);
                     uLCD.text_height(5);
                     uLCD.printf("E");
-                    mySpeaker.PlayNote(329.6276, 0.5, volume);
+                    myRGBled.write(0.0,1.0,0.0 );
+                    mySpeaker.PlayNote(329.6276 * octave, 0.5, volume);
                     break;
                 case 6:
                     uLCD.cls();
@@ -314,7 +502,8 @@
                     uLCD.text_width(5);
                     uLCD.text_height(5);
                     uLCD.printf("F");
-                    mySpeaker.PlayNote(349.2282, 0.5, volume);
+                    myRGBled.write(0.0,1.0,0.0 );
+                    mySpeaker.PlayNote(349.2282 * octave, 0.5, volume);
                     break;
                 case 7:
                     uLCD.cls();
@@ -322,7 +511,8 @@
                     uLCD.text_width(5);
                     uLCD.text_height(5);
                     uLCD.printf("FS");
-                    mySpeaker.PlayNote(369.9944, 0.5, volume);
+                    myRGBled.write(0.0,0.0,1.0 );
+                    mySpeaker.PlayNote(369.9944 * octave, 0.5, volume);
                     break;
                 case 8:
                     uLCD.cls();
@@ -330,7 +520,8 @@
                     uLCD.text_width(5);
                     uLCD.text_height(5);
                     uLCD.printf("G");
-                    mySpeaker.PlayNote(391.9954, 0.5, volume);
+                    myRGBled.write(0.0,0.0,1.0 );
+                    mySpeaker.PlayNote(391.9954 * octave, 0.5, volume);
                     break;
                 case 9:
                     uLCD.cls();
@@ -338,7 +529,8 @@
                     uLCD.text_width(5);
                     uLCD.text_height(5);
                     uLCD.printf("GS");
-                    mySpeaker.PlayNote(415.3047, 0.5, volume);
+                    myRGBled.write(1.0,0.0,0.0 );
+                    mySpeaker.PlayNote(415.3047 * octave, 0.5, volume);
                     break;
                 case 10:
                     uLCD.cls();
@@ -346,7 +538,8 @@
                     uLCD.text_width(5);
                     uLCD.text_height(5);
                     uLCD.printf("A");
-                    mySpeaker.PlayNote(440.0, 0.5, volume);
+                    myRGBled.write(1.0,0.0,0.0 );
+                    mySpeaker.PlayNote(440.0 * octave, 0.5, volume);
                     break;
                 case 11:
                     uLCD.cls();
@@ -354,7 +547,8 @@
                     uLCD.text_width(5);
                     uLCD.text_height(5);
                     uLCD.printf("AS");
-                    mySpeaker.PlayNote(466.1638, 0.5, volume);
+                    myRGBled.write(0.0,1.0,0.0 );
+                    mySpeaker.PlayNote(466.1638 * octave, 0.5, volume);
                     break;
                 case 12:
                     uLCD.cls();
@@ -362,7 +556,8 @@
                     uLCD.text_width(5);
                     uLCD.text_height(5);
                     uLCD.printf("B");
-                    mySpeaker.PlayNote(493.8833, 0.5, volume);
+                    myRGBled.write(0.0,1.0,0.0 );
+                    mySpeaker.PlayNote(493.8833 * octave, 0.5, volume);
                     break;
             }
         }