From f56184e9cdb85524da2a0e9a2784fb7a2e7fcd43 Mon Sep 17 00:00:00 2001 From: Bodmer Date: Wed, 11 Apr 2018 01:03:26 +0100 Subject: [PATCH] Add diagnostic sketches Do not load button class is touch screen chip select not defined --- Extensions/Touch.cpp | 40 +---- TFT_eSPI.cpp | 16 +- TFT_eSPI.h | 39 +++-- User_Setup_Select.h | 2 +- .../Read_User_Setup/Read_User_Setup.ino | 151 ++++++++++++++++++ .../Test_Touch_Controller.ino | 49 ++++++ 6 files changed, 246 insertions(+), 51 deletions(-) create mode 100644 examples/Test and diagnostics/Read_User_Setup/Read_User_Setup.ino create mode 100644 examples/Test and diagnostics/Test_Touch_Controller/Test_Touch_Controller.ino diff --git a/Extensions/Touch.cpp b/Extensions/Touch.cpp index f40ebf4..8a3efa8 100644 --- a/Extensions/Touch.cpp +++ b/Extensions/Touch.cpp @@ -1,11 +1,15 @@ // The following touch screen support code by maxpautsch was merged 1/10/17 // https://github.com/maxpautsch + // Define TOUCH_CS is the user setup file to enable this code + // A demo is provided in examples Generic folder -// Additions by Bodmer to double sample and use Z value to improve detection reliability + +// Additions by Bodmer to double sample, use Z value to improve detection reliability +// and to correct rotation handling + // See license in root directory. -#ifdef TOUCH_CS // If a pin has been allocated to the Touch screen load functions /*************************************************************************************** ** Function name: getTouchRaw ** Description: read raw touch position. Return false if not pressed. @@ -283,35 +287,3 @@ void TFT_eSPI::setTouch(uint16_t *parameters){ touchCalibration_invert_x = parameters[4] & 0x02; touchCalibration_invert_y = parameters[4] & 0x04; } - - -#else // TOUCH CS is not defined so generate dummy functions that do nothing - -/*************************************************************************************** -** Function name: Dummy functions for case where chip select pin is undefined -** Description: -***************************************************************************************/ - -uint8_t TFT_eSPI::getTouchRaw(uint16_t *x, uint16_t *y){ - return true; -} - -uint16_t TFT_eSPI::getTouchRawZ(void){ - return true; -} - -uint8_t TFT_eSPI::validTouch(uint16_t *x, uint16_t *y, uint16_t threshold){ - return true; -} - -uint8_t TFT_eSPI::getTouch(uint16_t *x, uint16_t *y, uint16_t threshold){ - return true; -} - -void TFT_eSPI::calibrateTouch(uint16_t *parameters, uint32_t color_bg, uint32_t color_fg, uint8_t size){ -} - -void TFT_eSPI::setTouch(uint16_t *parameters){ -} - -#endif // TOUCH_CS diff --git a/TFT_eSPI.cpp b/TFT_eSPI.cpp index f0572ef..bf415e4 100644 --- a/TFT_eSPI.cpp +++ b/TFT_eSPI.cpp @@ -195,6 +195,13 @@ TFT_eSPI::TFT_eSPI(int16_t w, int16_t h) fontsloaded |= 0x0100; // Bit 8 set #endif +#ifdef LOAD_FONT8N + fontsloaded |= 0x0200; // Bit 9 set +#endif + +#ifdef SMOOTH_FONT + fontsloaded |= 0x8000; // Bit 15 set +#endif } @@ -4573,6 +4580,12 @@ void TFT_eSPI::getSetup(setup_t &tft_settings) tft_settings.tft_spi_freq = SPI_FREQUENCY/100000; #endif +#if defined(TFT_SPI_OVERLAP) + tft_settings.overlap = true; +#else + tft_settings.overlap = false; +#endif + tft_settings.tft_driver = TFT_DRIVER; tft_settings.tft_width = _init_width; tft_settings.tft_height = _init_height; @@ -4677,10 +4690,9 @@ void TFT_eSPI::getSetup(setup_t &tft_settings) //////////////////////////////////////////////////////////////////////////////////////// #ifdef TOUCH_CS #include "Extensions/Touch.cpp" + #include "Extensions/Button.cpp" #endif -#include "Extensions/Button.cpp" - #include "Extensions/Sprite.cpp" #ifdef SMOOTH_FONT diff --git a/TFT_eSPI.h b/TFT_eSPI.h index 0dc49b3..7fc242a 100644 --- a/TFT_eSPI.h +++ b/TFT_eSPI.h @@ -90,21 +90,27 @@ #endif #endif - -#if defined (ESP8266) && defined (D0_USED_FOR_DC) - #define DC_C digitalWrite(TFT_DC, LOW) - #define DC_D digitalWrite(TFT_DC, HIGH) -#elif defined (ESP32) - #if defined (ESP32_PARALLEL) - #define DC_C GPIO.out_w1tc = (1 << TFT_DC) // Too fast for ST7735 - #define DC_D GPIO.out_w1ts = (1 << TFT_DC) - #else - #define DC_C GPIO.out_w1ts = (1 << TFT_DC); GPIO.out_w1tc = (1 << TFT_DC) - #define DC_D GPIO.out_w1tc = (1 << TFT_DC); GPIO.out_w1ts = (1 << TFT_DC) - #endif +#ifndef TFT_DC + #define DC_C // No macro allocated so it generates no code + #define DC_D // No macro allocated so it generates no code #else - #define DC_C GPOC=dcpinmask - #define DC_D GPOS=dcpinmask + #if defined (ESP8266) && defined (D0_USED_FOR_DC) + #define DC_C digitalWrite(TFT_DC, LOW) + #define DC_D digitalWrite(TFT_DC, HIGH) + #elif defined (ESP32) + #if defined (ESP32_PARALLEL) + #define DC_C GPIO.out_w1tc = (1 << TFT_DC) // Too fast for ST7735 + #define DC_D GPIO.out_w1ts = (1 << TFT_DC) + //#define DC_C digitalWrite(TFT_DC, LOW) + //#define DC_D digitalWrite(TFT_DC, HIGH) + #else + #define DC_C GPIO.out_w1ts = (1 << TFT_DC); GPIO.out_w1tc = (1 << TFT_DC) + #define DC_D GPIO.out_w1tc = (1 << TFT_DC); GPIO.out_w1ts = (1 << TFT_DC) + #endif + #else + #define DC_C GPOC=dcpinmask + #define DC_D GPOS=dcpinmask + #endif #endif #if defined (TFT_SPI_OVERLAP) @@ -173,8 +179,12 @@ #define tft_Write_8(C) GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t)C); WR_H // Write 16 bits to TFT +#ifdef PSEUDO_8_BIT + #define tft_Write_16(C) WR_L;GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t)(C >> 0)); WR_H +#else #define tft_Write_16(C) GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t)(C >> 8)); WR_H; \ GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t)(C >> 0)); WR_H +#endif // 16 bit write with swapped bytes #define tft_Write_16S(C) GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t) (C >> 0)); WR_H; \ @@ -340,6 +350,7 @@ typedef struct int16_t esp; uint8_t trans; uint8_t serial; +uint8_t overlap; uint16_t tft_driver; // Hexadecimal code uint16_t tft_width; // Rotation 0 width and height diff --git a/User_Setup_Select.h b/User_Setup_Select.h index 6617d5c..97db56e 100644 --- a/User_Setup_Select.h +++ b/User_Setup_Select.h @@ -106,7 +106,7 @@ #define PIN_MOSI 8 // SD1 #define PIN_MISO 7 // SD0 #define PIN_SCLK 6 // CLK -#define PIN_HWCS 0 // CMD +#define PIN_HWCS 0 // D3 #define PIN_D11 9 // SD2 #define PIN_D12 10 // SD4 diff --git a/examples/Test and diagnostics/Read_User_Setup/Read_User_Setup.ino b/examples/Test and diagnostics/Read_User_Setup/Read_User_Setup.ino new file mode 100644 index 0000000..f326cad --- /dev/null +++ b/examples/Test and diagnostics/Read_User_Setup/Read_User_Setup.ino @@ -0,0 +1,151 @@ +/* + This sketch reads the user setup information from the processor via the Serial Port + + It is a support and diagnostic sketch for the TFT_eSPI library: + https://github.com/Bodmer/TFT_eSPI + + The output is essentially a copy of the User_Setep configuration so can be used to + verify the correct settings are being picked up by the compiler. + + If support is needed the output can be cut and pasted into an Arduino Forum post and + already inlcudes the formatting [code]...[/code] markups. + + Written by Bodmer 9/4/18 +*/ + +#include +#include // Graphics library + +TFT_eSPI tft = TFT_eSPI(); // Invoke library + +#ifdef ESP8266 + ADC_MODE(ADC_VCC); // Read the supply voltage +#endif + +setup_t user; // The library defines the type "setup_t" as a struct + // Calling tft.getSetup(user) populates it with the settings +//------------------------------------------------------------------------------------------ + +void setup() { + // Use serial port + Serial.begin(115200); + + // Initialise the TFT screen + tft.init(); +} + +//------------------------------------------------------------------------------------------ + +void loop(void) { + +tft.getSetup(user); // + +Serial.printf("\n[code]\n"); +Serial.printf("Processor = ESP%i\n", user.esp, HEX); +Serial.printf("Frequency = %i MHz\n", ESP.getCpuFreqMHz()); +#ifdef ESP8266 +Serial.printf("Voltage = %2.2f V\n", ESP.getVcc() / 918.0); // 918 empirically determined +#endif +Serial.printf("Transactions = %s \n", (user.trans == 1) ? "Yes" : "No"); +Serial.printf("Interface = %s \n", (user.serial == 1) ? "SPI" : "Parallel"); +#ifdef ESP8266 +if (user.serial == 1) +Serial.printf("SPI overlap = %s \n\n", (user.overlap == 1) ? "Yes" : "No"); +#endif +if (user.tft_driver != 0xE9D) // For ePaper displays the size is defined in the sketch +{ + Serial.printf("Display driver = "); Serial.println(user.tft_driver, HEX); // Hexadecimal code + Serial.printf("Display width = %i \n", user.tft_width); // Rotation 0 width and height + Serial.printf("Display height = %i \n\n", user.tft_height); +} +else if (user.tft_driver == 0xE9D) Serial.printf("Display driver = ePaper\n\n"); + +if (user.r0_x_offset != 0) Serial.printf("R0 x offset = %i \n", user.r0_x_offset); // Offsets, not all used yet +if (user.r0_y_offset != 0) Serial.printf("R0 y offset = %i \n", user.r0_y_offset); +if (user.r1_x_offset != 0) Serial.printf("R1 x offset = %i \n", user.r1_x_offset); +if (user.r1_y_offset != 0) Serial.printf("R1 y offset = %i \n", user.r1_y_offset); +if (user.r2_x_offset != 0) Serial.printf("R2 x offset = %i \n", user.r2_x_offset); +if (user.r2_y_offset != 0) Serial.printf("R2 y offset = %i \n", user.r2_y_offset); +if (user.r3_x_offset != 0) Serial.printf("R3 x offset = %i \n", user.r3_x_offset); +if (user.r3_y_offset != 0) Serial.printf("R3 y offset = %i \n\n", user.r3_y_offset); + +if (user.pin_tft_mosi != -1) Serial.printf("MOSI = D%i (GPIO %i)\n", getPinName(user.pin_tft_mosi), user.pin_tft_mosi); +if (user.pin_tft_miso != -1) Serial.printf("MISO = D%i (GPIO %i)\n", getPinName(user.pin_tft_miso), user.pin_tft_miso); +if (user.pin_tft_clk != -1) Serial.printf("SCK = D%i (GPIO %i)\n", getPinName(user.pin_tft_clk), user.pin_tft_clk); + +#ifdef ESP8266 +if (user.overlap == true) +{ + Serial.printf("Overlap selected, following pins MUST be used:\n"); + + Serial.printf("MOSI = SD1 (GPIO 8)\n"); + Serial.printf("MISO = SD0 (GPIO 7)\n"); + Serial.printf("SCK = CLK (GPIO 6)\n"); + Serial.printf("TFT_CS = D3 (GPIO 0)\n\n"); + + Serial.printf("TFT_DC and TFT_RST pins can be user defined\n"); +} +#endif +if (user.pin_tft_cs != -1) Serial.printf("TFT_CS = D%i (GPIO %i)\n", getPinName(user.pin_tft_cs), user.pin_tft_cs); +if (user.pin_tft_dc != -1) Serial.printf("TFT_DC = D%i (GPIO %i)\n", getPinName(user.pin_tft_dc), user.pin_tft_dc); +if (user.pin_tft_rst != -1) Serial.printf("TFT_RST = D%i (GPIO %i)\n\n", getPinName(user.pin_tft_rst), user.pin_tft_rst); + +if (user.pin_tch_cs != -1) Serial.printf("TOUCH_CS = D%i (GPIO %i)\n\n", getPinName(user.pin_tch_cs), user.pin_tch_cs); + +if (user.pin_tft_wr != -1) Serial.printf("TFT_WR = D%i (GPIO %i)\n", getPinName(user.pin_tft_wr), user.pin_tft_wr); +if (user.pin_tft_rd != -1) Serial.printf("TFT_RD = D%i (GPIO %i)\n\n", getPinName(user.pin_tft_rd), user.pin_tft_rd); + +if (user.pin_tft_d0 != -1) Serial.printf("TFT_D0 = D%i (GPIO %i)\n", getPinName(user.pin_tft_d0), user.pin_tft_d0); +if (user.pin_tft_d1 != -1) Serial.printf("TFT_D1 = D%i (GPIO %i)\n", getPinName(user.pin_tft_d1), user.pin_tft_d1); +if (user.pin_tft_d2 != -1) Serial.printf("TFT_D2 = D%i (GPIO %i)\n", getPinName(user.pin_tft_d2), user.pin_tft_d2); +if (user.pin_tft_d3 != -1) Serial.printf("TFT_D3 = D%i (GPIO %i)\n", getPinName(user.pin_tft_d3), user.pin_tft_d3); +if (user.pin_tft_d4 != -1) Serial.printf("TFT_D4 = D%i (GPIO %i)\n", getPinName(user.pin_tft_d4), user.pin_tft_d4); +if (user.pin_tft_d5 != -1) Serial.printf("TFT_D5 = D%i (GPIO %i)\n", getPinName(user.pin_tft_d5), user.pin_tft_d5); +if (user.pin_tft_d6 != -1) Serial.printf("TFT_D6 = D%i (GPIO %i)\n", getPinName(user.pin_tft_d6), user.pin_tft_d6); +if (user.pin_tft_d7 != -1) Serial.printf("TFT_D7 = D%i (GPIO %i)\n\n", getPinName(user.pin_tft_d7), user.pin_tft_d7); + +uint16_t fonts = tft.fontsLoaded(); +if (fonts & (1 << 1)) Serial.printf("Font GLCD loaded\n"); +if (fonts & (1 << 2)) Serial.printf("Font 2 loaded\n"); +if (fonts & (1 << 4)) Serial.printf("Font 4 loaded\n"); +if (fonts & (1 << 6)) Serial.printf("Font 6 loaded\n"); +if (fonts & (1 << 7)) Serial.printf("Font 7 loaded\n"); +if (fonts & (1 << 9)) Serial.printf("Font 8N loaded\n"); +else +if (fonts & (1 << 8)) Serial.printf("Font 8 loaded\n"); +if (fonts & (1 << 15)) Serial.printf("Smooth font enabled\n"); +Serial.printf("\n"); + +if (user.serial==1) Serial.printf("Display SPI frequency = %2.1f MHz \n", user.tft_spi_freq/10.0); +if (user.pin_tch_cs != -1) Serial.printf("Touch SPI frequency = %2.1f MHz \n", user.tch_spi_freq/10.0); + +Serial.printf("[/code]\n"); + +while(1) yield(); + +} + +// Get pin name for ESP8266 +int8_t getPinName(int8_t pin) +{ + // For ESP32 pin labels on boards use the GPIO number + if (user.esp == 32) return pin; + + // For ESP8266 the pin labels are not the same as the GPIO number + // These are for the NodeMCU pin definitions: + // GPIO Dxx + if (pin == 16) return 0; + if (pin == 5) return 1; + if (pin == 4) return 2; + if (pin == 0) return 3; + if (pin == 2) return 4; + if (pin == 14) return 5; + if (pin == 12) return 6; + if (pin == 13) return 7; + if (pin == 15) return 8; + if (pin == 3) return 9; + if (pin == 1) return 10; + if (pin == 9) return 11; + if (pin == 10) return 12; +} + diff --git a/examples/Test and diagnostics/Test_Touch_Controller/Test_Touch_Controller.ino b/examples/Test and diagnostics/Test_Touch_Controller/Test_Touch_Controller.ino new file mode 100644 index 0000000..6c1e887 --- /dev/null +++ b/examples/Test and diagnostics/Test_Touch_Controller/Test_Touch_Controller.ino @@ -0,0 +1,49 @@ +// This sketch is to test the touch controller, nothing is displayed +// on the TFT. The TFT_eSPI library must be configured to suit your +// pins used. Make sure both the touch chip select and the TFT chip +// select are correctly defined to avoid SPI bus contention. + +// Make sure you have defined a pin for the touch controller chip +// select line in the user setup file or you will see "no member" +// compile errors for the touch functions! + +// It is a support and diagnostic sketch for the TFT_eSPI library: +// https://github.com/Bodmer/TFT_eSPI + +// The "raw" (unprocessed) touch sensor outputs are sent to the +// serial port. Touching the screen should show changes to the x, y +// and z values. x and y are raw ADC readings, not pixel coordinates. + +#include +#include +TFT_eSPI tft = TFT_eSPI(); + +//==================================================================== + +void setup(void) { + Serial.begin(115200); + Serial.println("\n\nStarting..."); + + tft.init(); +} + +//==================================================================== + +void loop() { + + uint16_t x, y; + + tft.getTouchRaw(&x, &y); + + Serial.printf("x: %i ", x); + + Serial.printf("y: %i ", y); + + Serial.printf("z: %i \n", tft.getTouchRawZ()); + + delay(250); + +} + +//==================================================================== +