关于触摸屏的那些事-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 815|回复: 4

关于触摸屏的那些事

[复制链接]
发表于 2022-7-28 11:09 | 显示全部楼层 |阅读模式
关于触摸屏网友eagler8谈的很透。现基于TFT_eSP和XPT2046作背景叽歪两句。触摸屏大致分为两类。既分离型和集成型。
      分离型:触摸屏与MCU分开的,需用线正确连接。此类共享SIP见图

/Users/tmld/Desktop/截屏2022-07-28 09.09.36.png

使用前需校正数据。TFT_eSP的校正程序Toucu_calibrat。示例程序有TFT_Button_labl_Datum,on_off_Button,keypad_240x320,keypad_480x320。
     集成型:触摸屏与MCU集成。此类大多独享SPI。速度快。网友fmzhangpei241  做的就是。需另外的驱动。下面是以修改好的驱动。不妥请砖正。
     编译时 警告: TFT_Touch-master 库要求运行在 avr 架构(),可能与你现在运行在 esp32 架构上的开发板()不兼容。不用管。
     编程时勿用上面示例程序中的特定指令,有莫名冲突。
/Users/tmld/Desktop/GMT024-10模块代码.zip
 楼主| 发表于 2022-7-28 11:29 | 显示全部楼层
截屏2022-07-28 09.09.36.png TFT_Touch-master.zip (20.4 KB, 下载次数: 0)

图片

图片

TFT_Touch-master.zip

20.4 KB, 下载次数: 0

 楼主| 发表于 2022-7-28 11:35 | 显示全部楼层
网友fmzhangpei241 的屏可用。其他不知道
 楼主| 发表于 2022-8-14 15:39 | 显示全部楼层
// Example of drawing a graphical "switch" and using
// the touch screen to change it's state.

// This sketch does not use the libraries button drawing
// and handling functions.

// Based on Adafruit_GFX library onoffbutton example.

// Touch handling for XPT2046 based screens is handled by
// the TFT_eSPI library.

// Calibration data is stored in SPIFFS so we need to include it
#include "FS.h"

#include <SPI.h>

#include <TFT_eSPI.h> // Hardware-specific library

TFT_eSPI tft = TFT_eSPI();       // Invoke custom library

#include <TFT_Touch.h>

// These are the pins used to interface between the 2046 touch controller and Arduino Pro
//独用SPI的触摸屏。
#define DOUT 39  /* Data out pin (T_DO) of touch screen */
#define DIN  32  /* Data in pin (T_DIN) of touch screen */
#define DCS  33  /* Chip select pin (T_CS) of touch screen */
#define DCLK 25  /* Clock pin (T_CLK) of touch screen */

/* Create an instance of the touch screen library */
TFT_Touch touch = TFT_Touch(DCS, DCLK, DIN, DOUT);

// This is the file name used to store the touch coordinate
// calibration data. Change the name to start a new calibration.
#define CALIBRATION_FILE "/TouchCalData3"

// Set REPEAT_CAL to true instead of false to run calibration
// again, otherwise it will only be done once.
// Repeat calibration if you change the screen rotation.
#define REPEAT_CAL false

bool SwitchOn = false;

// Comment out to stop drawing black spots
#define BLACK_SPO  

// Switch position and size
#define FRAME_X 100
#define FRAME_Y 64
#define FRAME_W 120
#define FRAME_H 50

// Red zone size
#define REDBUTTON_X FRAME_X
#define REDBUTTON_Y FRAME_Y
#define REDBUTTON_W (FRAME_W/2)
#define REDBUTTON_H FRAME_H

// Green zone size
#define GREENBUTTON_X (REDBUTTON_X + REDBUTTON_W)
#define GREENBUTTON_Y FRAME_Y
#define GREENBUTTON_W (FRAME_W/2)
#define GREENBUTTON_H FRAME_H

//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
void setup(void)
{
  Serial.begin(38400);//(9600);
  tft.init();

  // Set the rotation before we calibrate
  tft.setRotation(1);
  touch.setRotation(1);//屏与触摸板一致

  // call screen calibration  你的校正值,见TFT_Touch_Calbrate_V2.
  touch.setCal(526, 3443, 750, 3377, 320, 240, 1);

  // clear screen
  tft.fillScreen(TFT_BLUE);

  // Draw button (this example does not use library Button class)
  redBtn();
}
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
void loop()
{
  uint16_t x, y;

  // See if there's any touch data for us
  if (touch.Pressed())//
//  if (tft.getTouch(&x, &y))
  {
    x = touch.X();
    y = touch.Y();
    Serial.print(x); Serial.print(","); Serial.println(y);

    // Draw a block spot to show where touch was calculated to be
    #ifdef BLACK_SPOT
      tft.fillCircle(x, y, 2, TFT_BLACK);
    #endif
   
    if (SwitchOn)
    {
      if (((x > REDBUTTON_X) && (x < (REDBUTTON_X + REDBUTTON_W)))&&((y > REDBUTTON_Y) && (y <= (REDBUTTON_Y + REDBUTTON_H)))) {
        
          Serial.println("Red btn hit");
          redBtn();
      }
    }
    else //Record is off (SwitchOn == false)
    {
      if (((x > GREENBUTTON_X) && (x < (GREENBUTTON_X + GREENBUTTON_W)))&&((y > GREENBUTTON_Y) && (y <= (GREENBUTTON_Y + GREENBUTTON_H)))) {

          Serial.println("Green btn hit");
          greenBtn();
      }
    }

    Serial.println(SwitchOn);

  }
}
//------------------------------------------------------------------------------------------

void drawFrame()
{
  tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, TFT_BLACK);
}

// Draw a red button
void redBtn()
{
  tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, TFT_RED);
  tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, TFT_DARKGREY);
  drawFrame();
  tft.setTextColor(TFT_WHITE);
  tft.setTextSize(2);
  tft.setTextDatum(MC_DATUM);
  tft.drawString("ON", GREENBUTTON_X + (GREENBUTTON_W / 2), GREENBUTTON_Y + (GREENBUTTON_H / 2));
  SwitchOn = false;
}

// Draw a green button
void greenBtn()
{
  tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, TFT_GREEN);
  tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, TFT_DARKGREY);
  drawFrame();
  tft.setTextColor(TFT_WHITE);
  tft.setTextSize(2);
  tft.setTextDatum(MC_DATUM);
  tft.drawString("OFF", REDBUTTON_X + (REDBUTTON_W / 2) + 1, REDBUTTON_Y + (REDBUTTON_H / 2));
  SwitchOn = true;
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 08:31 , Processed in 0.083761 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表