Skip to content

Instantly share code, notes, and snippets.

@app-z
Created August 13, 2019 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save app-z/f5f8ddd9939adfd51912277db5251714 to your computer and use it in GitHub Desktop.
Save app-z/f5f8ddd9939adfd51912277db5251714 to your computer and use it in GitHub Desktop.
/* TFT demo
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <time.h>
#include <errno.h>
#include <sys/fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "esp_system.h"
#include "esp_heap_caps.h"
#include "tftspi.h"
#include "tft.h"
#include "spiffs_vfs.h"
#include "soc/rtc_cntl_reg.h"
// #include "tftspi.h"
#undef CONFIG_EXAMPLE_USE_WIFI
#ifdef CONFIG_EXAMPLE_USE_WIFI
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "freertos/event_groups.h"
#include "esp_attr.h"
#include <sys/time.h>
#include <unistd.h>
#include "lwip/err.h"
#include "apps/sntp/sntp.h"
#include "esp_log.h"
#include "nvs_flash.h"
#endif
#include "esp_camera.h"
#define CAMERA_MODEL_AI_THINKER
// #include "camera_pins.h"
// ==========================================================
// Define which spi bus to use TFT_VSPI_HOST or TFT_HSPI_HOST
#define SPI_BUS TFT_HSPI_HOST
// ==========================================================
static int _demo_pass = 0;
static uint8_t doprint = 1;
static uint8_t run_gs_demo = 0; // Run gray scale demo if set to 1
static struct tm* tm_info;
static char tmp_buff[64];
static time_t time_now, time_last = 0;
static const char *file_fonts[3] = {"/spiffs/fonts/DotMatrix_M.fon", "/spiffs/fonts/Ubuntu.fon", "/spiffs/fonts/Grotesk24x48.fon"};
#define GDEMO_TIME 1000
#define GDEMO_INFO_TIME 5000
//==================================================================================
// #if defined(CAMERA_MODEL_AI_THINKER)
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
void camera_init_(){
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.fb_count = 2;
// for display
config.frame_size = FRAMESIZE_QVGA;
config.pixel_format = PIXFORMAT_RGB565;
// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
printf("Camera init failed with error 0x%x", err);
return;
}
printf("Camera init OK\n");
}
static void _checkTime()
{
time(&time_now);
if (time_now > time_last) {
color_t last_fg, last_bg;
time_last = time_now;
tm_info = localtime(&time_now);
sprintf(tmp_buff, "%02d:%02d:%02d", tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec);
TFT_saveClipWin();
TFT_resetclipwin();
Font curr_font = cfont;
last_bg = _bg;
last_fg = _fg;
_fg = TFT_YELLOW;
_bg = (color_t){ 64, 64, 64 };
TFT_setFont(DEFAULT_FONT, NULL);
TFT_fillRect(1, _height-TFT_getfontheight()-8, _width-3, TFT_getfontheight()+6, _bg);
TFT_print(tmp_buff, CENTER, _height-TFT_getfontheight()-5);
cfont = curr_font;
_fg = last_fg;
_bg = last_bg;
TFT_restoreClipWin();
}
}
//---------------------
static int Wait(int ms)
{
uint8_t tm = 1;
if (ms < 0) {
tm = 0;
ms *= -1;
}
if (ms <= 50) {
vTaskDelay(ms / portTICK_RATE_MS);
//if (_checkTouch()) return 0;
}
else {
for (int n=0; n<ms; n += 50) {
vTaskDelay(50 / portTICK_RATE_MS);
if (tm) _checkTime();
//if (_checkTouch()) return 0;
}
}
return 1;
}
//---------------------
static void _dispTime()
{
Font curr_font = cfont;
if (_width < 240) TFT_setFont(DEF_SMALL_FONT, NULL);
else TFT_setFont(DEFAULT_FONT, NULL);
time(&time_now);
time_last = time_now;
tm_info = localtime(&time_now);
sprintf(tmp_buff, "%02d:%02d:%02d", tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec);
TFT_print(tmp_buff, CENTER, _height-TFT_getfontheight()-5);
cfont = curr_font;
}
//---------------------------------
static void disp_header(char *info)
{
TFT_fillScreen(TFT_BLACK);
TFT_resetclipwin();
_fg = TFT_YELLOW;
_bg = (color_t){ 64, 64, 64 };
if (_width < 240) TFT_setFont(DEF_SMALL_FONT, NULL);
else TFT_setFont(DEFAULT_FONT, NULL);
TFT_fillRect(0, 0, _width-1, TFT_getfontheight()+8, _bg);
TFT_drawRect(0, 0, _width-1, TFT_getfontheight()+8, TFT_CYAN);
TFT_fillRect(0, _height-TFT_getfontheight()-9, _width-1, TFT_getfontheight()+8, _bg);
TFT_drawRect(0, _height-TFT_getfontheight()-9, _width-1, TFT_getfontheight()+8, TFT_CYAN);
TFT_print(info, CENTER, 4);
_dispTime();
_bg = TFT_BLACK;
TFT_setclipwin(0,TFT_getfontheight()+9, _width-1, _height-TFT_getfontheight()-10);
}
//------------------------
static void test_times() {
if (doprint) {
uint32_t tstart, t1, t2;
disp_header("TIMINGS");
// ** Show Fill screen and send_line timings
tstart = clock();
TFT_fillWindow(TFT_BLACK);
t1 = clock() - tstart;
printf(" Clear screen time: %u ms\r\n", t1);
TFT_setFont(SMALL_FONT, NULL);
sprintf(tmp_buff, "Clear screen: %u ms", t1);
TFT_print(tmp_buff, 0, 140);
color_t *color_line = heap_caps_malloc((_width*3), MALLOC_CAP_DMA);
color_t *gsline = NULL;
if (gray_scale) gsline = malloc(_width*3);
if (color_line) {
float hue_inc = (float)((10.0 / (float)(_height-1) * 360.0));
for (int x=0; x<_width; x++) {
color_line[x] = HSBtoRGB(hue_inc, 1.0, (float)x / (float)_width);
if (gsline) gsline[x] = color_line[x];
}
disp_select();
tstart = clock();
for (int n=0; n<1000; n++) {
if (gsline) memcpy(color_line, gsline, _width*3);
int x1 = 0;
int y1 = 40+(n&63);
int x2 = dispWin.x2-dispWin.x1;
int y2 = 40+(n&63);
uint32_t len = (dispWin.x2-dispWin.x1+1);
printf("x1 = %d : y1 = %d : x2 = %d : y2 = %d : len = %d\n", x1, y1, x2, y2, len);
send_data(x1, y1, x2, y2, len, color_line);
// send_data(0, 40+(n&63), dispWin.x2-dispWin.x1, 40+(n&63), (uint32_t)(dispWin.x2-dispWin.x1+1), color_line);
wait_trans_finish(1);
}
t2 = clock() - tstart;
disp_deselect();
printf("Send color buffer time: %u us (%d pixels)\r\n", t2, dispWin.x2-dispWin.x1+1);
free(color_line);
sprintf(tmp_buff, " Send line: %u us", t2);
TFT_print(tmp_buff, 0, 144+TFT_getfontheight());
}
Wait(GDEMO_INFO_TIME);
}
}
// _width = DEFAULT_TFT_DISPLAY_WIDTH; // smaller dimension
// _height = DEFAULT_TFT_DISPLAY_HEIGHT; // larger dimension
#define FRAME_WIDTH 320
#define FRAME_HEIGHT 240
esp_err_t camera_capture(){
uint32_t tstart, t1, t2;
tstart = clock();
//acquire a frame
camera_fb_t * fb = esp_camera_fb_get();
if (!fb) {
printf("Camera Capture Failed\n");
return ESP_FAIL;
}
t1 = clock() - tstart;
printf("Capture camera time: %u ms\r\n", t1);
int i = 0, bufPos = 0;
uint8_t hb, lb;
color_t color;
color_t *color_line = heap_caps_malloc(_width*3, MALLOC_CAP_DMA);
tstart = clock();
for(int y = 0; y < _height; y++)
{
bufPos = 0;
for(int x = 0; x < _width; x++)
{
i = (y * FRAME_WIDTH + x) << 1;
hb = fb->buf[i] ;
lb = fb->buf[i + 1];
color.r = (lb & 0x1F) << 3;
color.g = (hb & 0x07) << 5 | (lb & 0xE0) >> 3;
color.b = hb & 0xF8;
color_line[bufPos] = color;
bufPos++;
// TFT_drawPixel(0, 0, TFT_BLACK, 1);
}
disp_select();
send_data(0, y, _width-1, y, _width, color_line);
wait_trans_finish(1);
disp_deselect();
}
free(color_line);
t1 = clock() - tstart;
printf("Send buffer time: %u ms\r\n", t1);
esp_camera_fb_return(fb);
printf("Capture frame ok.\n");
return ESP_OK;
}
#define ESP_LOG_DEBUG ESP_LOG_VERBOSE
//=============
void app_main()
{
//test_sd_card();
// ======== PREPARE DISPLAY INITIALIZATION =========
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
esp_err_t ret;
// === SET GLOBAL VARIABLES ==========================
// ===================================================
// ==== Set display type =====
tft_disp_type = DEFAULT_DISP_TYPE;
// ===================================================
// === Set display resolution if NOT using default ===
_width = DEFAULT_TFT_DISPLAY_WIDTH; // smaller dimension
_height = DEFAULT_TFT_DISPLAY_HEIGHT; // larger dimension
//_width = 128; // smaller dimension
//_height = 160; // larger dimension
// ===================================================
// ===================================================
// ==== Set maximum spi clock for display read ====
// operations, function 'find_rd_speed()' ====
// can be used after display initialization ====
max_rdclock = 4000000;
camera_init_();
Wait(-1000);
// ===================================================
// ====================================================================
// === Pins MUST be initialized before SPI interface initialization ===
// ====================================================================
TFT_PinsInit();
// ==== CONFIGURE SPI DEVICES(s) ====================================================================================
spi_lobo_device_handle_t spi;
spi_lobo_bus_config_t buscfg={
.miso_io_num=PIN_NUM_MISO, // set SPI MISO pin
.mosi_io_num=PIN_NUM_MOSI, // set SPI MOSI pin
.sclk_io_num=PIN_NUM_CLK, // set SPI CLK pin
.quadwp_io_num=-1,
.quadhd_io_num=-1,
.max_transfer_sz = 6*1024,
};
spi_lobo_device_interface_config_t devcfg={
.clock_speed_hz=8000000, // Initial clock out at 8 MHz
.mode=0, // SPI mode 0
.spics_io_num=-1, // we will use external CS pin
.spics_ext_io_num=PIN_NUM_CS, // external CS pin
.flags=LB_SPI_DEVICE_HALFDUPLEX, // ALWAYS SET to HALF DUPLEX MODE!! for display spi
};
// ====================================================================================================================
vTaskDelay(500 / portTICK_RATE_MS);
printf("\r\n==============================\r\n");
printf("TFT display DEMO, LoBo 11/2017\r\n");
printf("==============================\r\n");
printf("Pins used: miso=%d, mosi=%d, sck=%d, cs=%d\r\n", PIN_NUM_MISO, PIN_NUM_MOSI, PIN_NUM_CLK, PIN_NUM_CS);
printf("==============================\r\n\r\n");
// ==================================================================
// ==== Initialize the SPI bus and attach the LCD to the SPI bus ====
ret=spi_lobo_bus_add_device(SPI_BUS, &buscfg, &devcfg, &spi);
assert(ret==ESP_OK);
printf("SPI: display device added to spi bus (%d)\r\n", SPI_BUS);
disp_spi = spi;
// ==== Test select/deselect ====
ret = spi_lobo_device_select(spi, 1);
assert(ret==ESP_OK);
ret = spi_lobo_device_deselect(spi);
assert(ret==ESP_OK);
printf("SPI: attached display device, speed=%u\r\n", spi_lobo_get_speed(spi));
printf("SPI: bus uses native pins: %s\r\n", spi_lobo_uses_native_pins(spi) ? "true" : "false");
// ================================
// ==== Initialize the Display ====
printf("SPI: display init...\r\n");
TFT_display_init();
printf("OK\r\n");
// ---- Detect maximum read speed ----
max_rdclock = find_rd_speed();
printf("SPI: Max rd speed = %u\r\n", max_rdclock);
// ==== Set SPI clock used for display operations ====
spi_lobo_set_speed(spi, DEFAULT_SPI_CLOCK);
printf("SPI: Changed speed to %u\r\n", spi_lobo_get_speed(spi));
printf("\r\n---------------------\r\n");
printf("Graphics demo started\r\n");
printf("---------------------\r\n");
font_rotate = 0;
text_wrap = 0;
font_transparent = 0;
font_forceFixed = 0;
gray_scale = 0;
TFT_setGammaCurve(DEFAULT_GAMMA_CURVE);
TFT_setRotation(PORTRAIT);
TFT_setFont(DEFAULT_FONT, NULL);
TFT_resetclipwin();
disp_header("System INIT");
while(true){
camera_capture();
}
}
/*
*
* HIGH SPEED LOW LEVEL DISPLAY FUNCTIONS USING DIRECT TRANSFER MODE
*
*/
#ifndef _TFTSPI_H_
#define _TFTSPI_H_
#include "tftspi.h"
#include "spi_master_lobo.h"
#include "sdkconfig.h"
#include "stmpe610.h"
#define TOUCH_TYPE_NONE 0
#define TOUCH_TYPE_XPT2046 1
#define TOUCH_TYPE_STMPE610 2
#define TP_CALX_XPT2046 7472920
#define TP_CALY_XPT2046 122224794
#define TP_CALX_STMPE610 21368532
#define TP_CALY_STMPE610 11800144
// === Screen orientation constants ===
#define PORTRAIT 0
#define LANDSCAPE 1
#define PORTRAIT_FLIP 2
#define LANDSCAPE_FLIP 3
#define DISP_TYPE_ILI9341 0
#define DISP_TYPE_ILI9488 1
#define DISP_TYPE_ST7789V 2
#define DISP_TYPE_ST7735 3
#define DISP_TYPE_ST7735R 4
#define DISP_TYPE_ST7735B 5
#define DISP_TYPE_MAX 6
#if CONFIG_EXAMPLE_DISPLAY_TYPE == 1
// ** Set the correct configuration for ESP32-WROVER-KIT v3
// --------------------------------------------------------
#define DEFAULT_DISP_TYPE DISP_TYPE_ST7789V
#define DEFAULT_TFT_DISPLAY_WIDTH 240
#define DEFAULT_TFT_DISPLAY_HEIGHT 320
#define DISP_COLOR_BITS_24 0x66
#define DEFAULT_GAMMA_CURVE 0
#define DEFAULT_SPI_CLOCK 26000000
#define TFT_INVERT_ROTATION 0
#define TFT_INVERT_ROTATION1 1
#define TFT_RGB_BGR 0x00
#define USE_TOUCH TOUCH_TYPE_NONE
#define PIN_NUM_MISO 25 // SPI MISO
#define PIN_NUM_MOSI 23 // SPI MOSI
#define PIN_NUM_CLK 19 // SPI CLOCK pin
#define PIN_NUM_CS 22 // Display CS pin
#define PIN_NUM_DC 21 // Display command/data pin
#define PIN_NUM_TCS 0 // Touch screen CS pin
#define PIN_NUM_RST 18 // GPIO used for RESET control
#define PIN_NUM_BCKL 5 // GPIO used for backlight control
#define PIN_BCKL_ON 0 // GPIO value for backlight ON
#define PIN_BCKL_OFF 1 // GPIO value for backlight OFF
// --------------------------------------------------------
#elif CONFIG_EXAMPLE_DISPLAY_TYPE == 2
// ** Set the correct configuration for Adafruit TFT Feather
// ---------------------------------------------------------
#define DEFAULT_DISP_TYPE DISP_TYPE_ILI9341
#define DEFAULT_TFT_DISPLAY_WIDTH 240
#define DEFAULT_TFT_DISPLAY_HEIGHT 320
#define DISP_COLOR_BITS_24 0x66
#define DEFAULT_GAMMA_CURVE 0
#define DEFAULT_SPI_CLOCK 26000000
#define TFT_INVERT_ROTATION 0
#define TFT_INVERT_ROTATION1 0
#define TFT_RGB_BGR 0x08
#define USE_TOUCH TOUCH_TYPE_STMPE610
#define PIN_NUM_MISO 19 // SPI MISO
#define PIN_NUM_MOSI 18 // SPI MOSI
#define PIN_NUM_CLK 5 // SPI CLOCK pin
#define PIN_NUM_CS 15 // Display CS pin
#define PIN_NUM_DC 33 // Display command/data pin
#define PIN_NUM_TCS 32 // Touch screen CS pin (NOT used if USE_TOUCH=0)
#define PIN_NUM_RST 0 // GPIO used for RESET control (#16)
#define PIN_NUM_BCKL 0 // GPIO used for backlight control
#define PIN_BCKL_ON 0 // GPIO value for backlight ON
#define PIN_BCKL_OFF 1 // GPIO value for backlight OFF
// ---------------------------------------------------------
#elif CONFIG_EXAMPLE_DISPLAY_TYPE == 3
// ** Set the correct configuration for M5Stack TFT
// ---------------------------------------------------------
#define DEFAULT_DISP_TYPE DISP_TYPE_ILI9341
#define DEFAULT_TFT_DISPLAY_WIDTH 320
#define DEFAULT_TFT_DISPLAY_HEIGHT 240
#define DISP_COLOR_BITS_24 0x66
#define DEFAULT_GAMMA_CURVE 0
#define DEFAULT_SPI_CLOCK 26000000
#define TFT_INVERT_ROTATION 0
#define TFT_INVERT_ROTATION1 0
#define TFT_INVERT_ROTATION2 1 // Adapte M5Stack TFT
#define TFT_RGB_BGR 0x08
#define USE_TOUCH TOUCH_TYPE_NONE
#define PIN_NUM_MISO 19 // SPI MISO
#define PIN_NUM_MOSI 23 // SPI MOSI
#define PIN_NUM_CLK 18 // SPI CLOCK pin
#define PIN_NUM_CS 14 // Display CS pin
#define PIN_NUM_DC 27 // Display command/data pin
#define PIN_NUM_TCS 0 // Touch screen CS pin (NOT used if USE_TOUCH=0)
#define PIN_NUM_RST 33 // GPIO used for RESET control (#16)
#define PIN_NUM_BCKL 32 // GPIO used for backlight control
#define PIN_BCKL_ON 1 // GPIO value for backlight ON
#define PIN_BCKL_OFF 0 // GPIO value for backlight OFF
// ---------------------------------------------------------
#else
// Configuration for other boards, set the correct values for the display used
//----------------------------------------------------------------------------
#define DISP_COLOR_BITS_24 0x66
//#define DISP_COLOR_BITS_16 0x55 // Do not use!
// #############################################
// ### Set to 1 for some displays, ###
// for example the one on ESP-WROWER-KIT ###
// #############################################
#define TFT_INVERT_ROTATION 0
#define TFT_INVERT_ROTATION1 0
// ################################################
// ### SET TO 0X00 FOR DISPLAYS WITH RGB MATRIX ###
// ### SET TO 0X08 FOR DISPLAYS WITH BGR MATRIX ###
// ### For ESP-WROWER-KIT set to 0x00 ###
// ################################################
#define TFT_RGB_BGR 0x08
// ##############################################################
// ### Define ESP32 SPI pins to which the display is attached ###
// ##############################################################
// The pins configured here are the native spi pins for HSPI interface
// Any other valid pin combination can be used
#define PIN_NUM_MISO 12 // SPI MISO
#define PIN_NUM_MOSI 13 // SPI MOSI
#define PIN_NUM_CLK 14 // SPI CLOCK pin
#define PIN_NUM_CS 15 // Display CS pin
#define PIN_NUM_DC 2 // Display command/data pin
#define PIN_NUM_TCS 0 // Touch screen CS pin (NOT used if USE_TOUCH=0)
// --------------------------------------------------------------
// ** Set Reset and Backlight pins to 0 if not used !
// ** If you want to use them, set them to some valid GPIO number
#define PIN_NUM_RST 0 // GPIO used for RESET control
#define PIN_NUM_BCKL 0 // GPIO used for backlight control
#define PIN_BCKL_ON 0 // GPIO value for backlight ON
#define PIN_BCKL_OFF 1 // GPIO value for backlight OFF
// --------------------------------------------------------------
// #######################################################
// Set this to 1 if you want to use touch screen functions
// #######################################################
#define USE_TOUCH TOUCH_TYPE_NONE
// #######################################################
// #######################################################################
// Default display width (smaller dimension) and height (larger dimension)
// #######################################################################
#define DEFAULT_TFT_DISPLAY_WIDTH 128
#define DEFAULT_TFT_DISPLAY_HEIGHT 160
// #######################################################################
#define DEFAULT_GAMMA_CURVE 0
#define DEFAULT_SPI_CLOCK 32000000
#define DEFAULT_DISP_TYPE DISP_TYPE_ST7735B
//----------------------------------------------------------------------------
#define TFT_INVERT_ROTATION 0
#define TFT_INVERT_ROTATION1 1
#define TFT_INVERT_ROTATION2 0
#endif // CONFIG_EXAMPLE_ESP_WROVER_KIT
// ##############################################################
// #### Global variables ####
// ##############################################################
// ==== Converts colors to grayscale if 1 =======================
extern uint8_t gray_scale;
// ==== Spi clock for reading data from display memory in Hz ====
extern uint32_t max_rdclock;
// ==== Display dimensions in pixels ============================
extern int _width;
extern int _height;
// ==== Display type, DISP_TYPE_ILI9488 or DISP_TYPE_ILI9341 ====
extern uint8_t tft_disp_type;
// ==== Spi device handles for display and touch screen =========
extern spi_lobo_device_handle_t disp_spi;
extern spi_lobo_device_handle_t ts_spi;
// ##############################################################
// 24-bit color type structure
typedef struct __attribute__((__packed__)) {
//typedef struct {
uint8_t r;
uint8_t g;
uint8_t b;
} color_t ;
// ==== Display commands constants ====
#define TFT_INVOFF 0x20
#define TFT_INVONN 0x21
#define TFT_DISPOFF 0x28
#define TFT_DISPON 0x29
#define TFT_MADCTL 0x36
#define TFT_PTLAR 0x30
#define TFT_ENTRYM 0xB7
#define TFT_CMD_NOP 0x00
#define TFT_CMD_SWRESET 0x01
#define TFT_CMD_RDDID 0x04
#define TFT_CMD_RDDST 0x09
#define TFT_CMD_SLPIN 0x10
#define TFT_CMD_SLPOUT 0x11
#define TFT_CMD_PTLON 0x12
#define TFT_CMD_NORON 0x13
#define TFT_CMD_RDMODE 0x0A
#define TFT_CMD_RDMADCTL 0x0B
#define TFT_CMD_RDPIXFMT 0x0C
#define TFT_CMD_RDIMGFMT 0x0D
#define TFT_CMD_RDSELFDIAG 0x0F
#define TFT_CMD_GAMMASET 0x26
#define TFT_CMD_FRMCTR1 0xB1
#define TFT_CMD_FRMCTR2 0xB2
#define TFT_CMD_FRMCTR3 0xB3
#define TFT_CMD_INVCTR 0xB4
#define TFT_CMD_DFUNCTR 0xB6
#define TFT_CMD_PWCTR1 0xC0
#define TFT_CMD_PWCTR2 0xC1
#define TFT_CMD_PWCTR3 0xC2
#define TFT_CMD_PWCTR4 0xC3
#define TFT_CMD_PWCTR5 0xC4
#define TFT_CMD_VMCTR1 0xC5
#define TFT_CMD_VMCTR2 0xC7
#define TFT_CMD_RDID1 0xDA
#define TFT_CMD_RDID2 0xDB
#define TFT_CMD_RDID3 0xDC
#define TFT_CMD_RDID4 0xDD
#define TFT_CMD_GMCTRP1 0xE0
#define TFT_CMD_GMCTRN1 0xE1
#define TFT_CMD_POWERA 0xCB
#define TFT_CMD_POWERB 0xCF
#define TFT_CMD_POWER_SEQ 0xED
#define TFT_CMD_DTCA 0xE8
#define TFT_CMD_DTCB 0xEA
#define TFT_CMD_PRC 0xF7
#define TFT_CMD_3GAMMA_EN 0xF2
#define ST_CMD_VCOMS 0xBB
#define ST_CMD_FRCTRL2 0xC6
#define ST_CMD_PWCTR1 0xD0
#define ST7735_FRMCTR1 0xB1
#define ST7735_FRMCTR2 0xB2
#define ST7735_FRMCTR3 0xB3
#define ST7735_INVCTR 0xB4
#define ST7735_DISSET5 0xB6
#define ST7735_PWCTR1 0xC0
#define ST7735_PWCTR2 0xC1
#define ST7735_PWCTR3 0xC2
#define ST7735_PWCTR4 0xC3
#define ST7735_PWCTR5 0xC4
#define ST7735_VMCTR1 0xC5
#define ST7735_RDID1 0xDA
#define ST7735_RDID2 0xDB
#define ST7735_RDID3 0xDC
#define ST7735_RDID4 0xDD
#define ST7735_NOP 0x00
#define ST7735_SWRESET 0x01
#define ST7735_RDDID 0x04
#define ST7735_RDDST 0x09
#define ST7735_SLPIN 0x10
#define ST7735_SLPOUT 0x11
#define ST7735_PTLON 0x12
#define ST7735_NORON 0x13
#define ST7735_PWCTR6 0xFC
#define ST7735_GMCTRP1 0xE0
#define ST7735_GMCTRN1 0xE1
#define MADCTL_MY 0x80
#define MADCTL_MX 0x40
#define MADCTL_MV 0x20
#define MADCTL_ML 0x10
#define MADCTL_MH 0x04
#define TFT_CASET 0x2A
#define TFT_PASET 0x2B
#define TFT_RAMWR 0x2C
#define TFT_RAMRD 0x2E
#define TFT_CMD_PIXFMT 0x3A
#define TFT_CMD_DELAY 0x80
// Initialization sequence for ILI7749
// ====================================
static const uint8_t ST7789V_init[] = {
#if PIN_NUM_RST
15, // 15 commands in list
#else
16, // 16 commands in list
TFT_CMD_SWRESET, TFT_CMD_DELAY, // 1: Software reset, no args, w/delay
200, // 200 ms delay
#endif
TFT_CMD_FRMCTR2, 5, 0x0c, 0x0c, 0x00, 0x33, 0x33,
TFT_ENTRYM, 1, 0x45,
ST_CMD_VCOMS, 1, 0x2B,
TFT_CMD_PWCTR1, 1, 0x2C,
TFT_CMD_PWCTR3, 2, 0x01, 0xff,
TFT_CMD_PWCTR4, 1, 0x11,
TFT_CMD_PWCTR5, 1, 0x20,
ST_CMD_FRCTRL2, 1, 0x0f,
ST_CMD_PWCTR1, 2, 0xA4, 0xA1,
TFT_CMD_GMCTRP1, 14, 0xD0, 0x00, 0x05, 0x0E, 0x15, 0x0D, 0x37, 0x43, 0x47, 0x09, 0x15, 0x12, 0x16, 0x19,
TFT_CMD_GMCTRN1, 14, 0xD0, 0x00, 0x05, 0x0D, 0x0C, 0x06, 0x2D, 0x44, 0x40, 0x0E, 0x1C, 0x18, 0x16, 0x19,
TFT_MADCTL, 1, (MADCTL_MX | TFT_RGB_BGR), // Memory Access Control (orientation)
TFT_CMD_PIXFMT, 1, DISP_COLOR_BITS_24, // *** INTERFACE PIXEL FORMAT: 0x66 -> 18 bit; 0x55 -> 16 bit
TFT_CMD_SLPOUT, TFT_CMD_DELAY, 120, // Sleep out, // 120 ms delay
TFT_DISPON, TFT_CMD_DELAY, 120,
};
// Initialization sequence for ILI7341
// ====================================
static const uint8_t ILI9341_init[] = {
#if PIN_NUM_RST
23, // 24 commands in list
#else
24, // 24 commands in list
TFT_CMD_SWRESET, TFT_CMD_DELAY, // 1: Software reset, no args, w/delay
250, // 200 ms delay
#endif
TFT_CMD_POWERA, 5, 0x39, 0x2C, 0x00, 0x34, 0x02,
TFT_CMD_POWERB, 3, 0x00, 0XC1, 0X30,
0xEF, 3, 0x03, 0x80, 0x02,
TFT_CMD_DTCA, 3, 0x85, 0x00, 0x78,
TFT_CMD_DTCB, 2, 0x00, 0x00,
TFT_CMD_POWER_SEQ, 4, 0x64, 0x03, 0X12, 0X81,
TFT_CMD_PRC, 1, 0x20,
TFT_CMD_PWCTR1, 1, 0x23, //Power control VRH[5:0]
TFT_CMD_PWCTR2, 1, 0x10, //Power control SAP[2:0];BT[3:0]
TFT_CMD_VMCTR1, 2, 0x3e, 0x28, //VCM control
TFT_CMD_VMCTR2, 1, 0x86, //VCM control2
TFT_MADCTL, 1, // Memory Access Control (orientation)
(MADCTL_MX | TFT_RGB_BGR),
// *** INTERFACE PIXEL FORMAT: 0x66 -> 18 bit; 0x55 -> 16 bit
TFT_CMD_PIXFMT, 1, DISP_COLOR_BITS_24,
TFT_INVOFF, 0,
TFT_CMD_FRMCTR1, 2, 0x00, 0x18,
TFT_CMD_DFUNCTR, 4, 0x08, 0x82, 0x27, 0x00, // Display Function Control
TFT_PTLAR, 4, 0x00, 0x00, 0x01, 0x3F,
TFT_CMD_3GAMMA_EN, 1, 0x00, // 3Gamma Function: Disable (0x02), Enable (0x03)
TFT_CMD_GAMMASET, 1, 0x01, //Gamma curve selected (0x01, 0x02, 0x04, 0x08)
TFT_CMD_GMCTRP1, 15, //Positive Gamma Correction
0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00,
TFT_CMD_GMCTRN1, 15, //Negative Gamma Correction
0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F,
TFT_CMD_SLPOUT, TFT_CMD_DELAY, // Sleep out
200, // 120 ms delay
TFT_DISPON, TFT_CMD_DELAY, 200,
};
// Initialization sequence for ILI9488
// ====================================
static const uint8_t ILI9488_init[] = {
#if PIN_NUM_RST
17, // 17 commands in list
#else
18, // 18 commands in list
TFT_CMD_SWRESET, TFT_CMD_DELAY, // 1: Software reset, no args, w/delay
200, // 200 ms delay
#endif
0xE0, 15, 0x00, 0x03, 0x09, 0x08, 0x16, 0x0A, 0x3F, 0x78, 0x4C, 0x09, 0x0A, 0x08, 0x16, 0x1A, 0x0F,
0xE1, 15, 0x00, 0x16, 0x19, 0x03, 0x0F, 0x05, 0x32, 0x45, 0x46, 0x04, 0x0E, 0x0D, 0x35, 0x37, 0x0F,
0xC0, 2, //Power Control 1
0x17, //Vreg1out
0x15, //Verg2out
0xC1, 1, //Power Control 2
0x41, //VGH,VGL
0xC5, 3, //Power Control 3
0x00,
0x12, //Vcom
0x80,
#if TFT_INVERT_ROTATION
TFT_MADCTL, 1, (MADCTL_MV | TFT_RGB_BGR), // Memory Access Control (orientation), set to portrait
#else
TFT_MADCTL, 1, (MADCTL_MX | TFT_RGB_BGR), // Memory Access Control (orientation), set to portrait
#endif
// *** INTERFACE PIXEL FORMAT: 0x66 -> 18 bit;
TFT_CMD_PIXFMT, 1, DISP_COLOR_BITS_24,
0xB0, 1, // Interface Mode Control
0x00, // 0x80: SDO NOT USE; 0x00 USE SDO
0xB1, 1, //Frame rate
0xA0, //60Hz
0xB4, 1, //Display Inversion Control
0x02, //2-dot
0xB6, 2, //Display Function Control RGB/MCU Interface Control
0x02, //MCU
0x02, //Source,Gate scan direction
0xE9, 1, // Set Image Function
0x00, // Disable 24 bit data
0x53, 1, // Write CTRL Display Value
0x28, // BCTRL && DD on
0x51, 1, // Write Display Brightness Value
0x7F, //
0xF7, 4, // Adjust Control
0xA9,
0x51,
0x2C,
0x02, // D7 stream, loose
0x11, TFT_CMD_DELAY, //Exit Sleep
120,
0x29, 0, //Display on
};
// Initialization commands for 7735B screens
// ------------------------------------
static const uint8_t STP7735_init[] = {
#if PIN_NUM_RST
16, // 17 commands in list
#else
17, // 18 commands in list:
ST7735_SLPOUT, TFT_CMD_DELAY, // 2: Out of sleep mode, no args, w/delay
255, // 255 = 500 ms delay
#endif
TFT_CMD_PIXFMT, 1+TFT_CMD_DELAY, // 3: Set color mode, 1 arg + delay:
0x06, // 18-bit color 6-6-6 color format
10, // 10 ms delay
ST7735_FRMCTR1, 3+TFT_CMD_DELAY, // 4: Frame rate control, 3 args + delay:
0x00, // fastest refresh
0x06, // 6 lines front porch
0x03, // 3 lines back porch
10, // 10 ms delay
TFT_MADCTL , 1 , // 5: Memory access ctrl (directions), 1 arg:
0x08, // Row addr/col addr, bottom to top refresh
ST7735_DISSET5, 2 , // 6: Display settings #5, 2 args, no delay:
0x15, // 1 clk cycle nonoverlap, 2 cycle gate
// rise, 3 cycle osc equalize
0x02, // Fix on VTL
ST7735_INVCTR , 1 , // 7: Display inversion control, 1 arg:
0x0, // Line inversion
ST7735_PWCTR1 , 2+TFT_CMD_DELAY, // 8: Power control, 2 args + delay:
0x02, // GVDD = 4.7V
0x70, // 1.0uA
10, // 10 ms delay
ST7735_PWCTR2 , 1 , // 9: Power control, 1 arg, no delay:
0x05, // VGH = 14.7V, VGL = -7.35V
ST7735_PWCTR3 , 2 , // 10: Power control, 2 args, no delay:
0x01, // Opamp current small
0x02, // Boost frequency
ST7735_VMCTR1 , 2+TFT_CMD_DELAY, // 11: Power control, 2 args + delay:
0x3C, // VCOMH = 4V
0x38, // VCOML = -1.1V
10, // 10 ms delay
ST7735_PWCTR6 , 2 , // 12: Power control, 2 args, no delay:
0x11, 0x15,
ST7735_GMCTRP1,16 , // 13: Magical unicorn dust, 16 args, no delay:
0x09, 0x16, 0x09, 0x20, // (seriously though, not sure what
0x21, 0x1B, 0x13, 0x19, // these config values represent)
0x17, 0x15, 0x1E, 0x2B,
0x04, 0x05, 0x02, 0x0E,
ST7735_GMCTRN1,16+TFT_CMD_DELAY, // 14: Sparkles and rainbows, 16 args + delay:
0x0B, 0x14, 0x08, 0x1E, // (ditto)
0x22, 0x1D, 0x18, 0x1E,
0x1B, 0x1A, 0x24, 0x2B,
0x06, 0x06, 0x02, 0x0F,
10, // 10 ms delay
TFT_CASET , 4 , // 15: Column addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 2
0x00, 0x81, // XEND = 129
TFT_PASET , 4 , // 16: Row addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 1
0x00, 0x81, // XEND = 160
ST7735_NORON , TFT_CMD_DELAY, // 17: Normal display on, no args, w/delay
10, // 10 ms delay
TFT_DISPON , TFT_CMD_DELAY, // 18: Main screen turn on, no args, w/delay
255 // 255 = 500 ms delay
};
// Init for 7735R, part 1 (red or green tab)
// --------------------------------------
static const uint8_t STP7735R_init[] = {
#if PIN_NUM_RST
14, // 14 commands in list
#else
15, // 15 commands in list:
ST7735_SWRESET, TFT_CMD_DELAY, // 1: Software reset, 0 args, w/delay
150, // 150 ms delay
#endif
ST7735_SLPOUT , TFT_CMD_DELAY, // 2: Out of sleep mode, 0 args, w/delay
255, // 500 ms delay
ST7735_FRMCTR1, 3 , // 3: Frame rate ctrl - normal mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR2, 3 , // 4: Frame rate control - idle mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR3, 6 , // 5: Frame rate ctrl - partial mode, 6 args:
0x01, 0x2C, 0x2D, // Dot inversion mode
0x01, 0x2C, 0x2D, // Line inversion mode
ST7735_INVCTR , 1 , // 6: Display inversion ctrl, 1 arg, no delay:
0x07, // No inversion
ST7735_PWCTR1 , 3 , // 7: Power control, 3 args, no delay:
0xA2,
0x02, // -4.6V
0x84, // AUTO mode
ST7735_PWCTR2 , 1 , // 8: Power control, 1 arg, no delay:
0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
ST7735_PWCTR3 , 2 , // 9: Power control, 2 args, no delay:
0x0A, // Opamp current small
0x00, // Boost frequency
ST7735_PWCTR4 , 2 , // 10: Power control, 2 args, no delay:
0x8A, // BCLK/2, Opamp current small & Medium low
0x2A,
ST7735_PWCTR5 , 2 , // 11: Power control, 2 args, no delay:
0x8A, 0xEE,
ST7735_VMCTR1 , 1 , // 12: Power control, 1 arg, no delay:
0x0E,
TFT_INVOFF , 0 , // 13: Don't invert display, no args, no delay
TFT_MADCTL , 1 , // 14: Memory access control (directions), 1 arg:
0xC0, // row addr/col addr, bottom to top refresh, RGB order
TFT_CMD_PIXFMT , 1+TFT_CMD_DELAY, // 15: Set color mode, 1 arg + delay:
0x06, // 18-bit color 6-6-6 color format
10 // 10 ms delay
};
// Init for 7735R, part 2 (green tab only)
// ---------------------------------------
static const uint8_t Rcmd2green[] = {
2, // 2 commands in list:
TFT_CASET , 4 , // 1: Column addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 0
0x00, 0x7F+0x02, // XEND = 129
TFT_PASET , 4 , // 2: Row addr set, 4 args, no delay:
0x00, 0x01, // XSTART = 0
0x00, 0x9F+0x01 // XEND = 160
};
// Init for 7735R, part 2 (red tab only)
// -------------------------------------
static const uint8_t Rcmd2red[] = {
2, // 2 commands in list:
TFT_CASET , 4 , // 1: Column addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x7F, // XEND = 127
TFT_PASET , 4 , // 2: Row addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x9F // XEND = 159
};
// Init for 7735R, part 3 (red or green tab)
// -----------------------------------------
static const uint8_t Rcmd3[] = {
4, // 4 commands in list:
ST7735_GMCTRP1, 16 , // 1: Magical unicorn dust, 16 args, no delay:
0x02, 0x1c, 0x07, 0x12,
0x37, 0x32, 0x29, 0x2d,
0x29, 0x25, 0x2B, 0x39,
0x00, 0x01, 0x03, 0x10,
ST7735_GMCTRN1, 16 , // 2: Sparkles and rainbows, 16 args, no delay:
0x03, 0x1d, 0x07, 0x06,
0x2E, 0x2C, 0x29, 0x2D,
0x2E, 0x2E, 0x37, 0x3F,
0x00, 0x00, 0x02, 0x10,
ST7735_NORON , TFT_CMD_DELAY, // 3: Normal display on, no args, w/delay
10, // 10 ms delay
TFT_DISPON , TFT_CMD_DELAY, // 4: Main screen turn on, no args w/delay
100 // 100 ms delay
};
// ==== Public functions =========================================================
// == Low level functions; usually not used directly ==
esp_err_t wait_trans_finish(uint8_t free_line);
void disp_spi_transfer_cmd(int8_t cmd);
void disp_spi_transfer_cmd_data(int8_t cmd, uint8_t *data, uint32_t len);
void drawPixel(int16_t x, int16_t y, color_t color, uint8_t sel);
void send_data(int x1, int y1, int x2, int y2, uint32_t len, color_t *buf);
void TFT_pushColorRep(int x1, int y1, int x2, int y2, color_t data, uint32_t len);
int read_data(int x1, int y1, int x2, int y2, int len, uint8_t *buf, uint8_t set_sp);
color_t readPixel(int16_t x, int16_t y);
int touch_get_data(uint8_t type);
// Deactivate display's CS line
//========================
esp_err_t disp_deselect();
// Activate display's CS line and configure SPI interface if necessary
//======================
esp_err_t disp_select();
// Find maximum spi clock for successful read from display RAM
// ** Must be used AFTER the display is initialized **
//======================
uint32_t find_rd_speed();
// Change the screen rotation.
// Input: m new rotation value (0 to 3)
//=================================
void _tft_setRotation(uint8_t rot);
// Initialize all pins used by display driver
// ** MUST be executed before SPI interface initialization
//=================
void TFT_PinsInit();
// Perform display initialization sequence
// Sets orientation to landscape; clears the screen
// * All pins must be configured
// * SPI interface must already be setup
// * 'tft_disp_type', 'COLOR_BITS', '_width', '_height' variables must be set
//======================
void TFT_display_init();
//===================
void stmpe610_Init();
//============================================================
int stmpe610_get_touch(uint16_t *x, uint16_t *y, uint16_t *z);
//========================
uint32_t stmpe610_getID();
void IRAM_ATTR TFT_pushColorRep2(int x1, int y1, int x2, int y2, color_t *color, uint32_t len);
void direct_send(color_t *color, uint32_t len);
void disp_spi_transfer_addrwin2(uint16_t x1, uint16_t x2, uint16_t y1, uint16_t y2);
// ===============================================================================
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment