Arduino 上手教程
Tab5 触摸屏相关API与案例程序。
#include <M5Unified.h>#include <M5GFX.h> m5::touch_detail_t touchDetail;static int32_t w;static int32_t h; LGFX_Button button; void setup() { M5.begin(); w = M5.Lcd.width(); h = M5.Lcd.height(); M5.Lcd.fillScreen(WHITE); M5.Display.setRotation(0); M5.Display.setTextDatum(top_center); M5.Display.drawString("Button Released", w / 2, 0, &fonts::FreeMonoBold24pt7b); button.initButton(&M5.Lcd, w / 2, h / 2, 200, 200, TFT_BLUE, TFT_YELLOW, TFT_BLACK, "BTN", 4, 4); button.drawButton();} void loop() { M5.update(); touchDetail = M5.Touch.getDetail(); if (touchDetail.isPressed()) { if(button.contains(touchDetail.x, touchDetail.y)){ M5.Display.drawString("Button Pressed", w / 2, 0, &fonts::FreeMonoBold24pt7b); } } else { M5.Display.drawString("Button Released", w / 2, 0, &fonts::FreeMonoBold24pt7b); }}
该程序的功能是,当手指触摸屏幕时,若触摸点在按钮区域内,则显示“Button Pressed”,否则显示“Button Released”。
程序中只读取了一个触摸点,您也可以通过下述API开发使用Tab5的两点触控功能。
Tab5 触摸屏部分使用了 M5Unified
库中的 Touch_Class
,更多相关的API可以参考下方文档: