T-Display라는게 집에 굴러 다니고... 전 USB케이블이 데이터 통신 되는지 검사 하다가... 하는 김에 이걸 테스터로 쓸 수 없나 하다가... 조금 코드를 만졌더니 야밤이네요. 원래 목적은 이미 기억에 없습니다.
코딩은 주로 제미나이가... 저는 잔소리랑 인간 디버거 역할이었습니다.
환경 잠깐 만들때 Grok과 Chat-GPT가 상관없는 이야기로 우기는 통에 좀 시간 날렸구요... (그냥 환경이 낡아서 전부 업뎃이 필요했다는 것 뿐이었단 결론)
code_InfoAndBtnDblBuffer.ino
#include <TFT_eSPI.h>
#include <SPI.h>
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite sprite = TFT_eSprite(&tft);
bool lastBtn0State = HIGH;
bool lastBtn35State = HIGH;
int ballX, ballY;
int ballVX, ballVY;
int ballRadius = 10;
int ballColor = TFT_MAGENTA;
unsigned long previousMillis = 0;
unsigned long frameCount = 0; // 초당 프레임 수 계산용
unsigned long totalFrameCount = 0; // 시작부터 누적된 총 프레임 수
float frameRate = 0.0;
void drawAllElements();
void setup() {
// 시리얼 통신 초기화
Serial.begin(115200);
pinMode(TFT_BL, OUTPUT);
digitalWrite(TFT_BL, HIGH);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
sprite.createSprite(tft.width(), tft.height());
pinMode(0, INPUT_PULLUP);
pinMode(35, INPUT_PULLUP);
randomSeed(millis());
ballX = random(ballRadius, tft.width() - ballRadius);
ballY = random(ballRadius, tft.height() - ballRadius);
ballVX = 2;
ballVY = 2;
Serial.println("System Initialized");
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000) {
frameRate = (float)frameCount / ((float)(currentMillis - previousMillis) / 1000.0);
//frameRate = frameCount; // 근사치로 보고, 1초 동안의 프레임 수를 바로 FPS로 사용
// 시리얼 모니터로 프레임 정보 출력
Serial.print("FPS: ");
Serial.print(frameRate, 4);
Serial.print(" | Total Frames: ");
Serial.print(totalFrameCount);
Serial.print(" | Free Heap: ");
Serial.print(ESP.getFreeHeap());
Serial.println(" B");
frameCount = 0;
previousMillis = currentMillis;
}
frameCount++;
totalFrameCount++; // 총 프레임 수 증가
ballX += ballVX;
ballY += ballVY;
if (ballX + ballRadius >= tft.width() || ballX - ballRadius <= 0) {
ballVX *= -1;
ballX = constrain(ballX, ballRadius, tft.width() - ballRadius);
}
if (ballY + ballRadius >= tft.height() || ballY - ballRadius <= 0) {
ballVY *= -1;
ballY = constrain(ballY, ballRadius, tft.height() - ballRadius);
}
drawAllElements();
bool currentBtn0State = digitalRead(0);
bool currentBtn35State = digitalRead(35);
if (currentBtn0State != lastBtn0State || currentBtn35State != lastBtn35State) {
lastBtn0State = currentBtn0State;
lastBtn35State = currentBtn35State;
}
sprite.pushSprite(0, 0);
delay(1);
}
void drawAllElements() {
sprite.fillSprite(TFT_BLACK);
sprite.fillCircle(ballX, ballY, ballRadius, ballColor);
int lineHeight = 15;
int currentY = 10;
sprite.setTextColor(TFT_NAVY);
sprite.setTextSize(2);
sprite.setCursor(10, currentY);
sprite.println("ESP32 System Info");
currentY += lineHeight;
sprite.setTextColor(TFT_DARKGREEN);
sprite.setCursor(10, currentY);
sprite.print("Flash Size: ");
sprite.print(ESP.getFlashChipSize() / 1024 / 1024);
sprite.println(" MB");
currentY += lineHeight;
sprite.setTextColor(TFT_ORANGE);
sprite.setCursor(10, currentY);
sprite.print("Chip Rev: ");
sprite.println(ESP.getChipRevision());
currentY += lineHeight;
sprite.setCursor(10, currentY);
sprite.print("Resolution: ");
sprite.print(tft.width());
sprite.print("x");
sprite.println(tft.height());
currentY += lineHeight;
sprite.setCursor(10, currentY);
sprite.setTextColor(TFT_WHITE);
sprite.print("Board Btn: ");
if (digitalRead(0) == LOW) {
sprite.setTextColor(TFT_GREEN);
sprite.print("ON");
} else {
sprite.setTextColor(TFT_ORANGE);
sprite.print("OFF");
}
sprite.setTextColor(TFT_WHITE);
sprite.print("/");
if (digitalRead(35) == LOW) {
sprite.setTextColor(TFT_GREEN);
sprite.println("ON");
} else {
sprite.setTextColor(TFT_ORANGE);
sprite.println("OFF");
}
currentY += lineHeight;
sprite.setCursor(10, currentY);
sprite.setTextColor(TFT_WHITE);
sprite.print("Free Heap:");
sprite.print(ESP.getFreeHeap() / 1024.0, 1);
sprite.println(" KB");
currentY += lineHeight;
sprite.setCursor(10, currentY);
sprite.setTextColor(TFT_WHITE);
sprite.print("FPS:");
sprite.print(frameRate, 4);
sprite.print(" (");
sprite.print(totalFrameCount);
sprite.println(")");
}
음... 버튼이 쓸 수 있는게 2개 있으니 테트리스나 뱀게임을 만들 수 있을지도요...
3.7v배터리의 충방전을 지원한다니 하나 붙혀보면 진짜 모바일로 쓸 수 있으려나요...