programme de tournesol photovoltaique pour le cours d'interface

Dependencies:   BSP_DISCO_F746NG

main.cpp

Committer:
legrandpaul
Date:
2021-06-21
Revision:
0:6d4f8bd2994a

File content as of revision 0:6d4f8bd2994a:

#include "mbed.h"
#include "stm32746g_discovery_lcd.h"
#include "stm32746g_discovery_ts.h"
#include "SolarPannel.h"
#include "pres.h"


#define K 3.2

struct bouton{
  
  uint16_t  Xpos;
  uint16_t  Ypos;
  uint16_t  Xtaille;
  uint16_t  Ytaille;
  uint16_t  couleur;
};

void drawImage_image(int offsetX, int offsetY);
bouton init_bouton (bouton x, uint16_t  Xpos, uint16_t  Ypos, uint16_t  Xtaille, uint16_t  Ytaille, uint16_t  couleur);
void draw_bouton (bouton x, uint8_t *texte);
bool getbouton (bouton x);

TS_StateTypeDef TS_State;
SOLAR_PANNEL sl;

int main()
{
    struct bouton bp_gauche;
    struct bouton bp_droite;
    struct bouton bp_mode;
    
    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;
    bool boutton_test;
    
    uint8_t texte_D[] = ">";
    uint8_t texte_G[] = "<";
    uint8_t texte_manuel[] = "manuel";
    uint8_t texte_auto[] = "auto";
    
    int state = 0;

    BSP_LCD_Init();
    BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
    BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);

    BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE);
    HAL_Delay(1000);

    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);
    } 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(1000);
    /*BSP_LCD_SetFont(&Font12);
    BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
    BSP_LCD_SetTextColor(LCD_COLOR_WHITE);*/
    
    drawImage_image(0, 0);
    
    bp_droite = init_bouton (bp_droite, 425, 100, 30, 50, LCD_COLOR_WHITE);
    bp_gauche = init_bouton (bp_gauche, 25, 100, 30, 50, LCD_COLOR_WHITE);
    bp_mode = init_bouton (bp_mode, 205, 225, 80, 40, LCD_COLOR_WHITE);

    while(1) {
          
        switch (state){
          case 0 :
          
            draw_bouton(bp_gauche, texte_G);
            draw_bouton(bp_droite, texte_D);
            draw_bouton(bp_mode, texte_manuel);
          
            if (getbouton(bp_gauche))sl.rotate(LEFT, 0.3);
            else if (getbouton(bp_droite))sl.rotate(RIGHT, 0.3);  
            else sl.stop();
            
            if (getbouton(bp_mode))state = 1;
          
          break;
          
          case 1:
            if (getbouton(bp_mode) == 0) state = 2;
          break;
          
          case 2:
            drawImage_image(0, 0);
            draw_bouton(bp_mode, texte_auto);
            state = 3;
          break;
          
          case 3:
          
            if (sl.getDelta() >  0 && sl.getDelta() < 1)sl.rotate(RIGHT, (sl.getDelta()*K));
            else if (sl.getDelta() < 0 && sl.getDelta() > -1)sl.rotate(LEFT, (sl.getDelta() * (-K)));
            else sl.stop();
          
            if (getbouton(bp_mode)){
                state = 4;
                sl.stop();
            }
          
          break;
          
          case 4:
            if (getbouton(bp_mode) == 0){
                state = 0;
            } 
          break;
        }
    }
    
}

void draw_bouton (bouton x, uint8_t *texte){
    
    BSP_LCD_DrawRect( x.Xpos,  x.Ypos, x.Xtaille, x.Ytaille);
    BSP_LCD_FillRect( x.Xpos,  x.Ypos, x.Xtaille, x.Ytaille);
    
    BSP_LCD_SetFont(&Font12);
    BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
    BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
    BSP_LCD_DisplayStringAt(x.Xpos + (x.Xtaille/2) - (sizeof(texte)-1) ,  x.Ypos + (x.Ytaille/2), (uint8_t *)texte, LEFT_MODE);
}

bool getbouton (bouton x){
    
    BSP_TS_GetState(&TS_State);
      
    if (TS_State.touchDetected && TS_State.touchX[0] < (x.Xpos + x.Xtaille) && TS_State.touchX[0] > x.Xpos && TS_State.touchY[0] < (x.Ypos + x.Ytaille) && TS_State.touchY[0] > x.Ypos){
        return 1;          
    }
    else  return 0;
    
}

bouton init_bouton (bouton x, uint16_t  Xpos, uint16_t  Ypos, uint16_t  Xtaille, uint16_t  Ytaille, uint16_t  couleur){
    x.Xpos = Xpos;
    x.Ypos = Ypos;
    x.Xtaille = Xtaille;
    x.Ytaille = Ytaille;
    x.couleur = couleur;
    
    return x;
}

void drawImage_image(int offsetX, int offsetY){
    int x = 0;
    int y = 0;
    uint32_t* dataPtr = (uint32_t*)pres.data;
    while(y < pres.height) {
        while(x < pres.width) {
            BSP_LCD_DrawPixel(x + offsetX, y + offsetY, *dataPtr);
            dataPtr++;
            x++;
        }
        x = 0;
        y++;
    }
}