USSRII Mathieu DUCROCQ

Dependencies:   mbed BSP_DISCO_F746NG

Revision:
0:11a51c9ebb09
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jun 15 10:10:18 2021 +0000
@@ -0,0 +1,162 @@
+#include "mbed.h"
+#include "MPU6050.h"
+#include "stm32746g_discovery_lcd.h"
+#include "stm32746g_discovery_ts.h"
+#include <stdlib.h>
+#include <time.h> 
+
+mbed::I2C    i2c(I2C_SDA, I2C_SCL);
+MPU6050 mpu(i2c);
+Serial pc(USBTX, USBRX);
+
+int main()
+{
+    int timer=0;
+    int difficulte=50;
+    int16_t gx, gy, gz;
+    char pouaint[9];
+    int points=0;
+    int randome=0;
+    bool isAlive=true;
+    mpu.initialize();
+    bool mpu6050TestResult = mpu.testConnection();
+    if(mpu6050TestResult) {
+        pc.printf("MPU6050 test passed \r\n");
+    } else {
+        pc.printf("MPU6050 test failed \r\n");
+    }
+    BSP_LCD_Init();
+    BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
+    BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
+    BSP_LCD_Clear(LCD_COLOR_BLACK);
+    BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
+    TS_StateTypeDef TS_State;
+    BSP_TS_GetState(&TS_State);
+    while(!TS_State.touchDetected) 
+    {
+        BSP_TS_GetState(&TS_State);
+        BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
+        BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
+        BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
+        BSP_LCD_DisplayStringAt(0, 1, (uint8_t *)"Bienvenue dans", CENTER_MODE);
+        
+        BSP_LCD_SetTextColor(LCD_COLOR_MAGENTA);
+        BSP_LCD_DisplayStringAt(0, 60, (uint8_t *)"ULTRA SPACE SHOOTER", CENTER_MODE);
+        BSP_LCD_DisplayStringAt(0, 90, (uint8_t *)"REDEMPTION II", CENTER_MODE);
+
+        BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
+        BSP_LCD_DisplayStringAt(0, 150, (uint8_t *)"Touchez pour commencer", CENTER_MODE);
+        BSP_LCD_FillRect(10, 200, 50, 50);
+        BSP_LCD_DrawHLine(70, 225, 20);
+        
+        BSP_LCD_SetTextColor(LCD_COLOR_RED);
+        BSP_LCD_FillRect(400,200,50,50);
+        BSP_LCD_DrawHLine(370, 225, 20);
+    }
+    BSP_LCD_Clear(LCD_COLOR_BLACK);
+    int16_t posXV, posXE, posYV, posYE, posXTirV, posXTirE, posYTirV, posYTirE; 
+    bool canVShoot=true;
+    bool canEShoot=true;
+    posXV=10;
+    posYV=BSP_LCD_GetYSize()/2;
+    posXE=BSP_LCD_GetXSize()-60;
+    posYE=BSP_LCD_GetYSize()/2;
+    while(isAlive)
+    {
+        timer++;
+        if (timer>100){
+            timer=0;
+            difficulte-=1;
+            }
+        points++;
+        sprintf((char*)pouaint,"%d pts",points);
+        BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+        BSP_LCD_DisplayStringAt(0, 1, (uint8_t *)&pouaint, CENTER_MODE);
+        mpu.getRotation(&gx, &gy, &gz);
+        TS_State.touchX[1]=12;
+        HAL_Delay(difficulte);
+        BSP_LCD_Clear(LCD_COLOR_BLACK);
+        BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
+        BSP_LCD_FillRect(posXV, posYV, 40, 40);
+        BSP_LCD_SetTextColor(LCD_COLOR_RED);
+        BSP_LCD_FillRect(posXE, posYE, 40, 40);
+        BSP_TS_GetState(&TS_State);
+        srand (time(NULL));
+        randome = rand() % 2 + 1;
+        if(randome==1)
+            {
+                posYE-=5;
+                if(posYE<0) posYE=BSP_LCD_GetYSize();
+            }
+            else
+            {
+                posYE+=5;
+                if(posYE>BSP_LCD_GetYSize()) posYE=0;
+            }
+        if(gy<-2000)
+            {
+                posYV-=5;
+                if(posYV<0) posYV=BSP_LCD_GetYSize();
+            }
+            else if(gy>2000)
+            {
+                posYV+=5;
+                if(posYV>BSP_LCD_GetYSize()) posYV=0;
+            }
+        if(TS_State.touchDetected)
+        {
+            if(canVShoot)
+            {
+                    posXTirV=posXV+10;
+                    posYTirV=posYV+10;
+                    canVShoot=false;
+                }
+        }
+        if(!canVShoot)
+        {
+            if(posXTirV>BSP_LCD_GetXSize())
+            {
+                posXTirV=0;
+                canVShoot=true;
+            }
+            BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
+            BSP_LCD_DrawHLine(posXTirV, posYTirV, 20);
+            posXTirV+=20;
+        }
+        if(canEShoot)
+        {
+            posXTirE=posXE;
+            posYTirE=posYE+10;
+            canEShoot=false;
+            }
+        else
+        {
+            BSP_LCD_SetTextColor(LCD_COLOR_RED);
+            BSP_LCD_DrawHLine(posXTirE, posYTirE, 20);
+            posXTirE-=20;
+            if(posXTirE<0)
+            {
+                canEShoot=true;
+            }
+        }
+        
+        if((posXTirV>posXE-20)&&(posXTirV<posXE+20)&&(posYTirV>posYE)&&(posYTirV<posYE+40))
+        {
+            points+=10000;
+            posXTirV=0;
+            canVShoot=true;
+            }
+            
+        if((posXTirE>posXV-20)&&(posXTirE<posXV+20)&&(posYTirE>posYV)&&(posYTirE<posYV+40))
+        {
+            isAlive=false;
+            }
+    }
+    BSP_LCD_Clear(LCD_COLOR_MAGENTA);
+    while(true){
+        BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+        BSP_LCD_DisplayStringAt(0, 5, (uint8_t *)"FIN DE PARTIE", CENTER_MODE);
+        BSP_LCD_DisplayStringAt(0, 55, (uint8_t *)"NOMBRE DE POINTS :", CENTER_MODE);
+        BSP_LCD_DisplayStringAt(0, 115, (uint8_t *)&pouaint, CENTER_MODE);
+        }
+}