From f9996e229404f070c715d6aa9ac8b6d8d6c6028c Mon Sep 17 00:00:00 2001
From: Christof Schulze <christof.schulze@gmx.net>
Date: Sun, 25 Aug 2019 02:16:55 +0200
Subject: [PATCH] Display Backlight: automatically adjust to ambient sensor
 reading

This registers a Task, reading the ambient light sensor once aminute and
adjusting the display backlight accordingly
---
 epicardium/main.c               | 18 +++++++++++++
 epicardium/modules/brightness.c | 46 +++++++++++++++++++++++++++++++++
 epicardium/modules/meson.build  |  1 +
 epicardium/modules/modules.h    |  3 +++
 4 files changed, 68 insertions(+)
 create mode 100644 epicardium/modules/brightness.c

diff --git a/epicardium/main.c b/epicardium/main.c
index dadad9bf..5446909c 100644
--- a/epicardium/main.c
+++ b/epicardium/main.c
@@ -69,6 +69,23 @@ int main(void)
 		abort();
 	}
 
+	/* Automatically adjust the brightness of the display according to the
+	** light sensor */
+	if (xTaskCreate(
+		    vAutoBrightnessTask,
+		    (const char *)"DisplayBrightness",
+		    configMINIMAL_STACK_SIZE,
+		    NULL,
+		    tskIDLE_PRIORITY + 4,
+		    NULL) != pdPASS) {
+		LOG_CRIT(
+			"startup",
+			"Failed to create %s task!",
+			"vAutoBrightnessTask"
+		);
+		abort();
+	}
+
 	/* MAX30001 */
 	if (xTaskCreate(
 		    vMAX30001Task,
@@ -80,6 +97,7 @@ int main(void)
 		LOG_CRIT("startup", "Failed to create %s task!", "MAX30001");
 		abort();
 	}
+
 	/* API */
 	if (xTaskCreate(
 		    vApiDispatcher,
diff --git a/epicardium/modules/brightness.c b/epicardium/modules/brightness.c
new file mode 100644
index 00000000..7097d28f
--- /dev/null
+++ b/epicardium/modules/brightness.c
@@ -0,0 +1,46 @@
+#include "epicardium.h"
+#include "modules.h"
+#include "log.h"
+
+bool _autobrightness_enabled = true;
+
+void vAutoBrightnessTask(void *pvParameters)
+{
+	vTaskDelay(pdMS_TO_TICKS(500));
+
+	while (1) {
+		vTaskDelay(pdMS_TO_TICKS(60000));
+		if (_autobrightness_enabled) {
+			unsigned short int light;
+
+			while (epic_light_sensor_run() < 0)
+				LOG_CRIT(
+					"autobrightness",
+					"Could not start ambient sensor"
+				);
+
+			while (epic_light_sensor_get(&light) < 0) {
+				LOG_CRIT(
+					"autobrightness",
+					"Could not read light sensor"
+				);
+				vTaskDelay(pdMS_TO_TICKS(500));
+			}
+
+			epic_light_sensor_stop();
+
+			unsigned short int brightness;
+
+			if (light > 199)
+				brightness = 100;
+			else
+				brightness = light / 2 + 1;
+
+			if (epic_disp_open() < 0)
+				continue;
+
+			epic_disp_backlight(brightness);
+			epic_disp_close();
+		}
+	}
+}
diff --git a/epicardium/modules/meson.build b/epicardium/modules/meson.build
index 693ca9a8..be92565f 100644
--- a/epicardium/modules/meson.build
+++ b/epicardium/modules/meson.build
@@ -1,6 +1,7 @@
 module_sources = files(
   'bhi.c',
   'bme680.c',
+  'brightness.c',
   'buttons.c',
   'dispatcher.c',
   'display.c',
diff --git a/epicardium/modules/modules.h b/epicardium/modules/modules.h
index 2d51f786..d699485e 100644
--- a/epicardium/modules/modules.h
+++ b/epicardium/modules/modules.h
@@ -44,6 +44,9 @@ int personal_state_enabled();
 /* ---------- PMIC --------------------------------------------------------- */
 void vPmicTask(void *pvParameters);
 
+/* ---------- Automatically adjust Brightness ------------------------------ */
+void vAutoBrightnessTask(void *pvParameters);
+
 /* ---------- Watchdog ----------------------------------------------------- */
 void watchdog_init();
 void watchdog_clearer_init();
-- 
GitLab