granteeric

pb tft modif v2

May 2nd, 2023 (edited)
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 14.11 KB | None | 0 0
  1. #include <Arduino.h>
  2. #define PIN_NPN             2
  3. #define DEMIPERIODE_NPN     500
  4. #define PIN_PNP             3
  5. #define DEMIPERIODE_PNP     500
  6. #define NPN_IN              4
  7. #define PNP_IN              5
  8. #define NPN_WRONG           6
  9. #define PNP_WRONG           7
  10. #define VAL1                A0
  11. #define VAL2                A1
  12. #define VAL3                A2
  13. #define VAL4                A3
  14. #define VAL5                A4
  15. #define VAL6                A5
  16.  
  17.  
  18. #include<SPI.h> // introduction des librairties
  19.  
  20. #include<Adafruit_GFX.h>
  21. #include<Adafruit_ILI9341.h>
  22. #include <SPFD5408_TouchScreen.h>
  23. #define TFT_DC 9 //definition des broches serie
  24. #define TFT_CS 10
  25. #define TFT_T_CS 7
  26. #define ILI9341_BLACK 0x0000 ///< 0, 0, 0 //definition des couleurs
  27. #define ILI9341_NAVY 0x000F ///< 0, 0, 123
  28. #define ILI9341_DARKGREEN 0x03E0 ///< 0, 125, 0
  29. #define ILI9341_DARKCYAN 0x03EF ///< 0, 125, 123
  30. #define ILI9341_MAROON 0x7800 ///< 123, 0, 0
  31. #define ILI9341_PURPLE 0x780F ///< 123, 0, 123
  32. #define ILI9341_OLIVE 0x7BE0 ///< 123, 125, 0
  33. #define ILI9341_LIGHTGREY 0xC618 ///< 198, 195, 198
  34. #define ILI9341_DARKGREY 0x7BEF ///< 123, 125, 123
  35. #define ILI9341_BLUE 0x001F ///< 0, 0, 255
  36. #define ILI9341_GREEN 0x07E0 ///< 0, 255, 0
  37. #define ILI9341_CYAN 0x07FF ///< 0, 255, 255
  38. #define ILI9341_RED 0xF800 ///< 255, 0, 0
  39. #define ILI9341_MAGENTA 0xF81F ///< 255, 0, 255
  40. #define ILI9341_YELLOW 0xFFE0 ///< 255, 255, 0
  41. #define ILI9341_WHITE 0xFFFF ///< 255, 255, 255
  42. #define ILI9341_ORANGE 0xFD20 ///< 255, 165, 0
  43. #define ILI9341_GREENYELLOW 0xAFE5 ///< 173, 255, 41
  44. #define ILI9341_PINK 0xFC18 ///< 255, 130, 198
  45. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); // declarations
  46.  
  47. enum affiche { VAL_TENSION1, VAL_TENSION2, VAL_TENSION3, VAL_TENSION4, VAL_TENSION5, VAL_TENSION6, TEMPO_NPN, TEMPO_PNP };
  48.  
  49. struct strText{
  50.     affiche id;
  51.     String text;
  52.     uint16_t color;
  53.     uint16_t x;
  54.     uint16_t y;
  55.     uint8_t textSize;
  56. };
  57.  
  58. //prototype des fonctions
  59. void initTft();
  60. void tftPrint(const uint16_t& color,  const uint16_t& x, const uint16_t& y, const String text, const uint8_t& textSize = 2);
  61. void tftPrint(const strText& txt);
  62. void clearTxt(const strText& txt);
  63. void checkTension(const affiche& id,const float& tension, const int& seuil);    
  64.  
  65. //global variables
  66. int npn_in_haut = {0};
  67. int pnp_in_haut = {0};
  68. unsigned long temps_npn_in = {501};
  69. unsigned long temps_pnp_in = {501};
  70. unsigned long temps_actuel_npn{0};
  71. unsigned long temps_actuel_pnp{0};
  72. unsigned long temps_precedent_npn {0};
  73. unsigned long temps_precedent_pnp {0};
  74. uint16_t tftFondEcran {ILI9341_WHITE};
  75.  
  76. bool printIsOK{false};
  77.  
  78. strText text[6] {
  79.     {VAL_TENSION1,"", ILI9341_RED, 80, 30, 2},
  80.     {VAL_TENSION2,"", ILI9341_BLUE, 80, 60, 2},
  81.     {VAL_TENSION3,"", ILI9341_GREEN, 80, 90, 2},
  82.     {VAL_TENSION4,"", ILI9341_ORANGE, 80, 120, 2},
  83.     {VAL_TENSION5,"", ILI9341_YELLOW, 80, 150, 2},
  84.     {VAL_TENSION6,"", ILI9341_MAGENTA, 80, 180, 2}
  85. };
  86.  
  87. strText textOkBad[8] {
  88.     {VAL_TENSION1,"", ILI9341_RED, 80, 30, 2},
  89.     {VAL_TENSION2,"", ILI9341_RED, 80, 60, 2},
  90.     {VAL_TENSION3,"", ILI9341_RED, 80, 90, 2},
  91.     {VAL_TENSION4,"", ILI9341_RED, 80, 120, 2},
  92.     {VAL_TENSION5,"", ILI9341_RED, 80, 150, 2},
  93.     {VAL_TENSION6,"", ILI9341_RED, 80, 180, 2},
  94.     {TEMPO_NPN,"", ILI9341_RED, 168, 210, 2},
  95.     {TEMPO_PNP,"", ILI9341_RED, 168, 240, 2}
  96. };
  97.  
  98. const int textPrecedentSize {6};
  99. const int textOkBadPrecedentSize {8};
  100. strText textPrecedent[textPrecedentSize];
  101. strText textOkBadPrecedent[textOkBadPrecedentSize];
  102.  
  103. const String textOK ="OK";
  104. const String textBad ="BAD";
  105.  
  106. void setup() {
  107. pinMode(PIN_NPN, OUTPUT);
  108. pinMode(PIN_PNP, OUTPUT);
  109. pinMode (NPN_IN, INPUT);
  110. pinMode (NPN_WRONG, OUTPUT);
  111. pinMode (PNP_IN, INPUT);
  112. pinMode (PNP_WRONG, OUTPUT);
  113. pinMode (VAL1,INPUT);// entrée annalogique 0 +33.5v
  114. pinMode (VAL2,INPUT);// entrée annalogique 1 -33.5v
  115. pinMode (VAL3,INPUT);// entrée annalogique 2 11.5v
  116. pinMode (VAL4,INPUT);// entrée annalogique 3 25vac (13a/b)
  117. pinMode (VAL5,INPUT);// entrée annalogique 4 25vac (1a/b)
  118. pinMode (VAL6,INPUT);// entrée annalogique 5 17.5vac
  119.  
  120. //atribution des entrées analogiques
  121. //-------------------------------------------------------------------------------------------
  122. //Serial.begin(9600);
  123. //delay(100);
  124.  
  125. initTft();
  126.  
  127. }
  128. //--------------------------------------------------------------------------------------------
  129. void loop() {
  130.  
  131.     // envoie des impultion vers transistors npn et pnp
  132.     static unsigned long derniernpn = 0, dernierpnp = 0;
  133.  
  134.     if ((millis() - derniernpn) >= DEMIPERIODE_NPN) {
  135.         derniernpn = derniernpn + DEMIPERIODE_NPN;
  136.         digitalWrite (PIN_NPN, !digitalRead (PIN_NPN));
  137.     }
  138.  
  139.     if ((millis() - dernierpnp) >= DEMIPERIODE_PNP) {
  140.         dernierpnp = dernierpnp + DEMIPERIODE_PNP;
  141.         digitalWrite (PIN_PNP, !digitalRead (PIN_PNP));
  142.     }
  143.  
  144.     // verification de la tempo npn.
  145.     temps_actuel_npn = millis ();
  146.     if ( !digitalRead(NPN_IN) ) { //LOW
  147.         if ( temps_actuel_npn < temps_npn_in ){
  148.             textOkBad[TEMPO_NPN].text = textBad;
  149.         }
  150.         else {
  151.             temps_precedent_npn = temps_actuel_npn;
  152.             textOkBad[TEMPO_NPN].text = textOK;
  153.         }
  154.         tftPrint(textOkBad[TEMPO_NPN]);
  155.     }
  156.     else if ( (temps_actuel_npn - temps_precedent_npn > temps_npn_in)){
  157.     //digitalWrite (NPN_WRONG, HIGH);
  158.         textOkBad[TEMPO_NPN].text = textBad;
  159.         tftPrint(textOkBad[TEMPO_NPN]);
  160.     }
  161.  
  162.     // verification de la tempo pnp.
  163.     temps_actuel_pnp = millis ();
  164. //    pnp_in_haut = digitalRead (PNP_IN);
  165.     if (!digitalRead(PNP_IN)){  //LOW
  166.         if ( temps_actuel_pnp < temps_pnp_in ){
  167.             textOkBad[TEMPO_PNP].text = textBad;
  168.         }
  169.         else{
  170.             temps_precedent_pnp = temps_actuel_pnp;
  171.             textOkBad[TEMPO_PNP].text = textOK;
  172.         }
  173.         tftPrint(textOkBad[TEMPO_PNP]);
  174.     }
  175.     else if ( (temps_actuel_pnp - temps_precedent_pnp > temps_pnp_in)){
  176.         textOkBad[TEMPO_PNP].text = textBad;
  177.         tftPrint(textOkBad[TEMPO_PNP]);
  178.     }
  179.        
  180.  
  181.     //---------------LECTURE DES TENSIONS MAINFRAME-----------------
  182.     float tension1 = analogRead(VAL1);// Lire la valeur analogique +33.5v
  183.     // Diviser par 205 puis multiplié par 7.976-
  184.     //-pour convertir 4.2V encodés sur 10 bits
  185.     //(1024 valeurs: 1024/5=243.8095*7.976 == valeure réel)
  186.     float tension_a = tension1 / 204.8f * 7.976f ;
  187.     text[VAL_TENSION1].text = String(tension_a, 2);
  188.     tftPrint(text[VAL_TENSION1]);
  189.    
  190.    
  191.     //verifie la condition de tension +33.5v
  192.     //si tension inferieur a 28v = bad en rouge
  193.     // sinon ok en vert
  194.     checkTension(VAL_TENSION1,tension_a,28);    
  195.  
  196.     //lecture tension -33.5v
  197.     float tension2 = analogRead(VAL2);// Lire la valeur analogique -33.5v
  198.     // Diviser par 205 puis multiplié par 7.976-
  199.     //-pour convertir 4.2V encodés sur 10 bits
  200.     //(1024 valeurs: 1024/5=243.8095*7.976 == valeure réel)
  201.     float tension_b = tension2 / 204.8f * 7.976f ;
  202.     //affichage des valeurs sur ecran tft ILI9341
  203.     text[VAL_TENSION2].text = String(tension_b, 2);
  204.     tftPrint(text[VAL_TENSION2]);
  205.  
  206.  
  207.     //verifie la condition de tension -33.5v
  208.     //si tension inferieur a 28v = bad en rouge
  209.     // sinon ok en vert
  210.     checkTension(VAL_TENSION2,tension_b,28);    
  211.  
  212.     float tension3 = analogRead(VAL3);// Lire la valeur analogique 11.5v
  213.     // Diviser par 205 puis multiplié par 5.952-
  214.     //-pour convertir 4.2V encodés sur 10 bits
  215.     //(1024 valeurs: 1024/5=204.8*5.952 == valeure réel)
  216.     float tension_c = tension3 /243.8095f * 5.952f ;
  217.     //affichage des valeurs sur ecran tft ILI9341
  218.     text[VAL_TENSION3].text = String(tension_c, 2);
  219.     tftPrint(text[VAL_TENSION3]);
  220.  
  221.  
  222.    
  223.     //verifie la condition de tension 25vac
  224.     //si tension inferieur a 20vac = bad en rouge
  225.     // sinon ok en vert
  226.     checkTension(VAL_TENSION3,tension_c,20);    
  227.  
  228.     // Lire la valeur analogique
  229.     float tension4 = analogRead(VAL4);
  230.     // Diviser par 205 puis multiplié par 5.952-
  231.     //-pour convertir 4.2V encodés sur 10 bits
  232.     //(1024 valeurs: 1024/5=204.8*5.952 == valeure réel)
  233.     float tension_d = tension4 / 243.8095f *5.952f ;
  234.     //affichage des valeurs sur ecran tft ILI9341
  235.     text[VAL_TENSION4].text = String(tension_d, 2);
  236.     tftPrint(text[VAL_TENSION4]);
  237.    
  238.     checkTension(VAL_TENSION4,tension_d,20);    
  239.  
  240.  
  241.     // Lire la valeur analogique
  242.     float tension5 = analogRead(VAL5);
  243.     // Diviser par 205 puis multiplié par 4.1666-
  244.     //-pour convertir 4.2V encodés sur 10 bits
  245.     //(1024 valeurs: 1024/5=204.8*4.1666 == valeure réel)
  246.     float tension_e = tension5 / 204.8f *4.1666f ;
  247.     //affichage des valeurs sur ecran tft ILI9341
  248.     text[VAL_TENSION5].text = String(tension_e, 2);
  249.     tftPrint(text[VAL_TENSION5]);
  250.  
  251.     //verifie la condition de tension 17.5vac
  252.     //si tension inferieur a 13vac = bad en rouge
  253.     // sinon ok en vert
  254.     checkTension(VAL_TENSION5,tension_e,13);    
  255.  
  256.  
  257.     // Lire la valeur analogique
  258.     float tension6 = analogRead(VAL6);
  259.     // Diviser par 205 puis multiplié par 2.738-
  260.     //-pour convertir 4.2V encodés sur 10 bits
  261.     //(1024 valeurs: 1024/5=243.8095*2.738 == valeure réel)
  262.     float tension_f = tension6 / 204.8f * 2.738f ;
  263.     //affichage des valeurs sur ecran tft ILI9341
  264.     text[VAL_TENSION6].text = String(tension_f, 2);
  265.     tftPrint(text[VAL_TENSION6]);
  266.  
  267.    
  268.     //verifie la condition de tension 11.5vdc
  269.     //si tension inferieur a 9vdc= bad en rouge
  270.     // sinon ok en vert
  271.     checkTension(VAL_TENSION6,tension_f,9);    
  272. }
  273.  
  274. void initTft() {
  275.     //TEXTES STATIQUE ET TENSIONS NOMINALES.
  276.     tft.begin();
  277.     tft.setSPISpeed(8000000); // vitesse de la liaison serie
  278.  
  279.     delay(100);
  280.     tft.setRotation(0); //POSTION DE L'ECRAN
  281.     tft.fillScreen(tftFondEcran); //couleur fond d'ecran
  282.     tft.setTextColor(ILI9341_BLACK); //taille, couleur, et position de "nomine"
  283.     tft.setTextSize(2);
  284.     tft.setCursor(2,2);
  285.     tft.println("NOMIN");
  286.     tft.setCursor(80,2); //taille, couleur position et de "measur"
  287.     tft.println("MEASUR");
  288.     tft.setCursor(168,2); //taille, et position de "status"
  289.     tft.println("STATUS");
  290.     tft.setTextColor(ILI9341_RED); //taille, couleur et position des differentes tensions nominal
  291.     tft.setCursor(2,30);
  292.     tft.println("+33.5");
  293.     tft.setTextColor(ILI9341_BLUE);
  294.     tft.setCursor(2,60);
  295.     tft.println("-33.5");
  296.     tft.setTextColor(ILI9341_GREEN);
  297.     tft.setCursor(2,90);
  298.     tft.println("11.5DC");
  299.     tft.setTextColor(ILI9341_ORANGE);
  300.     tft.setCursor(2,120);
  301.     tft.println("25AC");
  302.     tft.setTextColor(ILI9341_YELLOW);
  303.     tft.setCursor(2,150);
  304.     tft.println("25AC");
  305.     tft.setTextColor(ILI9341_MAGENTA);
  306.     tft.setCursor(2,180);
  307.     tft.println("17.5AC");
  308.     tft.setTextColor(ILI9341_BLUE); //taille, couleur et position de "npn"
  309.     tft.setCursor(2,210);
  310.     tft.println("NPN");
  311.     tft.setTextColor(ILI9341_RED); //taille, couleur et position de "pnp"
  312.     tft.setCursor(2,240);
  313.     tft.println("PNP");
  314.     tft.fillRect(85,293,57,15,(ILI9341_PINK));//taille, couleur, encadré et position de "MT501"
  315.     tft.setTextColor(ILI9341_BLACK);
  316.     tft.setCursor(85,293);
  317.     tft.println("MT501");
  318.     tft.setTextColor(ILI9341_BLACK); //taille, couleur et position de "SN/TL020MT"
  319.     tft.setCursor(2,300);
  320.     tft.setTextSize(1);
  321.     tft.println("S/N TL-020MT");
  322.     //-------------------------------------------------------------------------------------------
  323.     //CREATION DU LOGO "INT"
  324.     tft.fillRect(160,280,8,45,(ILI9341_GREEN)); // barres verticales
  325.     tft.fillRect(168,280,8,45,(ILI9341_GREEN));
  326.     tft.fillRect(193,280,8,45,(ILI9341_GREEN));
  327.     tft.fillRect(201,280,8,45,(ILI9341_GREEN));
  328.     tft.fillRect(209,280,8,8,(ILI9341_GREEN)); //trait horizontale du "T"
  329.     tft.fillCircle(164,276,4,(ILI9341_BLUE)); //point bleu
  330.     tft.drawLine(193,320,176,290,(ILI9341_GREEN)); // barre oblique
  331.     tft.drawLine(193,319,176,289,(ILI9341_GREEN));
  332.     tft.drawLine(193,318,176,288,(ILI9341_GREEN));
  333.     tft.drawLine(193,317,176,287,(ILI9341_GREEN));
  334.     tft.drawLine(193,316,176,286,(ILI9341_GREEN));
  335.     tft.drawLine(193,315,176,285,(ILI9341_GREEN));
  336.     tft.drawLine(193,324,176,284,(ILI9341_GREEN));
  337.     tft.drawLine(193,313,176,283,(ILI9341_GREEN));
  338.     //--------------------------------------------------------------------------------------------
  339.     //LIGNES DE SEPARATION.
  340.     tft.drawFastVLine(75,0,258,(ILI9341_BLACK)); // lignes de separation verticales
  341.     tft.drawFastVLine(163,0,258,(ILI9341_BLACK));
  342.     tft.drawFastHLine(0,20,240,(ILI9341_BLACK)); // lignes de separation horizontales
  343.     tft.drawFastHLine(0,258,240,(ILI9341_BLACK));
  344.     tft.drawFastHLine(0,200,240,(ILI9341_BLACK));
  345. }
  346.  
  347.  
  348.  
  349. void tftPrint(const uint16_t& color,  const uint16_t& x, const uint16_t& y, const String text, const uint8_t& textSize) {
  350.     tft.setTextSize(textSize);
  351.     tft.setTextColor(color);
  352.     tft.setCursor(x,y);
  353.     tft.println(text);
  354. }
  355.  
  356. void tftPrint(const strText& txt){
  357.  if(txt.id == TEMPO_NPN || txt.id == TEMPO_PNP){
  358.     int pos=0;
  359.     for(int i = 0 ; i < textOkBadPrecedentSize ; i++){
  360.         if(textOkBadPrecedent[i].id == txt.id){
  361.             pos = i;
  362.             break;
  363.         }
  364.     }
  365.     if(txt.text != textOkBadPrecedent[pos].text ){
  366.         clearTxt(textOkBadPrecedent[pos]);
  367.         textOkBadPrecedent[pos] = txt;
  368.         tftPrint(txt.color,txt.x,txt.y,txt.text,txt.textSize);
  369.     }
  370.  }
  371.  else{
  372. int pos=0;
  373.     for(int i = 0 ; i < textPrecedentSize ; i++){
  374.         if(textPrecedent[i].id == txt.id){
  375.             pos = i;
  376.             break;
  377.         }
  378.     }
  379.     if(txt.text != textPrecedent[pos].text ){
  380.         clearTxt(textPrecedent[pos]);
  381.         textPrecedent[pos] = txt;
  382.         tftPrint(txt.color,txt.x,txt.y,txt.text,txt.textSize);
  383.     }
  384.  }
  385.  
  386. }
  387.  
  388. void clearTxt(const strText& txt){
  389.     tft.setTextSize(txt.textSize);
  390.     tft.setTextColor(tftFondEcran); //taille, couleur et position de "npn"
  391.     tft.setCursor(txt.x,txt.y);
  392.     tft.println(txt.text);
  393. }
  394.  
  395. void checkTension(const affiche& id,const float& tension, const int& seuil){
  396.     if (tension < seuil) textOkBad[id].text = textBad;
  397.     else textOkBad[id].text = textOK;
  398.     tftPrint(textOkBad[id]);
  399. }
Advertisement
Add Comment
Please, Sign In to add comment