概要
arduinoでtouchscreenやってみた。
写真
サンプルコード
# include <Adafruit_GFX.h>
# include "Adafruit_TFTLCD.h"
# include "TouchScreen.h"
# define XP 8
# define YP A3
# define XM A2
# define YM 9
# define TS_MINX 150
# define TS_MAXX 920
# define TS_MINY 120
# define TS_MAXY 940
# define LCD_CS A3
# define LCD_CD A2
# define LCD_WR A1
# define LCD_RD A0
# define LCD_RESET A4
# define BLACK 0x0000
# define BLUE 0x001F
# define RED 0xF800
# define GREEN 0x07E0
# define CYAN 0x07FF
# define MAGENTA 0xF81F
# define YELLOW 0xFFE0
# define WHITE 0xFFFF
# define BOXSIZE 40
# define PENRADIUS 3
# define MINPRESSURE 10
# define MAXPRESSURE 1000
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
int oldcolor,
currentcolor;
void setup(void)
{
Serial.begin(115200);
Serial.println(F("Paint!"));
tft.reset();
uint16_t identifier = 0x9341;
tft.begin(identifier);
tft.fillScreen(BLACK);
tft.setRotation(2);
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN);
tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN);
tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE);
tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
currentcolor = RED;
}
void loop()
{
Point p = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
{
if (p.y < (TS_MINY - 5))
{
Serial.println("erase");
tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, BLACK);
}
p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
p.x += 5;
p.y += 18;
if (p.y < BOXSIZE)
{
oldcolor = currentcolor;
if (p.x < BOXSIZE)
{
currentcolor = RED;
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
}
else if (p.x < BOXSIZE * 2)
{
currentcolor = YELLOW;
tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
}
else if (p.x < BOXSIZE * 3)
{
currentcolor = GREEN;
tft.drawRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, WHITE);
}
else if (p.x < BOXSIZE * 4)
{
currentcolor = CYAN;
tft.drawRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, WHITE);
}
else if (p.x < BOXSIZE * 5)
{
currentcolor = BLUE;
tft.drawRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, WHITE);
}
else if (p.x < BOXSIZE * 6)
{
currentcolor = MAGENTA;
tft.drawRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, WHITE);
}
if (oldcolor != currentcolor)
{
if (oldcolor == RED) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
if (oldcolor == YELLOW) tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
if (oldcolor == GREEN) tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN);
if (oldcolor == CYAN) tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN);
if (oldcolor == BLUE) tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE);
if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);
}
}
if (((p.y - PENRADIUS) > BOXSIZE) && ((p.y + PENRADIUS) < tft.height()))
{
tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
}
}
}
以上。
