Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # Bash script to automatically control the backlight brightness using the illumination sensor
- # Written because the Gnome 45.4 auto brightness is not smooth unlike my previous MacOS laptop
- # which was driving me insane.
- #
- # Features:
- # * smooth, flicker-free ramping
- # * sensitivity and delay to prevent constant adjustments
- # * manual adjustment though backlight keys or software(*)
- # * should work on all Framework 13 AMD and Intel laptops (**)
- #
- # (*) manual adjustment can be a bit glitchy at times
- # (**) Intel has not yet been tested
- #
- # Adapted from script by Michiel Toneman
- # Released under the Apache License V2.0
- # Define the appropriate devices for Intel and AMD systems
- if [ -f /sys/class/backlight/intel_backlight/brightness ]
- then
- screen_brightness=/sys/class/backlight/intel_backlight/brightness
- screen_actual_brightness=/sys/class/backlight/intel_backlight/brightness
- screen_max_brightness=/sys/class/backlight/intel_backlight/max_brightness
- else
- screen_brightness=/sys/class/backlight/amdgpu_bl1/brightness
- screen_actual_brightness=/sys/class/backlight/amdgpu_bl1/actual_brightness
- screen_max_brightness=/sys/class/backlight/amdgpu_bl1/max_brightness
- fi
- # Set some constants
- max=$(cat $screen_max_brightness)
- sensitivity=$((max/20))
- min=1
- delay=1 # Check every 5 seconds
- debug=0 # Set to 1 for debug output
- # Variables
- manual_adjust=0 # Start with manual adjustment set to 0
- last_target=$(cat $screen_actual_brightness) # Start the target brightness at the current screen brightness
- declare -i target=96000
- # Loop
- while [ 1 ]
- do
- doupdate=0
- # Get the current state of the backlight and illuminance sensor
- sensor=$(cat /sys/bus/iio/devices/iio:device0/in_illuminance_raw)
- if [ $debug -eq 1 ]
- then
- echo "Got sensor as $sensor"
- fi
- backlight=$(cat $screen_actual_brightness)
- # If the backlight has been manually changed since the last
- # change by the script, we take the delta and add it to the manual
- # adjustment.
- # Reset the manual adjustment by manually dimming to minimum
- if [ $((backlight - last_target)) -ne 0 ]
- then
- if [ $backlight -lt 4 ] # Backlight controls don't appear to go below 2
- then
- manual_adjust=0
- else
- #manual_adjust=$((manual_adjust + (backlight - last_target)))
- last_target=$backlight
- fi
- fi
- # The target brightness is the sensor reading plus the user's (positive or negative) adjustment
- #target=$(( (sensor / 3355 * 96000) + manual_adjust ))
- target=$(( sensor * 960 ))
- if [ $debug -eq 1 ]
- then
- echo "Calculated target as $target"
- fi
- # Check if delta between ambient and current backlight exceeds the sensitivity threshold
- # so that we don't keep changing brightness all the time
- if [ $backlight -gt $target ]
- then
- if [ $((backlight - target)) -gt $sensitivity ]
- then
- doupdate=1
- fi
- fi
- if [ $backlight -lt $target ]
- then
- if [ $((backlight - target)) -lt $sensitivity ]
- then
- doupdate=1
- fi
- fi
- # Check that we don't exceed the min and max brightness values
- if [ $target -gt $max ]
- then
- if [ $debug -eq 1 ]
- then
- echo "Adjusting target to max value"
- fi
- target=$max
- fi
- if [ $target -lt $min ]
- then
- if [ $debug -eq 1 ]
- then
- echo "Adjusting target to min value"
- fi
- target=$min
- fi
- if [ $doupdate -eq 1 ]
- then
- # Debug logging
- if [ $debug -eq 1 ]
- then
- echo “Starting brightness: $backlight”
- echo “Ambient light: $sensor”
- echo “Manual adjustment: $manual_adjust”
- echo “Target brightness: $target”
- echo “-------------------------------”
- echo “Sensitivity: $sensitivity”
- echo “Min: $min Max: $max”
- echo
- fi
- # Now change the brightness smoothly in single value in/decrements per 20ms
- intermediate=$backlight
- if [ $debug -eq 1 ]
- then
- echo "Entering while loop ..."
- fi
- #while [ $intermediate -ne $target ]
- diff=$(($intermediate - $target))
- if [ $debug -eq 1 ]
- then
- echo "Diff is $diff"
- fi
- diff_abs=$(( $diff * (($diff>0) - ($diff<0)) ))
- if [ $debug -eq 1 ]
- then
- echo "Diff Abs is $diff_abs"
- fi
- while [ $diff_abs -gt $sensitivity ]
- do
- if [ $intermediate -lt $target ]
- then
- #((intermediate++))
- #intermediate=($intermediate + 1000)
- intermediate=$((intermediate + $diff_abs/80))
- else
- #((intermediate--))
- #intermediate=($intermediate - 1000)
- intermediate=$((intermediate - $diff_abs/80))
- fi
- if [ $debug -eq 1 ]
- then
- echo "Setting s_b to $intermediate"
- fi
- echo $intermediate > $screen_brightness
- #screen_brightness=$intermediate
- sleep 0.01
- if [ $debug -eq 1 ]
- then
- echo "Re-checking target..."
- fi
- target=$(( $(cat /sys/bus/iio/devices/iio:device0/in_illuminance_raw) * 960 ))
- # Check that we don't exceed the min and max brightness
- if [ $target -gt $max ]
- then
- if [ $debug -eq 1 ]
- then
- echo "Adjusting target to max value"
- fi
- target=$max
- fi
- if [ $target -lt $min ]
- then
- if [ $debug -eq 1 ]
- then
- echo "Adjusting target to min value"
- fi
- target=$min
- fi
- diff=$(($intermediate - $target))
- if [ $debug -eq 1 ]
- then
- echo "Diff is $diff"
- fi
- diff_abs=$(( $diff * (($diff>0) - ($diff<0)) ))
- if [ $debug -eq 1 ]
- then
- echo "Diff Abs is $diff_abs"
- fi
- done
- if [ $debug -eq 1 ]
- then
- echo "Ended while loop!"
- fi
- # Remember what the target brightness was
- # If the next time round the last target brightness differs from the current backlight value
- # then the assumption is that someone has manually adjusted the brightness and we can use that delta
- last_target=$target
- fi
- if [ $debug -eq 1 ]
- then
- echo "Loop end, sleeping $delay"
- fi
- sleep $delay
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement