TFT + Touch + RFID on ESP32 #2414
Unanswered
nitrolack
asked this question in
Q&A - General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone,
i'm currently working on a little project, where i want to display some information on an tft display and want to check for rfids, if they are correct.
I want to display some numbers, or better said a menu, on a display and want to change information when i get a signal over a socket connection(future).
The esp32 isn't properly working with this setup, more likely the touch function(resisitive touch) doesn't work well, when i wire the mfrc522 to the vspi bus.
It just recognizes sometimes a touch but not fluent, more like every 1-3 seconds, and i tested it with a tftpaint.ino combined with an mfrc522.ino (gonna put the code down below)
So on i wanted to ask, if i should switch to another microcont. like stm32 nucleos64 or something absolutely different.
Or does someone could help me regarding this problem? (i can send you the libs, that i'm using on demand)
/*
Test MCU Friend parallel display and resistive touchscreen by drawing touch points
on screen, use something pointed for more accuracy
Need this modified Touchscreen library and one of:
*/
#define TFT_eSPIlib // comment out to use MCUFRIEND_kbv
#ifdef TFT_eSPIlib
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
#else
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#endif
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 3 // Configurable, see typical pin layout above
#define SS_PIN 5 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
#include <TouchScreen.h>
// adjust pressure sensitivity - note works 'backwards'
#define MINPRESSURE 200
#define MAXPRESSURE 1000
// some colours to play with
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// Either run TouchScreen_Calibr_native.ino and apply results to the arrays below
// or just use trial and error from drawing on screen
// ESP32 coordinates at default 12 bit resolution have range 0 - 4095
// however the ADC cannot read voltages below 150mv and tops out around 3.15V
// so the actual coordinates will not be at the extremes
// each library and driver may have different coordination and rotation sequence
const int coords[] = {3800, 400, 3800, 580}; // portrait - left, right, top, bottom
const int rotation = 3; // in rotation order - portrait, landscape, etc
const int XP = 14, XM = 15, YP = 4, YM = 12; // default ESP32 Uno touchscreen pins
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 365);
void setup() {
Serial.begin(115200);
#ifdef TFT_eSPIlib
Serial.println("TFT_eSPI library");
tft.begin();
#else
Serial.println("MCUFRIEND_kbv library");
idDisplay();
#endif
// screen orientation and background
String orientation;
switch (rotation) {
case 0:
orientation = "Portrait";
break;
case 1:
orientation = "Landscape";
break;
case 2:
orientation = "Portrait Inverted";
break;
case 3:
orientation = "Landscape Inverted";
break;
}
Serial.println(orientation);
tft.setRotation(rotation);
tft.fillScreen(BLACK);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Read personal data on a MIFARE PICC:")); //shows in serial that it is ready to read
}
void loop() {
// display touched point with colored dot
uint16_t pixel_x, pixel_y;
boolean pressed = Touch_getXY(&pixel_x, &pixel_y, true);
readRfid();
}
void readRfid(){
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
//some variables we need
byte block;
byte len;
MFRC522::StatusCode status;
//-------------------------------------------
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.println(F("Card Detected:"));
//-------------------------------------------
mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); //uncomment this to see all blocks in hex
//-------------------------------------------
Serial.print(F("Name: "));
byte buffer1[18];
block = 4;
len = 18;
//------------------------------------------- GET FIRST NAME
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(block, buffer1, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//PRINT FIRST NAME
for (uint8_t i = 0; i < 16; i++)
{
if (buffer1[i] != 32)
{
Serial.write(buffer1[i]);
}
}
Serial.print(" ");
//---------------------------------------- GET LAST NAME
byte buffer2[18];
block = 1;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(block, buffer2, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//PRINT LAST NAME
for (uint8_t i = 0; i < 16; i++) {
Serial.write(buffer2[i] );
}
//----------------------------------------
Serial.println(F("\nEnd Reading\n"));
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
boolean Touch_getXY(uint16_t *x, uint16_t *y, boolean showTouch) {
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
switch (rotation) {
case 0: // portrait
*x = map(p.x, coords[0], coords[1], 0, tft.width());
*y = map(p.y, coords[2], coords[3], 0, tft.height());
break;
case 1: // landscape
*x = map(p.y, coords[1], coords[0], 0, tft.width());
*y = map(p.x, coords[2], coords[3], 0, tft.height());
break;
case 2: // portrait inverted
*x = map(p.x, coords[1], coords[0], 0, tft.width());
*y = map(p.y, coords[3], coords[2], 0, tft.height());
break;
case 3: // landscape inverted
*x = map(p.y, coords[0], coords[1], 0, tft.width());
*y = map(p.x, coords[3], coords[2], 0, tft.height());
break;
}
if (showTouch) tft.fillCircle(*x, *y, 2, YELLOW);
}
return pressed;
}
#ifndef TFT_eSPIlib
void idDisplay() {
// MCUFRIEND_kbv library only
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
}
#endif
Beta Was this translation helpful? Give feedback.
All reactions