From 36945dae429ca95f066bf191ccdce944e8a2628a Mon Sep 17 00:00:00 2001 From: Northern Man <19808920+NorthernMan54@users.noreply.github.com> Date: Thu, 22 Aug 2024 14:21:29 -0400 Subject: [PATCH] [DISPLAY] Touch Based Display Blanking (#2015) --- main/ZdisplaySSD1306.ino | 34 ++++++++++++++++++++++++++++++++++ main/config_SSD1306.h | 16 ++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/main/ZdisplaySSD1306.ino b/main/ZdisplaySSD1306.ino index ed7bcfbd..7ccb8086 100644 --- a/main/ZdisplaySSD1306.ino +++ b/main/ZdisplaySSD1306.ino @@ -38,6 +38,9 @@ # include "ArduinoLog.h" # include "User_config.h" # include "config_SSD1306.h" +# ifdef DISPLAY_BLANKING +# include "driver/touch_sensor.h" +# endif SemaphoreHandle_t semaphoreOLEDOperation; @@ -83,6 +86,17 @@ void setupSSD1306() { boolean logoDisplayed = false; unsigned long nextDisplayPage = uptime() + DISPLAY_PAGE_INTERVAL; +# ifdef DISPLAY_BLANKING +unsigned long blankingStart = uptime() + DISPLAY_BLANKING_START; + +touch_value_t touchReadings[TOUCH_READINGS] = {0}; +touch_value_t touchCurrentReading = 0; +int touchIndex = 0; +int touchTotal = 0; +int touchAverage = 0; +int touchThreshold = 0; +# endif + /* module loop, for use in Arduino loop */ @@ -92,6 +106,26 @@ void loopSSD1306() { long enough since the last message and display not being used and a queue message waiting */ + +# ifdef DISPLAY_BLANKING + // Log.trace(F("touchAverage %d, touchCurrentReading %d, touchThreshold %d" CR), touchAverage, touchCurrentReading, touchThreshold); + touchTotal = touchTotal - touchReadings[touchIndex]; + touchCurrentReading = touchRead(DISPLAY_BLANKING_TOUCH_GPIO); + touchReadings[touchIndex] = touchCurrentReading; + touchTotal = touchTotal + touchReadings[touchIndex]; + touchIndex = (touchIndex + 1) % TOUCH_READINGS; + touchAverage = touchTotal / TOUCH_READINGS; + touchThreshold = touchAverage * TOUCH_THRESHOLD; + + if ((touchCurrentReading > touchAverage + touchThreshold || touchCurrentReading < touchAverage - touchThreshold) && displayState) { + blankingStart = uptime() + DISPLAY_BLANKING_START; + Oled.display->displayOn(); + } + if (uptime() > blankingStart && displayState) { + Oled.display->displayOff(); + } +# endif + if (jsonDisplay && displayState) { if (uptime() >= nextDisplayPage && uxSemaphoreGetCount(semaphoreOLEDOperation) && currentWebUIMessage && newSSD1306Message) { if (!Oled.displayPage(currentWebUIMessage)) { diff --git a/main/config_SSD1306.h b/main/config_SSD1306.h index 2e761186..4e8b797a 100644 --- a/main/config_SSD1306.h +++ b/main/config_SSD1306.h @@ -105,6 +105,22 @@ #define subjectMQTTtoSSD1306set "/commands/MQTTtoSSD1306/config" #define subjectSSD1306toMQTT "/SSD1306toMQTT" +/*-------------------Display Blanking via Touch----------------------*/ + +#ifdef DISPLAY_BLANKING +# ifndef DISPLAY_BLANKING_TOUCH_GPIO +# define DISPLAY_BLANKING_TOUCH_GPIO 2 // GPIO pin for touch sensor +# endif +# ifndef DISPLAY_BLANKING_START +# define DISPLAY_BLANKING_START 30 // 30 seconds after last touch +# endif +# ifndef DISPLAY_BLANKING_THRESHOLD +# define DISPLAY_BLANKING_THRESHOLD 10 +# endif +# define TOUCH_READINGS 100 // Number of readings to average +# define TOUCH_THRESHOLD 0.2 // 20% change in reading +#endif + /*-------------------EXTERNAL FUNCTIONS----------------------*/ extern void setupSSD1306();