From 05af18857a52de73f3f0880be45c00fd5114dfe7 Mon Sep 17 00:00:00 2001 From: Ratatoskr Date: Sat, 30 Dec 2023 12:01:12 +0100 Subject: [PATCH] Refactor brightness and colortemp_step calculations - Simplified calculation of brightness_step and colortemp_step for better readability. - Corrected the calculation of reached final values for brightness and color temperature. - Introduced sleep_duration variable for improved flexibility in sleep intervals. - Adjusted the loop to start from 0 to correctly set the initial values. - Added a comment to clarify the purpose of the loop and its iteration. - Set the current_brightness and current_colortemp directly to the end values after the loop, ensuring they reach the intended final values. - Moved the sleep_duration calculation inside the loop for consistency. This refactoring enhances the script's clarity and ensures accurate calculation of brightness and color temperature steps during the gradual adjustment process. The changes also include improvements in sleep duration handling. --- wecker.sh | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/wecker.sh b/wecker.sh index a82b2a5..fba85b0 100755 --- a/wecker.sh +++ b/wecker.sh @@ -169,14 +169,17 @@ else fi # Calculate steps for brightness and color temperature per step -brightness_step=$(bc <<< "($end_brightness - $start_brightness) / $steps") +brightness_step=$(( (end_brightness - start_brightness) / steps )) echo "Brightness difference per step: $brightness_step" -echo "Reached final brightness: $((brightness_step * steps)) from $end_brightness" -colortemp_step=$(bc <<< "$start_colortemp / $steps") +echo "Reached final brightness: $((brightness_step * steps + start_brightness)) from $end_brightness" + +colortemp_step=$(( start_colortemp / steps )) echo "Color temperature difference per step: $colortemp_step" -echo "Reached final colortemp: $((start_colortemp - (colortemp_step * steps))) from $end_colortemp" +echo "Reached final colortemp: $((start_colortemp - colortemp_step * steps)) from $end_colortemp" +sleep_duration=$((duration_seconds / steps)) + # Gradual adjustment of brightness and color temperature -for ((i = 1; i <= $steps; i++)); do +for ((i = 0; i <= $steps; i++)); do current_brightness=$(printf "%.0f" $(bc <<< "$start_brightness + ($brightness_step * $i)")) current_colortemp=$(printf "%.0f" $(bc <<< "$start_colortemp - ($colortemp_step * $i)")) @@ -200,10 +203,16 @@ for ((i = 1; i <= $steps; i++)); do sleep 12 echo "DEBUG: Wait 12 sec. between steps." else - sleep 27 # Changes every 30 (27) seconds + sleep $sleep_duration # Changes every 30 seconds fi done +# Set to end values. +current_brightness="$end_brightness" +current_colortemp="$end_colortemp" +set_brightness "$end_brightness" +set_colortemp "$end_colortemp" + # Execute the check_ip function. check_ip echo "$A72_ip"