|
楼主 |
发表于 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;
} |
|