Slave serial com

Dependencies:   mbed

Fork of Serial_Communication_Slave by Arthur Semjonov

Revision:
0:3457fac208b7
Child:
1:a1067f80d074
diff -r 000000000000 -r 3457fac208b7 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Feb 18 16:31:38 2014 +0000
@@ -0,0 +1,170 @@
+#include "mbed.h"
+#include "TARDIS_ARCHIVE.h"
+DigitalOut l(LED1), l2(LED2), l3(LED3), l4(LED4);
+/********************************************************/
+AnalogIn pitch(p19);                //Potentiometer sets up the pitch of the song
+AnalogIn tempo(p20);                //Potentiometer to set tempo of the song
+DigitalIn joyUP(p15);               //Play song of the Doctor (Master)
+DigitalIn joyDOWN(p12);             //Play song of the Companion (Slave)
+DigitalIn joyMID(p14);              //Stop playing either master or Slave song.
+PwmOut speaker(p26);                //Speaker on the mbed
+
+/*============SERIAL COMMUNICATION==============*/
+I2CSlave companion(p9, p10);          //mosi/miso/sck of the Slave
+/********************************************************/
+Serial pc(USBTX, USBRX);
+int send_note;                   //word we will send
+int rec_note;                    //value received from master
+const int addr = 0x52;
+
+/************************************|I Am the Doctor|**********************************/
+float frequency[]= {//JJ song
+};
+float beat[]= {//JJ beat
+};
+/***************************************************************************************/
+
+/*companion.write(switch_word); //load up word to send
+//test for I2C, and act accordingly
+int i = companion.receive();
+if (i == 3)
+{
+//companion is addressed, Master will write
+    recd_val= companion.read();*/
+
+
+int main()
+{
+    companion.address(addr);
+    while(1) {
+
+        if (joyUP) {
+            playTheDoctor();
+            wait(0.01);
+        } else if (joyDOWN) {
+            playTheCompanion();
+            wait(0.01);
+        } else if (joyMID) {
+            songOfMyPeople(frequency, beat);
+            wait(0.01);
+        }
+        wait (0.01);
+    }
+}
+//========================================================================================================================================
+//========================================================================================================================================
+//========================================================================================================================================
+
+/*********************************************************************
+******************************THE COMPANION***************************
+********************************aka slave*****************************
+**********************************************************************/
+/*TODO: 1) Figure out what stop() returns, if any
+        2) Add print statements to see what is getting sent/received
+        Bonus) Implement potentiameters for tempo and pitch */
+void playTheCompanion()
+{
+    for (int i=0; i<=8; i++) {
+        send_note = frequency[i];
+        companion.write(send_note);
+
+        float value = lookupNoteFrq(frequency, i);
+        speaker.period(1/(2*value)); // set PWM period
+        speaker=0.5;
+        wait(0.4*beat[i]);
+
+        if (joyMID) {
+            speaker = 0; //reset speaker duty cycle
+            companion.write(-1);
+            wait(2);     //delay for inferior humans
+            return;
+        } else if(joyDOWN) {
+            speaker = 0;
+            companion.stop();
+            return;
+        }
+    }
+    speaker = 0;
+    companion.stop();
+
+
+
+
+
+}
+
+/*********************************************************************
+******************************THE DOCTOR******************************
+********************************aka master****************************
+**********************************************************************/
+/*TODO: 1) Make sure while loop condition is doing what it suppose to
+        2) Initialize receive note, and play it
+        3) Implement interrupts
+        4) Stopping conditions
+        Bonus) Potentiometers for pitch and tempo
+*/
+void playTheDoctor()
+{
+
+    while(companion.receive()!=0) {
+        send_note = companion.write(addr);
+        playTheCompanion(rec_note);
+        if (joyMID) {
+            speaker = 0;                    //reset speaker duty cycle
+            companion.stop();           //force end condition for both master and companion
+            wait(2);                        //delay for inferior humans
+            return;
+        } else if(joyUP) {
+            speaker = 0;                   //read joyUp value.
+            companion.stop();           //force end condition due to interuppt
+            return;
+        }
+    }
+    speaker = 0;
+    companion.stop();//force end condition
+    return;
+
+
+}
+/*===================================================================*/
+//===================Goon Squad of helper functions==================//
+void playTheCompanion(int k)
+{
+    float frqNote = notes[k];
+    pc.printf("\n\rFrq of the companion note = '%f", k,
+              "\n\rFrq of the companion note = '%f'", frqNote);
+    speaker.period(1/(2*frqNote)); // set PWM period
+    speaker=0.5;
+    return;
+}
+
+/*====================Original Demo====================================*/
+void songOfMyPeople(float frequency[], float beat[])
+{
+    while(1) { //run infinetly unless interupted
+        for (int i=0; i<=8; i++) {
+            float value = lookupNoteFrq(frequency, i);
+            speaker.period(1/(2*value)); // set PWM period
+            speaker=0.5; // set duty cycle
+            wait(0.4*beat[i]);
+            if (joyMID) {
+                speaker = 0; //reset speaker duty cycle
+                wait(2);     //delay for inferior humans
+                return;
+            }
+        }
+        speaker = 0;
+        wait(0.25);
+    }
+}
+
+/*===================================================================*/
+float lookupNoteFrq(float frq[], int k)
+{
+    int location_of_note = frequency[k];
+    pc.printf("\n\rLocation of the note = '%d'", location_of_note);
+
+    float vNote = notes[location_of_note];
+    pc.printf("\n\rFrq of the note = '%f'", vNote);
+    return vNote;
+}
\ No newline at end of file