ece4180_team / Mbed 2 deprecated lab3part5_Bluetooth

Dependencies:   4DGL-uLCD-SE SDFileSystem_3 mbed-rtos mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "uLCD_4DGL.h"
00002 #include "mbed.h"
00003 #include "rtos.h"
00004 #include "SongPlayer.h"
00005 
00006 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
00007 AnalogOut DACout(p18);
00008 DigitalOut myled(LED1);
00009 PwmOut RGBLED_r(p23);
00010 PwmOut RGBLED_g(p24);
00011 PwmOut RGBLED_b(p25);
00012 Serial blue(p13,p14);
00013 
00014 float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
00015                  1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
00016                 };
00017 float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
00018                      0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
00019                     };
00020 
00021 // mutex to make the lcd lib thread safe
00022 Mutex lcd_mutex;
00023 
00024 // global variables
00025 int i = 0;
00026 int ix = 0;
00027 int iy = 90;
00028 char color = 'g';
00029 int flight_dir = 8;
00030 
00031 void drawairplane(int x, int y) {
00032     uLCD.filled_rectangle(x, y+7, x+27, y+13, WHITE);
00033     uLCD.filled_rectangle(x, y, x+4, y+6, BLUE);
00034     uLCD.filled_rectangle(x, y+14, x+4, y+20, BLUE);
00035     uLCD.filled_rectangle(x+16, y-6, x+20, y+6, BLUE);
00036     uLCD.filled_rectangle(x+16, y+14, x+20, y+26, BLUE);
00037     uLCD.filled_rectangle(x+20, y+9, x+27, y+11, BLUE);
00038     uLCD.filled_rectangle(x+15, y+9, x+17, y+11, BLUE);
00039     uLCD.filled_rectangle(x+10, y+9, x+12, y+11, BLUE);
00040     uLCD.filled_rectangle(x+5, y+9, x+7, y+11, BLUE);
00041 }
00042 
00043 // Thread 1
00044 // RGB LED
00045 void thread1(void const *args)
00046 {
00047     while(true) {         // thread loop
00048         /*
00049         if (i < 10) {
00050             RGBLED_r = 0.0;
00051             RGBLED_g = (float) i / 10;
00052             RGBLED_b = 0.0;
00053         } else if (10 <= i && i < 20) {
00054             RGBLED_r = 0.0;
00055             RGBLED_g = (float) (20 - i) / 10;
00056             RGBLED_b = 0.0;
00057         } else if (20 <= i && i < 30) {
00058             RGBLED_r = (float) (i - 20) / 10;
00059             RGBLED_g = (float) (i - 20) / 10;
00060             RGBLED_b = (float) (i - 20) / 10;
00061         } else if (30 <= i && i < 40) {
00062             RGBLED_r = (float) (40 - i) / 10;
00063             RGBLED_g = (float) (40 - i) / 10;
00064             RGBLED_b = (float) (40 - i) / 10;
00065         } else {
00066             i = -1;
00067         }
00068         i++;
00069         */
00070         if (color == 'r') {
00071             RGBLED_r = 0.8;
00072             RGBLED_g = 0.0;
00073             RGBLED_b = 0.0;
00074         } else if (color == 'g') {
00075             RGBLED_r = 0.0;
00076             RGBLED_g = 0.8;
00077             RGBLED_b = 0.0;
00078         } else if (color == 'b') {
00079             RGBLED_r = 0.0;
00080             RGBLED_g = 0.0;
00081             RGBLED_b = 0.8;
00082         } else {
00083             RGBLED_r = 0.0;
00084             RGBLED_g = 0.8;
00085             RGBLED_b = 0.0;
00086         }
00087         Thread::wait(200);    // wait 0.2s
00088     }
00089 }
00090 
00091 
00092 void thread2(void const *args)
00093 {
00094     while (true) {
00095         SongPlayer mySpeaker(p21);
00096         mySpeaker.PlaySong(note,duration);
00097         Thread::wait(6000);    // wait 6s
00098     }
00099 }
00100 
00101 
00102 void thread3(void const *args)
00103 {
00104     while (true) {
00105         time_t seconds = time(NULL);
00106         lcd_mutex.lock();
00107         uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
00108         char buffer[32];
00109         strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds));
00110         uLCD.locate(0,0);
00111         uLCD.printf(buffer);
00112         lcd_mutex.unlock();
00113         Thread::wait(1000);    // wait 1s
00114     }
00115 }
00116 
00117 void thread4(void const *args)
00118 {
00119     while (true) {
00120         lcd_mutex.lock();
00121         uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
00122         drawairplane(ix, iy);
00123         lcd_mutex.unlock();
00124         if (flight_dir == 8) {
00125             ix += 1;
00126         } else if (flight_dir == 7) {
00127             ix -= 1;
00128         } else if (flight_dir == 6) {
00129             iy += 1;
00130         } else if (flight_dir == 5) {
00131             iy -= 1;
00132         }
00133         if (ix > 100) {
00134             ix = 0;
00135         } else if (ix < 0) {
00136             ix = 99;
00137         } else if (iy > 113) {
00138             iy = 64;
00139         } else if (iy < 64) {
00140             iy = 113;
00141         }
00142         Thread::wait(500);    // wait 0.75s
00143     }
00144 }
00145 
00146 
00147 int main() {
00148     Thread t1(thread1); //start thread1
00149     Thread t2(thread2); //start thread2
00150     Thread t3(thread3); //start thread3
00151     Thread t4(thread4); //start thread3
00152     set_time(1256729737);
00153     while(1) {
00154         char bnum=0;
00155         char bhit=0;
00156         if (!blue.readable()) {
00157             continue;
00158         }
00159         if (blue.getc()=='!') {
00160             if (blue.getc()=='B') { //button data packet
00161                 bnum = blue.getc(); //button number
00162                 bhit = blue.getc(); //1=hit, 0=release
00163                 if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
00164                     myled = bnum - '0'; //current button number will appear on LEDs
00165                     switch (bnum) {
00166                         case '1': //number button 1
00167                             if (bhit=='1') {
00168                                 //add hit code here
00169                                 color = 'r';
00170                             } //else {
00171                                 //add release code here
00172                             //}
00173                             break;
00174                         case '2': //number button 2
00175                             if (bhit=='1') {
00176                                 //add hit code here
00177                                 color = 'g';
00178                             } //else {
00179                                 //add release code here
00180                             //}
00181                             break;
00182                         case '3': //number button 3
00183                             if (bhit=='1') {
00184                                 //add hit code here
00185                                 color = 'b';
00186                             } //else {
00187                                 //add release code here
00188                             //}
00189                             break;
00190                         case '4': //number button 4
00191                             if (bhit=='1') {
00192                                 //add hit code here
00193                             } //else {
00194                                 //add release code here
00195                             //}
00196                             break;
00197                         case '5': //button 5 up arrow
00198                             if (bhit=='1') {
00199                                 //add hit code here
00200                                 flight_dir = 5;
00201                             } //else {
00202                                 //add release code here
00203                             //}
00204                             break;
00205                         case '6': //button 6 down arrow
00206                             if (bhit=='1') {
00207                                 //add hit code here
00208                                 flight_dir = 6;
00209                             } //else {
00210                                 //add release code here
00211                             //}
00212                             break;
00213                         case '7': //button 7 left arrow
00214                             if (bhit=='1') {
00215                                 //add hit code here
00216                                 flight_dir = 7;
00217                             } //else {
00218                                 //add release code here
00219                             //}
00220                             break;
00221                         case '8': //button 8 right arrow
00222                             if (bhit=='1') {
00223                                 //add hit code here
00224                                 flight_dir = 8;
00225                             } //else {
00226                                 //add release code here
00227                             //}
00228                             break;
00229                         default:
00230                             break;
00231                     }
00232                 }
00233             }
00234         }
00235     }
00236     Thread::wait(200);    // wait 0.2s
00237 }