Added screen inactivity timeout to save battery!

This commit is contained in:
Dionysus 2024-05-26 00:39:05 -04:00
parent b5e0585302
commit 9a5379eea2
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE
2 changed files with 54 additions and 1 deletions

View File

@ -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` 5. Flash the device: `esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 115200 write_flash -z 0x1000 firmware.bin`
# Roapmap # Roapmap
- [X] Screen timeout on inactivity *(default 30 seconds)*
- [X] Wifi scanning & selection menu - [X] Wifi scanning & selection menu
- [ ] Saved wifi profiles - [ ] Saved wifi profiles
- [X] IRC Client - [X] IRC Client

View File

@ -54,6 +54,11 @@ bool readyToJoinChannel = false;
unsigned long lastStatusUpdateTime = 0; unsigned long lastStatusUpdateTime = 0;
const unsigned long STATUS_UPDATE_INTERVAL = 15000; const unsigned long STATUS_UPDATE_INTERVAL = 15000;
unsigned long lastActivityTime = 0;
const unsigned long INACTIVITY_TIMEOUT = 30000; // 30 seconds
bool screenOn = true;
struct WiFiNetwork { struct WiFiNetwork {
int index; int index;
int channel; int channel;
@ -72,6 +77,9 @@ void setup() {
pinMode(BOARD_POWERON, OUTPUT); pinMode(BOARD_POWERON, OUTPUT);
digitalWrite(BOARD_POWERON, HIGH); 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); Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL);
tft.begin(); tft.begin();
tft.setRotation(1); tft.setRotation(1);
@ -89,6 +97,7 @@ void setup() {
nick = "ACID_" + String(randomNum); nick = "ACID_" + String(randomNum);
} }
int renderFormattedMessage(String message, int cursorY, int lineHeight, bool highlightNick = false) { int renderFormattedMessage(String message, int cursorY, int lineHeight, bool highlightNick = false) {
uint16_t fgColor = TFT_WHITE; uint16_t fgColor = TFT_WHITE;
uint16_t bgColor = TFT_BLACK; 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 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() { void displayLines() {
tft.fillRect(0, STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_BAR_HEIGHT - INPUT_LINE_HEIGHT, TFT_BLACK); 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(); char incoming = getKeyboardInput();
if (incoming != 0) { if (incoming != 0) {
handleWiFiSelection(incoming); handleWiFiSelection(incoming);
lastActivityTime = millis(); // Reset activity timer
} }
} else { } else {
if (millis() - lastStatusUpdateTime > STATUS_UPDATE_INTERVAL) { if (millis() - lastStatusUpdateTime > STATUS_UPDATE_INTERVAL) {
@ -348,10 +372,18 @@ void loop() {
char incoming = getKeyboardInput(); char incoming = getKeyboardInput();
if (incoming != 0) { if (incoming != 0) {
handleKeyboardInput(incoming); handleKeyboardInput(incoming);
lastActivityTime = millis(); // Reset activity timer
}
// Check for inactivity
if (screenOn && millis() - lastActivityTime > INACTIVITY_TIMEOUT) {
turnOffScreen(); // Turn off screen and backlight
} }
} }
} }
bool connectToIRC() { bool connectToIRC() {
if (useSSL) { if (useSSL) {
client.setInsecure(); client.setInsecure();
@ -407,9 +439,15 @@ void handleIRC() {
sendIRC(pingResponse); sendIRC(pingResponse);
} else { } else {
parseAndDisplay(line); parseAndDisplay(line);
lastActivityTime = millis(); // Reset activity timer
if (!screenOn) {
turnOnScreen(); // Turn on screen and backlight
} }
} }
} }
}
void parseAndDisplay(String line) { void parseAndDisplay(String line) {
int firstSpace = line.indexOf(' '); int firstSpace = line.indexOf(' ');
@ -457,7 +495,7 @@ void parseAndDisplay(String line) {
void handleKeyboardInput(char key) { void handleKeyboardInput(char key) {
if (key == '\n' || key == '\r') { // Enter if (key == '\n' || key == '\r') { // Enter
if (inputBuffer.startsWith("/raw ")) { if (inputBuffer.startsWith("/raw ")) {
String rawCommand = inputBuffer.substring(5); // Remove "/raw " String rawCommand = inputBuffer.substring(5);
sendRawCommand(rawCommand); sendRawCommand(rawCommand);
} else { } else {
sendIRC("PRIVMSG " + String(channel) + " :" + inputBuffer); sendIRC("PRIVMSG " + String(channel) + " :" + inputBuffer);
@ -465,16 +503,30 @@ void handleKeyboardInput(char key) {
} }
inputBuffer = ""; inputBuffer = "";
displayInputLine(); displayInputLine();
lastActivityTime = millis(); // Reset activity timer
if (!screenOn) {
turnOnScreen(); // Turn on screen and backlight
}
} else if (key == '\b') { // Backspace } else if (key == '\b') { // Backspace
if (inputBuffer.length() > 0) { if (inputBuffer.length() > 0) {
inputBuffer.remove(inputBuffer.length() - 1); inputBuffer.remove(inputBuffer.length() - 1);
displayInputLine(); displayInputLine();
lastActivityTime = millis(); // Reset activity timer
if (!screenOn) {
turnOnScreen(); // Turn on screen and backlight
}
} }
} else { } else {
inputBuffer += key; inputBuffer += key;
displayInputLine(); displayInputLine();
lastActivityTime = millis(); // Reset activity timer
if (!screenOn) {
turnOnScreen(); // Turn on screen and backlight
} }
} }
}
void sendRawCommand(String command) { void sendRawCommand(String command) {
if (client.connected()) { if (client.connected()) {