This program is a reaction time game for two players on the QW dev kit. The winner and his reaction time are shown in the console window and transmitted via Sigfox.

Dependencies:   mbed

Fork of QW-Reactiontime by Quicksand

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* This program demonstrates how to use the board to play a reaction time game.
00002  * The winner and his reaction time are displayed in the console window and transmitted via Sigfox.
00003  * Open a serial connection to the board to see extra info and instructions.
00004  */
00005  
00006 #include "mbed.h"
00007 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 
00011 #define SER_BUFFER_SIZE 32
00012 
00013 /* The 4 onboard LEDs */
00014 DigitalOut LED_0 (PB_6);
00015 DigitalOut LED_1 (PA_7);
00016 DigitalOut LED_2 (PA_6);
00017 DigitalOut LED_3 (PA_5);
00018 
00019 /* The 2 user buttons */
00020 InterruptIn SW1(PA_8);
00021 InterruptIn SW2(PB_10);
00022 
00023 /* Function prototypes */
00024 void sw1interrupt();
00025 void sw2interrupt();
00026 void sertmout();
00027 bool modem_command_check_ok(char * command);
00028 void modem_setup();
00029 
00030 bool ser_timeout = false; 
00031 
00032 /* Serial port over USB */
00033 Serial pc(USBTX, USBRX);
00034 
00035 /* Serial connection to sigfox modem */
00036 Serial modem(PA_9, PA_10);
00037 
00038 /* User variables */
00039 uint16_t randomTime;
00040 uint16_t reactionTime;
00041 uint8_t winner;
00042 
00043 bool play = false;
00044 bool player1 = false;
00045 bool player2 = false;
00046 bool busyPlaying =  false;
00047 
00048 int main() {   
00049     
00050     time_t t;
00051     
00052     /* Setup TD120x */
00053     wait(3);
00054     modem_setup();
00055     
00056     /* Turn off all LED */
00057     LED_0 = 1;
00058     LED_1 = 1;
00059     LED_2 = 1;
00060     LED_3 = 1;
00061     
00062     /* Setup button interrupts */    
00063     SW1.fall(&sw1interrupt);
00064     SW2.fall(&sw2interrupt); 
00065     
00066     /* Intializes random number generator */
00067     srand((unsigned) time(&t));
00068     
00069     /* Push a button to start the first game */    
00070     pc.printf("\nPress a button to start the reaction time game...\n\r");
00071     
00072     while(1) {          
00073         
00074         /* When waiting to start a new game, show a loop on the leds */
00075         while(play == false){            
00076             if(play == false){  // Check every time, otherwise after interrupt, loop would be finished first
00077                 LED_0 = 1;LED_1 = 1;LED_2 = 1;LED_3 = 0;
00078                 wait_ms(100);
00079             }
00080             if(play == false){
00081                 LED_0 = 1;LED_1 = 1;LED_2 = 0;LED_3 = 1;
00082                 wait_ms(100); 
00083             }
00084             if(play == false){
00085                 LED_0 = 1;LED_1 = 0;LED_2 = 1;LED_3 = 1;
00086                 wait_ms(100); 
00087             }
00088             if(play == false){
00089                 LED_0 = 0;LED_1 = 1;LED_2 = 1;LED_3 = 1;
00090                 wait_ms(100);   
00091             }
00092             if(play == false){
00093                 LED_0 = 1;LED_1 = 0;LED_2 = 1;LED_3 = 1;
00094                 wait_ms(100);      
00095             }
00096             if(play == false){
00097                 LED_0 = 1;LED_1 = 1;LED_2 = 0;LED_3 = 1;
00098                 wait_ms(100); 
00099             }                                 
00100         }  
00101         
00102         /* After the 4 leds flash on, calculate the reaction time */
00103         while(busyPlaying == true && player1==false && player2 == false){
00104             reactionTime++;
00105             wait_ms(1);
00106         }
00107         
00108         /* When player 1 has won */       
00109         if(player1 == true){
00110             pc.printf("\nPlayer 1 was the fastest. His time was: %d ms\n\r", reactionTime);            
00111             LED_0 = 1;
00112             LED_1 = 1;
00113             LED_2 = 1;
00114             LED_3 = 1;  
00115             winner = 1;          
00116             char command[SER_BUFFER_SIZE];
00117             sprintf(command, "AT$SF=06%04x%04x,2,0\n", (int) winner, (int) reactionTime );
00118             pc.printf("Sending winner = %i and reaction time =  %i ms over Sigfox\n", winner, reactionTime);
00119             pc.printf("using modem command: %s\n", command);
00120             modem_command_check_ok(command);
00121             player1 = false;
00122             player2= false;
00123             busyPlaying = false;            
00124             play = false;
00125             reactionTime = 0;
00126             pc.printf("\nPress a button to start a new game...\n\r");
00127         }
00128         
00129         /* When player 2 has won */
00130         if(player2 == true){
00131             pc.printf("\nPlayer 2 was the fastest. His time was: %d ms\n\r", reactionTime);            
00132             LED_0 = 1;
00133             LED_1 = 1;
00134             LED_2 = 1;
00135             LED_3 = 1; 
00136             winner = 2;           
00137             char command[SER_BUFFER_SIZE];
00138             sprintf(command, "AT$SF=06%04x%04x,2,0\n", (int) winner, (int) reactionTime );
00139             pc.printf("Sending winner = %i and reaction time =  %i ms over Sigfox\n", winner, reactionTime);
00140             pc.printf("using modem command: %s\n", command);
00141             modem_command_check_ok(command);
00142             player1 = false;
00143             player2= false;
00144             busyPlaying = false;            
00145             play = false;
00146             reactionTime = 0;
00147             pc.printf("\nPress a button to start a new game...\n\r");
00148         }        
00149         
00150         /* Check if a new game has been started, when a game has been started then generate a random time */
00151         if(play == true){
00152             pc.printf("\nGame has started, push your button as quick as possible when leds go on!\n\r");
00153             randomTime =   (rand() % 5000)+1;   //random wait max 5 seconds
00154             wait_ms( randomTime + 1000 ); // wait for random time + 1 second
00155             busyPlaying =  true;
00156             LED_0 = 0;
00157             LED_1 = 0;
00158             LED_2 = 0;
00159             LED_3 = 0;
00160         } 
00161         
00162         
00163     }
00164 }
00165 
00166 void modem_setup()
00167 {
00168     /* Reset to factory defaults */
00169     if(modem_command_check_ok("AT&F")) 
00170     {
00171         pc.printf("Factory reset succesfull\r\n");
00172     }
00173     else 
00174     {
00175         pc.printf("Factory reset TD120x failed\r\n");
00176     }
00177     /* Disable local echo */
00178     modem.printf("ATE0\n");
00179     if(modem_command_check_ok("ATE0")) 
00180     {
00181         pc.printf("Local echo disabled\r\n");
00182     }
00183     /* Write to mem */
00184     if(modem_command_check_ok("AT&W")) 
00185     {
00186         pc.printf("Settings saved!\r\n");
00187     }    
00188 }
00189 
00190 bool modem_command_check_ok(char * command)
00191 {
00192     /* First clear serial data buffers */
00193     while(modem.readable()) modem.getc();
00194     /* Timeout for response of the modem */
00195     Timeout tmout;
00196     ser_timeout = false;
00197     /* Buffer for incoming data */
00198     char responsebuffer[6];
00199     /* Flag to set when we get 'OK' response */
00200     bool ok = false;
00201     bool error = false;
00202     /* Print command to TD120x */
00203     modem.printf(command);
00204     /* Newline to activate command */
00205     modem.printf("\n");
00206     /* Wait untill serial feedback, min 7 seconds before timeout */
00207     tmout.attach(&sertmout, 7.0);
00208     while(!modem.readable()&& ser_timeout == false);
00209     while(!ok && !ser_timeout && !error) 
00210     {
00211         if(modem.readable()) 
00212         {
00213             for(int i = 0; i < 5; i++)
00214             {
00215                 responsebuffer[i] = responsebuffer[i+1];
00216             }
00217             responsebuffer[5] = modem.getc();
00218             if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'O' && responsebuffer[3] == 'K' && responsebuffer[4] == '\r' && responsebuffer[5] == '\n' ) 
00219             {
00220                 ok = true;
00221             }
00222             else if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'E' && responsebuffer[3] == 'R' && responsebuffer[4] == 'R' && responsebuffer[5] == 'O' ) 
00223             {
00224                 error = true;
00225             }
00226         }
00227     }
00228     tmout.detach();
00229     return ok;
00230 }
00231 
00232 /* Button 1 ISR */
00233 void sw1interrupt()
00234 {    
00235         if(play == false && busyPlaying == false){          // If not already playing... 
00236          play = true;
00237          LED_0 = 1;
00238          LED_1 = 1;
00239          LED_2 = 1;
00240          LED_3 = 1;
00241         }
00242         if(busyPlaying == true && player2 != true){         // When playing and there is no winner yet...
00243             player1 = true;
00244         }   
00245          
00246 }
00247 
00248 /* Button 2 ISR */
00249 void sw2interrupt()
00250 {    
00251         if(play == false && busyPlaying == false){          // If not already playing... 
00252          play = true;   
00253          LED_0 = 1;
00254          LED_1 = 1;
00255          LED_2 = 1;
00256          LED_3 = 1; 
00257         }
00258         if(busyPlaying == true && player1 != true){         // When playing and there is no winner yet...
00259             player2 = true;
00260         }       
00261     
00262 }
00263 
00264 /* ISR for serial timeout */
00265 void sertmout()
00266 {
00267     ser_timeout = true;
00268 }