Advanced Simon Says

Dependencies:   PinDetect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 #include "SSD1306.h"
00004 
00005 
00006 DigitalOut Yellow(D13);
00007 DigitalOut LED[5] = {D8,D9,D10,D11,D12};
00008 PinDetect  BUTTON[5] = {D3,D4,D5,D6,D7};
00009 
00010 #define level0 0.4
00011 #define level1 0.15
00012 
00013 char temp_line[5] = {0};
00014 char line[5] = {0};
00015 char answer[5] = {0};
00016 int user_score = 0;
00017 int level = 0;
00018 int life = 0;
00019 int score_up = 0;
00020 
00021 uint8_t offset1 = 50;
00022 uint8_t offset2 = 75;
00023 char buf_0[8] = "0";
00024 char buf_1[8] = "1";
00025 char buf_2[8] = "2";
00026 char buf_3[8] = "3";
00027 
00028 void LED_BLINK();
00029 
00030 void START();
00031 void INIT();
00032 void SETLINE();
00033 void SHOWLINE();
00034 
00035 void KEY0PRESSED();
00036 void KEY1PRESSED();
00037 void KEY2PRESSED();
00038 void KEY3PRESSED();
00039 void KEY4PRESSED();
00040 void PRESSBUTTON();
00041 
00042 int COMPARE();
00043 
00044 void SCORE();
00045 
00046 void CLOSE();
00047 
00048 
00049 int main()
00050 {
00051 
00052     START();
00053         
00054     init();
00055     cls();
00056     wait(1.0);
00057     OLED_DrawBMP(0,0,128,8,(unsigned char *)ready);
00058     wait(2.0);
00059     OLED_DrawBMP(0,0,128,8,(unsigned char *)go);
00060     wait(1.5);
00061     cls();
00062     OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_full);
00063     LED_P23x32Str(offset2,3,buf_0);
00064 
00065     wait(0.5);
00066     
00067     printf("start\r\n");
00068     
00069     while(1){      
00070         if(user_score == 30 && level == 1)  break;
00071         
00072         INIT();
00073         SETLINE();
00074         PRESSBUTTON();
00075         
00076         wait(1.0);  // wait for answer time      
00077         
00078         int ret = COMPARE();
00079         
00080         if(ret < 0){
00081             printf("bubu\r\n");
00082             life--;
00083             if(life == 2){
00084                 score_up = 0;
00085                 SCORE();
00086             }else if(life == 1){
00087                 score_up = 0;
00088                 SCORE();
00089             }else if(life == 0){
00090                 if(level == 0){
00091                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_empty);
00092                     wait(0.1);
00093                 }else if(level == 1){
00094                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_empty);
00095                     wait(0.1);
00096                 }else{
00097                     printf("life error\r\n");
00098                 }
00099                 
00100                 break;
00101                 
00102             }else{
00103                 printf("score display error\r\n");
00104                 score_up = 0;
00105                 SCORE();
00106             }
00107         }else{
00108             printf("correct\r\n");
00109             score_up = 1;
00110             SCORE();
00111         }
00112                 
00113         wait(2.0);
00114     }
00115     
00116     CLOSE();
00117     
00118     printf("end\r\n");
00119 
00120     
00121 }
00122 
00123 
00124 
00125 void LED_BLINK( void ) {
00126     for(int i = 0; i < 5; i++)  LED[i] = 1;     Yellow = 0;     wait(0.5);
00127     for(int i = 0; i < 5; i++)  LED[i] = 0;     Yellow = 1;     wait(0.5);
00128     for(int i = 0; i < 5; i++)  LED[i] = 1;     Yellow = 0;     wait(0.5);
00129     for(int i = 0; i < 5; i++)  LED[i] = 0;     Yellow = 1;     wait(0.5);
00130     for(int i = 0; i < 5; i++)  LED[i] = 1;     Yellow = 0;     wait(0.5);
00131     for(int i = 0; i < 5; i++)  LED[i] = 0;     Yellow = 1;
00132 }
00133 
00134 // callbacks follow.
00135 void START( void ){
00136     user_score = 0;
00137     life = 3;
00138     score_up = 0;
00139     level = 0;
00140     
00141     LED_BLINK();
00142     
00143     printf("start\r\n");        // oled diaplay part
00144 }
00145 
00146 // initialize
00147 void INIT( void ) {
00148     for(int i = 0; i < 5; i++){
00149         line[i] = 0;
00150         answer[i] = 0;
00151     }
00152 }
00153 
00154 // set led random value and display
00155 void SETLINE( void ) {       
00156     for(int j = 0; j < 2; j++){ 
00157     
00158         for(int i = 0; i < 5; i++){
00159             temp_line[i] = rand() % 2;
00160             line[i] = line[i] + temp_line[i];
00161             printf("line[%d] = %d\r\n",i,line[i]);
00162         }
00163         
00164         int temp = 0;      
00165         for(int i = 0; i < 5; i++){
00166             temp = temp + temp_line[i];
00167         }
00168               
00169         if(temp == 0){   
00170             for(int i = 0; i < 5; i++){
00171                 temp_line[i] = 1;
00172             }
00173         }else{ 
00174             for(int i = 0; i < 5; i++){
00175                 temp_line[i] = temp_line[i];
00176             }
00177         } 
00178              
00179         for(int i = 0; i < 5; i++) {
00180             if(temp_line[i] > 0){
00181                 LED[i] = 1;
00182                 temp_line[i]--;
00183             }else {
00184                 LED[i] = 0;
00185             }
00186             
00187             if(level == 0)      wait(level0);
00188             else if(level == 1) wait(level1);
00189             else                wait(level0);
00190             
00191             LED[i] = 0;
00192             wait(0.02);
00193         }
00194     } 
00195 }
00196 
00197 
00198 // button 
00199 void KEY0PRESSED( void ) {
00200     answer[0]++;
00201     printf("answer[0] = %d\r\n",answer[0]); 
00202 }
00203 
00204 void KEY1PRESSED( void ) {
00205     answer[1]++;
00206     printf("answer[1] = %d\r\n",answer[1]);
00207 }
00208 
00209 void KEY2PRESSED( void ) {
00210     answer[2]++;
00211     printf("answer[2] = %d\r\n",answer[2]);
00212 }
00213 
00214 void KEY3PRESSED( void ) {
00215     answer[3]++;
00216     printf("answer[3] = %d\r\n",answer[3]);
00217 }
00218 
00219 void KEY4PRESSED( void ) {
00220     answer[4]++;
00221     printf("answer[4] = %d\r\n",answer[4]);
00222 }
00223 
00224 
00225 // interrupt
00226 void PRESSBUTTON( void ) {
00227     BUTTON[0].attach_asserted(&KEY0PRESSED);
00228     BUTTON[1].attach_asserted(&KEY1PRESSED);
00229     BUTTON[2].attach_asserted(&KEY2PRESSED);
00230     BUTTON[3].attach_asserted(&KEY3PRESSED);
00231     BUTTON[4].attach_asserted(&KEY4PRESSED);
00232     
00233     for(int i = 0; i < 5; i++) {
00234         BUTTON[i].mode(PullDown);
00235         BUTTON[i].setSampleFrequency(); // Defaults to 20ms.
00236     }   
00237     wait(5.0);
00238 }
00239 
00240 
00241 // compare user input and random value
00242 int COMPARE() {
00243     for(int i = 0; i < 5; i++) {
00244         printf("answer[%d]=%d\r\n",i,answer[i]);
00245         if(answer[i] != line[i]){
00246             answer[i] = 0;
00247             return -1;
00248         }else{
00249             answer[i] = 0;
00250         }
00251     }    
00252     return 0;
00253 }
00254 
00255 // score
00256 void SCORE() {
00257 
00258     wait(0.5);
00259     if(level == 0){
00260             if(user_score == 0){
00261                 if(score_up == 1){
00262                     Yellow = 0;  wait(0.2);
00263                     Yellow = 1;  wait(0.2);
00264                     Yellow = 0;  wait(0.2);
00265                     Yellow = 1;  wait(0.2);
00266                     Yellow = 0;  wait(0.2);
00267                     Yellow = 1;
00268                     user_score = user_score + 10;
00269                     printf("score : %d\r\n",user_score);
00270                     if(life == 3){
00271                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_full);
00272                         LED_P23x32Str(offset1,3,buf_1);
00273                         LED_P23x32Str(offset2,3,buf_0);
00274                     }else if(life == 2){
00275                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_two);
00276                         LED_P23x32Str(offset1,3,buf_1);
00277                         LED_P23x32Str(offset2,3,buf_0);
00278                     }else if(life == 1){
00279                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_one);
00280                         LED_P23x32Str(offset1,3,buf_1);
00281                         LED_P23x32Str(offset2,3,buf_0);
00282                     }else{
00283                         printf("level menu error\r\n");
00284                     }
00285                     
00286                 }else{
00287                     user_score = user_score;
00288                     printf("score : %d\r\n",user_score);
00289                     if(life == 3){
00290                         Yellow = 0;  wait(0.7);
00291                         Yellow = 1;  wait(0.2);
00292                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_full);
00293                         LED_P23x32Str(offset2,3,buf_0);
00294                     }else if(life == 2){
00295                         Yellow = 0;  wait(0.7);
00296                         Yellow = 1;  wait(0.2);
00297                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_two);
00298                         LED_P23x32Str(offset2,3,buf_0);
00299                     }else if(life == 1){
00300                         Yellow = 0;  wait(0.7);
00301                         Yellow = 1;  wait(0.2);
00302                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_one);
00303                         LED_P23x32Str(offset2,3,buf_0);
00304                     }else{
00305                         printf("level menu error\r\n");
00306                     }
00307                 }
00308                 wait(0.5);
00309             }
00310             else if(user_score == 10){
00311                 if(score_up == 1){
00312                     Yellow = 0;  wait(0.2);
00313                     Yellow = 1;  wait(0.2);
00314                     Yellow = 0;  wait(0.2);
00315                     Yellow = 1;  wait(0.2);
00316                     Yellow = 0;  wait(0.2);
00317                     Yellow = 1;
00318                     user_score = user_score + 10;
00319                     printf("score : %d\r\n",user_score);
00320                     if(life == 3){
00321                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_full);
00322                         LED_P23x32Str(offset1,3,buf_2);
00323                         LED_P23x32Str(offset2,3,buf_0);
00324                     }else if(life == 2){
00325                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_two);
00326                         LED_P23x32Str(offset1,3,buf_2);
00327                         LED_P23x32Str(offset2,3,buf_0);
00328                     }else if(life == 1){
00329                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_one);
00330                         LED_P23x32Str(offset1,3,buf_2);
00331                         LED_P23x32Str(offset2,3,buf_0);
00332                     }else{
00333                         printf("level menu error\r\n");
00334                     }
00335                 }else{
00336                     user_score = user_score;
00337                     printf("score : %d\r\n",user_score);
00338                     if(life == 3){
00339                         Yellow = 0;  wait(0.7);
00340                         Yellow = 1;  wait(0.2);
00341                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_full);
00342                         LED_P23x32Str(offset1,3,buf_1);
00343                         LED_P23x32Str(offset2,3,buf_0);
00344                     }else if(life == 2){
00345                         Yellow = 0;  wait(0.7);
00346                         Yellow = 1;  wait(0.2);
00347                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_two);
00348                         LED_P23x32Str(offset1,3,buf_1);
00349                         LED_P23x32Str(offset2,3,buf_0);
00350                     }else if(life == 1){
00351                         Yellow = 0;  wait(0.7);
00352                         Yellow = 1;  wait(0.2);
00353                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_one);
00354                         LED_P23x32Str(offset1,3,buf_1);
00355                         LED_P23x32Str(offset2,3,buf_0);
00356                     }else{
00357                         printf("level menu error\r\n");
00358                     }
00359                 }
00360                 wait(0.5);
00361             }
00362             else if(user_score == 20){
00363                 if(score_up == 1){
00364                     Yellow = 0;  wait(0.2);
00365                     Yellow = 1;  wait(0.2);
00366                     Yellow = 0;  wait(0.2);
00367                     Yellow = 1;  wait(0.2);
00368                     Yellow = 0;  wait(0.2);
00369                     Yellow = 1;
00370                     user_score = user_score + 10;
00371                     printf("score : %d\r\n",user_score);
00372                     if(life == 3){
00373                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_full);
00374                         LED_P23x32Str(offset1,3,buf_3);
00375                         LED_P23x32Str(offset2,3,buf_0);
00376                     }else if(life == 2){
00377                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_two);
00378                         LED_P23x32Str(offset1,3,buf_3);
00379                         LED_P23x32Str(offset2,3,buf_0);
00380                     }else if(life == 1){
00381                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_one);
00382                         LED_P23x32Str(offset1,3,buf_3);
00383                         LED_P23x32Str(offset2,3,buf_0);
00384                     }else{
00385                         printf("level menu error\r\n");
00386                     }
00387                     wait(0.5);
00388                     cls();
00389                     OLED_DrawBMP(0,0,128,8,(unsigned char *)levelup);
00390                     user_score = 0;
00391                     level = 1;
00392                     wait(0.5);
00393                     LED_BLINK();
00394                     cls();
00395                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_full);
00396                     LED_P23x32Str(offset2,3,buf_0);
00397                 }else{
00398                     user_score = user_score;
00399                     printf("score : %d\r\n",user_score);
00400                     if(life == 3){
00401                         Yellow = 0;  wait(0.7);
00402                         Yellow = 1;  wait(0.2);
00403                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_full);
00404                         LED_P23x32Str(offset1,3,buf_2);
00405                         LED_P23x32Str(offset2,3,buf_0);
00406                     }else if(life == 2){
00407                         Yellow = 0;  wait(0.7);
00408                         Yellow = 1;  wait(0.2);
00409                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_two);
00410                         LED_P23x32Str(offset1,3,buf_2);
00411                         LED_P23x32Str(offset2,3,buf_0);
00412                     }else if(life == 1){
00413                         Yellow = 0;  wait(0.7);
00414                         Yellow = 1;  wait(0.2);
00415                         OLED_DrawBMP(0,0,128,8,(unsigned char *)level0_one);
00416                         LED_P23x32Str(offset1,3,buf_2);
00417                         LED_P23x32Str(offset2,3,buf_0);
00418                     }else{
00419                         printf("level menu error\r\n");
00420                     }
00421                 }
00422                 wait(0.5);
00423             }else {
00424                 user_score = user_score;
00425                 printf("score error\r\n");
00426                 
00427                 wait(0.5);
00428             }
00429             
00430     }else if(level == 1){
00431         if(user_score == 0){
00432             if(score_up == 1){
00433                     Yellow = 0;  wait(0.2);
00434                     Yellow = 1;  wait(0.2);
00435                     Yellow = 0;  wait(0.2);
00436                     Yellow = 1;  wait(0.2);
00437                     Yellow = 0;  wait(0.2);
00438                     Yellow = 1;
00439                 user_score = user_score + 10;
00440                 printf("score : %d\r\n",user_score);
00441                 if(life == 3){
00442                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_full);
00443                     LED_P23x32Str(offset1,3,buf_1);
00444                     LED_P23x32Str(offset2,3,buf_0);
00445                 }else if(life == 2){
00446                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_two);
00447                     LED_P23x32Str(offset1,3,buf_1);
00448                     LED_P23x32Str(offset2,3,buf_0);
00449                 }else if(life == 1){
00450                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_one);
00451                     LED_P23x32Str(offset1,3,buf_1);
00452                     LED_P23x32Str(offset2,3,buf_0);
00453                 }else{
00454                     printf("level menu error\r\n");
00455                 }
00456                 
00457             }else{
00458                 user_score = user_score;
00459                 printf("score : %d\r\n",user_score);
00460                 if(life == 3){
00461                     Yellow = 0;  wait(0.7);
00462                     Yellow = 1;  wait(0.2);
00463                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_full);
00464                     LED_P23x32Str(offset2,3,buf_0);
00465                 }else if(life == 2){
00466                     Yellow = 0;  wait(0.7);
00467                     Yellow = 1;  wait(0.2);
00468                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_two);
00469                     LED_P23x32Str(offset2,3,buf_0);
00470                 }else if(life == 1){
00471                     Yellow = 0;  wait(0.7);
00472                     Yellow = 1;  wait(0.2);
00473                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_one);
00474                     LED_P23x32Str(offset2,3,buf_0);
00475                 }else{
00476                     printf("level menu error\r\n");
00477                 }
00478             }
00479             wait(0.5);
00480         }
00481         else if(user_score == 10){
00482             if(score_up == 1){
00483                     Yellow = 0;  wait(0.2);
00484                     Yellow = 1;  wait(0.2);
00485                     Yellow = 0;  wait(0.2);
00486                     Yellow = 1;  wait(0.2);
00487                     Yellow = 0;  wait(0.2);
00488                     Yellow = 1;
00489                 user_score = user_score + 10;
00490                 printf("score : %d\r\n",user_score);
00491                 if(life == 3){
00492                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_full);
00493                     LED_P23x32Str(offset1,3,buf_2);
00494                     LED_P23x32Str(offset2,3,buf_0);
00495                 }else if(life == 2){
00496                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_two);
00497                     LED_P23x32Str(offset1,3,buf_2);
00498                     LED_P23x32Str(offset2,3,buf_0);
00499                 }else if(life == 1){
00500                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_one);
00501                     LED_P23x32Str(offset1,3,buf_2);
00502                     LED_P23x32Str(offset2,3,buf_0);
00503                 }else{
00504                     printf("level menu error\r\n");
00505                 }
00506             }else{
00507                 user_score = user_score;
00508                 printf("score : %d\r\n",user_score);
00509                 if(life == 3){
00510                     Yellow = 0;  wait(0.7);
00511                     Yellow = 1;  wait(0.2);
00512                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_full);
00513                     LED_P23x32Str(offset1,3,buf_1);
00514                     LED_P23x32Str(offset2,3,buf_0);
00515                 }else if(life == 2){
00516                     Yellow = 0;  wait(0.7);
00517                     Yellow = 1;  wait(0.2);
00518                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_two);
00519                     LED_P23x32Str(offset1,3,buf_1);
00520                     LED_P23x32Str(offset2,3,buf_0);
00521                 }else if(life == 1){
00522                     Yellow = 0;  wait(0.7);
00523                     Yellow = 1;  wait(0.2);
00524                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_one);
00525                     LED_P23x32Str(offset1,3,buf_1);
00526                     LED_P23x32Str(offset2,3,buf_0);
00527                 }else{
00528                     printf("level menu error\r\n");
00529                 }
00530             }
00531             wait(0.5);
00532         }
00533         else if(user_score == 20){
00534             if(score_up == 1){
00535                     Yellow = 0;  wait(0.2);
00536                     Yellow = 1;  wait(0.2);
00537                     Yellow = 0;  wait(0.2);
00538                     Yellow = 1;  wait(0.2);
00539                     Yellow = 0;  wait(0.2);
00540                     Yellow = 1;
00541                 user_score = user_score + 10;
00542                 printf("score : %d\r\n",user_score);
00543                 if(life == 3){
00544                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_full);
00545                     LED_P23x32Str(offset1,3,buf_3);
00546                     LED_P23x32Str(offset2,3,buf_0);
00547                 }else if(life == 2){
00548                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_two);
00549                     LED_P23x32Str(offset1,3,buf_3);
00550                     LED_P23x32Str(offset2,3,buf_0);
00551                 }else if(life == 1){
00552                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_one);
00553                     LED_P23x32Str(offset1,3,buf_3);
00554                     LED_P23x32Str(offset2,3,buf_0);
00555                 }else{
00556                     printf("level menu error\r\n");
00557                 }
00558                 wait(1.0);
00559                 
00560             }else{
00561                 user_score = user_score;
00562                 printf("score : %d\r\n",user_score);
00563                 if(life == 3){
00564                     Yellow = 0;  wait(0.7);
00565                     Yellow = 1;  wait(0.2);
00566                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_full);
00567                     LED_P23x32Str(offset1,3,buf_2);
00568                     LED_P23x32Str(offset2,3,buf_0);
00569                 }else if(life == 2){
00570                     Yellow = 0;  wait(0.7);
00571                     Yellow = 1;  wait(0.2);
00572                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_two);
00573                     LED_P23x32Str(offset1,3,buf_2);
00574                     LED_P23x32Str(offset2,3,buf_0);
00575                 }else if(life == 1){
00576                     Yellow = 0;  wait(0.7);
00577                     Yellow = 1;  wait(0.2);
00578                     OLED_DrawBMP(0,0,128,8,(unsigned char *)level1_one);
00579                     LED_P23x32Str(offset1,3,buf_2);
00580                     LED_P23x32Str(offset2,3,buf_0);
00581                 }else{
00582                     printf("level menu error\r\n");
00583                 }
00584             }
00585             wait(0.5);
00586         }else {
00587             user_score = user_score;
00588             printf("score error\r\n");
00589             
00590             wait(0.5);
00591         }
00592     }
00593 } 
00594 
00595 void CLOSE( void ) {
00596     OLED_DrawBMP(0,0,128,8,(unsigned char *)end);
00597 }
00598 
00599