diff --git a/src/graphics/TFTDisplay.cpp b/src/graphics/TFTDisplay.cpp index 0e203a9d69..21b9c2bd11 100644 --- a/src/graphics/TFTDisplay.cpp +++ b/src/graphics/TFTDisplay.cpp @@ -516,6 +516,21 @@ extern unPhone unphone; TFTDisplay::TFTDisplay(uint8_t address, int sda, int scl, OLEDDISPLAY_GEOMETRY geometry, HW_I2C i2cBus) { LOG_DEBUG("TFTDisplay!\n"); + +#ifdef TFT_BL + GpioPin *p = new GpioHwPin(TFT_BL); + + if (!TFT_BACKLIGHT_ON) { // Need to invert the pin before hardware + auto virtPin = new GpioVirtPin(); + new GpioNotTransformer( + virtPin, p); // We just leave this created object on the heap so it can stay watching virtPin and driving en_gpio + p = virtPin; + } +#else + GpioPin *p = new GpioVirtPin(); // Just simulate a pin +#endif + backlightEnable = p; + #if ARCH_PORTDUINO if (settingsMap[displayRotate]) { setGeometry(GEOMETRY_RAWMODE, settingsMap[configNames::displayHeight], settingsMap[configNames::displayWidth]); @@ -569,13 +584,11 @@ void TFTDisplay::sendCommand(uint8_t com) // handle display on/off directly switch (com) { case DISPLAYON: { + backlightEnable->set(true); #if ARCH_PORTDUINO display(true); if (settingsMap[displayBacklight] > 0) digitalWrite(settingsMap[displayBacklight], TFT_BACKLIGHT_ON); -#elif defined(TFT_BL) && defined(TFT_BACKLIGHT_ON) - pinMode(TFT_BL, OUTPUT); - digitalWrite(TFT_BL, TFT_BACKLIGHT_ON); #elif !defined(RAK14014) && !defined(M5STACK) && !defined(UNPHONE) tft->wakeup(); tft->powerSaveOff(); @@ -594,13 +607,11 @@ void TFTDisplay::sendCommand(uint8_t com) break; } case DISPLAYOFF: { + backlightEnable->set(false); #if ARCH_PORTDUINO tft->clear(); if (settingsMap[displayBacklight] > 0) digitalWrite(settingsMap[displayBacklight], !TFT_BACKLIGHT_ON); -#elif defined(TFT_BL) && defined(TFT_BACKLIGHT_ON) - pinMode(TFT_BL, OUTPUT); - digitalWrite(TFT_BL, !TFT_BACKLIGHT_ON); #elif !defined(RAK14014) && !defined(M5STACK) && !defined(UNPHONE) tft->sleep(); tft->powerSaveOn(); @@ -689,13 +700,8 @@ bool TFTDisplay::connect() tft = new LGFX; #endif -#ifdef TFT_BL - pinMode(TFT_BL, OUTPUT); - digitalWrite(TFT_BL, TFT_BACKLIGHT_ON); - // pinMode(PIN_3V3_EN, OUTPUT); - // digitalWrite(PIN_3V3_EN, HIGH); + backlightEnable->set(true); LOG_INFO("Power to TFT Backlight\n"); -#endif #ifdef UNPHONE unphone.backlight(true); // using unPhone library diff --git a/src/graphics/TFTDisplay.h b/src/graphics/TFTDisplay.h index 42aa3abff5..595984fbc2 100644 --- a/src/graphics/TFTDisplay.h +++ b/src/graphics/TFTDisplay.h @@ -1,5 +1,6 @@ #pragma once +#include #include /** @@ -39,6 +40,12 @@ class TFTDisplay : public OLEDDisplay */ void setDetected(uint8_t detected); + /** + * This is normally managed entirely by TFTDisplay, but some rare applications (heltec tracker) might need to replace the + * default GPIO behavior with something a bit more complex. + */ + GpioPin *backlightEnable; + protected: // the header size of the buffer used, e.g. for the SPI command header virtual int getBufferOffset(void) override { return 0; }