|
本帖最后由 topdog 于 2021-4-13 00:56 编辑
[pre]#include <Elegoo_GFX.h> // https://github.com/Erutan409/Elegoo_GFX
#include <Elegoo_TFTLCD.h> // https://github.com/Erutan409/Elegoo_TFTLCD
#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 LIGHT_GREY 0xBDF7
#define DARK_GREY 0x7BEF
#define ORANGE 0xFBE0
#define BROWN 0x79E0
#define PINK 0xF81F
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// 全局变量
int valueBlock[200];
int timeBlock[200];
int locationBlock[200];
int valuePos;
int blockPos;
int temp;
int chk;
// 可编辑的变量
bool proDebug = 0;
uint16_t graphColor = BLUE;
uint16_t pointColor = BLACK;
uint16_t lineColor = GREEN;
String graphName = "Time Graph";
int graphRange = 50;
int markSize = 3;
// 计算拘束
const int numberOfMarks = 8;
const int originX = 45;
const int originY = 200;
const int sizeX = 270;
const int sizeY = 150;
const int deviation = 30;
int boxSize = (sizeX / numberOfMarks);
int mark[] = {(boxSize + deviation), ((boxSize * 2) + deviation), ((boxSize * 3) + deviation), ((boxSize * 4) + deviation), ((boxSize * 5) + deviation), ((boxSize * 6) + deviation), ((boxSize * 7) + deviation), ((boxSize * 8) + deviation)};
const int minorSizeY = (originY + 10);
const int minorSizeX = (originX - 10);
int numberSize = (sizeY / 6);
int number[] = {numberSize, (numberSize * 2), (numberSize * 3), (numberSize * 4), (numberSize * 5), (numberSize * 6)};
int numberValue = (graphRange / 6);
int val[] = {graphRange, (numberValue * 5), (numberValue * 4), (numberValue * 3), (numberValue * 2), numberValue};
void drawHome()
{
tft.fillScreen(BLACK);
delay(500);
tft.setCursor(10, 10);
tft.setTextColor(BLUE);
tft.setTextSize(5);
tft.println("Arduino CN");
tft.setCursor(10, 80);
tft.setTextColor(CYAN);
tft.setTextSize(3);
tft.println("Graphing");
tft.setCursor(30, 110);
tft.setTextColor(CYAN);
tft.setTextSize(2);
tft.println("History Graphs");
tft.setCursor(10, 160);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.println("ILI9341 LCD 240x320");
delay(4000);
tft.fillScreen(WHITE);
delay(500);
}
void drawGraph()
{
tft.fillScreen(WHITE);
// 绘制标题
tft.setCursor(10, 10); // 设置游标
tft.setTextColor(BLUE); // 设置文本的颜色
tft.setTextSize(4); // 设置文本的大小
tft.println(graphName);
// 绘制轮廓
tft.drawLine(originX, originY, (originX + sizeX), originY, graphColor);
tft.drawLine(originX, originY, originX, (originY - sizeY), graphColor);
// 绘制标记
for (int i = 0; i < numberOfMarks; i++)
{
tft.drawLine(mark, originY, mark, minorSizeY, graphColor);
}
// 绘制数字
for (int i = 0; i < 6; i++)
{
tft.drawLine(originX, (originY - number), minorSizeX, (originY - number), graphColor);
}
// 绘制数字的取值
for (int i = 0; i < 6; i++)
{
tft.setCursor((minorSizeX - 30), (number + numberSize));
tft.setTextColor(graphColor);
tft.setTextSize(1);
tft.println(val);
}
}
void graph()
{
timeBlock[valuePos] = ((millis() - 4500) / 1000);
temp += 1 ;
valueBlock[valuePos] = temp;
if (proDebug)
{
Serial.println(timeBlock[valuePos]);
}
if (blockPos < 8)
{
// 打印的时间
tft.setCursor((mark[valuePos] - 5), (originY + 16));
tft.setTextColor(graphColor, WHITE);
tft.setTextSize(1);
tft.println(timeBlock[valuePos]);
// 映射的值
locationBlock[valuePos] = map(temp, 0, graphRange, originY, (originY - sizeY));
// 画标点
tft.fillRect((mark[valuePos] - 1), (locationBlock[valuePos] - 1), markSize, markSize, pointColor);
// 尝试连接到前面的点
if (valuePos != 0)
{
tft.drawLine(mark[valuePos], locationBlock[valuePos], mark[(valuePos - 1)], locationBlock[(valuePos - 1)], lineColor);
}
blockPos++;
}
else
{
// 清除图形的画布
tft.fillRect((originX + 2), (originY - sizeY), sizeX, sizeY, WHITE);
// 映射值-当前点
locationBlock[valuePos] = map(temp, 0, graphRange, originY, (originY - sizeY));
//绘制点-当前点
tft.fillRect((mark[7]), (locationBlock[valuePos] - 1), markSize, markSize, pointColor);
// 画出所有点
for (int i = 0; i < 8; i++)
{
tft.fillRect((mark[(blockPos - (i + 1))] - 1), (locationBlock[(valuePos - i)] - 1), markSize, markSize, pointColor);
}
// 画出所有的线
for (int i = 0; i < 7; i++)
{
tft.drawLine(mark[blockPos - (i + 1)], locationBlock[valuePos - i], mark[blockPos - (i + 2)], locationBlock[valuePos - (i + 1)], lineColor);
}
// 改变时间标记
for (int i = 0; i <= 7; i++)
{
tft.setCursor((mark[(7 - i)] - 5), (originY + 16));
tft.setTextColor(graphColor, WHITE);
tft.setTextSize(1);
tft.println(timeBlock[valuePos - i]);
}
}
valuePos++;
}
void setup()
{
if (proDebug)
{
Serial.begin(9600);
while (!Serial) {};
}
tft.reset();
delay(500);
uint16_t identifier = tft.readID();
identifier = 0x9341;
tft.begin(identifier);
tft.setRotation(1);
drawHome();
drawGraph();
}
void loop()
{
graph();
delay(6000);
}[/pre] |
|