I am using an ESP32 connected to a ILI9341 display with an XPT2046 controller, using the following libraries:
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ILI9341.h> // Hardware-specific library for ILI9341
#include <U8g2_for_Adafruit_GFX.h>
#include "XPT2046_Touchscreen.h"
Display was working perfectly until I initialize the touch controller. The controller then works, but I cannot output anything to the display.
Here is my code (portions that deal with this):
// Display SPI - default VSPI for ESP32 WROOM32
#define TFT_MISO 19 // shared with T_DO
#define TFT_MOSI 23 // shared with T_DIN
#define TFT_SCLK 18 // shared with T_CLK
#define TFT_CS 5 // Chip select control pin
#define TFT_DC 4 // Data Command control pin
#define TFT_RST 2 // Reset pin (could connect to RST pin)
#define TFT_BL 16 // backlight
// screen initialization
Adafruit_ILI9341 display = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST, TFT_MISO);
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
// Touch SPI - default HSPI for ESP32 WROOM32, XPT2046 touch controller
#define TOUCH_CS 15 //HSPI CS
#define TOUCH_IRQ 17
// touch controller init
XPT2046_Touchscreen touch(TOUCH_CS); //, TOUCH_IRQ );
TS_Point rawLocation;
void initDisplay() {
// connect u8g2 procedures to Adafruit GFX
u8g2_for_adafruit_gfx.begin(display);
display.begin();
display.setRotation(3);
display.fillScreen(0x0000); // clear the screen, very slow!
// display state - diagnostics
uint8_t x = display.readcommand8(ILI9341_RDMODE);
Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
x = display.readcommand8(ILI9341_RDMADCTL);
Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
x = display.readcommand8(ILI9341_RDPIXFMT);
Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
x = display.readcommand8(ILI9341_RDIMGFMT);
Serial.print("Image Format: 0x"); Serial.println(x, HEX);
x = display.readcommand8(ILI9341_RDSELFDIAG);
Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
}
// draw static elements of the sensor screen
void drawStaticSensorScreen () {
// set up lines n
u8g2_for_adafruit_gfx.setForegroundColor(COLOR_WHITE);
display.drawFastHLine(1, LINE1_Y, 320, COLOR_WHITE); // draw banner line
display.drawFastHLine(1, LINE1_Y+2, 320, COLOR_WHITE); // draw banner line
// set up four measurement labels
u8g2_for_adafruit_gfx.setFont(MEAS_TEXT_FONT);
u8g2_for_adafruit_gfx.setCursor(MEAS1_TEXT_X, MEAS1_TEXT_Y);
u8g2_for_adafruit_gfx.print(MEAS1_TEXT);
u8g2_for_adafruit_gfx.setCursor(MEAS2_TEXT_X, MEAS2_TEXT_Y);
u8g2_for_adafruit_gfx.print(MEAS2_TEXT);
u8g2_for_adafruit_gfx.setCursor(MEAS3_TEXT_X, MEAS3_TEXT_Y);
u8g2_for_adafruit_gfx.print(MEAS3_TEXT);
u8g2_for_adafruit_gfx.setCursor(MEAS4_TEXT_X, MEAS4_TEXT_Y);
u8g2_for_adafruit_gfx.print(MEAS4_TEXT);
// for test purposes, fill in some sample measurements
// banner measurements
/*
u8g2_for_adafruit_gfx.setFont(MEAS_VAL_FONT);
u8g2_for_adafruit_gfx.setCursor(BANNER_VAL1_X, BANNER_VAL_Y);
u8g2_for_adafruit_gfx.print("22");
u8g2_for_adafruit_gfx.print(BANNER_VAL1_UNIT);
u8g2_for_adafruit_gfx.setCursor(320 - BANNER_VAL2_X - 100, BANNER_VAL_Y);
u8g2_for_adafruit_gfx.print("1000");
u8g2_for_adafruit_gfx.print(BANNER_VAL2_UNIT);*/
// main measurements
u8g2_for_adafruit_gfx.setFont(BANNER_FONT);
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR24_tr); //https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2_for_adafruit_gfx.setCursor(MEAS1_VAL_X, MEAS1_VAL_Y);
boost = 15;
u8g2_for_adafruit_gfx.print(banner_val1, 0);
u8g2_for_adafruit_gfx.print(MEAS1_VAL_UNIT);
u8g2_for_adafruit_gfx.setCursor(MEAS2_VAL_X, MEAS2_VAL_Y);
egt_temp_f = 700;
u8g2_for_adafruit_gfx.print(egt_temp_f, 0);
u8g2_for_adafruit_gfx.print(MEAS2_VAL_UNIT);
u8g2_for_adafruit_gfx.setCursor(MEAS3_VAL_X, MEAS3_VAL_Y);
coolant_temp_f = 180;
u8g2_for_adafruit_gfx.print(coolant_temp_f, 0);
u8g2_for_adafruit_gfx.print(MEAS3_VAL_UNIT);
u8g2_for_adafruit_gfx.setCursor(MEAS4_VAL_X, MEAS4_VAL_Y);
trans_temp_f = 170;
u8g2_for_adafruit_gfx.print(trans_temp_f, 0);
u8g2_for_adafruit_gfx.print(MEAS4_VAL_UNIT);
}
void setup() {
Serial.begin(115200);
//initialize the display
initDisplay();
drawStaticSensorScreen(); //draw static screen for sensor measurements, including banner
touch.begin();
touch.setRotation(3);
}
In loop() I output to the screen using the same method as in drawStaticSensorScreen, which works perfectly until I initialize the touch controller.
I understood that both the ILI and XPT controllers are ok being connected to the same SPT interface on the ESP32, as long as they have separate CS pins of course, and yet here I am.
Thoughts?