/************************************************************
* Si4684 ESP_AFFI
- pilote un afficheur 320x240 ILI9341 avec un ESP32 (connecté par ma carte "Carte_ILI9341_ESP32")
- utilise l'écran tactile (XPT2046)
- lit et écrit sur la SDcard connectée au dos de l'afficheur
- affiche des images bmp565
- effectue des capture d'écran
- décode et affiche les images jpg
- affiche et gère des boutons graphiques
- affiche et gère des panels de boutons graphiques exclusifs (1 seul bouton 'enfoncé' à la fois)
*************************************************************/
#include <Arduino.h>
//----------------------------------
String version = "2026-12-01";
//----------------------------------
#include "main_AFFI.h"
#include "TJpg_Decoder.h" // Décodeur JPEG matériel/logiciel optimisé ESP32
#include "mbedtls/base64.h" // encodeur/décodeur en format 'base64'
#include "bouton.h"
#include "etiquette.h"
#include "page_texte.h"
#include "TFT_eSPI.h"
#define SPI_READ_FREQUENCY 16000000
#define XPT2046_IRQ 36
#define XPT2046_MOSI 32 //T_DI 32
#define XPT2046_MISO 39 //T_DO 39
#define XPT2046_CLK 25
#define XPT2046_CS 33
//attention: ces valeurs ne sont pas celles par défaut pour SDA et SCL
uint16_t ROUGE = 63488;
uint16_t ORANGE = 62400;
uint16_t JAUNE = 65504;
uint16_t VERT = 2016;
uint16_t BLEU = 31;
uint16_t BLEU_CLAIR = 26079;
uint16_t CYAN = 2047;
uint16_t NOIR = 0x000;
uint16_t GRIS_1 = (27 << 11) | (54 << 5) | 27;
uint16_t GRIS_2 = (24 << 11) | (48 << 5) | 24;
uint16_t GRIS_3 = (20 << 11) | (40 << 5) | 20;
uint16_t GRIS_4 = (15 << 11) | (30 << 5) | 15;
uint16_t GRIS_5 = (13 << 11) | (26 << 5) | 13;
uint16_t GRIS_6 = (5 << 11) | (10 << 5) | 5;
uint16_t BLANC = 65535;
uint16_t color565px;
const int GPIO_SDA = 21;
const int GPIO_SCL = 22;
const int analogPin = 35;
const int _DX = 320;
const int _DY = 240;
SPIClass mySpi = SPIClass(VSPI);
XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
SPIClass mySpi2 = SPIClass(HSPI);
TFT_eSPI TFT = TFT_eSPI(); // Configurer le fichier User_Setup.h de la bibliothèque TFT_eSPI au préalable
TFT_eSprite sprite_frq = TFT_eSprite(&TFT);
struct ETALON_TS // etalon touch screen
{
int16_t x0;
int16_t dx;
int16_t y0;
int16_t dy;
};
ETALON_TS eTS;
struct POINT
{
uint16_t x;
uint16_t y;
};
// coordonnées des points de calibrage de l'écran tactile
POINT A, B;
uint8_t n_mult_0 =0; // index dans la liste; enregistré sur la SDcard
uint8_t n_mult_1 =0;
uint8_t n_mult_2 =0;
uint8_t n_mult_3 =0;
// sélection d'une station au démarrage, mais plus par la suite
// prévoir d'enregistrer ces 2 infos sur la SDcard (afin de rappeler la dernière station écoutée)
uint8_t btm = 2; // 10C
uint8_t bts = 10; // FRANCE MUSIQUE
uint8_t memo_n_bt_cat=0;
uint16_t x_touch, y_touch;
uint16_t memo_x_touch, memo_y_touch;
uint8_t SDcardOk=0;
boolean do_capt_screen = false;
float raddeg = M_PI/180.0;
float deg_to_rad = 2.0 * M_PI /360.0;
boolean test_touch_screen = false; // mettre true pour activer le test
unsigned long previousMillis = 0;
ETIQUETTE etiquette_rssi;
ETIQUETTE etiquette_ID;
ETIQUETTE etiquette_LABEL1; // en mode 'expert'
ETIQUETTE etiquette_LABEL2; // en bas, sous le catalogue
ETIQUETTE etiquette_Radiotext1; // en mode 'expert'
ETIQUETTE etiquette_Radiotext2; // tout en bas, en mode 'catalogue'
PAGE_TEXTE page_stations;
TOUCH_BOUTON bt_SCAN_stations;
TOUCH_BOUTON bt_param; // d'affichage
TOUCH_BOUTON bt_mode_expert; // situé sur la page 'catalogue' pour basculer en mode 'expert'
TOUCH_BOUTON bt_mode_catalogue; // situé sur la page 'expert' pour basculer en mode 'catalogue'
TOUCH_BOUTON bt_LOGO; // bouton caché sous le logo de la station, sert à l'afficher en tout-écran
TOUCH_BOUTON bt_img2; // caché sous l'image reçue (celle affichée sous le "catalogue")
TOUCH_BOUTON bt_stop_tout_ecran; // petit bouton en haut à gauche
TOUCH_BOUTON bt_to_SD; // save params to SDcard
TOUCH_BOUTON bt_cal; // calibrage du tactile
TOUCH_BOUTON_IMG bt_img1;
TOUCH_BOUTON bt_capture_ecran;
PANEL_BOUTONS panel_bt_multiplex;
PANEL_BOUTONS panel_bt_stations;
GRID_PAD gridPad1; // boutons invisibles sous l'image du "catalogue" de sélection de station (en tout-écran)
String libels_stations0[13]={"-","-","-","-","-","-","-","-","-","-","-","-","-"};
String libels_stations1[13]={"-","-","-","-","-","-","-","-","-","-","-","-","-"};
String libels_stations2[13]={"-","-","-","-","-","-","-","-","-","-","-","-","-"};
String libels_stations3[13]={"-","-","-","-","-","-","-","-","-","-","-","-","-"};
String RadioText;
uint8_t decal_tx=0;
struct STATION
{
uint8_t n_ml; // 0..3
uint8_t n_st; // 0..12
String s; // "/slideshows/generiques/320x240/F2F8_NRJ.jpg"; // sur SDcard
};
STATION lst_sta[52]; // liste des stations; 4 multiplex de 13 stations
String incomingBase64 = "";
boolean receivingSLS = false;
uint8_t num_bt_M; // 0..3
uint8_t num_M; // ex : 21
//uint8_t num_bt_M;
boolean choix_mutiplex_ok = false;
uint8_t num_bt_S;
uint8_t num_S;
uint32_t Sid;
uint16_t compte=0;
uint16_t couleur_fond_ecran = 229; // outil : https://rgbcolorpicker.com/565
enum GENERAL_MODE {MODE_EXPERT, MODE_PARAM, MODE_CATALOGUE};
GENERAL_MODE mode_general;
enum AFFI_MODE {MODE_PETITE, MODE_TOUT_ECRAN}; // de l'image
AFFI_MODE mode_affi;
//boolean LOGO_tt_ecran= false;
// États de la machine à états de réception
enum RX_STATE { WAIT_START, RECEIVING_DATA };
RX_STATE rxState = WAIT_START;
#define MAX_SLIDE_SIZE 51200 // Buffer statique de 50 Ko (suffisant (?) pour un MOT Slideshow DAB+ de 320x240)
uint8_t currentSlideBuffer[MAX_SLIDE_SIZE];
size_t currentSlideSize = 0;
size_t imgRAM_Size = 0; // taille de l'image enregistrée en RAM
String base64Buffer = "";
size_t expectedLen = 0;
String logo_generique_en_cours;
String memo_logo_generique_en_cours; // propre à la station, par défaut
String logo_en_cours;
String memo_logo_en_cours;
boolean logo_courant = false;
//-----------------------------------------------------------------------------------
float degTOrad(float angle) {return (angle * M_PI / 180.0);}
uint8_t decToBcd(int val) {return (uint8_t) ((val / 10 * 16) + (val % 10));}
uint16_t Color_To_565(uint8_t r, uint8_t g, uint8_t b) {return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3);}
void RGB565_to_888(uint16_t color565, uint8_t *R, uint8_t *G, uint8_t *B)
{
*R=(color565 & 0xF800) >> 8;
*G=(color565 & 0x7E0) >> 3;
*B=(color565 & 0x1F) << 3 ;
}
String desaccentue(String str)
{
String out = "";
uint8_t c;
for (int i = 0; i < str.length(); i++)
{
c = str[i];
if (c < 128){out += (char)c;}
switch (c)
{
case 0x90:
case 0x81:
out += 'a';
break;
case 0x82:
case 0x83:
case 0x92:
case 0x93:
out += 'e';
break;
case 0x89: // ù
out += 'u';
break;
case 0x9B: // ç
out += 'c';
break;
}
}
return out;
}
void TS_calibrate() // calibrage de l'écran tactile
{
uint8_t L = 20;
boolean ok;
String s1;
TFT.setTextColor(BLANC, NOIR);
TFT.setFreeFont(FF1);
// traitement point A
TFT.fillScreen(NOIR);
TFT.drawString("Cliquez sur le point A", 20, 50);
delay(300);
TFT.drawFastVLine(10, 0, L, JAUNE);
TFT.drawFastHLine(0, 10, L, JAUNE);
ok = false;
while (! ok)
{
if (ts.tirqTouched() && ts.touched())
{
memo_y_touch = y_touch;
TS_Point p = ts.getPoint();
s1 = "Ax=" + String(p.x) + " "; TFT.drawString(s1, 50, 100);
s1 = "Ay=" + String(p.y) + " "; TFT.drawString(s1, 50, 120);
delay(100);
if( (p.x<600) && (p.y < 600)) // évite de valider une position irréaliste
{
A.x = p.x; // point A : variable globale
A.y = p.y;
ok = true;
}
}
}
// traitement point B
TFT.fillScreen(NOIR);
delay(300);
TFT.drawString("Cliquez sur le point B", 20, 70);
TFT.drawFastVLine(310, 230-L/2, L, JAUNE);
TFT.drawFastHLine(310-L/2, 230, L, JAUNE);
ok = false;
while (! ok)
{
if (ts.tirqTouched() && ts.touched())
{
memo_y_touch = y_touch;
TS_Point p = ts.getPoint();
s1 = "Bx=" + String(p.x) + " "; TFT.drawString(s1, 50, 150);
s1 = "By=" + String(p.y) + " "; TFT.drawString(s1, 50, 170);
delay(100);
if( (p.x>3600) && (p.y > 3400)) // évite de valider une position irréaliste
{
B.x = p.x;
B.y = p.y;
ok = true;
}
}
}
if(SDcardOk==1)
{
TFT.fillScreen(NOIR);
TFT.drawString("Ok", 20, 60);
TFT.setFreeFont(FF0);
TFT.drawString("Write fichier params sur la SD", 10, 90);
write_params_tactile_SD();
delay(1000);
}
TFT.fillScreen(NOIR);
}
void init_sprites()
{
sprite_frq.createSprite(220, 55);
sprite_frq.loadFont(digitfont1);
//sprite_frq.setTextColor(JAUNE_2, NOIR);
sprite_frq.setTextDatum(MR_DATUM); // alignement du texte
}
void init_SDcard()
{
Serial.println("---------------------");
Serial.println("init_SDcard()");
String s1;
TFT.fillRect(0, 0, 480, 320, NOIR); // efface
TFT.setTextColor(BLANC, NOIR);
TFT.setFreeFont(FF1);
if(!SD.begin(5, mySpi2))
{
Serial.println("Card Mount Failed");
SDcardOk=0;
}
else
{
Serial.println("SDcard OK");
SDcardOk=1;
TFT.fillRect(0, 0, 480, 320, VERT);
delay(100);
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE)
{
Serial.println("NO SDcard");
SDcardOk=0;
return;
}
Serial.print("SDcard Type: ");
if(cardType == CARD_SD) {Serial.print("SDSC");}
else if(cardType == CARD_SDHC) {Serial.println("SDHC");}
uint32_t cardSize = SD.cardSize() / (1024 * 1024);
s1=(String)cardSize + " GB";
Serial.println(s1); Serial.println();
delay (100);
TFT.fillRect(0, 0, 480, 320, NOIR); // efface
}
uint16_t read_16(File fp)
{
uint8_t low;
uint16_t high;
low = fp.read();
high = fp.read();
return (high<<8)|low;
}
uint32_t read_32(File fp)
{
uint16_t low;
uint32_t high;
low = read_16(fp);
high = read_16(fp);
return (high<<8)|low;
}
void write_TFT_on_SDcard(String filename) // capture image bmp 320x240 RGB565 (5+6+5 = 16bits/px)
{
// Fonction optimisée pour l'afficheur ILI9341 avec la library 'TFT_eSPI'
// ne convient PAS pour les ESP32 Wroom + afficheur 3.5" TFT 480x320
Serial.println("write_TFT_on_SDcard()");
Serial.print("SDcardOk = "); Serial.println(SDcardOk);
if (SDcardOk==0) {return;}
uint8_t lineBuffer8[(320*2)];
uint16_t lineBuffer16[320];
uint8_t octet_A, octet_B;
TFT.readRect(0, 100, 320, 1, lineBuffer16); // lit une seule ligne
TFT.drawString("A", 310, 230); // pour terminer par une écriture sinon pas de write sur la SD plus bas
//File file1 = SD.open("/bmp565/capture.bmp", FILE_WRITE);
File file1 = SD.open(filename, FILE_WRITE);
if (file1)
{
// création entête bmp565 - 138 octets; voir bmp565_header[] dans le fichier main.h
for(uint8_t i=0; i<138; i++) { file1.write(bmp565_header[i]); }
for (int16_t y=240; y>0; y--)
{
TFT.readRect(0, y, 320, 1, lineBuffer16); // lit une ligne
TFT.drawString("A", 310, 230); // pour terminer par une écriture
uint16_t i=0;
for (int16_t x=0; x<320; x++) //320
{
color565px = lineBuffer16[x]; // BBBBBrrr rrrVVVVV
octet_A = (color565px & 0b1111111100000000) >> 8;
octet_B = (color565px & 0b0000000011111111);
lineBuffer8[i] = octet_A;
lineBuffer8[i+1] = octet_B;
i+=2;
}
file1.write(lineBuffer8, sizeof(lineBuffer8));
}
Serial.println("write end - Ok");
file1.close(); // referme le fichier
//TFT.fillRect(170, 115, 20, 8, VERT);
}
}
void affi_BMP_from_SD(String filename, uint16_t x0, uint16_t y0)
{
Serial.println("----------------------------");
Serial.println("affi_BMP_from_SD()");
uint8_t bmp_data[2]={0,0};
uint8_t a, b;
uint16_t x=0;
uint16_t y=0;
int16_t xmax;
uint32_t seekOffset;
uint16_t w, h, row, col;
File fp = SD.open(filename, "r");
if (!fp) { Serial.println("ERREUR open file on SD"); return; }
uint16_t etiq1 = read_16(fp);
// test bmp_header
if (etiq1 == 0x4D42)
{
read_32(fp);
read_32(fp);
seekOffset = read_32(fp);
Serial.print("seekOffset ="); Serial.println(seekOffset); // 138
Serial.print("seekOffset= "); Serial.println(seekOffset);
read_32(fp);
w = read_32(fp);
h = read_32(fp);
Serial.print("w= "); Serial.println(w); // 75
Serial.print("h= "); Serial.println(h); // 50
Serial.println(read_16(fp)); // 1
Serial.println(read_16(fp)); // 16
Serial.println(read_16(fp)); // 3
fp.seek(seekOffset);
}
y += h;
xmax = w;
if(xmax%2 == 1) {xmax +=1;}
uint16_t padding = (4 - ((w * 2) & 2)) & 2;
uint8_t lineBuffer[(w*2) + padding];
for (row = 0; row < h; row++)
{
fp.read(lineBuffer, sizeof(lineBuffer));
uint8_t* bptr = lineBuffer;
uint16_t* tptr = (uint16_t*)lineBuffer;
for (uint16_t col = 0; col < w; col++)
{
a = *bptr++;
b = *bptr++;
*tptr++ = (a <<8 | b);
}
TFT.pushImage(x0 + x, y0 + y, w, 1, (uint16_t*)lineBuffer);
y--;
}
fp.close();
}
void affi_jpg_from_SD(String filename, uint16_t x0, uint16_t y0)
{
//affichage direct de l'image contenue sur la SDcard
TFT.fillRect(x0, y0, 178, 136, NOIR);
TJpgDec.setJpgScale(2); // Échelle
TJpgDec.setCallback(tft_output);
TJpgDec.drawSdJpg(x0+4, y0+3, logo_en_cours);
}
void affi_jpg_from_RAM( uint16_t x0, uint16_t y0, uint8_t echelle)
{
TJpgDec.setJpgScale(echelle); // 1 = grand
TJpgDec.setCallback(tft_output);
TJpgDec.drawJpg(x0, y0, currentSlideBuffer, currentSlideSize); // variables globales
}
void copie_jpg_from_SD_to_RAM(String filename)
{
File file1 = SD.open(filename, FILE_READ);
if (!file1) { Serial.println("Erreur : impossible d'ouvrir le fichier");}
size_t imageSize = file1.size();
size_t bytesRead = file1.read(currentSlideBuffer, imageSize); //image en SD -> RAM
currentSlideSize = imageSize;
file1.close();
}
void write_params_multiplex_SD() // sur la SDcard
{
Serial.println("write fichier '/params_multiplex.txt'");
File file1 = SD.open("/params_multiplex.txt", FILE_WRITE);
String s1;
// enregistrement des numéros des multiplex de la région
s1 ="[n_mult_0]"; s1 += "<"; s1 += String(n_mult_0); s1 +=">";
file1.println(s1);
s1 ="[n_mult_1]"; s1 += "<"; s1 += String(n_mult_1); s1 +=">";
file1.println(s1);
s1 ="[n_mult_2]"; s1 += "<"; s1 += String(n_mult_2); s1 +=">";
file1.println(s1);
s1 ="[n_mult_3]"; s1 += "<"; s1 += String(n_mult_3); s1 +=">";
file1.println(s1);
file1.close();
delay(100);
}
void write_params_stations_SD(uint8_t multiplex, String libels[13]) // sur la SDcard
{
Serial.println("write fichier '/params_stations.txt'");
File file1 = SD.open("/params_stations.txt", FILE_APPEND); // ajoute à la fin sans effacer les données présentes
String s1;
// enregistrement des numéros des labels des stations
for(int n=0; n<13; n++)
{
String sn = String(n); if (n<10) {sn = '0' + sn;}
String mpx = String(multiplex); if (multiplex<10) {mpx = '0' + mpx;}
String s_L = libels[n];
Serial.print("s_L= "); Serial.println(s_L);
s_L= s_L.substring(0, 20); //5 char pour l'ID + 15 char pour le label
s1 ="[label_"; s1+=mpx; s1+="_"; s1+=sn; s1+="]<"; s1+=s_L; s1+=">"; // [label_01_06]<NOSTALGIE>
Serial.print("s1= "); Serial.println(s1);
file1.println(s1);
}
file1.println("----");
file1.close();
delay(100);
}
size_t getJpegSize(const uint8_t* buffer, size_t maxBufferSize)
{
// On cherche la séquence 0xFF 0xD9 (End Of Image)
for (size_t i = 0; i < maxBufferSize - 1; i++)
{
if (buffer[i] == 0xFF && buffer[i + 1] == 0xD9)
{
return i + 2; // On inclut les 2 octets de fin dans la taille totale
}
}
return 0; // Marqueur non trouvé (image corrompue ou trop grande)
}
int compte_Fichiers(fs::FS &fs, const char * dirname)
{
int compteur = 0;
File root = fs.open(dirname);
if (!root || !root.isDirectory()) {return -1;} // Erreur d'ouverture ou n'est pas un dossier
File file = root.openNextFile();
while (file)
{
if (!file.isDirectory()) {compteur++; }
file = root.openNextFile();
}
return compteur;
}
// Cherche, ouvre et lit le premier fichier commençant par "ABCD" à la racine "/"
// absolute_path(SD, "/", "ABCD");
String absolute_path(fs::FS &fs, const char * dirname, String prefixe)
{
String cheminComplet;
File root = fs.open(dirname);
if(!root || !root.isDirectory())
{
Serial.println("Erreur d'ouverture du repertoire");
return ("");
}
File file = root.openNextFile();
bool trouve = false;
while(file)
{
if(!file.isDirectory())
{
String nomFichier = String(file.name());
// Si le fichier commence par le préfixe
if(nomFichier.startsWith(prefixe))
{
//Serial.println("Fichier trouve");
trouve = true;
// Sur l'ESP32, file.name() inclut déjà le '/' initial si on est à la racine.
// Pour être sûr du chemin absolu (surtout dans un sous-dossier), on reconstruit le chemin complet :
cheminComplet = String(dirname) + (String(dirname).endsWith("/") ? "" : "/") + nomFichier;
}
file = root.openNextFile();
}
}
root.close();
Serial.println(cheminComplet);
return cheminComplet;
}
void write_image_jpg_to_SD(uint8_t *jpgBuffer)
{
Serial.println("write_image_jpg_to_SD");
uint16_t nb_fichiers;
String filename;
//const size_t TAILLE_MAX = 20480;
boolean next = true;
uint16_t n = 0;
while(next && (n < 1000) )
{
filename = "/slideshows/courants/" + String(n) + ".jpg";
if (! SD.exists(filename))
{
File file1 = SD.open(filename, FILE_WRITE);
size_t length = getJpegSize(jpgBuffer, MAX_SLIDE_SIZE);
Serial.println("AAA");
Serial.print("length= "); Serial.println(length); // ok
currentSlideSize = length;
size_t bytesWritten = file1.write(jpgBuffer, length);
file1.close();
// memo_logo_en_cours = filename;
next = false;
}
n++;
}
}
void write_params_tactile_SD()
{
Serial.println("write fichier '/params_tactile.txt'");
File file1 = SD.open("/params_tactile.txt", FILE_WRITE);
String s1;
// enregistrement de la calibration de l'écran tactile :
s1 ="[Ax]"; s1 += "<"; s1 += String(A.x); s1 +=">";
file1.println(s1);
s1 ="[Ay]"; s1 += "<"; s1 += String(A.y); s1 +=">";
file1.println(s1);
s1 ="[Bx]"; s1 += "<"; s1 += String(B.x); s1 +=">";
file1.println(s1);
s1 ="[By]"; s1 += "<"; s1 += String(B.y); s1 +=">";
file1.println(s1);
file1.println("----");
file1.close();
delay(100);
}
String read_line_params(uint16_t line_num, String filename)
{
Serial.println("read_line_params()");
int i = 1;
char buffer[64];
String s;
File file = SD.open(filename, "r");
while (file.available())
{
int l = file.readBytesUntil('\n', buffer, sizeof(buffer));
buffer[l] = 0;
if (line_num == i)
{
s = buffer;
file.close();
return(s);
}
i++;
}
return "";
}
int32_t extract_valeur(String ligne, String label)
{
String s2;
uint32_t valeur = 0;
int p1, p2;
p1 = ligne.indexOf('['); p2 = ligne.indexOf(']');
s2 = ligne.substring(p1+1, p2);
if (s2 == label)
{
p1 = ligne.indexOf('<'); p2 = ligne.indexOf('>');
s2 = ligne.substring(p1+1, p2);
valeur = s2.toInt();
return valeur;
}
return -1;
}
String extract_texte(String ligne, String label)
{
Serial.println("extract_texte()");
String s1;
String s2="";
int p1, p2, p3;
String sx = "[" + label + "]";
int L= sx.length();
p1 = ligne.indexOf(sx);
p2 = ligne.indexOf('<', p1); p3 = ligne.indexOf('>', p2);
s2 = ligne.substring(p2+1, p3);
return s2;
}
void read_fichier_params_tactile() // lecture du fichier '/params_tactile.txt' en SD
{
Serial.println("lecture du fichier 'params_tactile.txt' en SD");
String s1;
int valeur;
s1 = "Z";
uint8_t n=1;
while (s1 !="")
{
s1 = read_line_params(n, "/params_tactile.txt"); // retourne (par exemple): [Ax]<534>
// calibrage de l'écran tactile :
valeur = extract_valeur(s1, "Ax");
if(valeur != -1) {A.x = valeur;}
valeur = extract_valeur(s1, "Ay");
if(valeur != -1) {A.y = valeur;}
valeur = extract_valeur(s1, "Bx");
if(valeur != -1) {B.x = valeur;}
valeur = extract_valeur(s1, "By");
if(valeur != -1) {B.y = valeur;}
n++;
}
}
void read_params_multiplex()
{
String s1;
int valeur;
s1 = "Z";
uint8_t n=1;
while (s1 !="")
{
s1 = read_line_params(n, "/params_multiplex.txt"); // retourne (par exemple): [couleur_fond]<267>
// numéros de 4 multiplex de la région
valeur = extract_valeur(s1, "n_mult_0");
if(valeur != -1) {n_mult_0= valeur;}
valeur = extract_valeur(s1, "n_mult_1");
if(valeur != -1) {n_mult_1 = valeur;}
valeur = extract_valeur(s1, "n_mult_2");
if(valeur != -1) {n_mult_2 = valeur;}
valeur = extract_valeur(s1, "n_mult_3");
if(valeur != -1) {n_mult_3 = valeur;}
n++;
}
}
// voir aussi 'write_params_stations_SD('
void read_fichier_params_stations() // lecture du fichier '/params_stations.txt' en SD
{
String ligne_i;
String label_i;
String smult, sbouton;
uint8_t n_bouton;
uint8_t n_mult;
ligne_i= "Z";
uint8_t p1, p2, p3, p4;
String s1, s2;
for(int n=1; n<56; n++) // 56 ok (le fichier fait 56 lignes, 4 + 13*4 )
{
ligne_i = read_line_params(n, "/params_stations.txt"); // retourne (par exemple): [label_02_10]<FRANCE MUSIQUE>
Serial.print("ligne_i= "); Serial.println(ligne_i);
// [label_02_10]<FRANCE MUSIQUE> le 1er nombre est le n° du multiplex; le second est le numéro de la station
p1 = ligne_i.indexOf('_');
s1 = ligne_i.substring(p1+1, p1+3);
n_mult = s1.toInt();
p2 = ligne_i.indexOf('_', p1+2); // le second
s2 = ligne_i.substring(p2+1, p2+3);
n_bouton = s2.toInt();
p3 = ligne_i.indexOf('<'); p4 = ligne_i.indexOf('>', p3);
label_i = ligne_i.substring(p3+1, p4);
if(n_mult==0) { libels_stations0[n_bouton] = label_i;}
if(n_mult==1) { libels_stations1[n_bouton] = label_i;}
if(n_mult==2) { libels_stations2[n_bouton] = label_i;}
if(n_mult==3) { libels_stations3[n_bouton] = label_i;}
// libels_stations[n_mult][n_bouton] = label_i;
}
}
void affi_page_info()
{
TFT.fillScreen(NOIR);
TFT.setTextColor(JAUNE, NOIR); TFT.setFreeFont(FF1);
uint16_t y=0;
TFT.drawString("Radio DAB+ Si4684 ", 0, y); y+=20;
String s1="version " + version;
TFT.drawString(s1, 0, y); y+=20;
TFT.setTextColor(CYAN, NOIR);
TFT.drawString("Silicium628", 0, y); y+=40;
}
void trace_sinusoide()
{
float y;
float v;
for (uint16_t n=0; n<320; n++)
{
v = (float)n/20.0;
y = 180-50*sin(v);
TFT.drawPixel(n, (int)y, VERT);
}
}
//rappel:
//void PANEL_BOUTONS::init( x_i, y_i, dxT_i, dyT_i, nb_boutons_i, String nom_i, String s_i[6], char sens_i, TFT_eSPI *TFTi)
void affichage_boutons_stations()
{
//panel_bt_stations.init(10, 3, 120, 220, 13,"Stations", libels_stations2, 'V', &TFT);
if(num_bt_M == 0) { panel_bt_stations.init(10, 10, 120, 213, 13,"Stations", libels_stations0, 'V', &TFT); }
if(num_bt_M == 1) { panel_bt_stations.init(10, 10, 120, 213, 13,"Stations", libels_stations1, 'V', &TFT); }
if(num_bt_M == 2) { panel_bt_stations.init(10, 10, 120, 213, 13,"Stations", libels_stations2, 'V', &TFT); }
if(num_bt_M == 3) { panel_bt_stations.init(10, 10, 120, 213, 13,"Stations", libels_stations3, 'V', &TFT); }
// panel_bt_stations.init(10, 10, 120, 213, 13,"Stations", libels_stations[num_bt_M], 'V', &TFT);
panel_bt_stations.efface(&TFT);
panel_bt_stations.affiche(&TFT);
}
void affichages_widgets_expert()
{
etiquette_rssi.init(130, 28, 45, 20, "", &TFT); // etiquette_rssi.init(130, 28, 45, 20, "rssi:", &TFT);
etiquette_rssi.affiche("0", 0, 1, &TFT);
etiquette_ID.init(180, 28, 48, 20, "", &TFT); // etiquette_ID.init(180, 28, 48, 20, "ID:", &TFT);
etiquette_ID.affiche("0", 0, 1, &TFT);
bt_mode_catalogue.init(270, 28, 35, 18, 3, GRIS_5);
bt_mode_catalogue.label =" CAT"; // goto catalogue
bt_mode_catalogue.affiche(BLANC, VERT, 1, &TFT);
affi_BMP_from_SD("/bmp565/stations2.bmp", 269, 24); // toute petite image du cataloque
TFT.drawRect(268, 24, 42, 23, BLANC);
bt_param.init(230, 28, 35, 18, 3, GRIS_5);
bt_param.label ="PARAM";
bt_param.affiche(BLANC, VERT, 1, &TFT);
bt_LOGO.init(150, 90, 140, 80, 1, BLEU);
//bt_LOGO.affiche(BLANC, VERT, 1, &TFT);
etiquette_LABEL1.init(130, 50, 180, 18, "", &TFT); // en haut
etiquette_LABEL1.affiche("-", 0, 1, &TFT);
String libels_mutiplex[13];
String s1;
s1 = " "; s1 += Table_Multiplex[n_mult_0].label; libels_mutiplex[0] = s1; // (lu sur la SDcard)
s1 = " "; s1 += Table_Multiplex[n_mult_1].label; libels_mutiplex[1] = s1;
s1 = " "; s1 += Table_Multiplex[n_mult_2].label; libels_mutiplex[2] = s1;
s1 = " "; s1 += Table_Multiplex[n_mult_3].label; libels_mutiplex[3] = s1;
panel_bt_multiplex.init(135, 5, 180, 20, 4,"", libels_mutiplex, 'H', &TFT);
panel_bt_multiplex.affiche(&TFT);
etiquette_Radiotext1.init(0, 215, 310, 18, "", &TFT);// 305
etiquette_Radiotext1.affiche("-", 0, 1, &TFT);
//affichage_boutons_stations();
page_stations.init(130, 73, 180, 140, GRIS_5, "", &TFT);
if (mode_general == MODE_PARAM)
{
page_stations.init(130, 73, 180, 140, BLEU, "", &TFT);
bt_to_SD.init(140, 110, 50, 20, 3, GRIS_5);
bt_to_SD.label =" -> SD";
bt_to_SD.affiche(BLANC, VERT, 1, &TFT);
bt_cal.init(140, 140, 50, 20, 3, GRIS_5);
bt_cal.label ="CALIB";
bt_cal.affiche(BLANC, VERT, 1, &TFT);
bt_SCAN_stations.init(140, 80, 50, 20, 3, GRIS_5);
bt_SCAN_stations.label = " SCAN";
bt_SCAN_stations.affiche(BLANC, VERT, 1, &TFT);
}
// TFT.drawRect(150, 90, 140, 80, BLANC); while(1); // pour test
}
void affichages_widgets_catalogue()
{
etiquette_ID.init(170, 197, 48, 20, "", &TFT);
etiquette_ID.affiche("0", 0, 1, &TFT);
etiquette_Radiotext2.init(170, 220, 150, 18, "", &TFT);
bt_img2.init(82, 175, 85, 65, 1, BLEU);
//bt_img2.affiche(BLANC, VERT, 1, &TFT); pour test positionnement
bt_mode_expert.init(270, 198, 48, 20, 3, GRIS_5);
bt_mode_expert.label =" EXPERT";
bt_mode_expert.affiche(BLANC, VERT, 1, &TFT); // seulement CE bouton
}
void encadre_bouton_catalogue(uint8_t n)
{
uint16_t x, y, dx, dy;
x = gridPad1.bt_grid[memo_n_bt_cat].x0;
y = gridPad1.bt_grid[memo_n_bt_cat].y0;
dx = gridPad1.bt_grid[memo_n_bt_cat].dx;
dy = gridPad1.bt_grid[memo_n_bt_cat].dy;
TFT.drawRect(x, y, dx, dy, NOIR); // efface
x = gridPad1.bt_grid[n].x0;
y = gridPad1.bt_grid[n].y0;
dx = gridPad1.bt_grid[n].dx;
dy = gridPad1.bt_grid[n].dy;
TFT.drawRect(x, y, dx, dy, BLANC);
memo_n_bt_cat=n;
}
String convert_Dec_To_Hex(uint32_t n)
{
String hexNum;
long reste;
while (n > 0)
{
reste = n % 16;
n = n / 16;
if (reste < 10)
{
hexNum = String(reste) + hexNum;
}
else
{
switch (reste)
{
case 10: hexNum = "A" + hexNum; break;
case 11: hexNum = "B" + hexNum; break;
case 12: hexNum = "C" + hexNum; break;
case 13: hexNum = "D" + hexNum; break;
case 14: hexNum = "E" + hexNum; break;
case 15: hexNum = "F" + hexNum; break;
}
}
}
return hexNum;
}
void send_multiplex_to_ESP_radio(uint8_t n_multiplex) // voir la fonction 'void reception()' dans le fichier 'main_RADIO.cpp'
{
Serial.println("send_multiplex_to_ESP_radio()");
Serial.print("multiplex="); Serial.println(n_multiplex);
String s1;
s1 += "[multiplex]<";
s1 += String(n_multiplex);
s1 +="> ";
Serial2.println(s1);
delay(100);
}
void send_station_to_ESP_radio(uint8_t n_station) // voir la fonction 'void reception()' dans le fichier 'main_RADIO.cpp'
{
Serial.println("send_station_to_ESP_radio()");
Serial.print("station="); Serial.println(n_station);
String s1;
s1 += "[station]<";
s1 += String(n_station);
s1 +=">";
Serial2.println(s1);
}
// Multiplex Montpellier étendu, Canal 11C - 220.352 MHz
void calcul_num_M(uint8_t n_bt_mlt)
{
delay(100);
switch (n_bt_mlt)
{
case 0: {num_M = n_mult_0; } break; // 9C n_mult_0 =18;
case 1: {num_M = n_mult_1; } break; // 10B n_mult_1 =21;
case 2: {num_M = n_mult_2; } break; // 10C n_mult_2 =22;
case 3: {num_M = n_mult_3; } break; // 11C n_mult_3 =26;
default: {;} break;
}
delay(100);
}
void calcul_num_S(uint8_t n_bt_sta)
{
num_S = n_bt_sta; // à ajuster le cas échéant...
}
// Fonction de "callback" obligatoire pour TJpg_Decoder
// Elle indique au décodeur comment pousser les blocs de pixels (MCU) sur l'écran TFT
bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap)
{
// Si le bloc sort de l'écran, on arrête
if (y >= TFT.height()) return false;
// Dessine le bloc de pixels converti à l'écran
TFT.pushImage(x, y, w, h, bitmap);
return true; // Retourne true pour dire au décodeur de continuer
}
/*
// Fonction récursive pour lister les fichiers et dossiers
void listDir(fs::FS &fs, const char * dirname, uint8_t levels)
{
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("Failed to open directory");
return;
}
if(!root.isDirectory())
{
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file)
{
if(file.isDirectory())
{
Serial.printf(" DIR : %s\n", file.name());
// Si levels > 0, on descend dans le sous-dossier
if(levels)
{
listDir(fs, file.path(), levels - 1);
}
}
else
{
Serial.printf(" FILE: %s SIZE: %d bytes\n", file.name(), file.size());
}
file = root.openNextFile();
}
}
*/
void show_boutons_selected()
{
panel_bt_multiplex.bt_P[btm].selected = 1;
panel_bt_multiplex.bt_P[btm].affiche(NOIR, VERT, 1, &TFT); // concerne 1 seul bouton
panel_bt_stations.bt_P[bts].selected = 1;
panel_bt_stations.bt_P[bts].affiche(NOIR, VERT, 1, &TFT); // concerne 1 seul bouton
}
void setup_catalogue() // suite du setup en mode 'catalogue'
{
affi_BMP_from_SD("/bmp565/stations.bmp", 0, 0); // image du catalogue (tous les 49 boutons)
//cette image a été constituée une fois pour toutes lors de la 1ere utilisation (voir plus haut)
gridPad1.init(0, 0); // boutons virtuels invisibles sous les imagettes du catalogue
TFT.fillRect(0, 175, 320, 65, GRIS_6);
TFT.drawRect(82, 175, 85, 65, GRIS_3); // cadre en trait blanc sous le catalogue, autour de l'imagette reçue
TFT.setTextColor(GRIS_3);
TFT.setFreeFont(0);
TFT.drawString("image recue", 90, 180);
etiquette_LABEL2.init(170, 174, 149, 22, "", &TFT); // en bas
TJpgDec.setJpgScale(4); // Échelle
TJpgDec.drawSdJpg(0, 178, "/slideshows/generiques/320x240/dab+.jpg"); // par défaut; (le "petit poste vert")
bt_capture_ecran.init(300, 220, 20, 20, 1, GRIS_5);
bt_capture_ecran.affiche(BLANC, VERT, 1, &TFT);
}
void setup_expert() // suite du setup en mode ''expert''
{
affichages_widgets_expert();
panel_bt_multiplex.affiche(&TFT);
affichage_boutons_stations();
// delay(10);
show_boutons_selected();
bt_capture_ecran.init(300, 220, 20, 20, 1, GRIS_5);
bt_capture_ecran.affiche(BLANC, VERT, 1, &TFT);
}
void setup()
{
// étalonnage touch screen; ces valeurs peuvent varier d'un afficheur à l'autre
eTS.x0 = -30; eTS.y0 = -30;
eTS.dx = 11; eTS.dy = 14;
Serial.begin(115200); // par l'USB
delay(20);
Serial2.setRxBufferSize(4096); // augmenté pour éviter de perdre des caractères pendant le transfert.
Serial2.begin(921600, SERIAL_8N1, 16, 17); // RX, TX // filaire vers l'autre ESP
// incomingBase64.reserve(20000); // 20000 Évite la fragmentation de la RAM
Wire.begin(GPIO_SDA, GPIO_SCL, 921600);
// Start the SPI for the touch screen and init the TS library
mySpi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
ts.begin(mySpi);
ts.setRotation(3);
// mySpi2 -> pour le lecteur de SDcard
// mySpi2 -> partage du bus SPI - mêmes valeurs de GPIO (sck=14, miso=12)
// que pour l'afficheur ILI9341 , sauf CS=5. Voir sur le schéma ainsi que le fichier User_Setup.h
mySpi2.begin(14, 12, 13, 5);
init_SDcard();
delay(20);
if (SDcardOk == true)
{
read_fichier_params_tactile(); // sur la SDcard
read_params_multiplex();
read_fichier_params_stations();
}
TFT.init();
TFT.setRotation(3); // 0..3 à voir, suivant disposition de l'afficheur
TFT.fillScreen(NOIR);
init_sprites();
affi_page_info();
delay(500);
TFT.fillScreen(NOIR);
TFT.drawRect(0, 0, 320, 240, BLANC); // cadre principal pourtour de l'écran
// si les coordonnées des points de calibrage sont aux fraises...
if ( (A.x <300) || (A.x >700) || (A.y <200) || (A.y >600) ||
(B.x < 3500) || (B.x > 3900) || (B.y <3400) || (A.y >3800) )
{
TS_calibrate();
}
TJpgDec.setSwapBytes(true);
TJpgDec.setCallback(tft_output);
mode_general = MODE_EXPERT;
//mode= MODE_CATALOGUE;
String s0 = "/slideshows/generiques/320x240/";
uint8_t n=0;
uint8_t n_mul=0;
uint8_t n_sta=0;
lst_sta[n].n_ml=1; lst_sta[n].n_st=1; lst_sta[n].s=s0+"F2F6_RTL2.jpg"; n++;
lst_sta[n].n_ml=1; lst_sta[n].n_st=2; lst_sta[n].s=s0+"F2F7_FUN RADIO.jpg"; n++;
lst_sta[n].n_ml=1; lst_sta[n].n_st=3; lst_sta[n].s=s0+"F2F8_NRJ.jpg"; n++;
lst_sta[n].n_ml=1; lst_sta[n].n_st=4; lst_sta[n].s=s0+"F2F9_CHERIE FM.jpg"; n++;
lst_sta[n].n_ml=1; lst_sta[n].n_st=6; lst_sta[n].s=s0+"F2FA_NOSTALGIE.jpg"; n++;
lst_sta[n].n_ml=2; lst_sta[n].n_st=2; lst_sta[n].s=s0+"F2FD_BFM RADIO.jpg"; n++;
lst_sta[n].n_ml=1; lst_sta[n].n_st=9; lst_sta[n].s=s0+"F2FE_AIRZEN RADIO.jpg"; n++;
lst_sta[n].n_ml=3; lst_sta[n].n_st=9; lst_sta[n].s=s0+"F6D5_RADIO LANGA DOC.jpg"; n++;
lst_sta[n].n_ml=3; lst_sta[n].n_st=3; lst_sta[n].s=s0+"F6FD_100p100.jpg"; n++;
lst_sta[n].n_ml=3; lst_sta[n].n_st=4; lst_sta[n].s=s0+"F9F1_FUEGO.jpg"; n++;
lst_sta[n].n_ml=3; lst_sta[n].n_st=10; lst_sta[n].s=s0+"F9F5_MELODY.jpg"; n++;
lst_sta[n].n_ml=3; lst_sta[n].n_st=0; lst_sta[n].s=s0+"F20A_ICI HERAULT.jpg"; n++;
lst_sta[n].n_ml=2; lst_sta[n].n_st=4; lst_sta[n].s=s0+"F20D_RFM.jpg"; n++;
lst_sta[n].n_ml=2; lst_sta[n].n_st=5; lst_sta[n].s=s0+"F20E_Europe 2.jpg"; n++;
lst_sta[n].n_ml=3; lst_sta[n].n_st=2; lst_sta[n].s=s0+"F21B_ado.jpg"; n++;
lst_sta[n].n_ml=1; lst_sta[n].n_st=11; lst_sta[n].s=s0+"F21C_LATINA.jpg"; n++;
lst_sta[n].n_ml=3; lst_sta[n].n_st=5; lst_sta[n].s=s0+"F21D_Oui FM.jpg"; n++;
lst_sta[n].n_ml=3; lst_sta[n].n_st=8; lst_sta[n].s=s0+"F22A_RADIO NOVA.jpg"; n++;
lst_sta[n].n_ml=3; lst_sta[n].n_st=6; lst_sta[n].s=s0+"F22F_JAZZ RADIO.jpg"; n++;
lst_sta[n].n_ml=1; lst_sta[n].n_st=8; lst_sta[n].s=s0+"F24E_NOSTALGIE 60 70.jpg"; n++;
lst_sta[n].n_ml=2; lst_sta[n].n_st=11; lst_sta[n].s=s0+"F24F_MON PETIT INTER.jpg"; n++;
lst_sta[n].n_ml=2; lst_sta[n].n_st=7; lst_sta[n].s=s0+"F201_FRANCE INTER.jpg"; n++;
lst_sta[n].n_ml=2; lst_sta[n].n_st=9; lst_sta[n].s=s0+"F202_FRANCE_CULTURE.jpg"; n++;
lst_sta[n].n_ml=2; lst_sta[n].n_st=10; lst_sta[n].s=s0+"F203_FRANCE MUSIQUE.jpg"; n++;
lst_sta[n].n_ml=2; lst_sta[n].n_st=8; lst_sta[n].s=s0+"F204_FIP.jpg"; n++;
lst_sta[n].n_ml=2; lst_sta[n].n_st=6; lst_sta[n].s=s0+"F206_FRANCE INFO.jpg"; n++;
lst_sta[n].n_ml=0; lst_sta[n].n_st=0; lst_sta[n].s=s0+"F211_RTL.jpg"; n++;
lst_sta[n].n_ml=3; lst_sta[n].n_st=3; lst_sta[n].s=s0+"F213_EUROPE 1.jpg"; n++;
lst_sta[n].n_ml=1; lst_sta[n].n_st=7; lst_sta[n].s=s0+"F214_SKYROCK.jpg"; n++;
lst_sta[n].n_ml=1; lst_sta[n].n_st=10; lst_sta[n].s=s0+"F221_RADIO CLASSIQUE.jpg"; n++;
lst_sta[n].n_ml=1; lst_sta[n].n_st=12; lst_sta[n].s=s0+"F225_M RADIO.jpg"; n++;
lst_sta[n].n_ml=1; lst_sta[n].n_st=5; lst_sta[n].s=s0+"F226_RIRE et CHANSONS.jpg"; n++;
lst_sta[n].n_ml=2; lst_sta[n].n_st=1; lst_sta[n].s=s0+"F227_BFM BUSINESS.jpg"; n++;
lst_sta[n].n_ml=3; lst_sta[n].n_st=11; lst_sta[n].s=s0+"F650_SUD RADIO.jpg"; n++;
lst_sta[n].n_ml=2; lst_sta[n].n_st=0; lst_sta[n].s=s0+"F216_RMC.jpg"; n++;
//------------------------------------------------------------------------------------------------
if(0) // 1 pour la première utilisation, 0 ensuite
{
// on va afficher toutes les petites vignettes pour constituer un tableau visuel
// ensuite on fait une capture d'écran pour sauvegarder ce visuel sur la SDcard
// Le but est d'obtenir par la suite un affichage beaucoup plus rapide de cette grande image unique
uint8_t num=0;
uint8_t n_max=34;
for(uint8_t ny=0; ny<7; ny++)
{
for(uint8_t nx=0; nx<7; nx++)
{
bt_img1.init(4+45*nx, 4+34*ny, 8);
//bt_img1.img_path = "/slideshows/generiques/40x30/F2F8_NRJ.jpg"; // sur SDcard
bt_img1.img_path = lst_sta[num].s; // sur SDcard
bt_img1.affiche();
num++;
if(num>34) break;
}
if(num>34) break;
}
write_TFT_on_SDcard("/bmp565/stations.bmp");
TFT.fillScreen(0);
TFT.setFreeFont(FF0);
TFT.drawString("Capture ecran sauvee sur SDcard", 0, 0);
TFT.drawString("/bmp565/stations.bmp", 0, 20);
delay(4000);
}
//------------------------------------------------------------------------------------------------
//affi_BMP_from_SD("/bmp565/2.bmp", 0, 0);
//delay(1000);
TFT.fillScreen(couleur_fond_ecran);
TJpgDec.setJpgScale(2); // Échelle : 1 (taille réelle), 2 (moitié), 4 (quart), 8 (huitième)
TJpgDec.setSwapBytes(true); // INDISPENSABLE pour que les couleurs RGB565 soient justes sur ILI9341
TJpgDec.setCallback(tft_output); // Set the sketch callback function to render decoded blocks
bt_stop_tout_ecran.init(280, 2, 30, 30, 1, GRIS_6);
bt_stop_tout_ecran.label ="X";
// bt_stop_tout_ecran.selected=true; bt_stop_tout_ecran.affiche(VERT, VERT, 1, &TFT); while(1); // POUR TEST
num_bt_M = btm;
affichage_boutons_stations(); // concerne tous les boutons; les libellé sont définits
// voir le fonction (read_fichier_params_stations() // lecture du fichier '/params_stations.txt' en SD )
if (mode_general == MODE_CATALOGUE)
{
setup_catalogue();
affichages_widgets_catalogue();
}
if(mode_general == MODE_EXPERT)
{
setup_expert();
execute_bouton_multiplex(2); // 10C
execute_bouton_station(10); // FRANCE MUSIQUE
show_boutons_selected();
// ne pas envoyer ce qui suit dans la fonction 'setup_expert()' sinon en renvoit la config à la radio inutilement
/* calcul_num_M(btm);
send_multiplex_to_ESP_radio(num_M);
choix_mutiplex_ok = true;
delay(10);
calcul_num_S(bts);
send_station_to_ESP_radio(num_S);
*/
}
}
void get_XY_touch() // pour affi 320 x 240px
{
TS_Point p = ts.getPoint();
float dx = B.x - A.x;
float ech_x = 290.0 / dx;//300
float x0 = A.x;
float dy = B.y - A.y;
float ech_y = 210.0 / dy; //230
float y0 = A.y;
memo_x_touch = x_touch;
memo_y_touch = y_touch;
x_touch = 12 + uint16_t( (p.x - x0) * ech_x); //x_touch = 10 + uint16_t( (p.x - x0) * ech_x);
if(x_touch > 319){x_touch = 0;}
y_touch = 15 + uint16_t( (p.y - y0) * ech_y); //y_touch = 10 + uint16_t( (p.y - y0) * ech_y);
if(y_touch > 239){y_touch = 0;}
}
void affiche_page_stations()
{
page_stations.efface(&TFT);
String s1, s2, s3, s4;
for(int n=0; n<13; n++)
{
s1 = String(n);
if (n<10) {s1 = "0" + s1;}
uint32_t compid = LST_services[n].CompID;
s2 = convert_Dec_To_Hex(compid);
s2 = s2.substring(s2.length() - 1);
uint32_t servid = LST_services[n].ServiceID;
s3 = convert_Dec_To_Hex(servid);
s4 = String(LST_services[n].Label);
page_stations.set_texte(s1 + " " + s2 + " " + s3 + " " + s4, n);
}
page_stations.affiche(1, &TFT);
}
/*
// par IA
void afficherImageBase64(const String& base64Data, int posX = 0, int posY = 0)
{
Serial.println("void afficherImageBase64()");
size_t inputLength = base64Data.length();
size_t maxOutputLength = (inputLength * 3) / 4 + 4;
uint8_t* jpgBuffer = (uint8_t*)malloc(maxOutputLength);
if (jpgBuffer == NULL)
{
Serial.println("Erreur : Memoire RAM insuffisante");
return;
}
size_t actualJpgLength = 0;
// Décodage Base64 -> Binaire JPG
int result = mbedtls_base64_decode(
jpgBuffer,
maxOutputLength,
&actualJpgLength,
(const unsigned char*)base64Data.c_str(),
inputLength
);
if (result == 0)
{
// Dessine l'image aux coordonnées (posX, posY) directement depuis la RAM
JRESULT res = TJpgDec.drawJpg(posX, posY, jpgBuffer, actualJpgLength);
if (res != JDR_OK) { Serial.printf("Erreur decodage JPG : %d\n", res); }
}
else { Serial.printf("Erreur Base64 : -0x%04X\n", -result); }
free(jpgBuffer); // Libération de la mémoire
}
*/
// par IA
void processBase64Image(const String& b64Data) // recu de l'ESP_radio
{
size_t inputLen = b64Data.length();
size_t outputLen = 0;
// Estimation de la taille max du JPG (3 octets décodés pour 4 octets Base64)
size_t maxOutputLen = (inputLen * 3) / 4 + 2;
uint8_t* jpgBuffer = (uint8_t*)malloc(maxOutputLen); // permet de réserver de la mémoire dynamiquement sur le «tas»
if (jpgBuffer == NULL) { Serial.println("Erreur : RAM insuffisante pour le decodage."); return; }
// Décodage mbedtls
int result = mbedtls_base64_decode(
currentSlideBuffer, //jpgBuffer,
maxOutputLen,
&outputLen,
(const unsigned char*)b64Data.c_str(),
inputLen
);
if (result == 0)
{
logo_courant = true; // pour éviter d'écraser (plus tard) le logo courant avec un logo générique
Serial.printf("Decodage OK. Affi JPG (%d octets)...\n", outputLen);
if(mode_general != MODE_PARAM) // pour ne pas écraser les boutons du mode paramétrage
{
if(mode_general == MODE_EXPERT)
{
TFT.fillRect(131, 75, 178, 136, NOIR);
TFT.startWrite();
TJpgDec.drawJpg(131, 77, currentSlideBuffer, outputLen); // AFFICHE LE LOGO RECU
currentSlideSize = outputLen;
//affi_jpg_from_RAM(131, 75, outputLen);
}
if(mode_general == MODE_CATALOGUE)
{
TFT.startWrite();
TJpgDec.drawJpg(84, 178, currentSlideBuffer, outputLen);
}
TFT.endWrite();
}
// write_image_jpg_to_SD(currentSlideBuffer); // copie sur la SDcard (ajout)
}
receivingSLS = false; // permet la gestion des boutons et la réception rssi, label, texte...
}
void affi_RadioText()
{
if(mode_general == MODE_EXPERT)
{
etiquette_Radiotext1.efface(&TFT);
etiquette_Radiotext1.couleur_txt = CYAN;
String s1 = RadioText.substring(0,51);
etiquette_Radiotext1.affiche(s1, 0, 0, &TFT);
}
if(mode_general == MODE_CATALOGUE)
{
etiquette_Radiotext2.efface(&TFT);
etiquette_Radiotext2.couleur_txt = CYAN;
//String s1 = RadioText.substring(0,24);
etiquette_Radiotext2.affiche(RadioText,decal_tx, 0, &TFT);
bt_capture_ecran.affiche(BLANC, VERT, 1, &TFT);
}
}
void show_btm_bts() // tout en haut, à gauche, pour test...
{
TFT.setFreeFont(FF0);
TFT.setTextColor(JAUNE, NOIR);
String s1;
s1 = String(btm) +" "; TFT.drawString(s1, 20, 0);
s1 = String(bts) +" "; TFT.drawString(s1, 40, 0);
}
void toutes_les_500ms()
{
if (rxState == RECEIVING_DATA) {return;}
if((mode_general == MODE_CATALOGUE) && (mode_affi == MODE_PETITE))
{
TFT.setFreeFont(0);
TFT.setTextColor(JAUNE);
TFT.setTextDatum(0); // left
TFT.drawString("C", 0, 0);
if(RadioText.length() > 24)
{
decal_tx++;
if(decal_tx > 20)
{
decal_tx =0;
delay(300);
}
}
affi_RadioText();
}
// if(mode_general == MODE_EXPERT)
// {
// TFT.setTextDatum(0); // left
// TFT.drawString("E", 0, 0);
// }
// show_btm_bts(); // pour test
}
void RECEPTION()
{
// ****************************** RECEPTION ********************
if(Serial2.available())
{
uint16_t valeur1;
String s_in, s1, s2;
int p1, p2, p3;
s_in = Serial2.readStringUntil('\n');
Serial.print("s_in= "); Serial.println(s_in);
// Lecture ligne par ligne pour intercepter les commandes STARTSLS et ENDSLS
//String ligne = Serial2.readStringUntil('\n');
//s1.trim(); // Nettoie les \r ou espaces invisibles
if (rxState == WAIT_START)
{
if (s_in.startsWith("STARTSLS"))
{
// RECEPTION SLIDESHOW
Serial.println("-----------------------");
receivingSLS = true; // bloque les autres actions pendant la réception de l'image "SlideShow"
TFT.fillCircle(302, 80, 5, ROUGE);
// Extraction de la taille numérique collée après STARTSLS
String lenStr = s_in.substring(8);
Serial.print("lenStr= "); Serial.println(lenStr);
expectedLen = lenStr.toInt();
base64Buffer = "";
if (expectedLen > 0)
{
base64Buffer.reserve(expectedLen + 10); // Prévient la fragmentation RAM
}
rxState = RECEIVING_DATA;
Serial.printf("Debut reception SLS (Taille attendue : %d)\n", expectedLen);
}
}
else if (rxState == RECEIVING_DATA)
{
if (s_in.startsWith("ENDSLS"))
{
// FIN de la réception de l'image (après plusieurs passages de réception (ci-dessus) de tous les paquets)
rxState = WAIT_START;
if (base64Buffer.length() == expectedLen) // Validation stricte de l'intégrité avant décodage
{
TFT.fillCircle(302, 80, 5, ORANGE);
Serial.println("Donnees completes. Traitement...");
processBase64Image(base64Buffer); // et affichage
delay(50);
TFT.fillCircle(302, 80, 5, VERT);
TFT.drawCircle(302, 80, 5, GRIS_3);
}
else { Serial.printf("Erreur transfert : Recu %d octets sur %d attendus.\n", base64Buffer.length(), expectedLen);}
}
else
{
base64Buffer += s_in; // Accumulation des blocs Base64 reçus
}
}
if ((receivingSLS == false) && (mode_affi == MODE_PETITE))
{
p1 = s_in.indexOf("label");
if(p1 != -1)
{
s1 = extract_texte(s_in, "label");
s1 = s1.substring(0, 16); // pour ne pas dépasser du cadre
if(mode_general == MODE_EXPERT)
{
etiquette_LABEL1.efface(&TFT);
etiquette_LABEL1.couleur_txt = JAUNE;
etiquette_LABEL1.affiche(s1, 0, 1, &TFT);
}
if(mode_general == MODE_CATALOGUE)
{
etiquette_LABEL2.efface(&TFT);
etiquette_LABEL2.couleur_txt = JAUNE;
etiquette_LABEL2.affiche(s1, 0, 1, &TFT);
}
}
p1 = s_in.indexOf("LISTING");
if(p1 != -1)
{
//DABService LST_services_recu[16];
Serial2.readBytes(reinterpret_cast<uint8_t*>(&LST_services), sizeof(LST_services));
for(int n=0; n<13; n++)
{
Serial.println(n);
uint32_t compid = LST_services[n].CompID;
//Serial.print("CompID= "); Serial.println(convert_Dec_To_Hex(compid));
uint32_t servid = LST_services[n].ServiceID;
s1 = convert_Dec_To_Hex(servid);
s2 = String(LST_services[n].Label);
s2 = s2.substring(0, 15);
Serial.print("label= "); Serial.println(s2);
if(num_bt_M == 0) { libels_stations0[n] = s1 +'=' +s2;}
if(num_bt_M == 1) { libels_stations1[n] = s1 +'=' +s2;}
if(num_bt_M == 2) { libels_stations2[n] = s1 +'=' +s2;}
if(num_bt_M == 3) { libels_stations3[n] = s1 +'=' +s2;}
//libels_stations[num_bt_M] [n]= s1 +'=' +s2;
}
choix_mutiplex_ok = true;
affiche_page_stations();
affichage_boutons_stations();
}
p1 = s_in.indexOf("rssi");
if(p1 != -1)
{
if(mode_general == MODE_EXPERT)
{
s1 = extract_texte(s_in, "rssi");
etiquette_rssi.efface(&TFT);
etiquette_rssi.couleur_txt = BLEU_CLAIR;
etiquette_rssi.affiche(s1, 0, 1, &TFT);
}
}
p1 = s_in.indexOf("serviceID"); // "F203"
if(p1 != -1)
{
s1 = extract_texte(s_in, "serviceID");
etiquette_ID.efface(&TFT);
if(s1[0] == 'F')
{
s1=s1.substring(0, 4); // au cas où le texte reçu serait >4 caractères
if ((s1=="F20C") || (s1=="F20F")||(s1=="F200")) // pas de radios religieuses ici !
{
btm = 2; // 10C
bts = 10; // FRANCE MUSIQUE
execute_bouton_station(bts);
}
etiquette_ID.couleur_txt = VERT;
etiquette_ID.affiche(s1, 0, 1, &TFT);
}
}
// texte long, dit 'RadioText' ou 'serviceData'
p1 = s_in.indexOf("serviceData");
if(p1 != -1)
{
String rt1 = extract_texte(s_in, "serviceData");
//RadioText = rt1;
RadioText = desaccentue(rt1) ;
decal_tx =0;
affi_RadioText();
}
p1 = s_in.indexOf("<ACQ>"); // Acquiescement reçu du module radio
if(p1 != -1)
{
page_stations.efface(&TFT);
page_stations.set_texte("Scan OK", 2);
page_stations.set_texte("Affichage listing...", 3);
page_stations.affiche(1, &TFT);
// on demande l'affichage du résultat (qui figure dans l'ESP_radio)
String s1;
s1 += "[bt_affi_LST]<";
s1 += "1";
s1 +=">";
Serial2.println(s1);
}
}
}
}
void execute_bouton_multiplex(uint8_t n)
{
num_bt_M = n;
memo_logo_generique_en_cours="";
logo_courant = false; // pour afficher le logo générique dès qu'on change de station
page_stations.efface(&TFT);
//send_num_M_to_ESP_radio(26); // 18 - 21 - 22 - 26
etiquette_ID.efface(&TFT);
etiquette_LABEL1.efface(&TFT);
affichage_boutons_stations();
calcul_num_M(num_bt_M);
send_multiplex_to_ESP_radio(num_M);
delay(10);
calcul_num_S(num_bt_S);
send_station_to_ESP_radio(num_S);
delay(10);
choix_mutiplex_ok = true;
btm = num_bt_M;
}
void execute_bouton_station(uint8_t n)
{
num_bt_S = n;
//bts = num_bt_S;
memo_logo_generique_en_cours="";
//logo_courant = false; // pour afficher le logo générique
etiquette_ID.efface(&TFT);
etiquette_LABEL1.efface(&TFT);
String s1;
if(num_bt_M==0) {s1= libels_stations0[num_bt_S];}
if(num_bt_M==1) {s1= libels_stations1[num_bt_S];}
if(num_bt_M==2) {s1= libels_stations2[num_bt_S];}
if(num_bt_M==3) {s1= libels_stations3[num_bt_S];}
//s1= libels_stations[num_bt_M][num_bt_S];
s1=s1.substring(0, 4);
if ((s1=="") || (s1=="F20C") || (s1=="F20F")){return; } //pas de radios confessionnelles sur ce récepteur !
etiquette_ID.couleur_txt = VERT;
etiquette_ID.affiche(s1, 0, 1, &TFT); // très rapidement dès le clic sur 1 bouton station
// if (logo_courant == false) // alors on affiche le logo générique de la station
{
// rappel : absolute_path(SD, "/", "ABCD") cherche, ouvre et lit le premier fichier commençant par "ABCD" à la racine "/"
logo_en_cours = absolute_path(SD, "/slideshows/generiques/320x240", s1);
if (logo_en_cours != memo_logo_en_cours) // évite les réaffichages intempestifs du même logo
{
memo_logo_en_cours = logo_en_cours;
// Serial.print("generique= "); Serial.println(s2); // = "/slideshows/generiques/F2FA_NOSTALGIE.jpg"
affi_jpg_from_SD(logo_en_cours, 131, 75);
// copie en RAM de cette image pour pouvoir l'afficher ultérieurement en différents formats
// remarque: la réception (radio) d'une image actualisée écrasera cette image RAM
copie_jpg_from_SD_to_RAM(logo_en_cours);
}
}
calcul_num_M(num_bt_M);
send_multiplex_to_ESP_radio(num_M);
delay(10);
calcul_num_S(num_bt_S);
send_station_to_ESP_radio(num_S);
delay(10);
//affichage_boutons_stations();
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 500)
{
previousMillis = currentMillis;
if(receivingSLS == false)
{
toutes_les_500ms();
}
}
RECEPTION(); //de l'ESP32_RADIO par la liaison Serial2 // ******* commenter pour TEST *******
//---------------------------------------------------------------------------------------------
// ***** TRAITEMENT DES BOUTONS VIRTUELS LOCAUX ********
//---------------------------------------------------------------------------------------------
if (ts.tirqTouched() && ts.touched())
{
memo_y_touch = y_touch;
get_XY_touch();
// on invalide les boutons dans les modes LOGO plein écran et catalogue
if((mode_affi != MODE_TOUT_ECRAN) && (mode_general != MODE_CATALOGUE))
{
// BOUTONS MULTIPLEX
uint8_t memo_num_bt_M = num_bt_M;
num_bt_M = panel_bt_multiplex.traite_boutons(x_touch, y_touch, 4, &TFT);
if (num_bt_M != 255)
{
execute_bouton_multiplex(num_bt_M);
}
else {num_bt_M = memo_num_bt_M;} // on reprend la valeur précédente, sinon on la perd à chaque passage
// BOUTONS STATION
uint8_t memo_num_bt_S = num_bt_S;
num_bt_S = panel_bt_stations.traite_boutons(x_touch, y_touch, 13, &TFT);
if (num_bt_S != 255)
{
if(choix_mutiplex_ok == true)
{
execute_bouton_station(num_bt_S);
}
else
{
page_stations.efface(&TFT);
page_stations.set_texte("CHOISIR UN MULTIPLEX !", 1);
page_stations.affiche(1, &TFT);
}
}
else {num_bt_S = memo_num_bt_S;} // on reprend la valeur précédente
// BOUTON SCAN
bt_SCAN_stations.test_clic(x_touch, y_touch);
if(bt_SCAN_stations.cliked == true)
{
page_stations.efface(&TFT);
page_stations.set_texte("scan en cours...", 4);
page_stations.affiche(1, &TFT);
bt_SCAN_stations.cliked = false;
bt_SCAN_stations.selected = 1; bt_SCAN_stations.affiche(BLANC, VERT, 1, &TFT); delay(100);
bt_SCAN_stations.selected = 0; bt_SCAN_stations.affiche(BLANC, VERT, 1, &TFT); delay(100);
String s1;
s1 += "[btLST]<";
s1 += "1";
s1 +=">";
Serial2.println(s1);
}
// BOUTON PARAM
bt_param.test_clic(x_touch, y_touch);
if(bt_param.cliked == true)
{
bt_param.cliked = false;
page_stations.efface(&TFT);
//page_stations.set_texte("", 2);
//page_stations.affiche(1, &TFT);
if (mode_general == MODE_EXPERT)
{
mode_general = MODE_PARAM;
bt_param.selected = 1;
bt_param.affiche(BLANC, ROUGE, 1, &TFT);
}
else
{
mode_general = MODE_EXPERT;
bt_param.selected = 1;
bt_param.affiche(NOIR, VERT, 1, &TFT);
}
affichages_widgets_expert();
delay(500);
}
// BOUTON CAT (pour basculer en mode catalogue)
bt_mode_catalogue.test_clic(x_touch, y_touch);
if(bt_mode_catalogue.cliked == true)
{
bt_mode_catalogue.cliked = false;
//bt_mode_catalogue.selected = 1;
//bt_mode_catalogue.affiche(BLANC, VERT, 1, &TFT);
//delay(300);
mode_general = MODE_CATALOGUE;
TFT.fillScreen(NOIR);
setup_catalogue();
affichages_widgets_catalogue();
delay(500);
}
// BOUTON CAL (calibrage du tactile)
if(mode_general == MODE_PARAM)
{
bt_cal.test_clic(x_touch, y_touch); // calibrage du tactile
if(bt_cal.cliked == true)
{
bt_cal.cliked = false;
TS_calibrate();
setup();
}
bt_to_SD.test_clic(x_touch, y_touch);
if(bt_to_SD.cliked == true)
{
bt_to_SD.cliked = false;
bt_to_SD.selected = 1; bt_to_SD.affiche(BLANC, VERT, 1, &TFT); delay(100);
bt_to_SD.selected = 0; bt_to_SD.affiche(BLANC, VERT, 1, &TFT); delay(100);
write_params_stations_SD(0,libels_stations0); // écriture sur la SDcard
write_params_stations_SD(1,libels_stations1);
write_params_stations_SD(2,libels_stations2);
write_params_stations_SD(3,libels_stations3);
//write_params_stations_SD(0,libels_stations[0]); // écriture sur la SDcard
//write_params_stations_SD(1,libels_stations[1]);
//write_params_stations_SD(2,libels_stations[2]);
//write_params_stations_SD(3,libels_stations[3]);
page_stations.set_texte("Params saved OK", 5);
page_stations.affiche(1, &TFT);
}
}
}
// -----------------------------------------------------------------------------------------
// ce qui suit reste en fonction même en mode LOGO tout-écran, sauf en MODE_PARAM
bt_capture_ecran.test_clic(x_touch, y_touch); // calibrage du tactile
if(bt_capture_ecran.cliked == true)
{
bt_capture_ecran.cliked = false;
String filename;
boolean next = true;
uint16_t n = 0;
while(next && (n < 1000) )
{
filename = "/bmp565/captures/" + String(n) + ".bmp";
if (! SD.exists(filename))
{
write_TFT_on_SDcard(filename);
TFT.drawString("Capture ecran to SDcard OK", 0, 0);
TFT.drawString(filename, 0, 20);
next = false;
}
n++;
}
}
if(mode_general == MODE_CATALOGUE)
{
uint8_t num_bouton = gridPad1.test_clic(x_touch, y_touch);
if (num_bouton <= 35) // 7x5
{
// CLIC sur un "bouton" du catalogue
encadre_bouton_catalogue(num_bouton);
RadioText = "";
decal_tx =0;
TFT.fillRect(82, 175, 85, 65, NOIR); // efface imagette reçue
TFT.drawRect(82, 175, 85, 65, GRIS_3); // retrace le cadre
TFT.setTextColor(GRIS_3);
TFT.setFreeFont(0);
TFT.drawString("image recue", 90, 180);
String s1 = String(num_bouton);
if (num_bouton<10) s1 = " " +s1;
TFT.setTextColor(JAUNE, NOIR);
//TFT.drawString(s1, 100, 100);
String img_path = lst_sta[num_bouton-1].s;
String s2 = img_path.substring(36, 55);
String aSupprimer = ".jpg";
int index = s2.indexOf(aSupprimer);
if (index != -1)
{ // Vérifie si le texte est présent
s2.remove(index, aSupprimer.length());
}
s2 = s2.substring(0, 15);
TJpgDec.setJpgScale(4); // Échelle
TJpgDec.drawSdJpg(0, 178, img_path); // lu sur la SDcard
etiquette_ID.efface(&TFT);
etiquette_LABEL2.couleur_txt = JAUNE;
etiquette_LABEL2.efface(&TFT);
etiquette_LABEL2.affiche(s2, 0, 1, &TFT);
//num_M = lst_sta[num_bouton-1].n_ml;
calcul_num_M(lst_sta[num_bouton-1].n_ml);
send_multiplex_to_ESP_radio(num_M);
send_multiplex_to_ESP_radio(num_M); //(oui deux fois ! sinon foire)
delay(100);
calcul_num_S(lst_sta[num_bouton-1].n_st);
send_station_to_ESP_radio(num_S);
send_station_to_ESP_radio(num_S); //(oui deux fois ! sinon foire)
//delay(100);
}
// CLIC sur l'imagette au bas du mode catalogue
bt_img2.test_clic(x_touch, y_touch);
if(bt_img2.cliked == true)
{
bt_img2.cliked = false;
affi_jpg_from_RAM(0, 0, 1); // grande
mode_affi = MODE_TOUT_ECRAN;
bt_stop_tout_ecran.affiche(BLANC, GRIS_6, 2, &TFT);
}
// CLIC sur le bouton pour basculer en mode 'expert'
bt_mode_expert.test_clic(x_touch, y_touch);
if(bt_mode_expert.cliked == true)
{
bt_mode_expert.cliked = false;
// juste un flash
bt_mode_expert.selected = 1; bt_mode_expert.affiche(BLANC, VERT, 1, &TFT); delay(100);
bt_mode_expert.selected = 0; bt_mode_expert.affiche(BLANC, VERT, 1, &TFT); delay(100);
mode_general = MODE_EXPERT;
TFT.fillScreen(NOIR);
setup_expert();
affichages_widgets_expert();
}
}
if((mode_general != MODE_PARAM) && (mode_general != MODE_CATALOGUE))// on invalide le LOGO tout-écran, dans le MODE_PARAM
{
// BOUTON LOGO (bouton caché sous l'imagette : bascule en tout-écran)
bt_LOGO.test_clic(x_touch, y_touch);
if(bt_LOGO.cliked == true)
{
if(mode_affi == MODE_PETITE)
{
mode_affi = MODE_TOUT_ECRAN;
bt_LOGO.cliked = false;
TFT.fillScreen(GRIS_1);
TJpgDec.setJpgScale(1); // grand
TJpgDec.setCallback(tft_output);
TJpgDec.drawJpg(0, 0, currentSlideBuffer, currentSlideSize); // affiche l'image stockée en RAM
bt_stop_tout_ecran.affiche(BLANC, GRIS_6, 2, &TFT);
}
delay(300);
}
}
// CLIC sur le petit bouton de retour ('x' en haut à droite)
if (mode_affi == MODE_TOUT_ECRAN)
{
bt_stop_tout_ecran.test_clic(x_touch, y_touch);
if(bt_stop_tout_ecran.cliked == true)
{
bt_stop_tout_ecran.cliked = false;
TFT.fillScreen(NOIR);
delay(100);
mode_affi = MODE_PETITE;
if(mode_general == MODE_EXPERT)
{
setup_expert();
affichages_widgets_expert();
show_boutons_selected();
affi_jpg_from_RAM(131, 77, 2); // petite
}
if(mode_general == MODE_CATALOGUE)
{
setup_catalogue();
affichages_widgets_catalogue();
affi_jpg_from_RAM(84, 178, 4); // petite
}
}
}
}
}