From e8daf94cdae484eea559213e4f9084f40aaa2507 Mon Sep 17 00:00:00 2001
From: steel99xl <steel99xl@gmail.com>
Date: Sat, 10 Feb 2024 11:47:06 -0500
Subject: [PATCH] Now with the notes from the debug versoin

---
 AutomaticBrightness.sh | 48 +++++++++++++++++++++++++++++-------------
 1 file changed, 33 insertions(+), 15 deletions(-)

diff --git a/AutomaticBrightness.sh b/AutomaticBrightness.sh
index c811c35..5056a71 100755
--- a/AutomaticBrightness.sh
+++ b/AutomaticBrightness.sh
@@ -24,6 +24,9 @@ MinimumBrightness=001
 # 2 : Default | 1 : Add Offset | 0 : Subtract Offset, Recomended not to change
 op=2
 
+
+# Only look for flags -i or -d with an aditional value
+# AutomaticBrightness.sh -i 100
 while getopts i:d: flag
 do
     case "${flag}" in
@@ -34,6 +37,7 @@ do
     esac
 done
 
+# Verigy offset file exsits and if so read it
 if [[ -f /dev/shm/AB.offset ]]
 then
   OffSet=$(cat /dev/shm/AB.offset)
@@ -43,10 +47,10 @@ else
   $(chmod 666 /dev/shm/AB.offset)
 fi
 
-
+#if no offset or its less than 0 make 0
 OffSet=$((OffSet < 0 ? 0 : OffSet))
 
-
+# relativly change number in Offset file and write it
 if [[ $op -lt 2 ]]
 then
   if [[ $op -eq 1 ]]
@@ -56,6 +60,7 @@ then
     OffSet=$((OffSet - num))
   fi
 
+  # verify offset is not less than 0
   OffSet=$((OffSet < 0 ? 0 : OffSet))
 
   $(echo $OffSet > /dev/shm/AB.offset)
@@ -72,14 +77,17 @@ renice "$priority" "$$"
 
 sleep 5
 
+# Get screen max brightness value
 MaxScreenBrightness=$(find -L /sys/class/backlight -maxdepth 2 -name "max_brightness" 2>/dev/null | grep "max_brightness" | xargs cat)
 
+# Set path to current screen brightness value
 BLightPath=$(find -L /sys/class/backlight -maxdepth 2 -name "brightness" 2>/dev/null | grep "brightness")
 
+# Set path to current luminance sensor
 LSensorPath=$(find -L /sys/bus/iio/devices -maxdepth 2  -name "in_illuminance_raw" 2>/dev/null | grep "in_illuminance_raw")
 
 
-
+#Set the current light value so we have something to compare to
 OldLight=$(cat $LSensorPath)
 
 while true
@@ -94,16 +102,14 @@ do
     fi
 
 		Light=$(cat $LSensorPath)
+    ## apply offset to current light value
     Light=$((Light + OffSet))
 
-    if [[ $Light -lt $LightChange ]] 
-    then
-      MaxOld=$((OldLight + LightChange))
-      MinOld=$((OldLight - LightChange))
-    else
-      MaxOld=$((OldLight + OldLight/LightChange))
-      MinOld=$((OldLight - OldLight/LightChange))
-    fi
+    # Set allowed range for light 
+    
+    MaxOld=$((OldLight + OldLight/LightChange))
+    MinOld=$((OldLight - OldLight/LightChange))
+
 
     if [[ $Light -gt $MaxOld ]] || [[ $Light -lt $MinOld ]]
     then
@@ -111,13 +117,17 @@ do
 
 		  CurrentBrightness=$(cat $BLightPath)
 
-
+      # Add MinimumBrighness here to not effect comparison but the outcome
 		  Light=$(( $Light + $MinimumBrightness ))
-
+      
+      # Gernate a TempLight value for the screen to be set to
+      # Float math thanks Matthias_Wachter 
       TempLight=$(echo "scale=2; $Light * $SensorToDisplayScale" | bc)
       # REmoves float for latter checks
       TempLight=$(LANG=C printf "%.0f" $TempLight)
 
+
+      # Check we dont ask the screen to go brighter than it can
 		  if [[ $TempLight -gt $MaxScreenBrightness ]]
 		  then
 			  NewLight=$MaxScreenBrightness
@@ -125,13 +135,18 @@ do
 			  NewLight=$TempLight
 		  fi
 
+      # How diffrent should each stop be
 		  DiffCount=$(( ( $NewLight - $CurrentBrightness ) / $LevelSteps ))
 
+      # Step once per Screen Hz to make animation
 		  for i in $(eval echo {1..$LevelSteps} )
 		  do
 
+        # Set new relative light value
 			  NewLight=$(( $DiffCount ))
 
+
+        # Format values apropriatly for brightnessctl
 			  if [[ $NewLight -lt 0 ]]
 			  then
 			  NewLight=$( echo "$NewLight" | awk -F "-" {'print$2'})
@@ -139,12 +154,15 @@ do
 			  else
 			  NewLight=$(echo +$NewLight)
 			  fi
-
+        
+        # Adjust brightness relativly
 			  brightnessctl -q s $NewLight
+        # Sleep for the screen Hz time so he effect is visible
 			  sleep $AnimationDelay
 
 		  done
-
+      
+      # Store new light as old light for next comparison
       OldLight=$Light
     fi