From 580129ba353d681efa5943a12a1292deddb22ff6 Mon Sep 17 00:00:00 2001
From: Tim Rheinfels <tim.rheinfels@fau.de>
Date: Mon, 9 Dec 2019 17:05:39 +0100
Subject: [PATCH] + Implemented backlight control

---
 case/backlight.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++
 case/backlight.sh | 28 +++++++++++++-------------
 2 files changed, 64 insertions(+), 14 deletions(-)
 create mode 100644 case/backlight.py

diff --git a/case/backlight.py b/case/backlight.py
new file mode 100644
index 0000000..a4d5566
--- /dev/null
+++ b/case/backlight.py
@@ -0,0 +1,50 @@
+#!/usr/bin/python
+
+###
+###  @brief  Script for accessing the display's backlight
+###
+
+import RPi.GPIO as GPIO
+
+# Configuration
+backlight_channel = 16
+
+# Function for accessing the backlight
+def init():
+        # Adviced by the library...
+        GPIO.setwarnings( False )
+
+        GPIO.setmode( GPIO.BCM )
+        GPIO.setup( backlight_channel, GPIO.OUT )
+
+def enable():
+        init()
+        GPIO.output( backlight_channel, GPIO.HIGH )
+
+def disable():
+        init()
+        GPIO.output( backlight_channel, GPIO.LOW )
+
+# If the script is executed directly...
+if __name__ == '__main__':
+        import sys
+
+        en = None
+        if len(sys.argv) == 2:
+                if sys.argv[1] == 'on':
+                        en = True
+                elif sys.argv[1] == 'off':
+                        en = False
+
+        if en is None:
+                print( 'Usage: %s <state>' )
+                print( '' )
+                print( 'Parameters:' )
+                print( '  state: Either "on" or "off" to enable/disable the backlight' )
+                print( '' )
+                sys.exit( 1 )
+
+        if en:
+                enable()
+        else:
+                disable()
diff --git a/case/backlight.sh b/case/backlight.sh
index 13d03cf..dba275f 100755
--- a/case/backlight.sh
+++ b/case/backlight.sh
@@ -5,18 +5,18 @@ export DISPLAY=:0
 MONITOR_ACTIVE=false
 
 while true; do
-	if xset -q | grep "Monitor is On" >/dev/null 2>&1 ; then
-		if [ "$MONITOR_ACTIVE" != true ]; then
-			MONITOR_ACTIVE=true
-			#echo Monitor just switched on
-			xset led named "Scroll Lock"
-		fi
-	else
-		if [ "$MONITOR_ACTIVE" = true ]; then
-			MONITOR_ACTIVE=false
-			#echo Monitor just switched off
-			xset -led named "Scroll Lock"
-		fi
-	fi
-	sleep 1
+        if xset -q | grep "Monitor is On" >/dev/null 2>&1 ; then
+                if [ "$MONITOR_ACTIVE" != true ]; then
+                        MONITOR_ACTIVE=true
+                        python /opt/backlight.py on
+                        xset led named "Scroll Lock"
+                fi
+        else
+                if [ "$MONITOR_ACTIVE" = true ]; then
+                        MONITOR_ACTIVE=false
+                        python /opt/backlight.py off
+                        xset -led named "Scroll Lock"
+                fi
+        fi
+        sleep 1
 done
-- 
GitLab