Advertisement
Guest User

Untitled

a guest
Mar 18th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Bash script to automatically control the backlight brightness using the illumination sensor
  5. # Written because the Gnome 45.4 auto brightness is not smooth unlike my previous MacOS laptop
  6. # which was driving me insane.
  7. #
  8. # Features:
  9. # * smooth, flicker-free ramping
  10. # * sensitivity and delay to prevent constant adjustments
  11. # * manual adjustment though backlight keys or software(*)
  12. # * should work on all Framework 13 AMD and Intel laptops (**)
  13. #
  14. # (*) manual adjustment can be a bit glitchy at times
  15. # (**) Intel has not yet been tested
  16. #
  17. # Adapted from script by Michiel Toneman
  18. # Released under the Apache License V2.0
  19.  
  20. # Define the appropriate devices for Intel and AMD systems
  21. if [ -f /sys/class/backlight/intel_backlight/brightness ]
  22. then
  23. screen_brightness=/sys/class/backlight/intel_backlight/brightness
  24. screen_actual_brightness=/sys/class/backlight/intel_backlight/brightness
  25. screen_max_brightness=/sys/class/backlight/intel_backlight/max_brightness
  26. else
  27. screen_brightness=/sys/class/backlight/amdgpu_bl1/brightness
  28. screen_actual_brightness=/sys/class/backlight/amdgpu_bl1/actual_brightness
  29. screen_max_brightness=/sys/class/backlight/amdgpu_bl1/max_brightness
  30. fi
  31.  
  32. # Set some constants
  33. max=$(cat $screen_max_brightness)
  34. sensitivity=$((max/20))
  35. min=1
  36. delay=1 # Check every 5 seconds
  37. debug=0 # Set to 1 for debug output
  38.  
  39. # Variables
  40. manual_adjust=0 # Start with manual adjustment set to 0
  41. last_target=$(cat $screen_actual_brightness) # Start the target brightness at the current screen brightness
  42. declare -i target=96000
  43.  
  44. # Loop
  45. while [ 1 ]
  46. do
  47. doupdate=0
  48.  
  49. # Get the current state of the backlight and illuminance sensor
  50. sensor=$(cat /sys/bus/iio/devices/iio:device0/in_illuminance_raw)
  51. if [ $debug -eq 1 ]
  52. then
  53. echo "Got sensor as $sensor"
  54. fi
  55. backlight=$(cat $screen_actual_brightness)
  56.  
  57. # If the backlight has been manually changed since the last
  58. # change by the script, we take the delta and add it to the manual
  59. # adjustment.
  60. # Reset the manual adjustment by manually dimming to minimum
  61. if [ $((backlight - last_target)) -ne 0 ]
  62. then
  63. if [ $backlight -lt 4 ] # Backlight controls don't appear to go below 2
  64. then
  65. manual_adjust=0
  66. else
  67. #manual_adjust=$((manual_adjust + (backlight - last_target)))
  68. last_target=$backlight
  69. fi
  70. fi
  71.  
  72. # The target brightness is the sensor reading plus the user's (positive or negative) adjustment
  73. #target=$(( (sensor / 3355 * 96000) + manual_adjust ))
  74. target=$(( sensor * 960 ))
  75. if [ $debug -eq 1 ]
  76. then
  77. echo "Calculated target as $target"
  78. fi
  79.  
  80. # Check if delta between ambient and current backlight exceeds the sensitivity threshold
  81. # so that we don't keep changing brightness all the time
  82. if [ $backlight -gt $target ]
  83. then
  84. if [ $((backlight - target)) -gt $sensitivity ]
  85. then
  86. doupdate=1
  87. fi
  88. fi
  89. if [ $backlight -lt $target ]
  90. then
  91. if [ $((backlight - target)) -lt $sensitivity ]
  92. then
  93. doupdate=1
  94. fi
  95. fi
  96.  
  97. # Check that we don't exceed the min and max brightness values
  98. if [ $target -gt $max ]
  99. then
  100. if [ $debug -eq 1 ]
  101. then
  102. echo "Adjusting target to max value"
  103. fi
  104. target=$max
  105. fi
  106. if [ $target -lt $min ]
  107. then
  108. if [ $debug -eq 1 ]
  109. then
  110. echo "Adjusting target to min value"
  111. fi
  112. target=$min
  113. fi
  114.  
  115. if [ $doupdate -eq 1 ]
  116. then
  117. # Debug logging
  118. if [ $debug -eq 1 ]
  119. then
  120. echo “Starting brightness: $backlight”
  121. echo “Ambient light: $sensor”
  122. echo “Manual adjustment: $manual_adjust”
  123. echo “Target brightness: $target”
  124. echo “-------------------------------”
  125. echo “Sensitivity: $sensitivity”
  126. echo “Min: $min Max: $max”
  127. echo
  128. fi
  129.  
  130. # Now change the brightness smoothly in single value in/decrements per 20ms
  131. intermediate=$backlight
  132.  
  133. if [ $debug -eq 1 ]
  134. then
  135. echo "Entering while loop ..."
  136. fi
  137. #while [ $intermediate -ne $target ]
  138. diff=$(($intermediate - $target))
  139. if [ $debug -eq 1 ]
  140. then
  141. echo "Diff is $diff"
  142. fi
  143. diff_abs=$(( $diff * (($diff>0) - ($diff<0)) ))
  144. if [ $debug -eq 1 ]
  145. then
  146. echo "Diff Abs is $diff_abs"
  147. fi
  148.  
  149. while [ $diff_abs -gt $sensitivity ]
  150. do
  151. if [ $intermediate -lt $target ]
  152. then
  153. #((intermediate++))
  154. #intermediate=($intermediate + 1000)
  155. intermediate=$((intermediate + $diff_abs/80))
  156. else
  157. #((intermediate--))
  158. #intermediate=($intermediate - 1000)
  159. intermediate=$((intermediate - $diff_abs/80))
  160. fi
  161. if [ $debug -eq 1 ]
  162. then
  163. echo "Setting s_b to $intermediate"
  164. fi
  165. echo $intermediate > $screen_brightness
  166. #screen_brightness=$intermediate
  167. sleep 0.01
  168. if [ $debug -eq 1 ]
  169. then
  170. echo "Re-checking target..."
  171. fi
  172. target=$(( $(cat /sys/bus/iio/devices/iio:device0/in_illuminance_raw) * 960 ))
  173. # Check that we don't exceed the min and max brightness
  174. if [ $target -gt $max ]
  175. then
  176. if [ $debug -eq 1 ]
  177. then
  178. echo "Adjusting target to max value"
  179. fi
  180. target=$max
  181. fi
  182. if [ $target -lt $min ]
  183. then
  184. if [ $debug -eq 1 ]
  185. then
  186. echo "Adjusting target to min value"
  187. fi
  188. target=$min
  189. fi
  190. diff=$(($intermediate - $target))
  191. if [ $debug -eq 1 ]
  192. then
  193. echo "Diff is $diff"
  194. fi
  195. diff_abs=$(( $diff * (($diff>0) - ($diff<0)) ))
  196. if [ $debug -eq 1 ]
  197. then
  198. echo "Diff Abs is $diff_abs"
  199. fi
  200. done
  201. if [ $debug -eq 1 ]
  202. then
  203. echo "Ended while loop!"
  204. fi
  205.  
  206. # Remember what the target brightness was
  207. # If the next time round the last target brightness differs from the current backlight value
  208. # then the assumption is that someone has manually adjusted the brightness and we can use that delta
  209. last_target=$target
  210. fi
  211. if [ $debug -eq 1 ]
  212. then
  213. echo "Loop end, sleeping $delay"
  214. fi
  215. sleep $delay
  216. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement