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

Turn off/on display on S3 and T-Displays for battery safe #223

Open
wants to merge 10 commits into
base: development
Choose a base branch
from
37 changes: 22 additions & 15 deletions CO2_Gadget.ino
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ bool activeOTA = false;
bool mustInitMenu = false;
bool menuInitialized = false;
uint16_t DisplayBrightness = 100;
bool displayOn = true;
bool displayReverse = false;
bool showFahrenheit = false;
bool displayShowTemperature = true;
Expand Down Expand Up @@ -366,11 +367,12 @@ static int64_t lastReadingsCommunicationTime = 0;
static int startCheckingAfterUs = 1900000;

void wakeUpDisplay() {
if (actualDisplayBrightness == 0) {
if (!displayOn) {
#if defined(SUPPORT_OLED) || defined(SUPPORT_TFT)
setDisplayBrightness(DisplayBrightness);
// publishMQTTLogData("Display woken up. Setting display brightness to " + String(DisplayBrightness));
// Serial.println("-->[MAIN] Display woken up. Setting display brightness to " + String(DisplayBrightness));
turnOnDisplay();
// setDisplayBrightness(DisplayBrightness);
// publishMQTTLogData("Display woken up. Setting display brightness to " + String(DisplayBrightness));
// Serial.println("-->[MAIN] Display woken up. Setting display brightness to " + String(DisplayBrightness));
#endif
lastTimeButtonPressed = millis();
}
Expand Down Expand Up @@ -513,17 +515,21 @@ void adjustBrightnessLoop() {
}

if (inMenu) {
setDisplayBrightness(DisplayBrightness);
wakeUpDisplay();
// setDisplayBrightness(DisplayBrightness);
return;
}

// Display backlight IS sleeping
if ((actualDisplayBrightness == 0) && (actualDisplayBrightness != DisplayBrightness)) {
// if ((actualDisplayBrightness == 0) && (actualDisplayBrightness != DisplayBrightness)) {
if (!displayOn) {
if ((!displayOffOnExternalPower) && (workingOnExternalPower)) {
setDisplayBrightness(DisplayBrightness);
wakeUpDisplay();
// setDisplayBrightness(DisplayBrightness);
}
if (timeToDisplayOff == 0) {
setDisplayBrightness(DisplayBrightness);
wakeUpDisplay();
// setDisplayBrightness(DisplayBrightness);
}
return;
}
Expand All @@ -536,18 +542,19 @@ void adjustBrightnessLoop() {

// If configured not to turn off the display on external power and it's working on external power, do nothing and return (except if DisplayBrightness is 0, then turn on display))
if ((workingOnExternalPower) && (!displayOffOnExternalPower)) {
if (actualDisplayBrightness == 0) {
setDisplayBrightness(DisplayBrightness); // Exception: When USB connected (just connected) & TFT is OFF -> Turn Display ON
// publishMQTTLogData("Turning on display on external power. Actual brightness: " + String(actualDisplayBrightness));
// Serial.println("-->[MAIN] Turning on display on external power. Actual brightness: " + String(actualDisplayBrightness));
// delay(10);
}
wakeUpDisplay();
// if (actualDisplayBrightness == 0) {
// setDisplayBrightness(DisplayBrightness); // Exception: When USB connected (just connected) & TFT is OFF -> Turn Display ON
// publishMQTTLogData("Turning on display on external power. Actual brightness: " + String(actualDisplayBrightness));
// Serial.println("-->[MAIN] Turning on display on external power. Actual brightness: " + String(actualDisplayBrightness));
// delay(10);
//}
return;
}

if (timeToDisplayOff == 0) return; // If timeToDisplayOff is 0, don't turn off the display

if ((actualDisplayBrightness != 0) && (millis() - lastTimeButtonPressed >= timeToDisplayOff * 1000) && DisplayBrightness > 0) {
if (displayOn && (millis() - lastTimeButtonPressed >= timeToDisplayOff * 1000) && DisplayBrightness > 0) {
if ((workingOnExternalPower) && (displayOffOnExternalPower)) {
Serial.println("-->[MAIN] Turning off display on external power to save power. Actual brightness: " + String(actualDisplayBrightness));
turnOffDisplay();
Expand Down
16 changes: 8 additions & 8 deletions CO2_Gadget_Buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Button2 btnUp(BTN_UP); // Initialize the up button
Button2 btnDwn(BTN_DWN); // Initialize the down button

void IRAM_ATTR buttonUpISR() {
if (actualDisplayBrightness == 0) { // Turn on the display only if it's OFF
if (!displayOn) { // Turn on the display only if it's OFF
shouldWakeUpDisplay = true;
lastTimeButtonPressed = millis();
}
Expand All @@ -19,7 +19,7 @@ void IRAM_ATTR buttonUpISR() {
}

void IRAM_ATTR buttonDownISR() {
if (actualDisplayBrightness == 0) { // Turn on the display only if it's OFF
if (!displayOn) { // Turn on the display only if it's OFF
shouldWakeUpDisplay = true;
lastTimeButtonPressed = millis();
}
Expand All @@ -35,13 +35,13 @@ void IRAM_ATTR buttonDownISR() {
// }

void initButtons() {
// Set Interrupt Service Routine to turn on the display on button UP or DOWN press (nothing to do with the button functionality for the menu itself)
#if BTN_UP != -1
// Set Interrupt Service Routine to turn on the display on button UP or DOWN press (nothing to do with the button functionality for the menu itself)
#if BTN_UP != -1
attachInterrupt(BTN_UP, buttonUpISR, RISING);
#endif
#if BTN_DWN != -1
// attachInterrupt(BTN_DWN, buttonUpISR, RISING); // Conflicts with Improv because of GPIO 0
#endif
#endif
#if BTN_DWN != -1
// attachInterrupt(BTN_DWN, buttonUpISR, RISING); // Conflicts with Improv because of GPIO 0
#endif

btnUp.setLongClickTime(LONGCLICK_TIME_MS);
btnUp.setLongClickHandler([](Button2 &b) { nav.doNav(enterCmd); });
Expand Down
80 changes: 43 additions & 37 deletions CO2_Gadget_OLED.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// clang-format on
// #include <U8x8lib.h>
#include <U8g2lib.h>

#include "bootlogo.h"
#include "icons.h"
U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE); // Frame Buffer: clearBuffer/sendBuffer. More RAM usage, Faster
Expand All @@ -36,25 +37,30 @@ void setDisplayReverse(bool reverse) {
}

void setDisplayBrightness(uint32_t newBrightness) {
Serial.printf("-->[OLED] Setting display brightness value at %d\n", newBrightness);
u8g2.setContrast(newBrightness);
actualDisplayBrightness = newBrightness;
Serial.printf("-->[OLED] Setting display brightness value at %d\n", newBrightness);
u8g2.setContrast(newBrightness);
actualDisplayBrightness = newBrightness;
}

void turnOffDisplay() {
setDisplayBrightness(0); // Turn off the display
setDisplayBrightness(0); // Turn off the display
displayOn = false;
}
void turnOnDisplay() {
setDisplayBrightness(DisplayBrightness); // Turn off the display
displayOn = true;
}

void displaySplashScreen() {
u8g2.clearDisplay();
u8g2.firstPage();
do {
// u8g2.drawXBMP(30, 0, 59, 20, eMarieteLogo);
// u8g2.drawXBM(7, 23, 46, 36, CO2Logo);
// u8g2.drawXBM(60, 32, 61, 23, GadgetLogo);
u8g2.drawXBM(0, 0, 128, 64, splash);
} while (u8g2.nextPage());
u8g2.setFont(MENUFONT);
u8g2.clearDisplay();
u8g2.firstPage();
do {
// u8g2.drawXBMP(30, 0, 59, 20, eMarieteLogo);
// u8g2.drawXBM(7, 23, 46, 36, CO2Logo);
// u8g2.drawXBM(60, 32, 61, 23, GadgetLogo);
u8g2.drawXBM(0, 0, 128, 64, splash);
} while (u8g2.nextPage());
u8g2.setFont(MENUFONT);
}

/***************************************************************************************
Expand All @@ -65,37 +71,37 @@ void displaySplashScreen() {
// notificationText = string to display.
// notificationTypes one of enum notificationTypes notifyNothing, notifyInfo, notifyWarning, notifyError
bool displayNotification(String notificationText, notificationTypes notificationType) {
uint16_t textColor, boxColor, backgroundColor, boxMarging = 15;
return true;
uint16_t textColor, boxColor, backgroundColor, boxMarging = 15;
return true;
}

bool displayNotification(String notificationText, String notificationText2, notificationTypes notificationType) {
uint16_t textColor, boxColor, backgroundColor, boxMarging = 15;
return true;
uint16_t textColor, boxColor, backgroundColor, boxMarging = 15;
return true;
}

void initDisplay(bool fastMode = false) { // fastMode not used in OLED display. Just for compatibility with TFT and other displays.
Serial.printf("-->[OLED] Initialized: \t#%s#\n",
((u8g2.begin()) ? "OK" : "Failed"));
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB12_tr);
u8g2.drawStr(0, 15, " eMariete.com");
u8g2.drawStr(0, 33, " CO2 Gadget");
u8g2.drawStr(0, 51, " Air Quality");
} while (u8g2.nextPage());
u8g2.setFont(MENUFONT);
if (displayReverse) {
u8g2.setDisplayRotation(U8G2_R2);
} else {
u8g2.setDisplayRotation(U8G2_R0);
}
setDisplayBrightness(DisplayBrightness);
displaySplashScreen();
delay(1000);
void initDisplay(bool fastMode = false) { // fastMode not used in OLED display. Just for compatibility with TFT and other displays.
Serial.printf("-->[OLED] Initialized: \t#%s#\n",
((u8g2.begin()) ? "OK" : "Failed"));
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB12_tr);
u8g2.drawStr(0, 15, " eMariete.com");
u8g2.drawStr(0, 33, " CO2 Gadget");
u8g2.drawStr(0, 51, " Air Quality");
} while (u8g2.nextPage());
u8g2.setFont(MENUFONT);
if (displayReverse) {
u8g2.setDisplayRotation(U8G2_R2);
} else {
u8g2.setDisplayRotation(U8G2_R0);
}
setDisplayBrightness(DisplayBrightness);
displaySplashScreen();
delay(1000);
}

void displayShowValues(bool forceRedraw = false) {
void displayShowValues(bool forceRedraw = false) {
if ((co2 == 0) || (co2 > 9999)) return;
String co2Str = String(co2);
if (co2Str.length() < 4) {
Expand Down
21 changes: 20 additions & 1 deletion CO2_Gadget_TFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,26 @@ void setDisplayBrightness(uint16_t newBrightness) {
}

void turnOffDisplay() {
setDisplayBrightness(0); // Turn off the display
tft.writecommand(TFT_DISPOFF); // Comando para apagar la pantalla
tft.writecommand(TFT_SLPIN);
digitalWrite(TFT_BACKLIGHT, LOW); // turn of lcd backlight
#ifdef TTGO_TDISPLAY
setDisplayBrightness(0);
#endif
displayOn = false;
}

void turnOnDisplay() {
// tft.init();
tft.writecommand(TFT_SLPOUT);
delay(120);
tft.writecommand(TFT_DISPON); // turn on display
digitalWrite(TFT_BACKLIGHT, HIGH); // turn on lcd backlight
#ifdef ARDUINO_LILYGO_T_DISPLAY_S3
actualDisplayBrightness = 16; // At the beginning brightness is at maximum level
#endif
setDisplayBrightness(DisplayBrightness);
displayOn = true;
}

void displaySplashScreen() {
Expand Down