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

Touch Based Display Blanking #2015

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions main/ZdisplaySSD1306.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand All @@ -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)) {
Expand Down
16 changes: 16 additions & 0 deletions main/config_SSD1306.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading