【问题标题】:Arduino TFT Touch shield: draw in loopArduino TFT 触摸屏:循环绘制
【发布时间】:2017-06-14 22:54:10
【问题描述】:

我正在尝试使用 TFT 触摸板构建菜单。现在我的问题是我无法在循环函数中绘制任何东西。

如果我在循环中编写任何绘图函数,屏幕会变白。我认为这是因为屏幕需要一些时间来构建。所以我加了一个延迟(1000)。但随后屏幕每秒闪烁一次,这显然也不是我想要的。

下一个奇怪的事情是,一旦触摸显示屏,程序就会停止工作。在下面的代码中,我有三个绘图函数。其中两个在工作,一个不在。 (见 cmets)

#include <SeeedTouchScreen.h>

#include <TFTConsole.h>
#include <Adafruit_TFTLCD.h>
#include <Adafruit_GFX.h>


#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define TS_MINX 169
#define TS_MINY 208
#define TS_MAXX 1781
#define TS_MAXY 1820

#define YP A2  // must be an analog pin, use "An" notation!
#define XM A3  // must be an analog pin, use "An" notation!
#define YM 8   // can be a digital pin
#define XP 9   // can be a digital 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);
TouchScreen ts = TouchScreen(XP, YP, XM, YM);
boolean touched = false;


void setup() {
  Serial.begin(9600);
  Serial.print("Starting...");

  tft.reset();

  tft.begin(0x9325);

  tft.setRotation(1);

  tft.fillScreen(BLACK);

  //Print "PPM CO" Text
  tft.setCursor(50, 30);
  tft.setTextColor(GREEN);
  tft.setTextSize(3);
  tft.print("Hello World"); //<- this is displayed fine
  delay(1000);
}
boolean first = true;
void loop() {
  if (first) {
    first = false;
    tft.drawCircle(119, 160, 20, random(0xFFFF)); //<- This is also displayed
  }


  Point p = ts.getPoint();


  p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
  p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
  if (p.x <= 240 && p.y <= 320 && p.x >= 0 && p.y >= 0) {
    Serial.println("don't touch me!");
    touched = true;
  }
  else {
    touched = false;
  }
  if (touched) {

    tft.drawCircle(119, 180, 20, RED); //<- This is not displayed and makes the screen flash
    delay(1000);
  }


  //tft.fillScreen(BLUE);
  //delay(500);

}

只有当我在代码中有最后一个 drawCircle 时,如果我停止触摸显示屏,变量 touched 不会切换回 false。

有人知道我做错了什么吗?

更新:我通过让触摸事件仅在触摸显示屏时发生一次来阻止显示屏闪烁。但是我仍然遇到没有绘制任何内容的问题...

void loop() {
  //Touchposition bestimmen
  Point p = ts.getPoint();
  p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
  p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
  if (p.x <= 240 && p.y <= 320 && p.x >= 0 && p.y >= 0) {
    if (released) {
      touched = true;
      released = false;
    }
  }
  else {
    //Serial.println("Touch me where I like it!");
    touched = false;
    released = true;
  }
  if (touched && !released) {
    Serial.println("don't touch me!");
    drawButton(100, 100, "Manuell");
    touched = false;
    delay(500);
  }

}

【问题讨论】:

    标签: c++ arduino-uno touchscreen


    【解决方案1】:

    我注意到 TFT 显示屏和触摸屏共享一些模拟引脚。我认为这是造成问题的原因。 所以我把我的代码分成了处理触摸屏的部分。然后我可以将引脚重新分配给 TFT 监视器并绘制它。我不知道这个理论是否有道理,但它是有效的:

     void loop() {
      //Touchposition bestimmen
      if (ts.isTouching()) {
        Point p = ts.getPoint();
        p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
        p.y = map(p.x, TS_MINY, TS_MAXY, 0, 320);
        if (released) {
          released = false;
        }
      }
      else {
        //Serial.println("Touch me where I like it!");
        released = true;
      }
    
      if (ts.isTouching() && !released) {
        //re assing pins to tft because they are also used by the touchscreen
        Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
        Serial.println("don't touch me!");
        drawButton(100, 100, "Manuell");
        touched = false;
      }
    }
    

    drawButton funton 包含几个绘图函数

    【讨论】:

      猜你喜欢
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多