1

所以我有一个 Arduino MEGA2560 和一个 TFT 屏蔽触摸屏。我使用其中一个示例制作 2 个按钮以显示在屏幕上,只需使用drawRect(). 但是当我按下它们时,如何让这两个盒子做一些事情呢?我知道这 2 个盒子的坐标,那么如何让它们“感知”触摸并转移到另一个显示屏幕?也许代码示例会很有帮助!谢谢。

我当前的代码如下:您可以向其中添加必要的部分。

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library

#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);


void setup(void) {
  Serial.begin(9600);
  progmemPrintln(PSTR("TFT LCD"));

#ifdef USE_ADAFRUIT_SHIELD_PINOUT
  progmemPrintln(PSTR("Using Adafruit 2.8\" TFT Arduino Shield Pinout"));
#else
  progmemPrintln(PSTR("Using Adafruit 2.8\" TFT Breakout Board Pinout"));
#endif

  tft.reset();

  uint16_t identifier = tft.readID();

  if(identifier == 0x9325) {
    progmemPrintln(PSTR("Found ILI9325 LCD driver"));
  } else if(identifier == 0x9328) {
    progmemPrintln(PSTR("Found ILI9328 LCD driver"));
  } else if(identifier == 0x7575) {
    progmemPrintln(PSTR("Found HX8347G LCD driver"));
  } else {
    progmemPrint(PSTR("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    progmemPrintln(PSTR("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
    progmemPrintln(PSTR("  #define USE_ADAFRUIT_SHIELD_PINOUT"));
    progmemPrintln(PSTR("should appear in the library header (Adafruit_TFT.h)."));
    progmemPrintln(PSTR("If using the breakout board, it should NOT be #defined!"));
    progmemPrintln(PSTR("Also if using the breakout, double-check that all wiring"));
    progmemPrintln(PSTR("matches the tutorial."));
    return;
  }

  tft.begin(identifier);



  progmemPrint(PSTR("Text                     "));
  Serial.println(startText());
  delay(0);



  progmemPrintln(PSTR("Done!"));
}

void loop(void) {

    startText();
    delay(9999999);

}



unsigned long startText() {
  tft.fillScreen(BLACK);
  unsigned long start = micros();
  tft.setCursor(0, 0);
  tft.println();
  tft.println();
  tft.setTextColor(GREEN);    tft.setTextSize(2.8);
  tft.println("Welcome ");
  tft.println();
  tft.setTextColor(WHITE);    tft.setTextSize(2.5);

  tft.println();

  tft.drawRect(5, 150, 110, 110, YELLOW);
  tft.drawRect(130, 150, 110, 110, RED);
  tft.setCursor(155, 170);
  tft.setTextColor(RED);
  tft.println("OFF");
  tft.fillRect(5, 150, 110, 110, YELLOW);
  tft.fillRect(13, 158, 94, 94, BLACK);
  tft.setTextColor(GREEN);
  tft.setCursor(20, 170);
  tft.println("ON");

  return micros() - start;

}

// Copy string from flash to serial port
// Source string MUST be inside a PSTR() declaration!
void progmemPrint(const char *str) {
  char c;
  while(c = pgm_read_byte(str++)) Serial.print(c);
}

// Same as above, with trailing newline
void progmemPrintln(const char *str) {
  progmemPrint(str);
  Serial.println();
}
4

2 回答 2

1

您需要触摸屏库

#include <TouchScreen.h>
//inside loop  
    TSPoint p = ts.getPoint();
    // Retrieve a point  
    p = ts.getPoint();
    Serial.print("X = "); Serial.print(p.x);
    Serial.print("\tY = "); Serial.print(p.y);
    Serial.print("\tPressure = "); Serial.println(p.z);
于 2015-11-04T12:24:10.170 回答
-2

Here is an UART TFT LCD, it supports WYSIWYG editor to build your GUI in PC and download it via USB.

It can be controlled by Arduino via UART, so just use the Serial.print() you can make it display many images. And you can even build Font by yourself and download to the LCD.

See https://www.indiegogo.com/projects/nextion-a-cost-effective-high-performance-tft-hmi/x/4467372

于 2015-04-15T13:01:13.947 回答