LAST PUBLISH MATRIX PROJECT

Dependencies:   mbed

main.cpp

Committer:
antoine_carpentier
Date:
2021-06-22
Revision:
1:7a7366246b30
Parent:
0:c78ec5cab98d
Child:
2:552c5891b8c5

File content as of revision 1:7a7366246b30:

////////////// INCLUSION DES LIBRAIRIES (L1-L18)//////////////
#include "mbed.h"
#include "LEDMatrix.h"
#include "smallFont.h"
#include "stm32746g_discovery_ts.h"
#include "stm32746g_discovery_lcd.h"
#include "fleche.h"
#include "dessin.h"
#include "drapeau.h"
#include "croixbmp.h"
#include "gommebmp.h"
#include "allemagnebmp.h"
#include "belgiquebmp.h"
#include "beninbmp.h"
#include "francebmp.h"
#include "lgbpbmp.h"
#include "italiebmp.h"
/////////////////FIN INCLUSION DES LIBRAIRIES/////////////////

//////DEFINITION LARGEUR ET LONGUEURE DE LECRAN LED (L20-L23)//////
#define WIDTH   64
#define HEIGHT  32
//////// FIN DEFINITION LARGEUR ET LONGUEURE DE LECRAN LED ////////

//stb c'est le latch
// LEDMatrix    (a, b,  c, d,  oe_1, r1, r2, b1, b2, g1, g2, stb_1, clk_1);
LEDMatrix matrix(D1,D10,D0,D11,D7,   D5, D3, D4, D2, D8, D9, D12,   D13);
Ticker scanner;

// Display Buffer
uint8_t displaybuf[2][WIDTH *HEIGHT] = {
    0x00
};


/////////DEFINITION DES PROTOTYPES (L36-L40)/////////
void page_accueil(void);
void page_dessin(void);
void page_drapeau(void);
/////////FIN DE DEFINITION DES PROTOTYPES////////////

/////////DEFINITION DES VARIABLES GLOBALES (L42-L46)/////////
int xb=0, yb=0; // La valeur de x et y correspondante a l'ecran LCD
uint8_t text[30]; // SPRINTF
int p=0;// Pour le multicolor
/////////FIN DE DEFINITION DES VARIABLES GLOBALES ////////////

////////////////DEFINITION DES FONCTIONS DE DESSIN D'IMAGE (L48-L237)/////////////////////
//fleche
void drawImage_fleche(int offsetX, int offsetY)
{
    int x = 0;
    int y = 0;
    uint32_t* dataPtr = (uint32_t*)fleche.data;
    while(y < fleche.height) {
        while(x < fleche.width) {
            BSP_LCD_DrawPixel(x + offsetX, y + offsetY, *dataPtr);
            dataPtr++;
            x++;
        }
        x = 0;
        y++;
    }
}

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

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

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

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

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

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

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


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

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


//dessin
void drawImage_dessin(int offsetX, int offsetY)
{
    int x = 0;
    int y = 0;
    uint32_t* dataPtr = (uint32_t*)dessin.data;
    while(y < dessin.height) {
        while(x < dessin.width) {
            BSP_LCD_DrawPixel(x + offsetX, y + offsetY, *dataPtr);
            dataPtr++;
            x++;
        }
        x = 0;
        y++;
    }
}
////////////////FIN DEFINITION DES FONCTIONS DE DESSIN D'IMAGE (L48-L237)/////////////////////


/// FONCTION DE SCAN DE L'ECRAN LED//////
void scan()
{
    matrix.scan();
}
///FIN FONCTION DE SCAN DE L'ECRAN LED//////

///////////////////////// DEBUT DU MAIN (L247-LXXX)////////////////////////////////////////
int main()
{
    TS_StateTypeDef TS_State; // Definition de l'objet qui servira pour la detection de clic sur la STM32
    
    
    //Definition des variables locales au main
    uint16_t x, y;
    uint8_t status;
    uint8_t idx;
    uint8_t prev_nb_touches = 0;
    int i,  c=1;
    int page = 0;
    int couleur=7;//1 rouge; 2 vert; 3 jaune; 4 bleu; 5 violet; 6 cyan; 7 blanc
    
    
    ////////////////INITIALISATION DE LECRAN DE LA STM32//////////////
    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());               // Recupération d'un status
    if (status != TS_OK) {                                                      // SI LE STATUS N'EST PAS OK L'INIT FAIL
        BSP_LCD_Clear(LCD_COLOR_WHITE);
        BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
        BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
        BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);   
    } 
    else {                                                                      // SINON "INITIALISATION S'AFFICHE
        BSP_LCD_Clear(LCD_COLOR_WHITE);
        BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
        BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
        BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"INITIALISATION", CENTER_MODE);        
    }

    HAL_Delay(500);                         // Delais en ms
    BSP_LCD_SetFont(&Font12);               // Police d'écriture
    BSP_LCD_SetBackColor(LCD_COLOR_WHITE);  // Fond blanc
    BSP_LCD_SetTextColor(LCD_COLOR_BLUE);   // Coleur du texte bleu
    
    initFonts(); 

    matrix.begin((uint8_t *)displaybuf, WIDTH, HEIGHT);     // Sauv en mémoire la taille de la matrice LED
    
    scanner.attach(scan, 0.0005);   //Gère l'affiche sur la matrice LED
                                    //Un scan trop lent (>0.0009) provoque un effet de scintillement
                                    //Un scan trop rapide (<0.0001) provoque un effet d'allumage des LEDs qui sont sur la même ligne
    
    matrix.clear();        
    matrix.drawRect(0,0,21,64,4);
    matrix.drawRect(21,0,43,64,7);
    matrix.drawRect(43,0,64,64,1);   
    matrix.swap();
    while (!matrix.synchro());
    wait(1);
    matrix.clear();
        
    for (i=200; i>-400; i-=4) 
        {
            matrix.clear();
            matrix.drawCharString(i,0,"MATRICE LED LP SESAM",c,fonts[0]);
            matrix.drawCharString(i,15,"CARPENTIER ANTOINE",c,fonts[0]);
            matrix.swap();
            wait(0.05);
            while (!matrix.synchro());
        }
        matrix.clear();
        
        BSP_LCD_Clear(LCD_COLOR_WHITE);
        
    while (1) {  
        
        BSP_TS_GetState(&TS_State);
        if (TS_State.touchDetected) 
        {
            // Clear lines corresponding to old touches coordinates
            if (TS_State.touchDetected < prev_nb_touches) 
            {
                for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) 
                {
                    BSP_LCD_ClearStringLine(idx);
                }
            }
            prev_nb_touches = TS_State.touchDetected;


           
            for (idx = 0; idx < TS_State.touchDetected; idx++) 
            {
                x = TS_State.touchX[idx];
                y = TS_State.touchY[idx];
                xb = x/7.5;
                yb = y/7.5;
               
            }
        }
    
        //VERS PAGE DESSIN
       if ((page == 0) && (x>= 10 && x<= 235) && (y>= 10 && y<= 235))
        {
            x=0;
            y=272;
            BSP_LCD_Clear(LCD_COLOR_WHITE);
            matrix.clear();
            matrix.swap();
            while (!matrix.synchro()); 
            page = 1;
            HAL_Delay(300);
            }
            
            
      // VERS DRAPEAU   
      if ((page==0)&&(x>=245 && x<=470)&&(y>= 10 && y<= 235))
      {
        x=0;
        y=0;
        BSP_LCD_Clear(LCD_COLOR_WHITE);
        matrix.clear();
        matrix.swap();
        while (!matrix.synchro()); 
        page = 2;
        HAL_Delay(100);
        }
            
        if (page == 0)
        {
             page_accueil();
            }
        
        else if (page == 2)
        {
            page_drapeau();
                           
               //HAUT GAUCHE
                if ((x>= 8 && x<= 236) && (y>= 29 && y<= 103))
                {
                    x = 0;
                    y = 0;
                    //ALLEMAGNE
                    matrix.clear();
                    matrix.drawRect(0,10,64,21,3);
                    matrix.drawRect(0,21,64,32,1); 
                    matrix.swap();
                    while (!matrix.synchro());
              
                    }
                   
                // HAUT DROITE  
                else if ((x>= 244 && x<= 472) && (y>= 29 && y<= 103))
                {
                     x = 0;
                     y = 0;
                    // BELGE
                    matrix.clear();        
                    matrix.drawRect(21,0,43,64,3);
                    matrix.drawRect(43,0,64,64,1);
                    matrix.swap();
                    while (!matrix.synchro());
                   
                     
                    }
                    
                // MILIEU GAUCHE 
                else if ((x>= 8 && x<= 236) && (y>= 110 && y<= 184))
                {
                    x = 0;
                    y = 0;
                    //BENIN
                    matrix.clear();        
                    matrix.drawRect(0,0,21,64,2);
                    matrix.drawRect(21,0,64,16,3);
                    matrix.drawRect(21,16,64,32,1);  
                    matrix.swap();
                    while (!matrix.synchro());
                    
                    }
        
                // MILIEU DROITE  
                else if ((x>= 244 && x<= 472) && (y>= 110 && y<= 184))
                {
                    x = 0;
                    y = 0;
                    //LGBT
                    matrix.clear();//1 rouge; 2 vert; 3 jaune; 4 bleu; 5 violet; 6 cyan; 7 blanc
                    matrix.drawRect(0,0,64,4,7);
                    matrix.drawRect(0,4,64,8,1);
                    matrix.drawRect(0,8,64,12,3);
                    matrix.drawRect(0,12,64,16,2);
                    matrix.drawRect(0,16,64,20,4);
                    matrix.drawRect(0,20,64,24,6);
                    matrix.drawRect(0,24,64,28,5);
                    matrix.drawRect(0,28,64,32,7);
                    matrix.swap();
                    while (!matrix.synchro());
                        
                    }
                    
                // BAS GAUCHE    
                else if ((x>= 8 && x<= 236) && (y>= 192 && y<= 266))
                {
                    x = 0;
                    y = 0;
                    //FRANCE
                    matrix.clear();        
                    matrix.drawRect(0,0,21,64,4);
                    matrix.drawRect(21,0,43,64,7);
                    matrix.drawRect(43,0,64,64,1);    
                    matrix.swap();
                    while (!matrix.synchro());
                    
                    }
                    
                    
                // BAS DROITE
                else if ((x>= 244 && x<= 472) && (y>= 192 && y<= 266))
                {
                     x = 0;
                     y = 0;
                     //ITALIE
                     matrix.clear();        
                     matrix.drawRect(0,0,21,64,2);
                     matrix.drawRect(21,0,43,64,7);
                     matrix.drawRect(43,0,64,64,1);  
                     matrix.swap();
                     while (!matrix.synchro());
                    }
                
                if ((page==2)&&(x>= 458 && x<= 480) && (y>= 0 && y<= 22))
                {
                 BSP_LCD_Clear(LCD_COLOR_WHITE);
                 matrix.clear();
                 matrix.swap();
                 while (!matrix.synchro()); 
                 x = 0;
                 y = 0;
                 page = 0;
                 
                    }     
            
            }
    
    
        else if(page == 1)
        {     
         page_dessin();
        
         matrix.swap();
         while (!matrix.synchro());   
        
            if (y<241)
            {
            
            
                if(couleur != 0)
                {
                    BSP_LCD_DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
                    matrix.drawPoint(xb,yb,couleur);
                    }
                else
                {
                    BSP_LCD_DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_WHITE);
                    BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
                    BSP_LCD_FillRect(x-7.5,y-7.5,22.5,22.5);
                    
                    matrix.drawPoint(xb,yb,couleur);
                    matrix.drawPoint(xb+1,yb+1,couleur);
                    matrix.drawPoint(xb-1,yb-1,couleur);
                    matrix.drawPoint(xb+1,yb-1,couleur);
                    matrix.drawPoint(xb-1,yb+1,couleur);
                    matrix.drawPoint(xb-1,yb,couleur);
                    matrix.drawPoint(xb,yb-1,couleur);
                    matrix.drawPoint(xb+1,yb,couleur);
                    matrix.drawPoint(xb,yb+1,couleur);
                    }
            
                matrix.swap();
                while (!matrix.synchro());
            }
            
          
        if (x<=43 && x>=2 && y<=270 && y>=242 )
        {
            p=0;
            couleur = 1;
            }
        
        if (x<=86 && x>=45 && y<=270 && y>=242 )
        {
            p=0;
            couleur = 2;
            }
            
        if (x<=129 && x>=88 && y<=270 && y>=242 )
        {
            p=0;
            couleur = 4;
            }
        
        if (x<=172 && x>=131 && y<=270 && y>=242 )
        {
            p=0;
            couleur = 5;
            }
        
        if (x<=215 && x>=174 && y<=270 && y>=242 )
        {
            p=0;
            couleur = 6;
            }
        
        if (x<=258 && x>=217 && y<=270 && y>=242 )
        {
            p=0;
            couleur = 3;
            }
            
        if (x<=301 && x>=260 && y<=270 && y>=242 )
        {
            p=0;
            couleur = 7;
            }
            
        if (x<=344 && x>=303 && y<=270 && y>=242 )
        {
            p=1;      
            }
    
        // GOMME
        if (x<=396 && x>=355 && y<=267 && y>=254 )
        {
            p=0;
            couleur = 0;
            }
            
        // EFFACE TOUT
        if (x<=438 && x>=410 && y<=267 && y>=254 )
        {
            x=0;
            y=0;
            BSP_LCD_Clear(LCD_COLOR_WHITE);
            matrix.clear();
            }
            
         if ((x>= 458 && x<= 480) && (y>= 240 && y<= 270))
             {
             BSP_LCD_Clear(LCD_COLOR_WHITE);
             matrix.clear();
             matrix.swap();
             while (!matrix.synchro()); 
             x = 0;
             y = 0;
             page = 0;
             }
             
        
        //MULTICOLOR   
        if (p==1)
        {
            
            if (x<68 && y<91)couleur=7;
            if (x>=68 && x<136 && y<91)couleur=6;
            if (x>=136 && x<204 && y<91)couleur=5;
            if (x>=204 && x<272 && y<91)couleur=3;
            if (x>=272 && x<340 && y<91)couleur=4;
            if (x>=340 && x<408 && y<91)couleur=2;
            if (x>=408 && x<480 && y<91)couleur=1;   
            
            if (x<68   && y<91)couleur=1;
            if (x>=68  && x<136 && y<181 && y>90)couleur=2;
            if (x>=136 && x<204 && y<181 && y>90)couleur=3;
            if (x>=204 && x<272 && y<181 && y>90)couleur=4;
            if (x>=272 && x<340 && y<181 && y>90)couleur=5;
            if (x>=340 && x<408 && y<181 && y>90)couleur=6;
            if (x>=408 && x<480 && y<181 && y>90)couleur=7;  
            
            if (x<68   && y<91)couleur=5;
            if (x>=68  && x<136 && y>180)couleur=7;
            if (x>=136 && x<204 && y>180)couleur=2;
            if (x>=204 && x<272 && y>180)couleur=4;
            if (x>=272 && x<340 && y>180)couleur=1;
            if (x>=340 && x<408 && y>180)couleur=3;
            if (x>=408 && x<480 && y>180)couleur=6;  
           
        }
    }//FIN IF PAGE == 1 
    
    
    
    
    }// FIN WHILE
    

}// FIN MAIN
///////////////////////// FIN DU MAIN (L247-L691)////////////////////////////////////////









//////////////////// FONCTIONS PAGES (L700-L773) ///////////////////////////////////////////////////////
void page_accueil()
{
    BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
    BSP_LCD_DrawRect(10, 10, 225, 225);   //Bouton dessin 
    BSP_LCD_DrawRect(245, 10, 225, 225);   //Bouton drapeau
    
    sprintf((char*)text, "DESSIN");
    BSP_LCD_DisplayStringAt(95, LINE(20), (uint8_t *)&text, LEFT_MODE);
    drawImage_dessin(15,15);
    
    sprintf((char*)text,"DRAPEAU");
    BSP_LCD_DisplayStringAt(120, LINE(20), (uint8_t *)&text, CENTER_MODE);    
    drawImage_drapeau(250, 15);      
    }
    
    
void page_dessin()
{
    // ROUGE
        BSP_LCD_SetTextColor(LCD_COLOR_RED);
        BSP_LCD_FillRect(2,242,41, 28);
        //VERT
        BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
        BSP_LCD_FillRect(45,242,41, 28);
        //BLEU
        BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
        BSP_LCD_FillRect(88,242,41, 28);
        //VIOLET
        BSP_LCD_SetTextColor(LCD_COLOR_MAGENTA);
        BSP_LCD_FillRect(131,242,41, 28);
        //CYAN
        BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
        BSP_LCD_FillRect(174,242,41, 28);
        //JAUNE
        BSP_LCD_SetTextColor(LCD_COLOR_YELLOW);
        BSP_LCD_FillRect(217,242,41, 28);
        //BLANC
        BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
        BSP_LCD_DrawRect(260, 242, 41, 28);
        //MULTICOLOR
        BSP_LCD_SetTextColor(LCD_COLOR_RED);
        BSP_LCD_FillRect(303,242,10, 28);
        BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
        BSP_LCD_FillRect(314,242,10, 28);
        BSP_LCD_SetTextColor(LCD_COLOR_YELLOW);
        BSP_LCD_FillRect(325,242,10, 28);
        BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
        BSP_LCD_FillRect(336,242,10, 28);
         
        drawImage_gomme(355,245); //GOMME
        drawImage_croix(410,243); //EFFACER TOUT
        drawImage_fleche(445,245); // Retour
                
        BSP_LCD_SetTextColor(LCD_COLOR_BLACK);  
        BSP_LCD_DrawHLine(0,240,480);        
    }

void page_drapeau()
{          
                                        // Au dessus 22px de la barre + 7//
   /* 8px */ BSP_LCD_DrawRect(8, 29, 228, 74);    /* 8px */    BSP_LCD_DrawRect(244, 29, 228, 74);  /* 8px */
             drawImage_allemagne(10,31);          /* 7px */    drawImage_belgique (246,31);
             
   /* 8px */ BSP_LCD_DrawRect(8, 110, 228, 74);   /* 8px */    BSP_LCD_DrawRect(244, 110, 228, 74); /* 8px */
             drawImage_benin    (10,111);         /* 7px */    drawImage_lgbt     (246,111);
                                                  
   /* 8px */ BSP_LCD_DrawRect(8, 191, 228, 73);   /* 8px */    BSP_LCD_DrawRect(244, 191, 228, 73); /* 8px */
             drawImage_france   (10,192);                      drawImage_italie   (246,192);
                                              
    drawImage_fleche(447,0);// RETOUR
    
    }
//////////////////// FIN DES FONCTIONS PAGES (L693-L766) ///////////////////////////////////////////////////////