1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
// EMETTEUR uPesy ESP32 Wroom DevKit
#include <SPI.h>
#include <TFT_eSPI.h>
#include <XPT2046_Touchscreen.h> // https : //github. com/PaulStoffregen/XPT2046_Touchscreen
#include <esp_now.h>
#include <WiFi.h>
TFT_eSPI tft = TFT_eSPI();
// Touchscreen pins
#define XPT2046_IRQ 36 // T_IRQ
#define XPT2046_MOSI 32 // T_DIN
#define XPT2046_MISO 39 // T_OUT
#define XPT2046_CLK 25 // T_CLK
#define XPT2046_CS 33 // T_CS
SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define FONT_SIZE 2
// definition des boutons
#define BUTTON_WIDTH 80
#define BUTTON_HEIGHT 60
#define BUTTON_MARGIN 10
// Structure pour représenter un bouton
struct Button {
int x, y;
bool state;
};
Button buttons[9]; // 10 boutons
// Coordonnées de l'écran tactile : (x, y) et pression (z)
int x, y, z;
// Imprimer les informations de l'écran tactile concernant X, Y et la pression (Z) sur le moniteur série
void printTouchToSerial(int touchX, int touchY, int touchZ) {
Serial.print("X = ");
Serial.print(touchX);
Serial.print(" | Y = ");
Serial.print(touchY);
Serial.print(" | Pressure = ");
Serial.print(touchZ);
Serial.println();
}
/******************************************************/
//******************** Adresse Mac PC01
uint8_t broadcastAddress1[] = {0xb4, 0xe6, 0x2d, 0x78, 0x83, 0x54};
// ************** la structure du récepteur
typedef struct struct_message {
char a[32];
bool b;
} struct_message;
// Create a struct_message called myData
struct_message myData;
esp_now_peer_info_t peerInfo;
// *****************************rappel lorsque les données sont envoyées
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nÉtat de l'envoi du dernier paquet:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Succès" : "Échec");
}
int touchX, touchY, touchZ , i;
//************************************************
// SETUP
//************************************************
void setup()
{
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Erreur d'initialisation de l'ESP-NOW");
return;
}
// Une fois que ESPNow est Init avec succès, nous nous inscrirons pour l'envoi du CB .
// obtenir l'état du paquet transmis.
esp_now_register_send_cb(OnDataSent);
// Enregistrer un pair
memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Ajouter un pair
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Échec de l'ajout d'un pair");
return;
}
// Start the SPI for the touchscreen and init the touchscreen
touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
touchscreen.begin(touchscreenSPI);
// Set the Touchscreen rotation in landscape mode
// Note: in some displays, the touchscreen might be upside down, so you might need to set the rotation to 3: touchscreen.setRotation(3);
touchscreen.setRotation(3);
// Start the tft display
tft.init();
// Set the TFT display rotation in landscape mode
tft.setRotation(1);
// Clear the screen before writing to it
tft.fillScreen(TFT_BLACK);
// Initialisation des positions des boutons
int buttonX = 20;
int buttonY = 20;
for (int i = 0; i < 9; ++i)
{
buttons[i].x = buttonX;
buttons[i].y = buttonY;
buttons[i].state = false;
// Affichage des boutons
drawButton(buttonX, buttonY, buttons[i].state);
buttonX += BUTTON_WIDTH + BUTTON_MARGIN;
if (buttonX + BUTTON_WIDTH > tft.width()) {
buttonX = 20;
buttonY += BUTTON_HEIGHT + BUTTON_MARGIN;
}
}
}
//************************************************
// LOOP
//************************************************
void loop()
{
// Checks if Touchscreen was touched, and prints X, Y and Pressure (Z) info on the TFT display and Serial Monitor
if (touchscreen.tirqTouched() && touchscreen.touched())
{
// Get Touchscreen points
TS_Point p = touchscreen.getPoint();
// Calibrate Touchscreen points with map function to the correct width and height
touchX = map(p.x, 200, 3700, 1, SCREEN_WIDTH);
touchY = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);
touchZ = p.z;
//printTouchToSerial(touchX, touchY, touchZ);
for (int i = 0; i < 9; ++i)
{
if (touchX >= buttons[i].x && touchX <= buttons[i].x + BUTTON_WIDTH &&
touchY >= buttons[i].y && touchY <= buttons[i].y + BUTTON_HEIGHT)
{
// Si un bouton est touché, inverse son état et met à jour l'affichage
buttons[i].state = !buttons[i].state;
drawButton(buttons[i].x, buttons[i].y, buttons[i].state);
if ((i==0) && (buttons[0].state ==1))
{
Serial.println("Btn 1 enfoncé");
// Set values to send
strcpy(myData.a, "PC01=> ON");
myData.b = true;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress1, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("PC01 Envoyé avec succès");
}else {
Serial.println("Erreur d'envoi des données PC01");
}
}
if((i==0) && (buttons[0].state ==0))
{
// Set values to send
strcpy(myData.a, "PC01=> OFF");
myData.b = false;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress1, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK)
{
Serial.println("Envoyé avec succès");
}else{
Serial.println("Erreur d'envoi des données");
}
}
}
//break;
delay(200);
}
}
}
// Dessine un bouton à une position donnée avec un état donné
void drawButton(int x, int y, bool state) {
uint16_t color = state ? TFT_RED : TFT_GREEN;
tft.fillRect(x, y, BUTTON_WIDTH, BUTTON_HEIGHT, color);
tft.drawRect(x, y, BUTTON_WIDTH, BUTTON_HEIGHT, TFT_WHITE);
} |
Partager