Arthur Semjonov
/
Serial_Communication_pub
Master Code (Doctor) pub
Fork of Serial_Communication by
Revision 1:0167f0cacae5, committed 2014-02-19
- Comitter:
- ArthurSemjonov
- Date:
- Wed Feb 19 16:14:10 2014 +0000
- Parent:
- 0:757da73b539a
- Commit message:
- The Doctor (master) I2C
Changed in this revision
diff -r 757da73b539a -r 0167f0cacae5 Companion.h
diff -r 757da73b539a -r 0167f0cacae5 Doctor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Doctor.cpp Wed Feb 19 16:14:10 2014 +0000 @@ -0,0 +1,179 @@ +#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==============*/ +I2C doctor(p9, p10); //mosi/miso/sck of the Doctor +/********************************************************/ +Serial pc(USBTX, USBRX); +int send_note; //word we will send +int rec_note; //value received from master +const int addr = 0x42; +const int stop = 0xFF; +/************************************|I Am the Doctor|**********************************/ +float frequency[]= {D3, E3, F3, G3, + F3 ,E3, D3, E3, D3 + }; +float beat[]= {.7,.7,.7,0.3,.7,.7,.7,0.3,0.3, 0.3}; +/***************************************************************************************/ +// +int main() +{ + + while(1) { + + if (joyUP) { + playTheDoctor(); + doctor.stop(); + wait(0.01); + } else if (joyDOWN) { + + playTheCompanion(); + wait(0.01); + } else if (joyMID) { +// pc.printf("\n\rMAIN PROGRAM OF THE DOCTOR INITIATED"); +// +// doctor.start(); +// doctor.write(addr); +// int j = frequency[4]; +// int k = doctor.write(F3); +// pc.printf("\n\rHi = '%d'", j); +// doctor.stop(); + songOfMyPeople(frequency, beat); + // wait(2); + } + wait (0.01); + } +} +//======================================================================================================================================== +//======================================================================================================================================== +//======================================================================================================================================== + +/********************************************************************* +******************************THE COMPANION*************************** +********************************aka slave***************************** +**********************************************************************/ + +void playTheCompanion() +{ + doctor.start(); + //doctor.read(addr); + doctor.write(addr|0x01); +//send address, with R/W bit set to Read + int prev=0; + int cur; + while(1) { + rec_note = doctor.read(addr); + cur = rec_note; + if(rec_note==stop) { + speaker = 0; + //doctor.write(addr|0x00); + doctor.stop(); + return; + } + if(cur!=prev) playTheCompanion(cur); + prev = rec_note; + //wait(0.01); + if (joyMID) { + speaker = 0; //reset speaker duty cycle + doctor.write(stop); + doctor.stop(); + wait(1); //delay for inferior humans + return; + } else if(joyUP) { + speaker = 0; //read joyUp value. + doctor.write(stop); + doctor.stop(); + return; + + } + } +//speaker = 0; + +//return; +} + +/********************************************************************* +******************************THE DOCTOR****************************** +********************************aka master**************************** +**********************************************************************/ + +void playTheDoctor() +{ + doctor.start(); + doctor.write(addr); + for (int i=0; i<=sizeof(frequency)/sizeof(int)-1; i++) { + send_note = frequency[i]; + doctor.write(send_note); + pc.printf("\n\rDoctor sent note = '%d", 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) { + pc.printf("\n\rDoctor song was interrupted by demo"); + speaker = 0; //reset speaker duty cycle + doctor.stop(); + wait(2); //delay for inferior humans + return; + } else if(joyDOWN) { + speaker = 0; + doctor.stop(); + return; + } + } + doctor.write(stop); + doctor.stop(); + speaker=0; + return; +} +/*===================================================================*/ +//===================Goon Squad of helper functions==================// +void playTheCompanion(int k) +{ + float frqNote = notes[k]; + pc.printf("\n\rNote I got = '%d", k); + 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
diff -r 757da73b539a -r 0167f0cacae5 TARDIS_ARCHIVE.h --- a/TARDIS_ARCHIVE.h Tue Feb 18 01:19:44 2014 +0000 +++ b/TARDIS_ARCHIVE.h Wed Feb 19 16:14:10 2014 +0000 @@ -1,10 +1,81 @@ + +/* + * music.h + * + * Created on: Feb "some day", 2014 + * Authors: COSC 4290 Students of Dr Dennis Brylow. + */ + +#ifndef TARDIS_H_ +#define TARDIS_H_ +/*=====================NOTES=============================*/ + +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, + C3,Cs3,D3,Eb3,E3,F3,Fs3,G3,Gs3,A3,Bb3,B3, + C4,Cs4,D4,Eb4,E4,F4,Fs4,G4,Gs4,A4,Bb4,B4, + C5,Cs5,D5,Eb5,E5,F5,Fs5,G5,Gs5,A5,Bb5,B5, + C6,Cs6,D6,Eb6,E6,F6,Fs6,G6,Gs6,A6,Bb6,B6, + C7,Cs7,D7,Eb7,E7,F7,Fs7,G7,Gs7,A7,Bb7,B7, + C8,Cs8,D8,Eb8,E8,F8,Fs8,G8,Gs8,A8,Bb8,B8,R}; +/*=======================================================*/ + + +/*=====================FREQUENCIES=======================*/ +float notes[]={16.35,17.32,18.35,19.45,20.60,21.83,23.12,24.50,25.96,27.50,29.14,30.87, + 32.70,34.65,36.71,38.89,41.20,43.65,46.25,49.00,51.91,55.00,58.27,61.74, + 65.41,69.30,73.42,77.78,82.41,87.31,92.50,98.00,103.8,110.0,116.5,123.5, + 130.8,138.6,146.8,155.6,164.8,174.6,185.0,196.0,207.7,220.0,233.1,246.9, + 261.6,277.2,293.7,311.1,329.6,349.2,370.0,392.0,415.3,440.0,466.2,493.9, + 523.3,554.4,587.3,622.3,659.3,698.5,740.0,784.0,830.6,880.0,932.3,987.8, + 1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976, + 2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951, + 4186,4435,4699,4978,5274,5588,5920,6272,6645,7040,7459,7902}; + /*=============================Prototypes===============*/ +/*Demo song*/ void songOfMyPeople(float frequency[], float beat[]); -void songOfYourPeople(int note); //location of the note as agreed in the protocol + + +/** ___________________Used for demo___________________ + * Look up the frequency of the note from music.h file + * @param frq[], array of the song + * @param k, location of the note in the array + * _____________________________________________________ + */ float lookupNoteFrq(float frq[], int k); -void playTheSlave(int frequency); -void playTheDoctor(); -void playTheCompanion(); \ No newline at end of file + + +//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=// + +/* _________________________________________ + * Parses the note recevied by the I2C slave. + * Helper function of playTheCompanion. Which takes and integer value and references the frequency + * related to that note. After frequency is found, function plays the note. + * @param k, music note + * _________________________________________ */ +void playTheCompanion(int k); + +/* _________________________________________________________________________ + * I2C data link (Master) which sends notes of the song to linked slave mbed + * while simultaniously playing the same note on it's own mbed + * Features: + 1) Hold mid joystick to stop both master and slave + 2) When Slave button is pressed (joyDown), it will stop playing slave song and + ________________________________________________________________________________________*/ +void playTheDoctor(void); + +/* _____________________________________________________________________ + * I2C data link (Master) which reads notes of the song from a companion + * (slave) and plays the song of their people + * Features: + 1) Hold mid joystick to stop both master and slave + 2) When Master button is pressed (joyUp), it will stop playing slave song and +_________________________________________________________________________________________*/ +void playTheCompanion(void); + +#endif /* TARDIS_H_ */ \ No newline at end of file
diff -r 757da73b539a -r 0167f0cacae5 main.cpp --- a/main.cpp Tue Feb 18 01:19:44 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,139 +0,0 @@ -#include "mbed.h" -#include "music.h" -#include "TARDIS_ARCHIVE.h" - -/********************************************************/ -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==============*/ -I2C doctor_tardis (p9, p10); //mosi/miso/sck of the Doctor -/********************************************************/ -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[]= {D3, E3, F3, G3, - F3 ,E3, D3, E3, D3 - }; -float beat[]= {.7,.7,.7,0.3,.7,.7,.7,0.3,0.3, 0.3}; -/***************************************************************************************/ - -int main() -{ - while(1) { - - if (joyUP) { - playTheDoctor(); - wait(0.01); - } else if (joyDOWN) { - playTheCompanion(); - wait(0.01); - } else if (joyMID) { - songOfMyPeople(frequency, beat); - //breakConnection(); //Dalek attack - wait(0.01); - } - wait (0.01); - } -} -//======================================================================================================================================== -//======================================================================================================================================== -//======================================================================================================================================== - - -void playTheCompanion() -{ - doctor_tardis.start(); - doctor_tardis.write(addr|0x01); - while(doctor_tardis.read(addr)!=0) { - rec_note = doctor_tardis.read(addr); - pc.printf("\n\rReceived note = '%d'", rec_note); - speaker.period(1/(2*notes[rec_note])); // set PWM period - speaker=0.5; - } - doctor_tardis.stop(); - return; -} - -void playTheDoctor() -{ - doctor_tardis.start(); - doctor_tardis.write(addr); - - for (int i=0; i<=8; i++) { - send_note = frequency[i]; - doctor_tardis.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 - doctor_tardis.stop(); - wait(2); //delay for inferior humans - return; - } else if(joyDOWN) { - doctor_tardis.stop(); - return; - } - } - doctor_tardis.stop(); -} - -void playTheSlave(int frequency) -{ - float frqNote = notes[frequency]; - pc.printf("\n\rFrq of the note = '%f", frequency, "\n\rFrq of the note = '%f'", frqNote); - speaker.period(1/(2*frqNote)); // set PWM period - speaker=0.5; -} - -/*--------------Play the song of the Doctor--------------*/ -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); - } -} -/*--------------Play the song of the Companions--------------*/ - -void songOfYourPeople(int frequency) -{ - -} - - -/** Look up the frequency of the note from music.h file - * @param frq[], array of the song - * @param k, location of the note in the array - */ -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
diff -r 757da73b539a -r 0167f0cacae5 music.h --- a/music.h Tue Feb 18 01:19:44 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -/* - * music.h - * - * Created on: Feb "some day", 2014 - * Authors: COSC 4290 Students of Dr Dennis Brylow. - */ - -#ifndef MUSIC_H_ -#define MUSIC_H_ -/*=====================NOTES=============================*/ - -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, - C3,Cs3,D3,Eb3,E3,F3,Fs3,G3,Gs3,A3,Bb3,B3, - C4,Cs4,D4,Eb4,E4,F4,Fs4,G4,Gs4,A4,Bb4,B4, - C5,Cs5,D5,Eb5,E5,F5,Fs5,G5,Gs5,A5,Bb5,B5, - C6,Cs6,D6,Eb6,E6,F6,Fs6,G6,Gs6,A6,Bb6,B6, - C7,Cs7,D7,Eb7,E7,F7,Fs7,G7,Gs7,A7,Bb7,B7, - C8,Cs8,D8,Eb8,E8,F8,Fs8,G8,Gs8,A8,Bb8,B8,R}; -/*=======================================================*/ - - -/*=====================FREQUENCIES=======================*/ -float notes[]={16.35,17.32,18.35,19.45,20.60,21.83,23.12,24.50,25.96,27.50,29.14,30.87, - 32.70,34.65,36.71,38.89,41.20,43.65,46.25,49.00,51.91,55.00,58.27,61.74, - 65.41,69.30,73.42,77.78,82.41,87.31,92.50,98.00,103.8,110.0,116.5,123.5, - 130.8,138.6,146.8,155.6,164.8,174.6,185.0,196.0,207.7,220.0,233.1,246.9, - 261.6,277.2,293.7,311.1,329.6,349.2,370.0,392.0,415.3,440.0,466.2,493.9, - 523.3,554.4,587.3,622.3,659.3,698.5,740.0,784.0,830.6,880.0,932.3,987.8, - 1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976, - 2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951, - 4186,4435,4699,4978,5274,5588,5920,6272,6645,7040,7459,7902}; - - -#endif /* MUSIC_H_ */ \ No newline at end of file