#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <TimeLib.h>
// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
//time
#define TIME_MSG_LEN 11 //時間字串長度含標頭 T 為 11 字元 (T+10位數之 Unix 時戳)
#define TIME_HEADER 'T' //時間同步訊息標頭
#define TIME_REQUEST 7 //ASCII bell character requests a time sync message
//時間字串格式 T1262347200 //noon Jan 1 2010
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// If using the breakout, change pins as desired
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
//buttom pin
const int button1 = 7;
const int button2 = 8;
int buttonState1 = 0;
int buttonState2 = 0;
int previousButtonState1 = 0;
int previousButtonState2 = 0;
int outputNumber1 = 0;
int outputNumber2 = 0;
int previousValue = 0;//分針暫存器
int previousValue1 = 0;//時針暫存器
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup()
{
Serial.begin(9600);
Serial.println("ILI9341 Test!");
tft.begin();
// read diagnostics (optional but can help debug problems)
uint8_t x = tft.readcommand8(ILI9341_RDMODE);
/*tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE); tft.setTextSize(1);
tft.println("Hello World!");
tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(2);
tft.println(1234.56);
tft.setTextColor(ILI9341_RED); tft.setTextSize(3);
tft.println(0xDEADBEEF, HEX);
tft.println();*/
tft.setCursor(0, 10);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(5);
tft.println("Now time");
//current time
int16_t xx = tft.width() / 2; // 圆心 x 坐标
int16_t yy = tft.height() / 1.7; // 圆心 y 坐标
int16_t radius1 = 110; // 圆的半径
uint16_t color1 = ILI9341_RED; // 圆的颜色
tft.fillCircle(xx, yy, radius1, color1);
int16_t aa = tft.width() / 2; // 圆心 x 坐标
int16_t bb = tft.height() / 1.7; // 圆心 y 坐标
int16_t radius2 = 105; // 圆的半径
uint16_t color2 = ILI9341_BLACK; // 圆的颜色
tft.fillCircle(aa, bb, radius2, color2);
tft.setRotation(0);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2); // 调整字体大小
tft.setCursor(0, 35);
int Lx1 = 125;
int Ly1 = 120;
int Lx2 = 220;
int Ly2 = 185;
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.drawLine(Lx1, Ly1, Lx2, Ly2, ILI9341_RED);
/*tft.setTextSize(2);
tft.println("12:51");
tft.setTextSize(1);
tft.println("my foonting turlingdromes.");
tft.println("And hooptiously drangle me");
tft.println("with crinkly bindlewurdles,");
tft.println("Or I will rend thee");
tft.println("in the gobberwarts");
tft.println("with my blurglecruncheon,");
tft.println("see if I don't!");*/
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void loop()
{
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
if (buttonState1 != previousButtonState1)
{
if (buttonState1 == HIGH)
{
outputNumber1 = (outputNumber1 + 1) % 5; // 循环生成0到4之间的数字
// 根据outputNumber设置不同的输出
switch (outputNumber1)
{
case 1:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 150);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(5);
tft.println("Hello");
Serial.println(outputNumber1);
break;
case 2:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 150);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(5);
tft.println("good");
Serial.println(outputNumber1);
break;
case 3:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 150);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(5);
tft.println("line");
Serial.println(outputNumber1);
break;
case 4:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 150);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(5);
tft.println("XYZabc");
Serial.println(outputNumber1);
break;
case 5:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 150);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(5);
tft.println("bad");
Serial.println(outputNumber1);
break;
}
}
}
previousButtonState1 = buttonState1;
if (outputNumber1 == 1)
{
while (buttonState2 == HIGH)
{
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 150);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(5);
tft.println("World");
Serial.println(outputNumber2);
break;
}
}
int hours = hour();
int minutes = minute();
int seconds = second();
// 格式化时间字符串
/*char timeStr[9]; // HH:MM:SS
sprintf(timeStr, "%02d:%02d:%02d", hours, minutes, seconds);
//Serial.println(seconds);
// 清除之前的时间并显示新时间
tft.fillRect(0, 50, tft.width(), 15, ILI9341_BLACK);
tft.setCursor(0, 50);
tft.print(timeStr);
// 可以添加延迟以更新时间显示频率
delay(1000); // 1秒更新一次时间*/
//秒針
int xx1 = 125;
int y1 = 120;
//int xx2 = 220;
//int y2 = 185;
uint16_t color3 = ILI9341_RED;
uint16_t color4 = ILI9341_BLACK;
float angle; // 旋转角度
float angle1;// 旋转角度時鐘
float radians; // 角度转弧度
int lineLength = 107; // 直线的长度
int xx2;// 终点 x 坐标
int y2;// 终点 y 坐标
tft.setRotation(1);
//int xx3;// 消除终点鬼影 x 坐标
//int y3;// 消除终点鬼影 y 坐标
//分針
int xxx1 = 125;
int yy1 = 120;
int xxx2;
int yy2;
int lineLength2 = 80;
float angle2 = 0; // 旋转角度
float angle3 = 0;// 旋转角度時鐘
float angle4 = 0; // 鬼影旋转角度
//時針
int xxxx1 = 125;
int yyy1 = 120;
int xxxx2;
int yyy2;
int lineLength3 = 110;
float angle5 = 0; // 旋转角度
float angle6 = 0; // 鬼影旋转角度
/*tft.setCursor(0, 20);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
tft.println(previousValue1);
tft.setCursor(0, 25);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
tft.println(previousValue);*/
//tft.drawLine(xx1, y1, xx2, y2, ILI9341_WHITE);
for(angle = 1;angle < 61;angle++)
{
angle1 = angle * 6;
radians = (angle1 * 3.141592 / 180);
xx2 = xx1 + cos(radians) * lineLength;
y2 = y1 + sin(radians) * lineLength;
tft.drawLine(xx1, y1, xx2, y2, ILI9341_WHITE);
delay(1);
tft.drawLine(xx1, y1, xx2, y2, ILI9341_BLACK);
//xx3 = xx2;
//y3 = y2;
//tft.drawLine(xx1, y1, xx3, y3, ILI9341_BLACK);
if(angle1 == 360)
{
//angle2 = calculateSomething(previousValue);
//angle2++;
previousValue++;
angle3 = previousValue *6;
radians = (angle3 * 3.141592 / 180);
xxx2 = xxx1 + cos(radians) * lineLength2;
yy2 = yy1 + sin(radians) * lineLength2;
tft.drawLine(xxx1, yy1, xxx2, yy2, ILI9341_YELLOW);
Serial.println(previousValue);
Serial.println(previousValue1);
angle4 = (previousValue *6) - 6;
radians = (angle4 * 3.141592 / 180);
xxx2 = xxx1 + cos(radians) * lineLength2;
yy2 = yy1 + sin(radians) * lineLength2;
tft.drawLine(xxx1, yy1, xxx2, yy2, ILI9341_BLACK);
}
else if(previousValue == 60)
{
previousValue1++;
angle5 = previousValue1 *30;
radians = (angle5 * 3.141592 / 180);
xxxx2 = xxxx1 + cos(radians) * lineLength3;
yyy2 = yyy1 + sin(radians) * lineLength3;
tft.drawLine(xxxx1, yyy1, xxxx2, yyy2, ILI9341_BLUE);
angle6 = (previousValue1 *30) - 30;
radians = (angle6 * 3.141592 / 180);
xxxx2 = xxxx1 + cos(radians) * lineLength3;
yyy2 = yyy1 + sin(radians) * lineLength3;
tft.drawLine(xxxx1, yyy1, xxxx2, yyy2, ILI9341_BLACK);
previousValue = 0;
}
tft.setCursor(0, 30);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
tft.println(previousValue);
//tft.fillRect(0, 30, 2, 15, ILI9341_BLACK);
//tft.setCursor(0, 30);
}
/*delay(60000);
xxx2 = xx2* lineLength2;
yy2 = y2* lineLength2;
tft.drawLine(xxx1, yy1, xxx2, yy2, ILI9341_WHITE);*/
}