Moode Forum
[IDEA] Keep backlight on while playing then turn off - Printable Version

+- Moode Forum (https://moodeaudio.org/forum)
+-- Forum: moOde audio player (https://moodeaudio.org/forum/forumdisplay.php?fid=3)
+--- Forum: Feature requests (https://moodeaudio.org/forum/forumdisplay.php?fid=8)
+--- Thread: [IDEA] Keep backlight on while playing then turn off (/showthread.php?tid=2401)



Keep backlight on while playing then turn off - smartroad - 04-21-2020

Hi all!

At the moment the backlight for the screen (I'm using the RPi 7") turns off after a set time when the screen is pressed and again after something has been played (if that option is turned on)

Is there a way to keep the screen on WHILE the player plays? Only turning off the screen when the unit has not been touched or played for the set time?

Cheers for the time and effort for this, awesome bit of software!!!


RE: Keep backlight on while playing then turn off - Tim Curtis - 04-21-2020

Good idea. It's prolly doable and I'll add to the TODO list :-)


RE: Keep backlight on while playing then turn off - smartroad - 04-21-2020

Cool, glad it wasn't a stupid idea!!


RE: Keep backlight on while playing then turn off - smartroad - 04-23-2020

(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.


RE: Keep backlight on while playing then turn off - Tim Curtis - 04-23-2020

I was looking at this yesterday and there might be a simpler approach. Try the modification below. It should keep the display from blanking while any audio is playing (MPD, Airplay, etc). When audio is not playing the configured screen blank setting is honored.

sudo killall watchdog.sh
sudo nano /var/www/command/watchdog.sh
Comment out the outermost if statement and its corresponding fi.
sudo /var/www/command/watchdog.sh &
Code:
    # Audio output
    CARD_NUM=$(sqlite3 $SQL_DB "SELECT value FROM cfg_mpd WHERE param='device'")
    HW_PARAMS=$(cat /proc/asound/card$CARD_NUM/pcm0p/sub0/hw_params)
    #if [[ $HW_PARAMS != $HW_PARAMS_LAST ]]; then
        TIME_STAMP=$(date +'%Y%m%d %H%M%S')
        if [[ $HW_PARAMS = "closed" ]]; then
            LOG_MSG=" watchdog: Info: Audio output is (closed)"
        else
            TIME_STAMP=$(date +'%Y%m%d %H%M%S')
            LOG_MSG=" watchdog: Info: Audio output is (in use)"
            # Wake display on play
            WAKE_DISPLAY=$(sqlite3 $SQL_DB "SELECT value FROM cfg_system WHERE param='wake_display'")
            if [[ $WAKE_DISPLAY = "1" ]]; then
                export DISPLAY=:0
                xset s reset > /dev/null 2>&1
            fi
        fi
        #echo $TIME_STAMP$LOG_MSG >> /var/log/moode.log
    #fi

-Tim


RE: Keep backlight on while playing then turn off - smartroad - 04-24-2020

(04-23-2020, 01:47 PM)Tim Curtis Wrote: I was looking at this yesterday and there might be a simpler approach. Try the modification below. It should keep the display from blanking while any audio is playing (MPD, Airplay, etc). When audio is not playing the configured screen blank setting is honored.

sudo killall watchdog.sh
sudo nano /var/www/command/watchdog.sh
Comment out the outermost if statement and its corresponding fi.
sudo /var/www/command/watchdog.sh &
Code:
    # Audio output
    CARD_NUM=$(sqlite3 $SQL_DB "SELECT value FROM cfg_mpd WHERE param='device'")
    HW_PARAMS=$(cat /proc/asound/card$CARD_NUM/pcm0p/sub0/hw_params)
    #if [[ $HW_PARAMS != $HW_PARAMS_LAST ]]; then
        TIME_STAMP=$(date +'%Y%m%d %H%M%S')
        if [[ $HW_PARAMS = "closed" ]]; then
            LOG_MSG=" watchdog: Info: Audio output is (closed)"
        else
            TIME_STAMP=$(date +'%Y%m%d %H%M%S')
            LOG_MSG=" watchdog: Info: Audio output is (in use)"
            # Wake display on play
            WAKE_DISPLAY=$(sqlite3 $SQL_DB "SELECT value FROM cfg_system WHERE param='wake_display'")
            if [[ $WAKE_DISPLAY = "1" ]]; then
                export DISPLAY=:0
                xset s reset > /dev/null 2>&1
            fi
        fi
        #echo $TIME_STAMP$LOG_MSG >> /var/log/moode.log
    #fi

-Tim

That seems to be working! Thanks Big Grin


RE: Keep backlight on while playing then turn off - Tim Curtis - 04-24-2020

Nice :-) I'll include in upcoming 6.5.0 release. It makes sense for this to be the default behavior.


RE: Keep backlight on while playing then turn off - new_bloke - 05-28-2020

(04-24-2020, 12:35 PM)Tim Curtis Wrote: Nice :-) I'll include in upcoming 6.5.0 release. It makes sense for this to be the default behavior.

This doesn't seem to be working for me on 6.5.2. Are there any particular settings that I need to make here? The screen is definitely blanking during playback...


RE: Keep backlight on while playing then turn off - Tim Curtis - 05-28-2020

Look in System Config under the Local Display section.