Millun Atluri / Mbed 2 deprecated ReactionGame

Dependencies:   PinDetect SDFileSystem Speaker mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 #include "wave_player.h"
00004 #include "Speaker.h"
00005 #include "PinDetect.h"
00006 #include <iostream>
00007 #include <ctime>
00008 
00009 
00010 DigitalOut led1(LED1);
00011 DigitalOut led2(LED2);
00012 DigitalOut led3(LED3);
00013 DigitalOut led4(LED4);
00014 
00015 SDFileSystem sd(p5, p6, p7, p8, "sd");
00016 AnalogOut DACout(p18);
00017 Serial pc(USBTX, USBRX);
00018 Serial blue(p28,p27);
00019 wave_player waver(&DACout);
00020 PinDetect  pin(p14);
00021 
00022 bool joyPressedBool = false;
00023 bool phonePressedBool = false;
00024 clock_t joyTime;
00025 clock_t phoneTime;
00026 clock_t ticks = CLOCKS_PER_SEC;
00027 
00028  void joyPressed( void ) {
00029     joyPressedBool = true;
00030     joyTime = clock();
00031     //joyTime = (double)joyTime/(double)ticks;
00032     led2 = 1;
00033     pc.printf("Player 1 pressed!\n");
00034     //wait(3);
00035  }
00036  
00037  void joyReleased( void ) {
00038     joyPressedBool = true;
00039     joyTime = clock();
00040     led2 = 0;
00041     //wait(3);
00042  }
00043  
00044  void joyPressedHeld( void ) {
00045 
00046  }
00047  
00048  void joyReleasedHeld( void ) {
00049 
00050  }
00051 // 
00052 //BusOut mbedleds(LED1,LED2,LED3,LED4);
00053 ////BusOut/In is faster than multiple DigitalOut/Ins
00054 // 
00055 //class Nav_Switch
00056 //{
00057 //public:
00058 //    Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
00059 //    int read();
00060 ////boolean functions to test each switch
00061 //    bool up();
00062 //    bool down();
00063 //    bool left();
00064 //    bool right();
00065 //    bool fire();
00066 ////automatic read on RHS
00067 //    operator int ();
00068 ////index to any switch array style
00069 //    bool operator[](int index) {
00070 //        return _pins[index];
00071 //    };
00072 //private:
00073 //    BusIn _pins;
00074 // 
00075 //};
00076 //Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
00077 //    _pins(up, down, left, right, fire)
00078 //{
00079 //    _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
00080 //    wait(0.001); //delays just a bit for pullups to pull inputs high
00081 //}
00082 //inline bool Nav_Switch::up()
00083 //{
00084 //    return !(_pins[0]);
00085 //}
00086 //inline bool Nav_Switch::down()
00087 //{
00088 //    return !(_pins[1]);
00089 //}
00090 //inline bool Nav_Switch::left()
00091 //{
00092 //    return !(_pins[2]);
00093 //}
00094 //inline bool Nav_Switch::right()
00095 //{
00096 //    return !(_pins[3]);
00097 //}
00098 //inline bool Nav_Switch::fire()
00099 //{
00100 //    return !(_pins[4]);
00101 //}
00102 //inline int Nav_Switch::read()
00103 //{
00104 //    return _pins.read();
00105 //}
00106 //inline Nav_Switch::operator int ()
00107 //{
00108 //    return _pins.read();
00109 //}
00110 // 
00111 //Nav_Switch myNav( p9, p6, p7, p5, p8); //pin order on Sparkfun breakout
00112 
00113 
00114 int main() {
00115        pin.mode( PullDown );
00116        pin.attach_asserted( &joyPressed );
00117        pin.attach_deasserted( &joyReleased );
00118        pin.setSampleFrequency();
00119    do{
00120     //read in the sound file
00121     FILE *wave_file;
00122     wave_file = fopen("/sd/countdown.wav", "r");
00123     led1 = 1;
00124     //play the countdown sound
00125     //waver.play(wave_file);
00126     led1 = 0;
00127     fclose(wave_file);
00128    
00129     pc.printf("START\n\n");
00130     
00131     //start the timer
00132     clock_t startTime = clock();
00133 
00134 
00135 //    //setup bluetooth
00136 //    char bnum = 0;
00137 //    do{
00138 //       if (blue.getc()=='!') {
00139 //            if (blue.getc()=='B') { //button data
00140 //                bnum = blue.getc(); //button number
00141 //            }
00142 //        }
00143 //    }while(blue.getc() != 'B');
00144 //    
00145 
00146 
00147     
00148     //while joystick or phone button has not been pressed
00149     pin.attach_asserted_held( &joyPressedHeld );
00150     pin.attach_deasserted_held( &joyReleasedHeld );
00151 
00152 
00153     
00154         //save time of one that was pressed
00155         //keep running until one that wasn't pressed is now pressed
00156         //print over serial
00157     while(!joyPressedBool || !phonePressedBool){
00158         led4 = 1;
00159         char bnum=0;
00160         char bhit=0;
00161         if (blue.getc()=='!') {
00162                     if (blue.getc()=='B') { //button data packet
00163                         bnum = blue.getc(); //button number
00164                         bhit = blue.getc(); //1=hit, 0=release
00165                         if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
00166                             switch (bnum) {
00167                                 case '1': //number button 1
00168                                 if (bhit=='1') {
00169                                     phoneTime = clock();
00170                                     phonePressedBool = true;
00171                                     pc.printf("Player 2 pressed!\n");
00172                                     led3 = 1;
00173                                 } 
00174                                                                 wait(1);
00175                                 break;
00176 
00177                             }
00178                        // pc.printf("hgere?1\n\n");
00179                         }
00180                     }
00181    //pc.printf("hgere?2\n\n");
00182         }
00183     }
00184     
00185    pc.printf("We have a winner! ");
00186    led1 = 1;
00187     if(joyTime < phoneTime) {
00188         pc.printf("Player 1 won!\n\n");
00189     }else if(phoneTime < joyTime) {
00190         pc.printf("Player 2 won!\n\n");
00191     }
00192 
00193     wait(30);
00194     }while(1);
00195     
00196 }