Code: Select all
#!/usr/bin/env python3
import subprocess
import time
import sys
# read arguments from the run command:
# idle time before dim (in seconds)
idleTimeBeforeDimMS = int( sys.argv[1] )*1000
# brightness when dimmed (between 0 and 255)
brightnessDimmed = int( sys.argv[2] )
brightnessFull = 255
def get(cmd):
# just a helper function
return subprocess.check_output(cmd).decode("utf-8").strip()
isIdle0 = False
stateChanged = False
timeIntervalToWatchChangesS = 100 / 1000
while True:
time.sleep( timeIntervalToWatchChangesS )
currentIdleTimeMS = int( get("xprintidle") )
isIdle = currentIdleTimeMS > idleTimeBeforeDimMS
stateChanged = isIdle0 != isIdle
if isIdle and stateChanged:
# idling
bashCommand = "echo 50 > /sys/class/backlight/rpi_backlight/brightness"
subprocess.run(['bash', '-c', bashCommand])
elif not isIdle and stateChanged:
# active
bashCommand = "echo 255 > /sys/class/backlight/rpi_backlight/brightness"
subprocess.run(['bash', '-c', bashCommand])
# set current state as initial one for the next loop cycle
isIdle0 = isIdle