Doubea Pierre / Mbed 2 deprecated HW3

Dependencies:   SDFileSystem TextLCD mbed-rtos mbed wave_player FATFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**********************************
00002  * Author: Seungcheol Baek
00003  * Institution: Georgia Tech
00004  *
00005  * Title: MAIN
00006  * Class: ECE2035
00007  * Assignment: Project 2
00008  **********************************/
00009 
00010 //includes
00011 #include <string.h>
00012 #include <stdio.h>
00013 #include <math.h>
00014 #include <cstdarg>
00015 #include "mbed.h"
00016 #include "rtos.h"
00017 #include "TextLCD.h"
00018 #include "SDFileSystem.h"
00019 #include "CommModule.h"
00020 #include "wave_player.h"
00021 #include "FATFileSystem.h"
00022 
00023 //defines
00024 #define BAUDRATE            9600
00025 #define BUFFSIZE            100
00026 #define GRAVITY             1.0
00027 #define PHIGH               10   // NEW
00028 #define PLOW                5    // NEW
00029 #define PI                  3.141592653589793238462643f
00030 #define BOMB_WAVFILE        "/sd/wavfiles/bomb.wav"
00031 #define MONKEY_WAVFILE      "/sd/wavfiles/monkey.wav"
00032 #define ACK                 "status-ack"
00033 
00034 //function prototypes
00035 int invert(int value);
00036 void print(const char *format, ...);
00037 void lowerCase(char *src);
00038 void startGame(void);
00039 void waitForAck(void);
00040 void playBombSound(void);
00041 void playMonkeySound(void);
00042 void pb1_hit_callback(void);
00043 void pb2_hit_callback(void);
00044 void pb3_hit_callback(void);
00045 void pb4_hit_callback(void);
00046 void getworld (int**world, char *World);
00047 void updateShot(int row, int column, int del);
00048 void colorTile(int row, int column, int strength);
00049 void deleteTile(int row, int column);
00050 void paaUpdate(int power, int angle);
00051 void hint(int row, int column, int power, int angle);
00052 
00053 //declare functions (assembly subroutines)
00054 extern "C" void setup_sequence(void);
00055 extern "C" void seg_driver_initialize(void);
00056 extern "C" void seg_driver(int value);
00057 
00058 //initialize hardware
00059 SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sck, cs
00060 TextLCD lcd(p26, p25, p24, p23, p22, p21); // rs, e, d4-d7
00061 AnalogOut DACout(p18);
00062 wave_player waver(&DACout);
00063 DigitalIn pb1(p30);
00064 DigitalIn pb2(p29);
00065 DigitalIn pb3(p28);
00066 DigitalIn pb4(p27);
00067 
00068 //communication device
00069 commSerial serDevice(USBTX, USBRX, BAUDRATE); //tx, rx
00070 
00071 // Global variables for push buttons
00072 char volatile power=PHIGH, angle=45, fire;   // NEW
00073 
00074 //main
00075 int main() {
00076     //initialize and clear 7-Segment Display (assembly subroutine)
00077     setup_sequence();
00078     seg_driver_initialize();
00079     seg_driver(0);
00080 
00081     // Use internal pullups for pushbuttons
00082     pb1.mode(PullUp);    
00083     pb2.mode(PullUp);
00084     pb3.mode(PullUp);
00085     pb4.mode(PullUp);
00086 
00087     //variables
00088     char World[10000];
00089   
00090     //check for wav file
00091     lcd.cls();
00092     lcd.printf("Locating WAV file...");
00093     FILE *test_file;
00094     while(1) {
00095         test_file=fopen(BOMB_WAVFILE,"r");
00096         if(test_file != NULL) {break;}
00097         wait(0.5);
00098     }
00099     fclose(test_file);
00100     while(1) {
00101         test_file=fopen(MONKEY_WAVFILE,"r");
00102         if(test_file != NULL) {break;}
00103         wait(0.5);
00104     }
00105     fclose(test_file);
00106     
00107     //notification
00108     lcd.cls();
00109     lcd.printf("Angry Monkeys");
00110     wait(1);
00111 
00112     
00113     /******** HW 3 *********/
00114 
00115     /** HW3.A During the check off, You should show the seg_driver is working correctly **/
00116     int i;
00117     for(i=0;i<10;i++){
00118             seg_driver(i);
00119             wait(0.3);
00120     }
00121     
00122     /** HW3.B During the check off, You should show the pushbuttons are debounced **/
00123     
00124     //temp variables
00125     int num_cannon=30, val1, val2, val3, val4;
00126     
00127     fire=0; //pb4 is set to fire
00128     lcd.cls();    
00129     while(fire<9){
00130         
00131         /****   BEGIN - your code goes here for HW3.B  ****/
00132         
00133         //read inputs
00134         val1 = invert(pb1.read());
00135         wait(0.2);
00136         val2 = invert(pb2.read());
00137         wait(0.2);
00138         val3 = invert(pb3.read());
00139         wait(0.2);
00140         val4 = invert(pb4.read());
00141         wait(0.2);
00142         
00143         if(val1){
00144             pb1_hit_callback(); //power 
00145         }
00146         if(val2){
00147             pb2_hit_callback(); //angle
00148         }
00149         if(val3){
00150             pb3_hit_callback(); //angle
00151         }
00152         if(val4){
00153             pb4_hit_callback(); //fire
00154         }
00155         
00156         //have fun... 
00157         
00158         /****    END - your code stops here   ****/
00159         
00160         //print lcd
00161         lcd.cls();
00162         lcd.printf("Left cannon:%d\n", num_cannon);
00163         if(power==PHIGH)                         // NEW
00164             lcd.printf("Angle:%d PHIGH", angle); // NEW
00165         else                                     // NEW
00166             lcd.printf("Angle:%d PLOW", angle);  // NEW
00167         
00168         wait(0.02);                              // NEW
00169         
00170     }
00171    
00172     /******** HW 3 END*********/
00173     /** Note that you can use the HW3 for the Project 2 by moving it to the appropriate place in the Project 2. 
00174         If you do not want to use HW3 for the Project 2, then you can simply remove it after check off. **/
00175     
00176     
00177     /******** Project 2 *********/    
00178     //loop
00179     while(1) {
00180         
00181         //synchronize front end display  
00182         startGame();                            
00183 
00184         //receive World
00185         serDevice.receiveData(World);            
00186         wait(1);
00187         
00188         //get world that will be used for your work
00189         int *world;
00190         getworld(&world, World);
00191         
00192         /****   BEGIN - your code goes here for project 2  ****/
00193         
00194         //debug statements
00195         print("testing");
00196         print("string1: %s\nstring2: %s", "hello", "world");
00197         print("int: %d, int: %d", world[2], world[3]);
00198         
00199         //Just for test...
00200         for(i=1;i<10;i++){
00201             updateShot(i-1,i,0);
00202             wait(1);
00203         } 
00204                        
00205         playBombSound();
00206         playMonkeySound();
00207         
00208         for(i=0;i<5;i++){       
00209             deleteTile(world[4*i+2],world[4*i+3]);
00210             wait(1);
00211         }
00212         //Just for test ends...
00213         
00214         
00215         //have fun... 
00216         
00217         /****    END - your code stops here   ****/
00218         free(world);  
00219     }
00220     //end loop
00221 }
00222 
00223 //fcn to send update
00224 void updateShot(int row, int column, int del){
00225     //temp variables
00226     char buffer[BUFFSIZE];
00227     
00228     //construct message
00229     sprintf(buffer, "%s-%d-%d-%d;", "update", row, column, del);
00230 
00231     //send message
00232     serDevice.sendData(buffer);
00233 }
00234 
00235 //fcn to send color
00236 void colorTile(int row, int column, int strength){
00237     //temp variables
00238     char buffer[BUFFSIZE];
00239     
00240     //construct message
00241     sprintf(buffer, "%s-%d-%d-%d;", "color", row, column, strength);
00242 
00243     //send message
00244     serDevice.sendData(buffer);
00245 }    
00246     
00247 //fcn to send delete
00248 void deleteTile(int row, int column){
00249     //temp variables
00250     char buffer[BUFFSIZE];
00251     
00252     //construct message
00253     sprintf(buffer, "%s-%d-%d;", "delete", row, column);
00254 
00255     //send message
00256     serDevice.sendData(buffer);
00257 } 
00258 
00259 //fcn to send power and angle
00260 void paaUpdate(int power, int angle){
00261     //temp variables
00262     char buffer[BUFFSIZE];
00263     
00264     //construct message
00265     sprintf(buffer, "%s-%d-%d;", "paa", power, angle);
00266 
00267     //send message
00268     serDevice.sendData(buffer);
00269 } 
00270 
00271 //fcn to send hint
00272 void hint(int row, int column, int power, int angle){
00273     //temp variables
00274     char buffer[BUFFSIZE];
00275     
00276     //construct message
00277     sprintf(buffer, "%s-%d-%d-%d-%d;", "hint", row, column, power, angle);
00278 
00279     //send message
00280     serDevice.sendData(buffer);
00281 }
00282 
00283 //fcn to get acknowledgement from serial peripheral
00284 void waitForAck(void) {
00285     //get acknowlegement
00286     char buffer[BUFFSIZE];
00287     while(1) {
00288         serDevice.receiveData(buffer);
00289         lowerCase(buffer);
00290         if(strcmp(ACK, buffer) == 0) {
00291             break;
00292         }
00293         memset(&buffer[0],0,strlen(buffer));     
00294     }
00295     return;
00296 }
00297 
00298 //fcn to initialize the frontend display
00299 void startGame(void) {
00300     //temp variables
00301     char buffer[BUFFSIZE];
00302 
00303     //construct message
00304     sprintf(buffer, "start");
00305 
00306     //send message
00307     serDevice.sendData(buffer);
00308 
00309     //wait for acknowledgement
00310     waitForAck();
00311 }
00312 
00313 //fcn to play bomb noise
00314 void playBombSound(void) {
00315     //open wav file
00316     FILE *wave_file;
00317     wave_file=fopen(BOMB_WAVFILE,"r");
00318     
00319     //play wav file
00320     waver.play(wave_file);
00321     
00322     //close wav file
00323     fclose(wave_file);
00324 }
00325 
00326 //fcn to play bomb noise
00327 void playMonkeySound(void) {
00328     //open wav file
00329     FILE *wave_file;
00330     wave_file=fopen(MONKEY_WAVFILE,"r");
00331     
00332     //play wav file
00333     waver.play(wave_file);
00334     
00335     //close wav file
00336     fclose(wave_file);
00337 }
00338 
00339 //fcn to print to console
00340 void print(const char *format, ...) {
00341     //temp variables
00342     char buffer[BUFFSIZE];
00343     char temp[BUFFSIZE-6];
00344     
00345     //construct message part 1
00346     sprintf(buffer, "print-");
00347     
00348     //construct message part 2
00349     va_list arguments;
00350     va_start(arguments, format);
00351     vsnprintf(temp, BUFFSIZE-7, format, arguments);
00352     va_end(arguments);
00353     
00354     //concatenate parts
00355     strcat(buffer, temp);
00356     
00357     //send message
00358     serDevice.sendData(buffer);
00359     
00360     //wait for acknowledgement
00361     waitForAck();  
00362 }
00363 
00364 //fcn to convert string to lowercase
00365 void lowerCase(char *src) {
00366     int i=0;;
00367     while(src[i] != '\0') {
00368         if((src[i] > 64) && (src[i] < 91)) {
00369             src[i]+=32;
00370         }
00371         i++;
00372     }
00373     return;
00374 }
00375 
00376 //function to perform bitwise inversion
00377 int invert(int value) {
00378     if (value == 0) {
00379         return 1;
00380     } else {
00381         return 0;
00382     }
00383 }
00384 
00385 // Callback routine is interrupt activated by a debounced pb hit
00386 void pb1_hit_callback (void) {
00387     if(power==PHIGH)    // NEW
00388         power=PLOW;     // NEW
00389     else                // NEW
00390         power=PHIGH;    // NEW
00391 }
00392 void pb2_hit_callback (void) {
00393     if(angle<90)
00394         angle++;
00395     else if(angle>=90) 
00396         angle=0;
00397 }
00398 void pb3_hit_callback (void) {
00399     if(angle>0)
00400         angle--;
00401     else if(angle<=0)
00402         angle=90;
00403 }
00404 void pb4_hit_callback (void) {
00405     fire++;
00406 }
00407 
00408 //func. to get world
00409 void getworld (int**world, char *World){
00410     int i;
00411     char temp[3];
00412     
00413     //allocate world
00414     *world = (int*)malloc(sizeof(int)*(((World[2]<<8)+World[3])*4+2));
00415     
00416     //get it
00417     (*world)[0]=(World[0]<<8)+World[1];
00418     (*world)[1]=(World[2]<<8)+World[3];
00419     for(i=0;i<((*world)[1]*4);i++){
00420         temp[0] = World[(2*i)+4];
00421         temp[1] = World[(2*i)+5];
00422         temp[2] = '\0';   
00423         sscanf(temp, "%d", &((*world)[i+2]));            
00424     }
00425 }
00426