Project for playing a song on 2 meeds sending notes over I2C

Dependencies:   C12832 mbed

Files at this revision

API Documentation at this revision

Comitter:
jad19925
Date:
Thu Feb 13 22:54:56 2014 +0000
Parent:
4:bd0675c58f5e
Child:
6:84c34161559e
Commit message:
Sending Songs works

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mario.h Show annotated file Show diff for this revision Revisions of this file
music.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Feb 13 21:21:44 2014 +0000
+++ b/main.cpp	Thu Feb 13 22:54:56 2014 +0000
@@ -9,7 +9,7 @@
 DigitalIn fire(p14);
 PwmOut spkr(p26);
 C12832 lcd(p5, p7, p6, p8, p11);
-DigitalIn up(p15); 
+DigitalIn up(p15);
 DigitalIn down(p12);
 BusOut leds(LED1,LED2,LED3,LED4);
 
@@ -22,7 +22,7 @@
 char note;
 char length;
 const int slaveAddress = 0x42;
-int received = 0;
+char received = 0;
 
 int main()
 {
@@ -31,7 +31,7 @@
 #endif
     lcd.cls();
     lcd.printf("hello world");
-    
+
     while(1) {
         if (fire) {
             leds=0xf;
@@ -49,48 +49,107 @@
             }
             spkr=0.0;
             //end speaker code
-        }
-        else 
-        {
+        } else {
             leds=joy;
 #ifdef MASTER
-            if(up)
-            {//master sends its song to slave
+            if(up) {
+                //master sends its song to slave
                 i2cPort.start();
                 //send address
                 i2cPort.write(slaveAddress);
                 //send tempo (BPM)
                 //loop through song
-                i2cPort.write(6);
-//                for(char j=0;j<10;j++)
-//                {
-//                    //send frequency
-//                    i2cPort.write(j);
-//                    //play note
-//                    //wait length
-//                    wait(1);
-//                }
-//                //send stop code
-//                i2cPort.write(0xFF);
+                for(int i=0;i<50/*(sizeof(marioNotes)/sizeof(NoteName))*/;i++){
+                    if(marioNotes[i] == R) {
+                        i2cPort.write(R);
+                        spkr=0.0;
+                        wait(.7*marioBeats[i]/12);
+                    }
+                    else{
+                        i2cPort.write(marioNotes[i]);
+                        spkr.period(1/(notes[marioNotes[i]]));
+                        spkr=.5;
+                        wait(.7*marioBeats[i]/12);
+                        spkr=0.0;
+                    }
+                }
+                spkr=0.0;
+                //send stop code
+                i2cPort.write(0xFF);
+                wait(.5);
                 //close i2c port
                 i2cPort.stop();
-            }
-            else if(down)
-            {//slave should send it's song to master
+            } else if(down) {
+                //slave should send it's song to master
+                i2cPort.start();
+                //send address with read command
+                i2cPort.write(slaveAddress|0x01);
                 
+                do {
+                    received = i2cPort.read(1);
+                    spkr=0.0;
+                    if(received == R) {
+                        spkr=0.0;
+                    }
+                    else{
+                        spkr.period(1/(notes[received]));
+                        spkr=.5;
+                    }
+                    lcd.cls();
+                    lcd.locate(0,0);
+                    lcd.printf("Received: %d",received);
+                } while(received != 0xFF);
+                
+                lcd.cls();
+                lcd.locate(0,0);
+                lcd.printf("done");
+                i2cPort.stop();
             }
 #endif
 #ifdef SLAVE
             received = slave.receive();
-            if(I2CSlave::ReadAddressed==received){//master is requesting slave song
-                
+            if(I2CSlave::ReadAddressed==received) { //master is requesting slave song
+                for(int i=0;i<50/*(sizeof(marioNotes)/sizeof(NoteName))*/;i++){
+                    if(marioNotes[i] == R) {
+                        slave.write(R);
+                        spkr=0.0;
+                        wait(.7*marioBeats[i]/12);
+                    }
+                    else{
+                        slave.write(marioNotes[i]);
+                        spkr.period(1/(notes[marioNotes[i]]));
+                        spkr=.5;
+                        wait(.7*marioBeats[i]/12);
+                        spkr=0.0;
+                    }
+                }
+                spkr=0.0;
+                slave.write(0xFF);
+                slave.stop();
             }
-            else if(I2CSlave::WriteAddressed==received){//master is sending its song
-                received = slave.read();
+            else if(I2CSlave::WriteAddressed==received) { //master is sending its song
+                do {
+                    received = slave.read();
+                    spkr=0.0;
+                    if(received == R) {
+                        spkr=0.0;
+                    }
+                    else{
+                        spkr.period(1/(notes[received]));
+                        spkr=.5;
+                    }
+                    lcd.cls();
+                    lcd.locate(0,0);
+                    lcd.printf("Received: %d",received);
+                } while(received != 0xFF);
                 lcd.cls();
                 lcd.locate(0,0);
-                lcd.printf("Sent: %d",received);
+                lcd.printf("done");
+                slave.stop();
             }
+            lcd.cls();
+            lcd.locate(0,0);
+            lcd.printf("%d",received);
 #endif
         }
     }
--- a/mario.h	Thu Feb 13 21:21:44 2014 +0000
+++ b/mario.h	Thu Feb 13 22:54:56 2014 +0000
@@ -1,4 +1,6 @@
 #include "music.h"
+const int TEMPO = 100;
+
 //mario theme song
 NoteName marioNotes[]={E5,E5,E5,R,C5,E5,G5,R,G4,R,
                        C5,R,G4,R,E4,R,A4,R,B4,R,Bb4,A4,
--- a/music.h	Thu Feb 13 21:21:44 2014 +0000
+++ b/music.h	Thu Feb 13 22:54:56 2014 +0000
@@ -1,3 +1,4 @@
+
 enum NoteName {C0=0,Cs0,D0,Eb0,E0,F0,Fs0,G0,Gs0,A0,Bb0,B0,
                  C1,Cs1,D1,Eb1,E1,F1,Fs1,G1,Gs1,A1,Bb1,B1,
                  C2,Cs2,D2,Eb2,E2,F2,Fs2,G2,Gs2,A2,Bb2,B2,