Sebastien Luzy / Mbed OS Code_capteur-APDS9960_ecran-STM32F746G-DISCO

Dependencies:   TS_DISCO_F746NG LCD_DISCO_F746NG BSP_DISCO_F746NG BUTTON_GROUP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "stm32746g_discovery_lcd.h"
00003 #include "stm32746g_discovery_ts.h"
00004 #include "button_group.hpp"
00005 #include "Adafruit_APDS9960.h "
00006 
00007 Serial pc(USBTX, USBRX); // tx, rx
00008 
00009 I2C * i2c_sensor;
00010 Adafruit_APDS9960 *sensor;
00011 
00012 using namespace Mikami;
00013 TS_DISCO_F746NG ts_;
00014 LCD_DISCO_F746NG lcd_;
00015 
00016 
00017 void init_sensor()
00018 {
00019     i2c_sensor = new I2C(I2C_SDA, I2C_SCL);
00020     sensor = new Adafruit_APDS9960(&pc);
00021     bool initok = sensor->begin(i2c_sensor);
00022     if (!initok)
00023     {
00024         pc.printf("\n\rErreur avec l'initialisation du capteur de couleur\n\r");
00025     }else pc.printf("\n\rLe capteur de couleur a ete initialise\n\r");
00026     sensor->enableColor(true);
00027 }
00028 
00029 
00030 int main()
00031 {
00032     init_sensor();
00033     
00034     unsigned int boutton_commencer = 0;
00035     unsigned int boutton_reset = 0;
00036     
00037     unsigned int nbAlea = 0;
00038     unsigned int couleur;
00039     unsigned int couleur1;
00040     unsigned int couleur2;
00041     unsigned int couleur3;
00042     unsigned int couleur4;
00043     unsigned int sequence = 0;
00044     unsigned int i = 0;
00045     
00046     unsigned int reponse;
00047     unsigned int reponse1;
00048     unsigned int reponse2;
00049     unsigned int reponse3;
00050     unsigned int reponse4;
00051     unsigned int sequence_reponse = 0;
00052     unsigned int j = 0;
00053     
00054     BSP_LCD_Init();
00055     BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
00056     BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
00057  
00058  
00059     while(1)
00060     {      
00061         //Ecran d'acceuil
00062         BSP_LCD_Clear(LCD_COLOR_BLACK);
00063         BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
00064         BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
00065         BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
00066     
00067         BSP_LCD_DisplayStringAt(0, 40, (uint8_t *)"BIENVENUE", CENTER_MODE);
00068         HAL_Delay(2000);
00069         
00070         BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
00071         BSP_LCD_SetTextColor(LCD_COLOR_RED);
00072         BSP_LCD_DisplayStringAt(0, 90, (uint8_t *)"SUR UN", CENTER_MODE);
00073         HAL_Delay(2000);
00074     
00075         BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
00076         BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
00077         BSP_LCD_DisplayStringAt(0, 140, (uint8_t *)"CAPTEUR DE COULEUR", CENTER_MODE);
00078         HAL_Delay(2000);
00079         
00080         
00081         //Bouton commencer la partie
00082         Button commencer(lcd_, ts_, 88, 205, 300, 50,
00083                      LCD_COLOR_BLUE, LCD_COLOR_BLACK, "Commencer la partie", Font20);
00084         commencer.Draw(LCD_COLOR_WHITE, LCD_COLOR_BLACK);
00085         
00086         while(boutton_commencer == 0)
00087         {    
00088             if(commencer.Touched())
00089             {
00090                 boutton_commencer = 1;
00091             }
00092         }
00093         boutton_commencer = 0;
00094         
00095         
00096         //Ecran montrant la séquence à répéter pour gagner
00097         BSP_LCD_Clear(LCD_COLOR_BLACK);
00098         BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
00099         BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
00100         BSP_LCD_DisplayStringAt(0, 40, (uint8_t *)"Repeter la sequence", CENTER_MODE);
00101         BSP_LCD_SetTextColor(LCD_COLOR_RED);
00102         BSP_LCD_DisplayStringAt(0, 80, (uint8_t *)"une fois celle-ci", CENTER_MODE);
00103         BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
00104         BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)"terminee", CENTER_MODE);
00105         HAL_Delay(4000);
00106      
00107      
00108         //Initialisation de la séquence 
00109         while(sequence == 0){
00110             srand(time(NULL));  
00111             nbAlea=rand()%4;
00112             //nbAlea=rand()%3;
00113             
00114             switch(nbAlea){
00115                 case 0:
00116                     couleur = LCD_COLOR_RED;
00117                     i = i + 1;
00118                     break;
00119                 case 1:
00120                     couleur = LCD_COLOR_GREEN;  
00121                     i = i + 1;
00122                     break;
00123                 case 2:
00124                     couleur = LCD_COLOR_BLUE;
00125                     i = i + 1;
00126                     break;
00127                 case 3:   
00128                     couleur = LCD_COLOR_WHITE;
00129                     i = i + 1;
00130                     break;
00131             }  
00132             
00133             switch(i){
00134                 case 1:
00135                     couleur1 = couleur;
00136                     BSP_LCD_SetTextColor(couleur1);
00137                     BSP_LCD_FillRect(50, 170, 70, 70);
00138                     HAL_Delay(1000);
00139                     break;
00140                 case 2:
00141                     couleur2 = couleur;
00142                     BSP_LCD_SetTextColor(couleur2);
00143                     BSP_LCD_FillRect(150, 170, 70, 70);
00144                     HAL_Delay(1000);
00145                     break;
00146                 case 3:
00147                     couleur3 = couleur;   
00148                     BSP_LCD_SetTextColor(couleur3);
00149                     BSP_LCD_FillRect(250, 170, 70, 70);
00150                     HAL_Delay(1000);
00151                     break;
00152                 case 4:   
00153                     couleur4 = couleur;   
00154                     BSP_LCD_SetTextColor(couleur4);
00155                     BSP_LCD_FillRect(350, 170, 70, 70);
00156                     HAL_Delay(1000);
00157                     sequence = 1;
00158                     i = 0;
00159                     break;
00160             }
00161             wait_us(2000000);
00162         }
00163         sequence = 0;
00164         
00165         
00166         //Ecran "tour de l'utilisateur"
00167         BSP_LCD_Clear(LCD_COLOR_BLACK);
00168         BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
00169         BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
00170         BSP_LCD_DisplayStringAt(0, 40, (uint8_t *)"A vous de jouer !", CENTER_MODE);
00171         BSP_LCD_SetTextColor(LCD_COLOR_RED);
00172         BSP_LCD_DisplayStringAt(0, 80, (uint8_t *)"Quelle etait la sequence ?", CENTER_MODE);
00173         //BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
00174         //BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)"Appuyer sur le bouton de couleur correspondant", CENTER_MODE);
00175         HAL_Delay(4000);
00176         
00177             
00178         //create some variables to store the color data in
00179         uint16_t r, g, b, c;
00180             
00181         //wait for color data to be ready
00182         while(!sensor->colorDataReady()){
00183         wait_us(5000);
00184         }
00185         
00186         while(sequence_reponse == 0){
00187             //get the data and print the different channels
00188             sensor->getColorData(&r, &g, &b, &c);
00189             pc.printf("red: ");
00190             pc.printf("%u ", r);
00191             
00192             pc.printf(" green: ");
00193             pc.printf("%u ", g);
00194             
00195             pc.printf(" blue: ");
00196             pc.printf("%u ", b);
00197             
00198             pc.printf(" clear: ");
00199             pc.printf("%u ", c);
00200             pc.printf("\r\n");
00201         
00202             if(r>400 && g>400 && b>400)
00203             {
00204                 pc.printf("Test 0: blanc\r\n");
00205                 reponse = LCD_COLOR_WHITE;
00206                 j = j + 1;
00207             }
00208             else if(r>g && r>b && r>100)
00209             {
00210                 pc.printf("Test 1: rouge\r\n");
00211                 reponse = LCD_COLOR_RED;
00212                 j = j + 1;
00213             }
00214             else if(b>r && b>g && b>100)
00215             {
00216                 pc.printf("Test 2: bleu\r\n");
00217                 reponse = LCD_COLOR_BLUE;
00218                 j = j + 1;
00219             }
00220             else if(g>r && g>b && g>100)
00221             {
00222                 pc.printf("Test 3: vert\r\n");
00223                 reponse = LCD_COLOR_GREEN;
00224                 j = j + 1;
00225             }
00226             
00227             switch(j){
00228                 case 1:
00229                     reponse1 = reponse;
00230                     BSP_LCD_SetTextColor(reponse1);
00231                     BSP_LCD_FillRect(50, 170, 70, 70);
00232                     HAL_Delay(1000);
00233                     break;
00234                 case 2:
00235                     reponse2 = reponse;
00236                     BSP_LCD_SetTextColor(reponse2);
00237                     BSP_LCD_FillRect(150, 170, 70, 70);
00238                     HAL_Delay(1000);
00239                     break;
00240                 case 3:
00241                     reponse3 = reponse;   
00242                     BSP_LCD_SetTextColor(reponse3);
00243                     BSP_LCD_FillRect(250, 170, 70, 70);
00244                     HAL_Delay(1000);
00245                     break;
00246                 case 4:   
00247                     reponse4 = reponse;   
00248                     BSP_LCD_SetTextColor(reponse4);
00249                     BSP_LCD_FillRect(350, 170, 70, 70);
00250                     HAL_Delay(1000);
00251                     sequence_reponse = 1;
00252                     j = 0;
00253                     break;
00254             }          
00255             wait_us(1000000);
00256         }
00257         sequence_reponse = 0;
00258         
00259         if(couleur1==reponse1 && couleur2==reponse2 && couleur3==reponse3 && couleur4==reponse4)
00260         {
00261             BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
00262             BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)"VOUS AVEZ GAGNE", CENTER_MODE);
00263         }
00264         else
00265         {
00266             BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
00267             BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)"VOUS AVEZ PERDU", CENTER_MODE);    
00268         }
00269         
00270         
00271         //Bouton rejouer la partie       
00272         Button reset(lcd_, ts_, 410, 5, 60, 40,
00273                      LCD_COLOR_BLUE, LCD_COLOR_BLACK, "Rejouer", Font12);
00274         reset.Draw(LCD_COLOR_WHITE, LCD_COLOR_BLACK);
00275         
00276         while(boutton_reset == 0)
00277         {    
00278             if(reset.Touched())
00279             {
00280                 boutton_reset = 1;
00281             }
00282         }
00283         boutton_reset = 0;
00284     }
00285 }