Projet d'interfaçage avec le joystick réalisé par Ronan CHERIAUX

Dependencies:   BSP_DISCO_F746NG

Revision:
0:1c8761215497
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jun 26 09:17:35 2020 +0000
@@ -0,0 +1,351 @@
+#include "mbed.h"
+#include "stm32746g_discovery_lcd.h"
+#include "stm32746g_discovery_ts.h"
+#include "fleche_g.h"
+#include "fleche_d.h"
+#include "fleche_b.h"
+#include "fleche_h.h"
+#include "modf_code.h"
+#include "retour.h"
+#include "drawBitmap.h"
+
+//On assigne les deux ports analogiques du joystick en sorti
+AnalogIn inX(A4);
+AnalogIn inY(A5);
+int main()
+{
+//Variable d'initialisation
+    TS_StateTypeDef TS_State;
+    uint16_t x, y;
+    uint8_t text[30];
+    uint8_t status;
+    uint8_t idx;
+    uint8_t cleared = 0;
+    uint8_t prev_nb_touches = 0;
+//Valeur des 2 axes du joystick
+    float axeX;
+    float axeY;
+    
+//Code a faire pour deverouillez l'electro-aimant
+    char code_a = 2; //gauche 
+    char code_b = 4;//droite
+    char code_c = 2;//gauche
+    char code_d = 4;//droite
+    
+//Variable qui vont varié via la position du Joystick
+    char essai_a = 0;
+    char essai_b = 0;
+    char essai_c = 0;
+    char essai_d = 0;
+
+//Variables qui vont servir à bloquer des parties du programme    
+    char bloque = 0;
+    char bloque_bouton = 0;
+    char reset_mdp = 0;
+    char victoire = 0;
+
+//Variable utilisé pour connaitre le nombre de valeur deja selectionner pour le score
+    char nombre = 0;
+//Permet de positionner les images de fleche sur l'ecran
+    int col = 60;
+
+//Variable permettant de donner un resultat a chaque variable d'essais
+    char val = 0;
+
+    
+//Variable utilisé pour le retour en arriere
+    char val_a =0;
+    char val_b =0;
+    char val_c =0;
+    char val_d =0;
+    char i;
+    
+    BSP_LCD_Init();
+    BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
+    BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
+    BSP_LCD_Clear(LCD_COLOR_WHITE);
+    BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO " , CENTER_MODE);
+    HAL_Delay(1000);
+//Initialisation de l'ecran
+    status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
+    if (status != TS_OK) {
+        BSP_LCD_SetFont(&Font24);
+        BSP_LCD_Clear(LCD_COLOR_GREEN);
+        BSP_LCD_SetBackColor(LCD_COLOR_GREEN);
+        BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+        BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
+    }  
+    BSP_LCD_Clear(LCD_COLOR_WHITE);
+    
+    
+    while(1){
+        BSP_TS_GetState(&TS_State);
+        //Cette partie represente les boutons ainsi que l'utilisation du joystick, ils ne sont pas utilisable en cas de victoire
+        if(victoire == 0){
+            BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+            BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
+            BSP_LCD_DisplayStringAt(0, LINE(1), (uint8_t *)"Chercher le code ", CENTER_MODE);
+            //Creation d'un bouton tactile
+            drawBitmap(0,0,(const uint8_t *)modf_code);
+            uint16_t x = TS_State.touchX[0];
+            uint16_t y = TS_State.touchY[0];
+            //Cette partie est pour modifier le mot de passe
+            if (TS_State.touchDetected) {
+                if(((x > 0) && (x < 100)) &&((y > 0) && (y<50))){
+                        BSP_LCD_Clear(LCD_COLOR_WHITE);
+                        BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+                        BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
+                        BSP_LCD_DisplayStringAt(0, LINE(1), (uint8_t *)"Modification mot de passe ", CENTER_MODE);
+                        reset_mdp = 1;
+                        code_a = 0;
+                        code_b = 0;
+                        code_c = 0;
+                        code_d = 0;
+                        col = 60;
+                        nombre = 0;
+                        
+                        while(reset_mdp ==1){
+                            //On recupere la valeur des 2 axes du joystick
+                            axeX = inX.read();
+                            axeY = inY.read();
+                            if( axeX == 1.0){
+                                if (bloque == 0 ){
+                                    drawBitmap(col,100,(const uint8_t *)fleche_h);
+                                    val = 1;
+                                    bloque = 1;
+                                    nombre = nombre + 1;
+                                    col = col + 90;
+                                    wait_us(200000);
+                                }
+                            }
+                            if( axeY <= 0.40){
+                                if (bloque == 0 ){
+                                    drawBitmap(col,100,(const uint8_t *)fleche_g);
+                                    val = 2;
+                                    bloque = 1;
+                                    nombre = nombre + 1;
+                                    col = col + 90;
+                                    wait_us(200000);
+                                }
+                            }
+                            if( axeX <= 0.40){
+                                if (bloque == 0 ){
+                                    drawBitmap(col,100,(const uint8_t *)fleche_b);
+                                    val = 3;
+                                    bloque = 1;
+                                    nombre = nombre + 1;
+                                    col = col + 90;
+                                    wait_us(200000);
+                                }
+                            }
+                            if( axeY == 1.0){
+                                if (bloque == 0 ){
+                                    drawBitmap(col,100,(const uint8_t *)fleche_d);
+                                    val = 4;
+                                    bloque = 1;
+                                    nombre = nombre + 1;
+                                    col = col + 90;
+                                    wait_us(200000);
+                                }
+                            }
+                            if (bloque == 1){
+                                switch(nombre) {
+                                    case 1:
+                                        code_a = val;
+                                        bloque = 0;
+                                        break;
+                                    case 2:
+                                        code_b = val;
+                                        bloque = 0;
+                                        break;
+                                    case 3:
+                                        code_c = val;
+                                        bloque = 0;
+                                        break;
+                                    case 4:
+                                        code_d = val;
+                                        nombre = 0;
+                                        reset_mdp = 0;
+                                        col = 60;
+                                        bloque = 0;
+                                        BSP_LCD_Clear(LCD_COLOR_WHITE);
+                                        break;
+
+                                    }
+                                }
+                            }
+                        }
+                    }
+            //Bouton pour supprimer le dernier resultat
+            if(nombre > 0){
+                drawBitmap(380,0,(const uint8_t *)retour);
+                uint16_t x = TS_State.touchX[0];
+                uint16_t y = TS_State.touchY[0];
+                if (TS_State.touchDetected) {
+                    if(((x > 380) && (x < 480)) &&((y > 0) && (y<100))){
+                        col = 60;
+                        //On réaffiche les anciens résultats grace val_a, val_b
+                        for( i =0; i< nombre ; i++){
+                            switch(i) {
+                                case 0:
+                            
+                                    val = 0;
+                                    break;
+                                case 1:
+                                
+                                    essai_a = val_a;
+                                    val = val_a;
+                                    break;
+                                case 2:
+                                
+                                    essai_b = val_b;
+                                    val = val_b;
+                                    break;
+                                default :
+                                    wait_us(5000);
+                            }    
+                            switch(val){
+                                case 0:
+                                    BSP_LCD_Clear(LCD_COLOR_WHITE);
+                                    break;
+                                case 1:
+                                    drawBitmap(col,100,(const uint8_t *)fleche_h);
+                                    col = col +90;
+                                    break;
+                                case 2:
+                                    drawBitmap(col,100,(const uint8_t *)fleche_g);
+                                    col = col +90;
+                                    break;
+                                case 3:
+                                    drawBitmap(col,100,(const uint8_t *)fleche_b);
+                                    col = col +90;
+                                    break;
+                                case 4:
+                                    drawBitmap(col,100,(const uint8_t *)fleche_d);
+                                    col = col +90;
+                                    break;
+                                default :
+                                    wait_us(5000);
+                                }
+                            }
+                            nombre = nombre - 1;
+                            wait_us(200000);
+                        }
+                    }
+                }
+            }
+
+        wait_us(200000);
+        axeX = inX.read();
+        axeY = inY.read();
+        //Utilisation du joystick pour tenter de trouver le bon mot de passe
+        //Si l'on fait varier la position du joystick selon la position x ou y alors les valeurs vont varier d'environ 0.30 jusqu'à 1
+        if(bloque_bouton == 0){
+            if( axeX == 1.0){
+                if (bloque == 0 ){
+                    drawBitmap(col,100,(const uint8_t *)fleche_h);
+                    val = 1;
+                    bloque = 1;
+                    bloque_bouton = 1;
+                    nombre = nombre + 1;
+                    col = col + 90;
+                }
+            }
+            if( axeY <= 0.40){
+                if (bloque == 0 ){
+                    drawBitmap(col,100,(const uint8_t *)fleche_g);
+                    val = 2;
+                    bloque = 1;
+                    bloque_bouton = 1;
+                    nombre = nombre + 1;
+                    col = col + 90;
+                }
+            }
+            if( axeX <= 0.40){
+                if (bloque == 0 ){
+                    drawBitmap(col,100,(const uint8_t *)fleche_b);
+                    val = 3;
+                    bloque = 1;
+                    bloque_bouton = 1;
+                    nombre = nombre + 1;
+                    col = col + 90;
+                }
+            }
+            if( axeY == 1.0){
+                if (bloque == 0 ){
+                    drawBitmap(col,100,(const uint8_t *)fleche_d);
+                    val = 4;
+                    bloque = 1;
+                    bloque_bouton = 1;
+                    nombre = nombre + 1;
+                    col = col + 90;
+                }
+            }
+        }
+        if (bloque == 1){
+            switch(nombre) {
+                case 0:
+                    BSP_LCD_Clear(LCD_COLOR_WHITE);
+                    break;
+                case 1:
+                
+                    essai_a = val;
+                    val_a = val;
+                    bloque = 0;
+                    bloque_bouton = 0;
+                    break;
+                case 2:
+                
+                    essai_b = val;
+                    val_b = val;
+                    bloque = 0;
+                    bloque_bouton = 0;
+                    break;
+                case 3:
+                
+                    essai_c = val;
+                    val_c = val;
+                    bloque = 0;
+                    bloque_bouton = 0;
+                    break;
+                    
+                case 4:
+                    
+                    essai_d = val;
+                    break;
+                    
+            }
+            //Une fois positions mise on verifie si le code correspond au mot de passe
+            if(essai_d != 0){
+                //Si le code est bon on affiche un ecran vert avec ecrit victoire et on fait varier la variable "victoire" pour desactiver l'utilisation des bouton ainsi que du joystick
+                if(essai_a == code_a && essai_b == code_b && essai_c == code_c && essai_d == code_d){
+                    if ( victoire == 0){
+                        BSP_LCD_Clear(LCD_COLOR_GREEN);
+                        BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+                        BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
+                        BSP_LCD_DisplayStringAt(0, LINE(2), (uint8_t *)"VICTOIRE", CENTER_MODE);
+                    }
+                    victoire = 1;
+                }
+                //Si il ne s'agit pas du mot de passe un ecran rouge apparait avec ecrit "FAUX" et le joueur est invité à reessayer
+                else{
+                    wait_us(200000);
+                    BSP_LCD_Clear(LCD_COLOR_RED);
+                    BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+                    BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
+                    BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"FAUX ", CENTER_MODE);
+                    wait_us(500000);
+                    essai_a = 0;
+                    essai_b = 0;
+                    essai_c = 0;
+                    essai_d = 0;
+                    nombre = 0;
+                    col = 60;
+                    BSP_LCD_Clear(LCD_COLOR_WHITE);
+                    bloque = 0;
+                    bloque_bouton = 0;
+                }
+            }
+        }
+    }
+}   
\ No newline at end of file