Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colors do not match for 180x60 ST7735S tft LCD #3582

Open
BimoSora2 opened this issue Dec 16, 2024 · 1 comment
Open

Colors do not match for 180x60 ST7735S tft LCD #3582

BimoSora2 opened this issue Dec 16, 2024 · 1 comment

Comments

@BimoSora2
Copy link

Color not suitable for 180x60 tft LCD, uint16_t textColor = safeColor565(143, 255, 255); turns yellow no blue and on the esp3266 there are line gaps on the LCD that are not full
20241216_201825

#include <Adafruit_ST7735.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <SPI.h>
#include <Wire.h>

#define TFT_MOSI D7
#define TFT_SCLK D5
#define TFT_CS D8
#define TFT_DC D6
#define TFT_RST D3
#define MARGIN 5
#define TEXT_HEIGHT 10

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
Adafruit_BMP280 bmp;
Adafruit_MPU6050 mpu;

float temperature = 0, pressure = 0, altitude = 0;

String tempC, tempF, pressureStr, altitudeStr;
String accX = "X: 0", accY = "Y: 0", accZ = "Z: 0";
String prevTempC, prevTempF, prevPressureStr, prevAltitudeStr;
String prevAccX = "", prevAccY = "", prevAccZ = "";

bool bmpAvailable = false;
bool mpuAvailable = false;
bool isInverted = true;

unsigned long sensorMillis1 = 0;
const long sensorInterval1 = 1000;

unsigned long sensorMillis2 = 0;
const long sensorInterval2 = 1000;

uint16_t safeColor565(int r, int g, int b) {
    r = constrain(r, 0, 255);
    g = constrain(g, 0, 255);
    b = constrain(b, 0, 255);
    uint8_t r5 = round(r * 31.0 / 255.0);
    uint8_t g6 = round(g * 63.0 / 255.0);
    uint8_t b5 = round(b * 31.0 / 255.0);
    return (r5 << 11) | (g6 << 5) | b5;
}

void centerText(const String& text, int y, uint16_t textColor) {
    int16_t x1, y1;
    uint16_t w, h;
    tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
    int x = (tft.width() - w) / 2;
    tft.setTextColor(textColor);
    tft.setCursor(x, y);
    tft.print(text);
}

String getValidValue(float value, int decimal, String unit) {
    if (isnan(value) || isinf(value)) {
        return "0" + unit;
    }
    return String(value, decimal) + unit;
}

void hardwareReset() {
    pinMode(TFT_RST, OUTPUT);
    digitalWrite(TFT_RST, LOW);   // Reset aktif LOW
    delay(100);
    digitalWrite(TFT_RST, HIGH);  // Lepaskan reset
    delay(100);
}

void setup() {
    Serial.begin(115200);
    Serial.println("Booting...");

    hardwareReset();

    tft.initR(INITR_MINI160x80);
    
    tft.invertDisplay(isInverted);
    tft.setAddrWindow(0, 0, 160, 80);
    tft.setRotation(0);
    tft.fillScreen(safeColor565(0, 0, 0));

    bmpAvailable = bmp.begin(0x76);
    if (!bmpAvailable) {
        Serial.println("BMP280 not found! Continuing with default values...");
    }

    mpuAvailable = mpu.begin();
    if (!mpuAvailable) {
        Serial.println("MPU6050 not found! Continuing without IMU data...");
    }

    uint16_t borderColor = safeColor565(30, 30, 30);
    tft.drawRect(0, 0, tft.width(), tft.height(), borderColor);

    tft.setTextSize(1);
}

void loop() {
  if (bmpAvailable) {
      temperature = bmp.readTemperature();
      pressure = bmp.readPressure() / 100.0F;
      altitude = bmp.readAltitude(1013.25);
  }

  float fahrenheit = (temperature * 9 / 5) + 32;

  tempC = getValidValue(temperature, 1, " *C");
  tempF = getValidValue(fahrenheit, 1, " *F");
  pressureStr = getValidValue(pressure, 0, " hPa");
  altitudeStr = getValidValue(altitude, 1, " m");

  sensors_event_t a, g, temp;

  if (mpuAvailable) {
      mpu.getEvent(&a, &g, &temp);
      accX = "X: " + getValidValue(a.acceleration.x, 1, "");
      accY = "Y: " + getValidValue(a.acceleration.y, 1, "");
      accZ = "Z: " + getValidValue(a.acceleration.z, 1, "");
  }

  if(millis() - sensorMillis1 >= sensorInterval1) {
    sensorMillis1 = millis();
    displayBMP280Data();
  }

  if(millis() - sensorMillis2 >= sensorInterval2) {
    sensorMillis2 = millis();
    displayMPU6050Data();
  }
}

void displayBMP280Data() {
    uint16_t textColor = safeColor565(255, 255, 255);
    // Display BMP280 data
    if (tempC != prevTempC) {
        centerText(tempC, 25, textColor);
        prevTempC = tempC;
    }
    if (tempF != prevTempF) {
        centerText(tempF, 40, textColor);
        prevTempF = tempF;
    }
    if (pressureStr != prevPressureStr) {
        centerText(pressureStr, 55, textColor);
        prevPressureStr = pressureStr;
    }
    if (altitudeStr != prevAltitudeStr) {
        centerText(altitudeStr, 70, textColor);
        prevAltitudeStr = altitudeStr;
    }
}

void displayMPU6050Data() {
    uint16_t textColor = safeColor565(255, 255, 255);
    // Display MPU6050 data
    if (accX != prevAccX) {
        centerText(accX, 95, textColor);
        prevAccX = accX;
    }
    if (accY != prevAccY) {
        centerText(accY, 110, textColor);
        prevAccY = accY;
    }
    if (accZ != prevAccZ) {
        centerText(accZ, 125, textColor);
        prevAccZ = accZ;
    }
}```
@Basitadam
Copy link

Try using TFT_eSPI library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants