Code sensor IR

Dependencies:   BSP_DISCO_F746NG

Revision:
0:8c1b74ecac29
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jun 25 16:51:20 2020 +0000
@@ -0,0 +1,149 @@
+#include "mbed.h"
+#include "button.h"
+#include "view.h"
+#include "stm32746g_discovery_lcd.h"
+#include "stm32746g_discovery_ts.h"
+#include <list>
+#define SCREENWIDTH 480
+#define SCREENHEIGHT 272
+
+uint8_t text[30];
+AnalogIn capteur (A0);
+
+void setBackgroundColor(uint32_t color)
+{
+    BSP_LCD_Clear(color);
+}
+
+void setHalfColor(uint32_t color0, uint32_t color1)
+{
+    BSP_LCD_Clear(color0);
+
+    int x = SCREENWIDTH>>1;
+    while (x < SCREENWIDTH) {
+        int y = 0;
+        while (y < SCREENHEIGHT) {
+            BSP_LCD_DrawPixel(x, y, color1);
+            y++;
+        }
+        x++;
+    }
+}
+
+void updateButton()
+{
+
+
+}
+void drawButton(uint32_t color0, int posX, int posY, int width, int height)
+{
+}
+
+
+
+int main()
+{
+    TS_StateTypeDef TS_State;
+    uint16_t x, y;
+    uint8_t status;
+    uint8_t idx;
+    uint8_t cleared = 0;
+    uint8_t prev_nb_touches = 0;
+    int etat = 0;
+    int i = 0;
+
+    BSP_LCD_Init();
+    BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
+    BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
+
+    status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
+    if (status != TS_OK) {
+        BSP_LCD_Clear(LCD_COLOR_RED);
+        BSP_LCD_SetBackColor(LCD_COLOR_RED);
+        BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+        BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
+        while (1);
+    } else {
+        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 OK", CENTER_MODE);
+    }
+
+    HAL_Delay(100);
+    BSP_LCD_SetFont(&Font12);
+    BSP_LCD_SetBackColor(LCD_COLOR_LIGHTCYAN);
+    BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE);
+    View v;
+    v.i=&i;
+
+    Timer timer;
+    timer.start();
+
+    while(1) {
+        v.drawImage(0, 0);
+        float valeur = capteur.read();
+        BSP_LCD_SetFont(&Font24);
+        //sprintf((char*)text, "valeur: %5.3f", valeur);
+        //BSP_LCD_DisplayStringAt(0, LINE(1), (uint8_t *)&text, LEFT_MODE);
+
+      
+        sprintf((char*)text, "Nombre de personne: %d", i);
+        BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)&text, LEFT_MODE);
+
+
+        //sprintf((char*)text, "etat: %d", etat);
+        //BSP_LCD_DisplayStringAt(0, LINE(2), (uint8_t *)&text, LEFT_MODE);
+        switch (etat) {
+            case 0 :
+                // il n'y a personne
+                if (valeur > 0.68) {
+                    etat = 1;
+                    i++;
+                    timer.reset();
+                }
+                break;
+            case 1:
+                if (timer.read()>1) {
+                    etat=2;
+                }
+                break;
+            case 2 :
+                // il y a quelqu'un
+                if (valeur <0.4) {
+                    // la personne est passée : il faut incrémenter
+                    etat = 0;
+                    timer.reset();
+                }
+                break;
+
+            default :
+                // ce cas ne devrait jamais se produire
+                etat = 0;
+        } // fin du switch
+
+
+
+
+        BSP_TS_GetState(&TS_State);
+        if (TS_State.touchDetected) {
+            cleared = 0;
+            /*sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
+            BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
+            */for (idx = 0; idx < TS_State.touchDetected; idx++) {
+                x = TS_State.touchX[idx];
+                y = TS_State.touchY[idx];
+                v.contain(x, y);
+            }
+
+
+        } else {
+            if (!cleared) {
+                v.draw();
+                /*sprintf((char*)text, "Touches: 0");
+                BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
+                */cleared = 1;
+            }
+        }
+    }
+}