I tried to make an icon for my TFT LCD ST7735 but when I uploaded only a white screen appeared for the image but for plain text it was normal. What is wrong?
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <SPI.h>
#include <SPIFFS.h> // Library for SPIFFS
// Pin definitions for ESP32
#define TFT_SDA 23 // SDA
#define TFT_RST 21 // RST
#define TFT_SCL 18 // SCL
#define TFT_DC 4 // A0
#define TFT_CS 17 // CS
#define TFT_LED 16 // LED+
// Create the TFT object
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_SDA, TFT_SCL, TFT_RST);
// Pin analog untuk sensor ACS712
const int BRAKE_PIN = 26;
const int RIGHT_R_PIN = 25;
const int LEFT_R_PIN = 33;
const int TURN_F_PIN = 32;
const int HAZARD_PIN = 35;
const int RF_PIN = 34;
const int LF_PIN = 39;
const int HORN = 36;
const float SENSITIVITY = 0.185; // Sensitivitas ACS712 (185 mV/A untuk 5A)
const float THRESHOLD = 0.1; // Ambang batas arus (dalam ampere)
bool allGood = true;
const int ICON_SIZE = 16; // Ukuran ikon (lebar dan tinggi)
const int ICON_SPACING = 4; // Jarak antar ikon
// Simbol ikon (Good dan Error) disimpan di SPIFFS
// Tidak lagi menggunakan PROGMEM, jadi penghapusan bagian PROGMEM tidak diperlukan
void setup(void) {
Serial.begin(9600);
Serial.print(F("Hello! ST77xx TFT Test"));
pinMode(TFT_LED, OUTPUT);
digitalWrite(TFT_LED, HIGH);
tft.initR(INITR_BLACKTAB); // Init ST7735S chip, black tab
Serial.println(F("Initialized"));
tft.setRotation(1); // Rotate display 90°
tft.fillScreen(ST77XX_BLACK);
// Inisialisasi SPIFFS
if(!SPIFFS.begin(true)){
Serial.println("SPIFFS Mount Failed");
return;
}
}
void loop() {
allGood = true; // Set allGood to true at the beginning of each loop
checkLamp("BRAKE", BRAKE_PIN);
checkLamp("RIGHT_R", RIGHT_R_PIN);
checkLamp("LEFT_R", LEFT_R_PIN);
checkLamp("TURN_F", TURN_F_PIN);
checkLamp("HAZARD", HAZARD_PIN);
checkLamp("RF", RF_PIN);
checkLamp("LF", LF_PIN);
checkLamp("HORN", HORN);
if (allGood) {
displayIconsAllGood();
}
Serial.println("--------------------");
delay(1000); // Tunggu 1 detik sebelum pengukuran berikutnya
}
void checkLamp(const char* lampName, int pin) {
float current = measureCurrent(pin);
Serial.print(lampName);
Serial.print(": ");
const char* status;
if (current < THRESHOLD) {
status = "ERROR";
allGood = false;
displayIcon(lampName, false); // Tampilkan ikon error di tengah
} else {
status = "GOOD";
if (allGood) {
displayIcon(lampName, true); // Tampilkan ikon good di atas
}
}
Serial.println(status);
}
float measureCurrent(int pin) {
int rawValue = analogRead(pin);
float voltage = (rawValue / 4095.0) * 3.3; // Konversi ke tegangan (ESP32 ADC: 12-bit, 3.3V)
float offsetVoltage = 1.65; // Tegangan offset ACS712 (setengah dari Vcc)
float current = (voltage - offsetVoltage) / SENSITIVITY;
return abs(current);
}
void displayIcon(const char* lampName, bool isGood) {
const char* iconFileName;
if (strcmp(lampName, "BRAKE") == 0) {
iconFileName = isGood ? "/brake_good.bmp" : "/brake_error.bmp";
} else if (strcmp(lampName, "RIGHT_R") == 0) {
iconFileName = isGood ? "/right_r_good.bmp" : "/right_r_error.bmp";
} else if (strcmp(lampName, "LEFT_R") == 0) {
iconFileName = isGood ? "/left_r_good.bmp" : "/left_r_error.bmp";
} else if (strcmp(lampName, "TURN_F") == 0) {
iconFileName = isGood ? "/turn_f_good.bmp" : "/turn_f_error.bmp";
} else if (strcmp(lampName, "HAZARD") == 0) {
iconFileName = isGood ? "/hazard_good.bmp" : "/hazard_error.bmp";
} else if (strcmp(lampName, "RF") == 0) {
iconFileName = isGood ? "/rf_good.bmp" : "/rf_error.bmp";
} else if (strcmp(lampName, "LF") == 0) {
iconFileName = isGood ? "/lf_good.bmp" : "/lf_error.bmp";
} else if (strcmp(lampName, "HORN") == 0) {
iconFileName = isGood ? "/horn_good.bmp" : "/horn_error.bmp";
} else {
return; // Jika nama lampu tidak sesuai, keluar dari fungsi
}
// Tentukan posisi ikon
int x = isGood ? ICON_SIZE * (8 - 1) + ICON_SPACING * (8 - 1) : (tft.width() - ICON_SIZE) / 2;
int y = isGood ? 0 : (tft.height() - ICON_SIZE) / 2;
// Gambar ikon dari file di SPIFFS
drawBmp(iconFileName, x, y);
}
void drawBmp(const char *filename, int16_t x, int16_t y) {
if ((x >= tft.width()) || (y >= tft.height())) return;
File bmpFile = SPIFFS.open(filename, "r");
if (!bmpFile) {
Serial.print(F("File not found: "));
Serial.println(filename);
return;
}
// Info header BMP
uint32_t bmpWidth, bmpHeight;
uint8_t bmpDepth, header[54];
// Read BMP header
if (bmpFile.read(header, 54) != 54) {
Serial.println(F("Not a BMP file"));
bmpFile.close();
return;
}
// Extract image dimensions and depth from header
bmpWidth = *(int32_t *)&header[18];
bmpHeight = *(int32_t *)&header[22];
bmpDepth = header[28];
if (bmpDepth != 24) {
Serial.println(F("Image is not 24-bit"));
bmpFile.close();
return;
}
// Set row size based on BMP format
uint32_t rowSize = (bmpWidth * 3 + 3) & ~3;
// Set starting position in the file
uint32_t bmpImageoffset = *(uint32_t *)&header[10];
bmpFile.seek(bmpImageoffset);
uint8_t sdbuffer[3 * ICON_SIZE];
uint8_t buffidx = sizeof(sdbuffer);
for (int i = 0; i < bmpHeight; i++) {
for (int j = 0; j < bmpWidth; j++) {
if (buffidx >= sizeof(sdbuffer)) {
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0;
}
uint8_t b = sdbuffer[buffidx++];
uint8_t g = sdbuffer[buffidx++];
uint8_t r = sdbuffer[buffidx++];
tft.drawPixel(x + j, y + (bmpHeight - 1 - i), tft.color565(r, g, b));
}
}
bmpFile.close();
}
void displayIconsAllGood() {
// Bersihkan layar
tft.fillScreen(ST77XX_BLACK);
// Tampilkan semua ikon "GOOD" secara berderet di atas layar
for (int i = 0; i < 8; i++) {
int x = i * (ICON_SIZE + ICON_SPACING);
int y = 0; // Posisikan di atas layar
// Misalnya untuk ikon BRAKE
drawBmp("/brake_good.bmp", x, y);
drawBmp("/right_r_good.bmp", x, y);
drawBmp("/left_r_good.bmp", x, y);
drawBmp("/turn_f_good.bmp", x, y);
drawBmp("/hazard_good.bmp", x, y);
drawBmp("/rf_good.bmp", x, y);
drawBmp("/lf_good.bmp", x, y);
drawBmp("/horn_good.bmp", x, y);
}
}