// 参照: https://wokwi.com/projects/375773409328174081
#include <Arduino.h>
// # include <structs.h>
#include <Adafruit_GFX.h> // 核となるグラフィックライブラリ
#include <SPI.h> // 表示に SPI を使用
#include <Adafruit_ILI9341.h>
#include <Wire.h> // FT6206 を使用するために必要
#include <Adafruit_FT6206.h>
// TODO: "7b" の意味が不明 -> 解決次第追記
#include <Fonts/FreeMonoBoldOblique12pt7b.h> // 等幅, 太字, 斜め, 12pt
#include <Fonts/FreeSerif9pt7b.h> // セリフ体, 9pt
#include <Fonts/FreeMono9pt7b.h> // 等幅, 9pt
// FT6206 はハードウェアの I2C (SCL/SDA) を使用
Adafruit_FT6206 ft6206 = Adafruit_FT6206();
// ディスプレイは SPI 及び 9, 10 番ピンを使用
const uint8_t DC_PIN = 9;
const uint8_t CS_PIN = 10;
Adafruit_ILI9341 tft = Adafruit_ILI9341(CS_PIN, DC_PIN);
int currentScreenIndex = 0;
uint16_t buttonTextX(uint16_t rectangleWidth, uint16_t strLength) {
return ((rectangleWidth - 8 * strLength) / 2);
}
uint16_t buttonTextY(uint16_t rectangleHeight, uint16_t strSize, uint16_t line, uint16_t of) {
if(of == 1) {
return ((rectangleHeight - strSize * 10) / 2 + 10);
}
if(of == 2) {
if(line == 1) {
return ((rectangleHeight - strSize * 10) / 2);
}
if(line == 2) {
return (2 * (rectangleHeight - strSize * 10) / 2);
}
}
return 0;
}
class Button {
public:
Button(bool isLatching, uint16_t x, uint16_t y, uint16_t l, uint16_t w, uint16_t backgroundColor, uint16_t fontColor, uint16_t fontSize);
void click(uint16_t x, uint16_t y);
void drawButton(String textLine1, String textLine2);
private:
bool _isLatching;
uint16_t _x;
uint16_t _y;
uint16_t _l;
uint16_t _w;
uint16_t _backgroundColor;
uint16_t _fontColor;
uint16_t _fontSize;
};
Button::Button(bool isLatching, uint16_t x, uint16_t y, uint16_t l, uint16_t w, uint16_t backgroundColor, uint16_t fontColor, uint16_t fontSize) {
_isLatching = isLatching;
_x = x;
_y = y;
_l = l;
_w = w;
_backgroundColor = backgroundColor;
_fontColor = fontColor;
_fontSize = fontSize;
};
void Button::click(uint16_t x, uint16_t y) {};
void Button::drawButton(String textLine1, String textLine2) {
// 半径
tft.fillRoundRect(&this->_x, &this->_y, &this->_l, &this->_w, 2, &this->_backgroundColor);
tft.fillRoundRect(_x, _y, _l, _w, 2, _backgroundColor);
tft.setTextSize(_fontSize);
tft.setTextColor(_fontColor);
if(textLine2.length() > 0) {
tft.setCursor((_x + buttonTextX(_l, textLine1.length())), (_y + buttonTextY(_w, _fontSize, 1, 2)));
tft.print(textLine1);
tft.setCursor((_x + buttonTextX(_l, textLine2.length())), (_y + buttonTextY(_w, _fontSize, 2, 2)));
tft.print(textLine2);
} else {
tft.setCursor((_x + buttonTextX(_l, textLine1.length())), (_y + buttonTextY(_w, _fontSize, 1, 1)));
Serial.println(buttonTextX(_l, textLine1.length()));
tft.print(textLine1);
};
};
// isLatching x y l w backgroundColor fontColor fontSize
Button measureButton( false, 0, 35, 100, 40, ILI9341_BLUE, ILI9341_YELLOW, 1);
Button forceTareButton( false, 0, 85, 100, 40, ILI9341_BLUE, ILI9341_YELLOW, 1);
Button distanceTareButton(false, 0, 135, 100, 40, ILI9341_BLUE, ILI9341_YELLOW, 1);
Button calibrationButton( false, 0, 185, 100, 40, ILI9341_BLUE, ILI9341_YELLOW, 1);
void drawHome() {
tft.fillScreen(ILI9341_BLACK);
// Adafruit_GFX_Button HMeasButton, HSetTareButton, HNavECButton, HNavCalButton;
currentScreenIndex = 0;
tft.setTextSize(2);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(55, 25);
tft.print("Shock Force"); // 衝撃力
measureButton.drawButton("Measure", "This"); // 測定
forceTareButton.drawButton("Force", "Tare"); // 力風袋
distanceTareButton.drawButton("Distance", "Tare"); // 距離風袋
calibrationButton.drawButton("Calibrate", ""); // 校正
/*
int16_t x,
int16_t y,
uint16_t w,
uint16_t h,
uint16_t outline,
uint16_t fill,
uint16_t textcolor,
char* label,
uint8_t textsize
HMeasBtn.initButton(&tft, 100, 100, 100, 50, ILI9341_YELLOW, ILI9341_YELLOW, ILI9341_BLUE, "Enter Calibration Number", 1);
HMeasBtn.drawButton(true);
*/
/*
tft.fillRoundRect(0, 40, 100, 40, 2, ILI9341_BLUE);
tft.setTextSize(1);
tft.setCursor(15, 55);
tft.print("Load Cell");
tft.setCursor(10, 75);
tft.print("Calibration");
*/
//tft.fillRoundRect(130, 40, 100, 40, 2, ILI9341_BLUE);
//tft.fillRoundRect( 10, 100, 100, 40, 2, ILI9341_BLUE);
//tft.fillRoundRect(130, 100, 100, 40, 2, ILI9341_BLUE);
}
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(1);
if(!ft6206.begin(40)) { // 感度係数を渡す
Serial.print("FT6206 を開始できません");
while(1);
}
Serial.println("静電容量式タッチスクリーンが起動しました");
tft.setFont(&FreeSerif9pt7b);
// コードページ 437 (CP437) を使用
tft.cp437(true);
// ホーム画面の描画
drawHome();
}
void loop() {
// タッチされるまで待つ
if(!ft6206.touched()) {
return;
}
// 点を取得
TS_Point p = ft6206.getPoint();
/*
// スクリーンタッチコントローラから生データを出力
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print(" -> ");
*/
// スクリーンに合わせて回転
/*
map(value, fromLow, fromHigh, toLow, toHigh)
value : マップする数値
fromLow : 現在の値の範囲の下限
fromHigh: 現在の値の範囲の上限
toLow : 目標値の範囲の下限
toHigh : 目標値の範囲の上限
*/
p.x = map(p.x, 0, 240, 240, 0);
//p.y = map(p.y, 0, 1, 0, 1);
// 再マップした (回転させた) 座標を出力
/*
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
*/
// 半径
tft.fillCircle(p.y, p.x, 3, ILI9341_YELLOW);
}
Loading
ili9341-cap-touch
ili9341-cap-touch