Arthur Semjonov
/
Serial_Communication_Slave
Slave serial com
Fork of Serial_Communication_Slave by
main.cpp@0:3457fac208b7, 2014-02-18 (annotated)
- Committer:
- ArthurSemjonov
- Date:
- Tue Feb 18 16:31:38 2014 +0000
- Revision:
- 0:3457fac208b7
- Child:
- 1:a1067f80d074
Slave unfinished code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ArthurSemjonov | 0:3457fac208b7 | 1 | #include "mbed.h" |
ArthurSemjonov | 0:3457fac208b7 | 2 | #include "TARDIS_ARCHIVE.h" |
ArthurSemjonov | 0:3457fac208b7 | 3 | DigitalOut l(LED1), l2(LED2), l3(LED3), l4(LED4); |
ArthurSemjonov | 0:3457fac208b7 | 4 | /********************************************************/ |
ArthurSemjonov | 0:3457fac208b7 | 5 | AnalogIn pitch(p19); //Potentiometer sets up the pitch of the song |
ArthurSemjonov | 0:3457fac208b7 | 6 | AnalogIn tempo(p20); //Potentiometer to set tempo of the song |
ArthurSemjonov | 0:3457fac208b7 | 7 | DigitalIn joyUP(p15); //Play song of the Doctor (Master) |
ArthurSemjonov | 0:3457fac208b7 | 8 | DigitalIn joyDOWN(p12); //Play song of the Companion (Slave) |
ArthurSemjonov | 0:3457fac208b7 | 9 | DigitalIn joyMID(p14); //Stop playing either master or Slave song. |
ArthurSemjonov | 0:3457fac208b7 | 10 | PwmOut speaker(p26); //Speaker on the mbed |
ArthurSemjonov | 0:3457fac208b7 | 11 | |
ArthurSemjonov | 0:3457fac208b7 | 12 | /*============SERIAL COMMUNICATION==============*/ |
ArthurSemjonov | 0:3457fac208b7 | 13 | I2CSlave companion(p9, p10); //mosi/miso/sck of the Slave |
ArthurSemjonov | 0:3457fac208b7 | 14 | /********************************************************/ |
ArthurSemjonov | 0:3457fac208b7 | 15 | Serial pc(USBTX, USBRX); |
ArthurSemjonov | 0:3457fac208b7 | 16 | int send_note; //word we will send |
ArthurSemjonov | 0:3457fac208b7 | 17 | int rec_note; //value received from master |
ArthurSemjonov | 0:3457fac208b7 | 18 | const int addr = 0x52; |
ArthurSemjonov | 0:3457fac208b7 | 19 | |
ArthurSemjonov | 0:3457fac208b7 | 20 | /************************************|I Am the Doctor|**********************************/ |
ArthurSemjonov | 0:3457fac208b7 | 21 | float frequency[]= {//JJ song |
ArthurSemjonov | 0:3457fac208b7 | 22 | }; |
ArthurSemjonov | 0:3457fac208b7 | 23 | float beat[]= {//JJ beat |
ArthurSemjonov | 0:3457fac208b7 | 24 | }; |
ArthurSemjonov | 0:3457fac208b7 | 25 | /***************************************************************************************/ |
ArthurSemjonov | 0:3457fac208b7 | 26 | |
ArthurSemjonov | 0:3457fac208b7 | 27 | /*companion.write(switch_word); //load up word to send |
ArthurSemjonov | 0:3457fac208b7 | 28 | //test for I2C, and act accordingly |
ArthurSemjonov | 0:3457fac208b7 | 29 | int i = companion.receive(); |
ArthurSemjonov | 0:3457fac208b7 | 30 | if (i == 3) |
ArthurSemjonov | 0:3457fac208b7 | 31 | { |
ArthurSemjonov | 0:3457fac208b7 | 32 | //companion is addressed, Master will write |
ArthurSemjonov | 0:3457fac208b7 | 33 | recd_val= companion.read();*/ |
ArthurSemjonov | 0:3457fac208b7 | 34 | |
ArthurSemjonov | 0:3457fac208b7 | 35 | |
ArthurSemjonov | 0:3457fac208b7 | 36 | int main() |
ArthurSemjonov | 0:3457fac208b7 | 37 | { |
ArthurSemjonov | 0:3457fac208b7 | 38 | companion.address(addr); |
ArthurSemjonov | 0:3457fac208b7 | 39 | while(1) { |
ArthurSemjonov | 0:3457fac208b7 | 40 | |
ArthurSemjonov | 0:3457fac208b7 | 41 | if (joyUP) { |
ArthurSemjonov | 0:3457fac208b7 | 42 | playTheDoctor(); |
ArthurSemjonov | 0:3457fac208b7 | 43 | wait(0.01); |
ArthurSemjonov | 0:3457fac208b7 | 44 | } else if (joyDOWN) { |
ArthurSemjonov | 0:3457fac208b7 | 45 | playTheCompanion(); |
ArthurSemjonov | 0:3457fac208b7 | 46 | wait(0.01); |
ArthurSemjonov | 0:3457fac208b7 | 47 | } else if (joyMID) { |
ArthurSemjonov | 0:3457fac208b7 | 48 | songOfMyPeople(frequency, beat); |
ArthurSemjonov | 0:3457fac208b7 | 49 | wait(0.01); |
ArthurSemjonov | 0:3457fac208b7 | 50 | } |
ArthurSemjonov | 0:3457fac208b7 | 51 | wait (0.01); |
ArthurSemjonov | 0:3457fac208b7 | 52 | } |
ArthurSemjonov | 0:3457fac208b7 | 53 | } |
ArthurSemjonov | 0:3457fac208b7 | 54 | //======================================================================================================================================== |
ArthurSemjonov | 0:3457fac208b7 | 55 | //======================================================================================================================================== |
ArthurSemjonov | 0:3457fac208b7 | 56 | //======================================================================================================================================== |
ArthurSemjonov | 0:3457fac208b7 | 57 | |
ArthurSemjonov | 0:3457fac208b7 | 58 | /********************************************************************* |
ArthurSemjonov | 0:3457fac208b7 | 59 | ******************************THE COMPANION*************************** |
ArthurSemjonov | 0:3457fac208b7 | 60 | ********************************aka slave***************************** |
ArthurSemjonov | 0:3457fac208b7 | 61 | **********************************************************************/ |
ArthurSemjonov | 0:3457fac208b7 | 62 | /*TODO: 1) Figure out what stop() returns, if any |
ArthurSemjonov | 0:3457fac208b7 | 63 | 2) Add print statements to see what is getting sent/received |
ArthurSemjonov | 0:3457fac208b7 | 64 | Bonus) Implement potentiameters for tempo and pitch */ |
ArthurSemjonov | 0:3457fac208b7 | 65 | void playTheCompanion() |
ArthurSemjonov | 0:3457fac208b7 | 66 | { |
ArthurSemjonov | 0:3457fac208b7 | 67 | for (int i=0; i<=8; i++) { |
ArthurSemjonov | 0:3457fac208b7 | 68 | send_note = frequency[i]; |
ArthurSemjonov | 0:3457fac208b7 | 69 | companion.write(send_note); |
ArthurSemjonov | 0:3457fac208b7 | 70 | |
ArthurSemjonov | 0:3457fac208b7 | 71 | float value = lookupNoteFrq(frequency, i); |
ArthurSemjonov | 0:3457fac208b7 | 72 | speaker.period(1/(2*value)); // set PWM period |
ArthurSemjonov | 0:3457fac208b7 | 73 | speaker=0.5; |
ArthurSemjonov | 0:3457fac208b7 | 74 | wait(0.4*beat[i]); |
ArthurSemjonov | 0:3457fac208b7 | 75 | |
ArthurSemjonov | 0:3457fac208b7 | 76 | if (joyMID) { |
ArthurSemjonov | 0:3457fac208b7 | 77 | speaker = 0; //reset speaker duty cycle |
ArthurSemjonov | 0:3457fac208b7 | 78 | companion.write(-1); |
ArthurSemjonov | 0:3457fac208b7 | 79 | wait(2); //delay for inferior humans |
ArthurSemjonov | 0:3457fac208b7 | 80 | return; |
ArthurSemjonov | 0:3457fac208b7 | 81 | } else if(joyDOWN) { |
ArthurSemjonov | 0:3457fac208b7 | 82 | speaker = 0; |
ArthurSemjonov | 0:3457fac208b7 | 83 | companion.stop(); |
ArthurSemjonov | 0:3457fac208b7 | 84 | return; |
ArthurSemjonov | 0:3457fac208b7 | 85 | } |
ArthurSemjonov | 0:3457fac208b7 | 86 | } |
ArthurSemjonov | 0:3457fac208b7 | 87 | speaker = 0; |
ArthurSemjonov | 0:3457fac208b7 | 88 | companion.stop(); |
ArthurSemjonov | 0:3457fac208b7 | 89 | |
ArthurSemjonov | 0:3457fac208b7 | 90 | |
ArthurSemjonov | 0:3457fac208b7 | 91 | |
ArthurSemjonov | 0:3457fac208b7 | 92 | |
ArthurSemjonov | 0:3457fac208b7 | 93 | |
ArthurSemjonov | 0:3457fac208b7 | 94 | } |
ArthurSemjonov | 0:3457fac208b7 | 95 | |
ArthurSemjonov | 0:3457fac208b7 | 96 | /********************************************************************* |
ArthurSemjonov | 0:3457fac208b7 | 97 | ******************************THE DOCTOR****************************** |
ArthurSemjonov | 0:3457fac208b7 | 98 | ********************************aka master**************************** |
ArthurSemjonov | 0:3457fac208b7 | 99 | **********************************************************************/ |
ArthurSemjonov | 0:3457fac208b7 | 100 | /*TODO: 1) Make sure while loop condition is doing what it suppose to |
ArthurSemjonov | 0:3457fac208b7 | 101 | 2) Initialize receive note, and play it |
ArthurSemjonov | 0:3457fac208b7 | 102 | 3) Implement interrupts |
ArthurSemjonov | 0:3457fac208b7 | 103 | 4) Stopping conditions |
ArthurSemjonov | 0:3457fac208b7 | 104 | Bonus) Potentiometers for pitch and tempo |
ArthurSemjonov | 0:3457fac208b7 | 105 | */ |
ArthurSemjonov | 0:3457fac208b7 | 106 | void playTheDoctor() |
ArthurSemjonov | 0:3457fac208b7 | 107 | { |
ArthurSemjonov | 0:3457fac208b7 | 108 | |
ArthurSemjonov | 0:3457fac208b7 | 109 | while(companion.receive()!=0) { |
ArthurSemjonov | 0:3457fac208b7 | 110 | send_note = companion.write(addr); |
ArthurSemjonov | 0:3457fac208b7 | 111 | playTheCompanion(rec_note); |
ArthurSemjonov | 0:3457fac208b7 | 112 | if (joyMID) { |
ArthurSemjonov | 0:3457fac208b7 | 113 | speaker = 0; //reset speaker duty cycle |
ArthurSemjonov | 0:3457fac208b7 | 114 | companion.stop(); //force end condition for both master and companion |
ArthurSemjonov | 0:3457fac208b7 | 115 | wait(2); //delay for inferior humans |
ArthurSemjonov | 0:3457fac208b7 | 116 | return; |
ArthurSemjonov | 0:3457fac208b7 | 117 | } else if(joyUP) { |
ArthurSemjonov | 0:3457fac208b7 | 118 | speaker = 0; //read joyUp value. |
ArthurSemjonov | 0:3457fac208b7 | 119 | companion.stop(); //force end condition due to interuppt |
ArthurSemjonov | 0:3457fac208b7 | 120 | return; |
ArthurSemjonov | 0:3457fac208b7 | 121 | } |
ArthurSemjonov | 0:3457fac208b7 | 122 | } |
ArthurSemjonov | 0:3457fac208b7 | 123 | speaker = 0; |
ArthurSemjonov | 0:3457fac208b7 | 124 | companion.stop();//force end condition |
ArthurSemjonov | 0:3457fac208b7 | 125 | return; |
ArthurSemjonov | 0:3457fac208b7 | 126 | |
ArthurSemjonov | 0:3457fac208b7 | 127 | |
ArthurSemjonov | 0:3457fac208b7 | 128 | } |
ArthurSemjonov | 0:3457fac208b7 | 129 | /*===================================================================*/ |
ArthurSemjonov | 0:3457fac208b7 | 130 | //===================Goon Squad of helper functions==================// |
ArthurSemjonov | 0:3457fac208b7 | 131 | void playTheCompanion(int k) |
ArthurSemjonov | 0:3457fac208b7 | 132 | { |
ArthurSemjonov | 0:3457fac208b7 | 133 | float frqNote = notes[k]; |
ArthurSemjonov | 0:3457fac208b7 | 134 | pc.printf("\n\rFrq of the companion note = '%f", k, |
ArthurSemjonov | 0:3457fac208b7 | 135 | "\n\rFrq of the companion note = '%f'", frqNote); |
ArthurSemjonov | 0:3457fac208b7 | 136 | speaker.period(1/(2*frqNote)); // set PWM period |
ArthurSemjonov | 0:3457fac208b7 | 137 | speaker=0.5; |
ArthurSemjonov | 0:3457fac208b7 | 138 | return; |
ArthurSemjonov | 0:3457fac208b7 | 139 | } |
ArthurSemjonov | 0:3457fac208b7 | 140 | |
ArthurSemjonov | 0:3457fac208b7 | 141 | /*====================Original Demo====================================*/ |
ArthurSemjonov | 0:3457fac208b7 | 142 | void songOfMyPeople(float frequency[], float beat[]) |
ArthurSemjonov | 0:3457fac208b7 | 143 | { |
ArthurSemjonov | 0:3457fac208b7 | 144 | while(1) { //run infinetly unless interupted |
ArthurSemjonov | 0:3457fac208b7 | 145 | for (int i=0; i<=8; i++) { |
ArthurSemjonov | 0:3457fac208b7 | 146 | float value = lookupNoteFrq(frequency, i); |
ArthurSemjonov | 0:3457fac208b7 | 147 | speaker.period(1/(2*value)); // set PWM period |
ArthurSemjonov | 0:3457fac208b7 | 148 | speaker=0.5; // set duty cycle |
ArthurSemjonov | 0:3457fac208b7 | 149 | wait(0.4*beat[i]); |
ArthurSemjonov | 0:3457fac208b7 | 150 | if (joyMID) { |
ArthurSemjonov | 0:3457fac208b7 | 151 | speaker = 0; //reset speaker duty cycle |
ArthurSemjonov | 0:3457fac208b7 | 152 | wait(2); //delay for inferior humans |
ArthurSemjonov | 0:3457fac208b7 | 153 | return; |
ArthurSemjonov | 0:3457fac208b7 | 154 | } |
ArthurSemjonov | 0:3457fac208b7 | 155 | } |
ArthurSemjonov | 0:3457fac208b7 | 156 | speaker = 0; |
ArthurSemjonov | 0:3457fac208b7 | 157 | wait(0.25); |
ArthurSemjonov | 0:3457fac208b7 | 158 | } |
ArthurSemjonov | 0:3457fac208b7 | 159 | } |
ArthurSemjonov | 0:3457fac208b7 | 160 | |
ArthurSemjonov | 0:3457fac208b7 | 161 | /*===================================================================*/ |
ArthurSemjonov | 0:3457fac208b7 | 162 | float lookupNoteFrq(float frq[], int k) |
ArthurSemjonov | 0:3457fac208b7 | 163 | { |
ArthurSemjonov | 0:3457fac208b7 | 164 | int location_of_note = frequency[k]; |
ArthurSemjonov | 0:3457fac208b7 | 165 | pc.printf("\n\rLocation of the note = '%d'", location_of_note); |
ArthurSemjonov | 0:3457fac208b7 | 166 | |
ArthurSemjonov | 0:3457fac208b7 | 167 | float vNote = notes[location_of_note]; |
ArthurSemjonov | 0:3457fac208b7 | 168 | pc.printf("\n\rFrq of the note = '%f'", vNote); |
ArthurSemjonov | 0:3457fac208b7 | 169 | return vNote; |
ArthurSemjonov | 0:3457fac208b7 | 170 | } |