lab4

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers head.h Source File

head.h

00001 #ifndef HEAD_H
00002 #define HEAD_H
00003 
00004 #include "mbed.h"
00005 #include "mbed.h"
00006 #include "uLCD_4DGL.h"
00007 #include "math.h"
00008 #include "rtos.h"
00009 #include <string>
00010 
00011 #define YELLOW              0xFFFF00
00012 #define ORANGE              0xffdd76
00013 #define MECYAN              0x009696
00014 
00015 AnalogIn timer(p20);
00016 Mutex stdio_mutex;
00017 uLCD_4DGL lcd(p28, p27, p30);
00018 
00019 AnalogIn steer(p17);
00020 AnalogIn force(p16);
00021 
00022 volatile int count=30;
00023 volatile int a=0;
00024 volatile int cheat=3;
00025 
00026 void start()
00027 {
00028     lcd.baudrate(300000);
00029     lcd.printf("Need for Speed  :P  \n");
00030     lcd.printf("------------------");
00031     lcd.printf("1. Rotation Sensor - To steer right or left\n\n");
00032     lcd.printf("2. Force Sensor - To speed up\n\n");
00033     lcd.printf("3. Touch Sensor - To Cheat (3 times max)\n\n\n");
00034     lcd.printf("  Touch to begin!\n");
00035 }
00036 
00037 void forward(void const *args)
00038 {
00039     while(1) {
00040         stdio_mutex.lock();
00041         if (timer<0.1) {
00042             if(count<29 && cheat>0) {
00043                 ++count;
00044                 ++count;
00045                 --cheat;
00046             }
00047             if(timer<0.1 && cheat<1) {
00048                 lcd.printf("STOP cheating");
00049             }
00050         }
00051         stdio_mutex.unlock();
00052         Thread::wait(400);
00053     }
00054 }
00055 
00056 void steering(void const *args)
00057 {
00058     while(1) {
00059         stdio_mutex.lock();
00060         lcd.cls();
00061         a=floor(steer*39);
00062         if(a>9) {
00063             a=9;
00064         }
00065         switch(a) {
00066             case (1): {
00067                 lcd.filled_rectangle(0, 98, 9, 110, BLUE);
00068                 break;
00069             }
00070             case (2): {
00071                 lcd.filled_rectangle(14, 98, 23, 110, BLUE);
00072                 break;
00073             }
00074             case (3): {
00075                 lcd.filled_rectangle(28, 98, 37, 110, BLUE);
00076                 break;
00077             }
00078             case (4): {
00079                 lcd.filled_rectangle(42, 98, 51, 110, BLUE);
00080                 break;
00081             }
00082             case (5): {
00083                 lcd.filled_rectangle(56, 98, 65, 110, BLUE);
00084                 break;
00085             }
00086             case (6): {
00087                 lcd.filled_rectangle(70, 98, 79, 110, BLUE);
00088                 break;
00089             }
00090             case (7): {
00091                 lcd.filled_rectangle(84, 98, 93, 110, BLUE);
00092                 //Thread::wait(200);
00093                 break;
00094             }
00095             case (8): {
00096                 lcd.filled_rectangle(98, 98, 107, 110, BLUE);
00097                 //  Thread::wait(200);
00098                 break;
00099             }
00100             case (9): {
00101                 lcd.filled_rectangle(112, 98, 121, 110, BLUE);
00102                 //  Thread::wait(200);
00103                 break;
00104             }
00105         }
00106         Thread::wait(200);
00107         stdio_mutex.unlock();
00108 
00109     }
00110 }
00111 
00112 void obstacle(void const *args)
00113 {
00114     while(1) {
00115         stdio_mutex.lock();
00116         switch(count) {
00117             case (30): {
00118                 lcd.filled_rectangle(0, 0, 90, 10, RED);
00119                 break;
00120             }
00121             case (29): {
00122                 lcd.filled_rectangle(0, 10, 90, 20, RED);
00123                 break;
00124             }
00125             case (28): {
00126                 lcd.filled_rectangle(0, 20, 90, 30, RED);
00127                 break;
00128             }
00129             case (27): {
00130                 lcd.filled_rectangle(0, 30, 90, 40, RED);
00131                 break;
00132             }
00133             case (26): {
00134                 lcd.filled_rectangle(0, 40, 90, 50, RED);
00135                 break;
00136             }
00137             case (25): {
00138                 lcd.filled_rectangle(0, 50, 90, 60, RED);
00139                 lcd.filled_rectangle(70, 0, 90, 10, GREEN);
00140                 break;
00141             }
00142             case (24): {
00143                 lcd.filled_rectangle(0, 60, 90, 70, RED);
00144                 lcd.filled_rectangle(70, 10, 127, 20, GREEN);
00145                 break;
00146             }
00147             case (23): {
00148                 lcd.filled_rectangle(0, 70, 90, 80, RED);
00149                 lcd.filled_rectangle(70, 20, 127, 30, GREEN);
00150                 break;
00151             }
00152             case (22): {
00153                 lcd.filled_rectangle(0, 80, 90, 90, RED);
00154                 lcd.filled_rectangle(70, 30, 127, 40, GREEN);
00155                 break;
00156             }
00157             case (21): {
00158                 lcd.filled_rectangle(0, 90, 90, 100, RED);
00159                 lcd.filled_rectangle(70, 40, 127, 50, GREEN);
00160                 break;
00161             }
00162             case (20): {
00163                 lcd.filled_rectangle(0, 100, 90, 110, RED);
00164                 lcd.filled_rectangle(70, 50, 127, 60, GREEN);
00165                 if(a<8) {
00166                     lcd.locate(3,8);
00167                     lcd.printf("YOU LOSE!!!");
00168                     exit(0);
00169                 }
00170                 break;
00171             }
00172             case (19): {
00173                 lcd.filled_rectangle(0, 110, 100, 120, RED);
00174                 lcd.filled_rectangle(70, 60, 127, 70, GREEN);
00175                 lcd.filled_rectangle(40,0 , 90,10 , WHITE);
00176                 break;
00177             }
00178             case (18): {
00179                 lcd.filled_rectangle(0, 120, 100, 130, RED);
00180                 lcd.filled_rectangle(70, 70, 127, 80, GREEN);
00181                 lcd.filled_rectangle(40,10 , 90,20 , WHITE);
00182                 break;
00183             }
00184             case (17): {
00185                 lcd.filled_rectangle(70, 80, 127, 90, GREEN);
00186                 lcd.filled_rectangle(40,20 , 90, 30, WHITE);
00187                 break;
00188             }
00189             case (16): {
00190                 lcd.filled_rectangle(70, 90, 127, 100, GREEN);
00191                 lcd.filled_rectangle(40,30 , 90, 40, WHITE);
00192                 break;
00193             }
00194             case (15): {
00195                 lcd.filled_rectangle(70, 100, 127, 110, GREEN);
00196                 lcd.filled_rectangle(40,40 , 90,50 , WHITE);
00197                 if(a>6) {
00198                     lcd.locate(3,8);
00199                     lcd.printf("YOU LOSE!!!");
00200                     exit(0);
00201                 }
00202                 break;
00203             }
00204             case (14): {
00205                 lcd.filled_rectangle(70, 110, 127, 120, GREEN);
00206                 lcd.filled_rectangle(40,50 , 90, 60, WHITE);
00207                 break;
00208             }
00209             case (13): {
00210                 lcd.filled_rectangle(70, 120, 127, 130, GREEN);
00211                 lcd.filled_rectangle(40, 60 , 90, 70, WHITE);
00212                 lcd.filled_rectangle(0, 0 , 30, 10, YELLOW);
00213                 lcd.filled_rectangle(60, 0 , 127, 10, YELLOW);
00214                 break;
00215             }
00216             case (12): {
00217                 lcd.filled_rectangle(40, 70, 90, 80 , WHITE);
00218                 lcd.filled_rectangle(0, 10, 30, 20, YELLOW);
00219                 lcd.filled_rectangle(60, 10, 127, 20, YELLOW);
00220                 break;
00221             }
00222             case (11): {
00223                 lcd.filled_rectangle(40, 80, 90, 90 , WHITE);
00224                 lcd.filled_rectangle(0, 20 , 30, 30, YELLOW);
00225                 lcd.filled_rectangle(60, 20 , 127, 30, YELLOW);
00226                 break;
00227             }
00228             case (10): {
00229                 lcd.filled_rectangle(40, 90, 90, 100 , WHITE);
00230                 lcd.filled_rectangle(0, 30 , 30, 40, YELLOW);
00231                 lcd.filled_rectangle(60, 30 , 127, 40, YELLOW);
00232                 break;
00233             }
00234             case (9): {
00235                 lcd.filled_rectangle(40,100 , 90, 110, WHITE);
00236                 lcd.filled_rectangle(0, 40 , 30, 50, YELLOW);
00237                 lcd.filled_rectangle(60, 40 , 127, 50, YELLOW);
00238                 if(a>3 && a<8 ) {
00239                     lcd.locate(3,8);
00240                     lcd.printf("YOU LOSE!!!");
00241                     exit(0);
00242                 }
00243                 break;
00244             }
00245             case (8): {
00246                 lcd.filled_rectangle(40,110 , 90, 120, WHITE);
00247                 lcd.filled_rectangle(0, 50 , 30, 60, YELLOW);
00248                 lcd.filled_rectangle(60, 50 , 127, 60, YELLOW);
00249                 break;
00250             }
00251             case (7): {
00252                 lcd.filled_rectangle(40,120 , 90, 130, WHITE);
00253                 lcd.filled_rectangle(0, 60 , 30, 70, YELLOW);
00254                 lcd.filled_rectangle(60, 60 , 127, 70, YELLOW);
00255                 break;
00256             }
00257             case (6): {
00258                 lcd.filled_rectangle(0, 70 , 30, 80, YELLOW);
00259                 lcd.filled_rectangle(60, 70 , 127, 80, YELLOW);
00260                 break;
00261             }
00262             case (5): {
00263                 lcd.filled_rectangle(0, 80 , 30, 90, YELLOW);
00264                 lcd.filled_rectangle(60, 80 , 127, 90, YELLOW);
00265                 break;
00266             }
00267             case (4): {
00268                 lcd.filled_rectangle(0, 90 , 30, 100, YELLOW);
00269                 lcd.filled_rectangle(60, 90 , 127, 100, YELLOW);
00270                 break;
00271             }
00272             case (3): {
00273                 lcd.filled_rectangle(0, 100 , 30, 110, YELLOW);
00274                 lcd.filled_rectangle(60, 100 , 127, 110, YELLOW);
00275                 if(a<4 || a>5 ) {
00276                     lcd.locate(3,8);
00277                     lcd.printf("YOU LOSE!!!");
00278                     exit(0);
00279                 }
00280                 break;
00281             }
00282             case (2): {
00283                 lcd.filled_rectangle(0, 110 , 30, 120, YELLOW);
00284                 lcd.filled_rectangle(60, 110 , 127, 120, YELLOW);
00285                 break;
00286             }
00287             case (1): {
00288                 lcd.filled_rectangle(0, 120 , 30, 130, YELLOW);
00289                 lcd.filled_rectangle(60, 120 , 127, 130, YELLOW);
00290                 break;
00291             }
00292 
00293         }
00294         stdio_mutex.unlock();
00295     }
00296 }
00297 
00298 #endif