/*
* タッチセンサーの動作確認
*/
#include <Adafruit_GFX.h> //
https://github.com/adafruit/Adafruit-GFX-Library
#include
<Fontx.h>
// https://github.com/h-nari/Fontx
#include <Humblesoft_GFX.h>
// https://github.com/h-nari/Humblesoft_GFX
#include <Humblesoft_ILI9341.h> //
https://github.com/h-nari/Humblesoft_ILI9341
#include <XPT2046_Touchscreen.h>
//https://github.com/PaulStoffregen/XPT2046_Touchscreen
#define CS_PIN 16 // ChipSelect for TouchScreen
#define LOWER 1000
#define MIDLE 3000
//フォントファイルのパスは絶対パス
IMPORT_BIN("/fontx/ILGH16XB.FNT", ILH16XB);
//16ドット半角ゴシックフォント
IMPORT_BIN("/fontx/ILGZ16XB.FNT", ILZ16XB);
//16ドット全角ゴシックフォント
//IMPORT_BIN("/fontx/ILMH16XB.FNT", ILH16XB);
//16ドット半角明朝フォント
//IMPORT_BIN("/fontx/ILMZ16XB.FNT", ILZ16XB);
//16ドット全角明朝フォント
extern const uint8_t ILH16XB[], ILZ16XB[];
Humblesoft_ILI9341 tft = Humblesoft_ILI9341();
RomFontx fontx(ILH16XB,ILZ16XB);
XPT2046_Touchscreen ts(CS_PIN);
void drawTextAndBounds(int16_t cx, int16_t cy, char *str,
uint16_t color)
{
int16_t x, y;
uint16_t w, h;
// tft.getTextBounds(str, cx, cy, &x, &y,
&w, &h);
// tft.drawRect(x, y, w, h, ILI9341_RED);
tft.setCursor(cx, cy);
tft.setTextColor(color);
tft.print(str);
}
void showPosition(uint16_t pos, uint16_t color) {
if (pos == 1) drawTextAndBounds(60,100,
"右下です",color);
if (pos == 2) drawTextAndBounds(60,100,
"右です",color);
if (pos == 3) drawTextAndBounds(60,100,
"右上です",color);
if (pos == 11) drawTextAndBounds(60,100,
"下です",color);
if (pos == 12) drawTextAndBounds(60,100,
"中央です",color);
if (pos == 13) drawTextAndBounds(60,100,
"上です",color);
if (pos == 21) drawTextAndBounds(60,100,
"左下です",color);
if (pos == 22) drawTextAndBounds(60,100,
"左です",color);
if (pos == 23) drawTextAndBounds(60,100,
"左上です",color);
}
uint16_t white,black;
void setup()
{
delay(500);Serial.begin(9600);
Serial.println();
Serial.println("Reset:");
white = tft.rgb("white");
black = tft.rgb("black");
tft.begin();
tft.setRotation(3);
tft.fillScreen("WHITE");
tft.setTextSize(2);
// tft.setCursor(20, 20);
tft.setTextColor("BLUE");
tft.setFont(&fontx);
tft.print("画面をタッチ\nしてください");
tft.setTextSize(3);
// drawTextAndBounds(60,100, "右上です",black);
ts.begin();
while (!Serial && (millis() <= 1000));
}
void loop(){
static int prev=0;
if (ts.touched()) {
TS_Point p = ts.getPoint();
Serial.print("Pressure = ");
Serial.print(p.z);
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.print(p.y);
Serial.println();
if (p.x < LOWER) {
if (p.y < LOWER) {
showPosition(prev, white);
prev=1;
showPosition(prev, black);
} else if (p.y < MIDLE)
{
showPosition(prev, white);
prev=2;
showPosition(prev, black);
} else {
showPosition(prev, white);
prev=3;
showPosition(prev, black);
}
} else if (p.x < MIDLE) {
if (p.y < LOWER) {
showPosition(prev, white);
prev=11;
showPosition(prev, black);
} else if (p.y < MIDLE)
{
showPosition(prev, white);
prev=12;
showPosition(prev, black);
} else {
showPosition(prev, white);
prev=13;
showPosition(prev, black);
}
} else {
if (p.y < LOWER) {
showPosition(prev, white);
prev=21;
showPosition(prev, black);
} else if (p.y < MIDLE)
{
showPosition(prev, white);
prev=22;
showPosition(prev, black);
} else {
showPosition(prev, white);
prev=23;
showPosition(prev, black);
}
}
delay(100);
}
}
|