ECE 111 At Oregon State University / Mbed 2 deprecated Lab3_GUI

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed_beat_factory.h"
00002 /************************************* Lab3 Mbed beat factory ***************************/
00003 /*                                                                                      */
00004 /*  File: main.cpp                                                                      */
00005 /*  Author: Ziad Eldebri                                                                */
00006 /*  Date Created: 8/6/2016                                                              */
00007 /*  Description: Receives song data from a python GUI and subsequently plays it. It     */
00008 /*               additionally provides the GUI with live song bar updates               */
00009 /*                                                                                      */
00010 /****************************************************************************************/
00011 /**********************
00012 *    Setup Portion    *
00013 **********************/
00014 
00015 Serial pc(USBTX, USBRX);
00016 PwmOut Speaker(PTE31);
00017 commStruct comm;
00018 songStruct song;
00019 BusOut test(LED_RED,LED_GREEN);
00020 
00021 int freq[25] = {220,233,246,261,277,293,311,329,349,369,392,415,440,466,493,523,554,587,622,659,698,740,784,831,0} ;
00022 //int  freq[25] =  {220,233,261,277,293,220,233,261,277,293,220,233,261,277,293,220,233,261,277,293,220,233,261,277,293};
00023 
00024 /************************************* serial_wait_packet     ***************************/
00025 /*                                                                                      */
00026 /*  File: main.cpp                                                                      */
00027 /*  Author: Ziad Eldebri                                                                */
00028 /*  Date Created: 8/6/2016                                                              */
00029 /*  Description: Receives song data from a python GUI and subsequently plays it. It     */
00030 /*               additionally provides the GUI with live song bar updates               */
00031 /*                                                                                      */
00032 /****************************************************************************************/
00033 
00034 char serial_wait_packet()
00035 {
00036     while( !pc.readable()) {};
00037     return pc.getc();
00038 }
00039 
00040 
00041 /************************************* get_serial_data        ***************************/
00042 /*                                                                                      */
00043 /*  File: main.cpp                                                                      */
00044 /*  Author: Ziad Eldebri                                                                */
00045 /*  Date Created: 8/6/2016                                                              */
00046 /*  Description: Receives song data from a python GUI and subsequently plays it. It     */
00047 /*               additionally provides the GUI with live song bar updates               */
00048 /*                                                                                      */
00049 /****************************************************************************************/
00050 
00051 
00052 int get_serial_data()
00053 {
00054 
00055     int byt_count= 0;
00056     int data_count = 0;
00057 
00058     /* Clear the the data array in the comm struct */
00059     for( int i = 0; i < 3; i++ ) {
00060         for( int j = 0; j <3; j++ ) {
00061             comm.data[ i ][ j ] = 0;
00062         }
00063     }
00064 
00065 
00066 
00067 
00068     if(serial_wait_packet() == '$') {
00069         comm.packetType = pc.getc();
00070 
00071         for(;;) {
00072 
00073             char data = pc.getc();
00074 
00075             if(data == '$' ) {
00076                 return 0;
00077             } else if(data == '\n') {
00078                 comm.data[data_count][byt_count]= '\n';
00079                 return byt_count ;
00080             } else {
00081                 comm.data[data_count][byt_count]= data;
00082                 byt_count++;
00083             }
00084 
00085         }
00086     }
00087 
00088     return 0;
00089 }
00090 
00091 /************************************* extract_from_packet    ***************************/
00092 /*                                                                                      */
00093 /*  File: main.cpp                                                                      */
00094 /*  Author: Ziad Eldebri                                                                */
00095 /*  Date Created: 8/6/2016                                                              */
00096 /*  Description: Receives song data from a python GUI and subsequently plays it. It     */
00097 /*               additionally provides the GUI with live song bar updates               */
00098 /*                                                                                      */
00099 /****************************************************************************************/
00100 
00101 void extract_from_packet()
00102 {
00103     /* Switch to specified packet */
00104     switch( comm.packetType ) {
00105 
00106             /* New Song Packet */
00107         case 'N':
00108             initializeSong();
00109             break;
00110 
00111             /* Tempo Packet */
00112         case 'T':
00113             song.tempo = atoi( comm.data[0] );
00114             break;
00115 
00116             /* Song length packet */
00117         case 'L':
00118             song.notes = atoi( comm.data[0] );
00119             break;
00120 
00121             /* Song data packet */
00122         case 'S':
00123             song.note[ song.currentNote ] = atoi(comm.data[ 0 ]);
00124             song.currentNote++;
00125             break;
00126 
00127             /* Play song packet */
00128         case 'P':
00129             playSong();
00130             break;
00131 
00132     }
00133 
00134 }
00135 
00136 void initializeSong( void )
00137 {
00138     for( int i = 0; i < 128; i++ ) {
00139         song.note[i] = 0;
00140     }
00141     song.tempo = 120;
00142     song.notes = 0;
00143     song.currentNote = 0;
00144 }
00145 
00146 
00147 void playSong( void )
00148 {
00149 
00150 
00151     for( int i = 0; i < song.notes; i++ ) {
00152         /* Send the current bar we're on to the gui */
00153         pc.printf("%d\n",i);
00154 
00155         /* Set the frequency of the PWM for the specified note */
00156         if(song.note[i] == 24) {
00157             Speaker.period(0);
00158             Speaker = 0;
00159         } else {
00160             Speaker.period( ((float)1/freq[song.note[i]]) );
00161 
00162             Speaker = 0.5;
00163 
00164             /* Wait a specific amount of time based on the tempo */
00165             wait_ms(((float)((float)60/(float)song.tempo)*(float)1000.0));
00166             test = ~ test;
00167         }
00168     }
00169 
00170     Speaker.period(0);
00171     Speaker = 0;
00172     /* Turn off speaker */
00173 
00174     /* Print an end of packet byte 0xFF */
00175     pc.printf("255\n");
00176 
00177 }
00178 
00179 
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190 
00191 
00192 int main()
00193 {
00194     test =3;
00195     while (true) {
00196 
00197         if( get_serial_data()> 0) {
00198 
00199             extract_from_packet();
00200             test = 0;
00201         }
00202 
00203 
00204     }
00205 
00206 }