(04-21-2020, 12:51 PM)Tim Curtis Wrote: Good idea. It's prolly doable and I'll add to the TODO list :-)
I hope you don't mind, but I made a quick and dirty python script in the meantime that monitors the state of MPD and adjusts the screen brightness accordingly.
For anyone who wants the same while waiting an official way here is my script.
Firstly you need to install mpd and rpi_backlight:
Code:
sudo pip3 install python-mpd2
sudo pip3 install rpi_backlight
those must be installed using sudo for it to work on boot.
Next create the script:
Code:
nano monitor.py
and copy script code:
Code:
from mpd import MPDClient
from time import sleep, time
from rpi_backlight import Backlight
player = MPDClient() # setup MPD client to read mpd status
backlight = Backlight() # setup backlight object
laststate = "" # holds the last state of mpd through the main loop
timepaused = time() # holds the time unit was paused or stopped
lastscreenstate = "on" # last state of the screen through the main loop ("on" or "off")
SCREENONTIME = 5*60 # time screen on for after playing n*60 where n is minutes to stay on for
SCREENBRIGHTNESS = 50 # % screen brightness
SCREENFADETIME = 0.5 # seconds to fade between screen brightness'
backlight.fade_duration = SCREENFADETIME
backlight.brightness = SCREENBRIGHTNESS
while(1): # repeat until able to connect to MPD
try:
player.connect("localhost", 6600)
print("connected")
break
except:
print("sleeping")
sleep(5)
while(1):
state = player.status()["state"] # obtain current playback status of mpd
if state != laststate: # when playback mode has changed
print (state) # show current mode
timepaused = time() # update time state change happened
if state == "pause" or state == "stop": # when the playback has paused or stopped
if time() - timepaused >= SCREENONTIME and lastscreenstate == "on": # if the screen is on and the delay to turn off screen has passed
backlight.brightness = 0 # fade out screen
lastscreenstate = "off" # mark screen as now off
elif state == "play" and lastscreenstate == "off": # if playing and the screen is off
backlight.brightness = SCREENBRIGHTNESS # turn on the screen to set backlight level
lastscreenstate = "on" # mark screen as now on
laststate = state
sleep(0.1) # sleep to prevent 100% thread use
save the file and quit nano. It is worth running "python3 monitor.py" to check script is working you can changed SCREENONTIME to be 5 so the screen will turn off in 5 seconds for testing (remember to change it back to the time you want it to have! default is 5*60).
Now we need to make the script work at boot time.
Code:
sudo nano /etc/rc.local
and add before "exit 0"
Code:
python3 "/home/pi/monitor.py" &
and the reboot the machine. Hopefully the script will be running and as long as it isn't playing the screen should turn off after the time set and turn on when playing again.