From 9a5379eea21fa4d7ae0fd81665e8b07aaf0f0c7f Mon Sep 17 00:00:00 2001 From: acidvegas Date: Sun, 26 May 2024 00:39:05 -0400 Subject: [PATCH] Added screen inactivity timeout to save battery! --- README.md | 1 + src/main.ino | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e5f9d31..2cda8da 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ This is being developed in my free time as a fun project. It is no where near be 5. Flash the device: `esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 115200 write_flash -z 0x1000 firmware.bin` # Roapmap +- [X] Screen timeout on inactivity *(default 30 seconds)* - [X] Wifi scanning & selection menu - [ ] Saved wifi profiles - [X] IRC Client diff --git a/src/main.ino b/src/main.ino index a97acd0..babeaa8 100644 --- a/src/main.ino +++ b/src/main.ino @@ -54,6 +54,11 @@ bool readyToJoinChannel = false; unsigned long lastStatusUpdateTime = 0; const unsigned long STATUS_UPDATE_INTERVAL = 15000; +unsigned long lastActivityTime = 0; +const unsigned long INACTIVITY_TIMEOUT = 30000; // 30 seconds +bool screenOn = true; + + struct WiFiNetwork { int index; int channel; @@ -72,6 +77,9 @@ void setup() { pinMode(BOARD_POWERON, OUTPUT); digitalWrite(BOARD_POWERON, HIGH); + pinMode(TFT_BL, OUTPUT); + digitalWrite(TFT_BL, HIGH); // Turn on the backlight initially + Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL); tft.begin(); tft.setRotation(1); @@ -89,6 +97,7 @@ void setup() { nick = "ACID_" + String(randomNum); } + int renderFormattedMessage(String message, int cursorY, int lineHeight, bool highlightNick = false) { uint16_t fgColor = TFT_WHITE; uint16_t bgColor = TFT_BLACK; @@ -166,6 +175,20 @@ int renderFormattedMessage(String message, int cursorY, int lineHeight, bool hig return cursorY; // Return the new cursor Y position for the next line } +void turnOffScreen() { + tft.writecommand(TFT_DISPOFF); // Turn off display + tft.writecommand(TFT_SLPIN); // Put display into sleep mode + digitalWrite(TFT_BL, LOW); // Turn off the backlight (Assuming TFT_BL is the backlight pin) + screenOn = false; +} + +void turnOnScreen() { + digitalWrite(TFT_BL, HIGH); // Turn on the backlight (Assuming TFT_BL is the backlight pin) + tft.writecommand(TFT_SLPOUT); // Wake up display from sleep mode + tft.writecommand(TFT_DISPON); // Turn on display + screenOn = true; +} + void displayLines() { tft.fillRect(0, STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_BAR_HEIGHT - INPUT_LINE_HEIGHT, TFT_BLACK); @@ -312,6 +335,7 @@ void loop() { char incoming = getKeyboardInput(); if (incoming != 0) { handleWiFiSelection(incoming); + lastActivityTime = millis(); // Reset activity timer } } else { if (millis() - lastStatusUpdateTime > STATUS_UPDATE_INTERVAL) { @@ -348,10 +372,18 @@ void loop() { char incoming = getKeyboardInput(); if (incoming != 0) { handleKeyboardInput(incoming); + lastActivityTime = millis(); // Reset activity timer + } + + // Check for inactivity + if (screenOn && millis() - lastActivityTime > INACTIVITY_TIMEOUT) { + turnOffScreen(); // Turn off screen and backlight } } } + + bool connectToIRC() { if (useSSL) { client.setInsecure(); @@ -407,10 +439,16 @@ void handleIRC() { sendIRC(pingResponse); } else { parseAndDisplay(line); + lastActivityTime = millis(); // Reset activity timer + if (!screenOn) { + turnOnScreen(); // Turn on screen and backlight + } } } } + + void parseAndDisplay(String line) { int firstSpace = line.indexOf(' '); int secondSpace = line.indexOf(' ', firstSpace + 1); @@ -457,7 +495,7 @@ void parseAndDisplay(String line) { void handleKeyboardInput(char key) { if (key == '\n' || key == '\r') { // Enter if (inputBuffer.startsWith("/raw ")) { - String rawCommand = inputBuffer.substring(5); // Remove "/raw " + String rawCommand = inputBuffer.substring(5); sendRawCommand(rawCommand); } else { sendIRC("PRIVMSG " + String(channel) + " :" + inputBuffer); @@ -465,17 +503,31 @@ void handleKeyboardInput(char key) { } inputBuffer = ""; displayInputLine(); + lastActivityTime = millis(); // Reset activity timer + if (!screenOn) { + turnOnScreen(); // Turn on screen and backlight + } } else if (key == '\b') { // Backspace if (inputBuffer.length() > 0) { inputBuffer.remove(inputBuffer.length() - 1); displayInputLine(); + lastActivityTime = millis(); // Reset activity timer + if (!screenOn) { + turnOnScreen(); // Turn on screen and backlight + } } } else { inputBuffer += key; displayInputLine(); + lastActivityTime = millis(); // Reset activity timer + if (!screenOn) { + turnOnScreen(); // Turn on screen and backlight + } } } + + void sendRawCommand(String command) { if (client.connected()) { sendIRC(command);