MarkWattTech-Tutorials icon indicating copy to clipboard operation
MarkWattTech-Tutorials copied to clipboard

Auto Dimming

Open OsiMood opened this issue 3 years ago • 2 comments

Great script, still missing an auto dimming function, or the screen remain to max brightness all the time.

OsiMood avatar Sep 07 '22 04:09 OsiMood

I wrote a script to do Auto Dimming. Have a strong feeling it may not work for everyone, as it heavily depends on the type of touchscreen one has:

#!/bin/bash

event_device="/dev/input/event0"
event_log="/home/kiosk/event_log.txt"
dim=18
bright=80
night=25
time_to_idle=60
counter=0

# Start evtest in the background to continuously monitor input events
sudo evtest "$event_device" > "$event_log" &

sleep 1

# Get the process ID of the evtest command
evtest_pid=$!

# Function to stop evtest and clean up
function cleanup {
    kill "$evtest_pid"
    wait "$evtest_pid"
    rm "$event_log"
    exit 0
}

# Set up a trap to call the cleanup function on script exit
trap cleanup EXIT

# Start monitoring the event log file with inotifywait

while true; do

    # Check if it's midnight
    if [ "$(date +%H:%M)" = "00:00" ]; then
        # Clear the event log file
        echo -n "" > "$event_log"
    fi

    inotifywait -t 1 -e modify "$event_log" 2> /dev/null
    exit_code=$?

    if [ $exit_code -eq 0 ]; then

        # Someone is using the kiosk

        if grep -q "night" /home/kiosk/day_or_night; then

                /home/kiosk/adjust_brightness.sh $night

        else

                /home/kiosk/adjust_brightness.sh $bright

        fi

        counter=0

    elif [ $exit_code -eq 2 ]; then

        let counter++

        if [ $counter -ge $time_to_idle ]; then

            # Screen has been idle

            if [ $counter -eq $time_to_idle ]; then

                /home/kiosk/adjust_brightness.sh $dim

            fi

            counter=$time_to_idle

        else

            if grep -q "night" /home/kiosk/day_or_night; then

                /home/kiosk/adjust_brightness.sh $night

            else

                /home/kiosk/adjust_brightness.sh $bright

            fi

        fi

    fi

done

The above bash script is to be invoked in /etc/rc.local as a background task, and it will run continuously after power up.

Also the script calls this shell script below called adjust_brightness.sh:

#!/bin/bash

# Step 1: Get the initial value of /sys/class/backlight/10-0045/brightness
initial_value=$(cat /sys/class/backlight/10-0045/brightness)

# Step 2: Gradually increase or decrease the value to the input value by incrementing or decrementing it by 1 each time
if (( $initial_value < $1 )); then
    for ((i=$initial_value; i<=$1; i++)); do
        # Step 3: Send the incremented value to /sys/class/backlight/10-0045/brightness
        sudo echo $i | sudo tee /sys/class/backlight/10-0045/brightness > /dev/null
        # Step 4: Wait for 0.05 seconds before sending the next tee command
#        sleep 0.005
    done
else
    for ((i=$initial_value; i>=$1; i--)); do
        # Step 3: Send the decremented value to /sys/class/backlight/10-0045/brightness
        sudo echo $i | sudo tee /sys/class/backlight/10-0045/brightness > /dev/null
        # Step 4: Wait for 0.05 seconds before sending the next tee command
#        sleep 0.005
    done
fi

# Step 5: Send one final tee to /sys/class/backlight/10-0045/brightness with the value being exactly the input value
sudo echo $1 | sudo tee /sys/class/backlight/10-0045/brightness > /dev/null

# Step 6: Exit the script
exit 0

seemebreakthis avatar Jan 28 '24 11:01 seemebreakthis

I just wanted to chime in and say that this is the only solution I've really found that works with the latest release of raspbian. I kept it simple and just write to /sys/class/backlight/10-0045/bl_power (0 turns on the backlight, 1 turns it off), instead of adjusting the brightness differently based on day or night. I don't know why none of the other tutorials work (like xset, DPMS, xscreensaver) but this is the only thing that works consistently and without a lot of fussing.

If anyone else finds this in the future, you need to install inotify-tools and evtest, and you need to make the script executable using chmod (and a little Google if you don't know what to do).

I might also still install Wallpanel via HACS, since even with the backlight off you still need to blank the screen to avoid burn-in. This also prevent accidentally clicking something just to wake up the screen.

sellison2 avatar Mar 21 '24 00:03 sellison2