Vadim Ielkin / Mbed 2 deprecated Projet_accéléromètre

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG mbed

main.cpp

Committer:
Tsarvadim
Date:
2018-06-12
Revision:
1:f034e131e408
Parent:
0:19846e16566e
Child:
2:3bb535973904

File content as of revision 1:f034e131e408:

#include "mbed.h"
#include "TS_DISCO_F746NG.h"
#include "LCD_DISCO_F746NG.h"
#include <time.h>   // pour rand
#include <stdio.h>
#include <stdlib.h>

LCD_DISCO_F746NG lcd;
TS_DISCO_F746NG ts;

I2C i2c(PB_9,PB_8);

TS_StateTypeDef TS_State;

//adress de l'accelerometre
int addr = 0x14;

//Tableau contenant les commandes
char cmd[2];
char resultat[2];


//Tableau contenant les données du capteur
char tmp[40];

//Position du pion 
int position_pion_x = 0;
int position_pion_y = 30;

//Variable contenant le score
int score = 0;

//Variable de la position du checkpoint
int position_checkpoint_x = 250;
int position_checkpoint_y = 125;


//Variable choix utilisateur menu
bool choix_jeu = false;
bool choix_info = false;

//Prototype des méthode
void jeu();
void information();

int main()
{
    //Variable utilisé pour stocker les données de l'écran tactiles
    uint16_t x, y;
    uint8_t status;
    
    lcd.SetBackColor(0);
    //Initialisation
    
    lcd.SetTextColor(LCD_COLOR_WHITE);
    lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"Bienvenue sur AngleTarget", CENTER_MODE);
    wait(2);

    lcd.SetTextColor(LCD_COLOR_BLACK);
    status = ts.Init(lcd.GetXSize(), lcd.GetYSize());
    if (status != TS_OK) {
        lcd.Clear(LCD_COLOR_RED);
        lcd.SetTextColor(LCD_COLOR_WHITE);
        lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Initialisation fail ", CENTER_MODE);
    } else {
        lcd.Clear(LCD_COLOR_WHITE);
    }
    
    lcd.SetBackColor(LCD_COLOR_WHITE);
    //Affichage du menu
    lcd.SetFont(&Font16);
    lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"Veuillez choisir une option", CENTER_MODE);
    
    //"Bouton" jeu
    lcd.SetTextColor(LCD_COLOR_BLACK);
    lcd.FillRect(250, 150, 150, 100);
    lcd.SetBackColor(LCD_COLOR_BLACK);
    lcd.SetTextColor(LCD_COLOR_WHITE);
    lcd.SetFont(&Font16);
    lcd.DisplayStringAt(95,190, (uint8_t *)"Jeu", CENTER_MODE);
    
    //"Bouton" informations
    lcd.SetTextColor(LCD_COLOR_RED);
    lcd.FillRect(40,150,150,100);
    lcd.SetBackColor(LCD_COLOR_RED);
    lcd.SetTextColor(LCD_COLOR_WHITE);
    lcd.SetFont(&Font16);
    lcd.DisplayStringAt(85,190, (uint8_t *)"Info", LEFT_MODE);
    
    lcd.SetBackColor(LCD_COLOR_WHITE);
    while(1) 
    {
        //Recuperation des informations tactiles
        ts.GetState(&TS_State);
        if (TS_State.touchDetected) 
        {
            x = TS_State.touchX[0];
            y = TS_State.touchY[0];
            
            //Si on appuie sur la case jeu on lance le jeu
            if((x >=250 && y>=150) && (x<400 && y<250))
                choix_jeu = true;
                
            //Si on appuie sur la case information on lance la methode information
            if((x >=50 && y>=150) && (x<200&& y<250))
                choix_info = true;
        }
        
        
        //Si on appuie sur la case jeu on lance le jeu
        if(choix_jeu == true)
        {
            jeu();
        }
        
        //Si on appuie sur la case information on lance la methode information
        if(choix_info == true)
        {
            information();
            choix_info = false;
            choix_jeu = true;
        }
        
    }
}

//Methode information
void information()
{
    uint16_t x, y;
    bool choix_retour = false;
    lcd.Clear(LCD_COLOR_WHITE);
    lcd.SetTextColor(LCD_COLOR_BLACK);
    
    //Affichage des informations
    lcd.SetFont(&Font12);
    lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"Bienvenue dans la rubrique information sur AngleTarget", CENTER_MODE);
    lcd.SetFont(&Font12);
    lcd.DisplayStringAt(0, LINE(3), (uint8_t *)"Developpe par Kenny Guillouche, Nathan Delafosse et Vadim Ielkin", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(4), (uint8_t *)"Pour jouer, rendez vous dans le menu principal et clique sur jouer", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Les regles sont simples. vous devez placer votre pion sur une forme generer", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"Un jeu MANGame", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(3), (uint8_t *)"Developpe par Kenny Guillouche, Nathan Delafosse et Vadim Ielkin", CENTER_MODE);
    
    //Bouton jeu
    lcd.SetTextColor(LCD_COLOR_BLACK);
    lcd.FillRect(165, 150, 150, 100);
    lcd.SetBackColor(LCD_COLOR_BLACK);
    lcd.SetTextColor(LCD_COLOR_WHITE);
    lcd.SetFont(&Font16);
    lcd.DisplayStringAt(0,190, (uint8_t *)"JEU", CENTER_MODE);
    do
    {
        ts.GetState(&TS_State);
        if (TS_State.touchDetected) 
        {
            x = TS_State.touchX[0];
            y = TS_State.touchY[0];
            if((x >=165 && y>=150) && (x<315 && y<250))
            {
                choix_retour = true;
                choix_info = false;
                jeu();
            }
        }
    }   
    while(choix_retour == false);
         
}

//Methode jeu
void jeu()
{
    uint16_t x, y;
    bool cheat;
    cheat = false;
    bool stopjeu = false;
    
    //Initialisation du jeu
    lcd.Clear(LCD_COLOR_WHITE);
    lcd.SetTextColor(LCD_COLOR_BLACK);
    lcd.FillRect(position_pion_x, position_pion_y, 30, 30);
    
    //Generation du score
    sprintf(tmp,"Score : %d",score);
    lcd.DisplayStringAt(0, LINE(1),(uint8_t *)tmp,CENTER_MODE);
    
    //Generation du premier checkpoint
    lcd.SetTextColor(LCD_COLOR_RED);
    lcd.FillRect(position_checkpoint_x, position_checkpoint_y, 10, 10); 
    
    srand(time(NULL));
    
    while(stopjeu == false) 
    {
        //Recuperation de la coordonnée en X
        resultat[0] = 0;
        cmd[0] = 0x04;        //Command read
        i2c.write(addr, cmd, 1);
        i2c.read(addr, resultat, 1);
        
        //Si accelerometre penché vers la droite
        if((int)resultat[0]<52 && (int)resultat[0]>8)
        {
            //On modifie notre postion du pion vers la droite
            if(position_pion_x<450)     //Limite ecran
            {
                lcd.SetTextColor(LCD_COLOR_WHITE);
                lcd.FillRect(position_pion_x, position_pion_y, 30, 30);
                position_pion_x += 1;
                lcd.SetTextColor(LCD_COLOR_BLACK);
                lcd.FillRect(position_pion_x, position_pion_y, 30, 30);
            } 
        }
        
       //Si accelerometre penché vers la hauche
        else if((int)resultat[0]> 200 && (int)resultat[0]<250)
        {
            //On modifie notre postion du pion vers la gauche
            if(position_pion_x>0)       //Limite ecran
            {
                lcd.SetTextColor(LCD_COLOR_WHITE);
                lcd.FillRect(position_pion_x, position_pion_y, 30, 30);
                position_pion_x -= 1;
                lcd.SetTextColor(LCD_COLOR_BLACK);
                lcd.FillRect(position_pion_x, position_pion_y, 30, 30); 
            }
        }
        
        //Recuperation de la coordonnée en Y
        resultat[0] = 0;
        cmd[0] = 0x06;        //Command read
        i2c.write(addr, cmd, 1);
        i2c.read(addr, resultat, 1);
        
        //Si accelerometre penché vers le haut
        if((int)resultat[0]<52 && (int)resultat[0]>8)
        {
            //On modifie notre postion du pion vers le haut
            if(position_pion_y>30)      //Limite ecran
            {
                lcd.SetTextColor(LCD_COLOR_WHITE);
                lcd.FillRect(position_pion_x, position_pion_y, 30, 30);
                position_pion_y -= 1;
                lcd.SetTextColor(LCD_COLOR_BLACK);
                lcd.FillRect(position_pion_x, position_pion_y, 30, 30);
            } 
        }
        
        //Si accelerometre penché vers le bas 
        else if((int)resultat[0]<248 && (int)resultat[0]>200)
        {
            //On modifie notre postion du pion vers le bas
            if(position_pion_y<242)     //Limite ecran
            {
                lcd.SetTextColor(LCD_COLOR_WHITE);
                lcd.FillRect(position_pion_x, position_pion_y, 30, 30);
                position_pion_y += 1;
                lcd.SetTextColor(LCD_COLOR_BLACK);
                lcd.FillRect(position_pion_x, position_pion_y, 30, 30);
            } 
        }
        
        //colission pion/checkpoint
        if((position_pion_x + 30 >position_checkpoint_x) && (position_pion_x < position_checkpoint_x + 10) && (position_pion_y + 30 >position_checkpoint_y) && (position_pion_y  < position_checkpoint_y +10))
        {
            //On efface
            lcd.Clear(LCD_COLOR_WHITE);
            lcd.SetTextColor(LCD_COLOR_BLACK);
            lcd.FillRect(position_pion_x, position_pion_y, 30, 30);
            
            //Actualisation du score
            score++;
            sprintf(tmp,"Score : %d",score);
            lcd.DisplayStringAt(0, LINE(1),(uint8_t *)tmp,CENTER_MODE);

            //Position aléatoire du prochain checkpoint
            position_checkpoint_x = (rand()%450) + 10; //generation nombre aleatoire entre 10 et 460
            position_checkpoint_y = (rand()%215) + 35 ; //generation nombre aleatoire entre 35 et 250
            
            //Affichage nouveau checkpoint
            lcd.SetTextColor(LCD_COLOR_RED);
            lcd.FillRect(position_checkpoint_x, position_checkpoint_y, 10, 10);
        }
        
        //Si il a reussit à faire +20 de score
        if(score>20)
        {
             //On efface
            lcd.Clear(LCD_COLOR_WHITE);
            lcd.SetFont(&Font24);
            
            //On affiche victoire
            lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Victoire", CENTER_MODE);
            wait(5);
            
            //Reinitialisation
            stopjeu =true;
            score = 0;
            position_pion_x = 0;
            position_pion_y = 30;
            lcd.SetFont(&Font16);
        }
        
        //Recuperation des informations tactiles
        ts.GetState(&TS_State);
        if (TS_State.touchDetected) 
        {
             x = TS_State.touchX[0];
             y = TS_State.touchY[0];
         }
        
        //si on appuie sur la bonne position 1
        if((x >=250 && y>=150) && (x<275 && y<175))
        {
            cheat = true;
        }
        
        //si on appuie sur la bonne position 2 apres la 1
        if((x >=150 && y>=250) && (x<175 && y<275) && (cheat == true))
        {
            //On efface
            lcd.Clear(LCD_COLOR_WHITE);
            lcd.SetFont(&Font24);
            
            //On affiche victoire
            lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Victoire", CENTER_MODE);
            wait(5);
            
            //Reinitialisation
            stopjeu =true;
            score = 0;
            position_pion_x = 0;
            position_pion_y = 30;
            lcd.SetFont(&Font16);
        }
        wait(0.005);
    }
}