From 3cb7c68020c476d5ad517e0ca6f63b8a0b788be8 Mon Sep 17 00:00:00 2001 From: Antoine RAYNARD Date: Sat, 22 Jan 2022 20:30:59 +0100 Subject: [PATCH 1/2] Add 2 parameters -u and -U, respectively Fan-OFF and Fan-ON temperature thresholds, to enable passive-cooling functionality --- pwm-fan.sh | 79 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/pwm-fan.sh b/pwm-fan.sh index a075d7b..57c8c82 100644 --- a/pwm-fan.sh +++ b/pwm-fan.sh @@ -106,7 +106,7 @@ export_pwmchip_channel () { fan_initialization () { if [[ -z "$TIME_STARTUP" ]]; then - TIME_STARTUP=60 + TIME_STARTUP=10 fi cache 'test_fan' local READ_MAX_DUTY_CYCLE=$(cat $CHANNEL_FOLDER'period') @@ -125,6 +125,7 @@ fan_initialization () { echo 1 > $CHANNEL_FOLDER'enable' sleep $TIME_STARTUP echo $((MAX_DUTY_CYCLE/2)) > $CHANNEL_FOLDER'duty_cycle' + ON_OFF_STATE=1 echo '[pwm-fan] Initialization done. Duty cycle at 50% now: '$((MAX_DUTY_CYCLE/2))' ns.' sleep 1 } @@ -155,6 +156,12 @@ fan_run_thermal () { THERMAL_ABS_THRESH_HIGH=75 fi THERMAL_ABS_THRESH=($THERMAL_ABS_THRESH_LOW $THERMAL_ABS_THRESH_HIGH) + if [[ -z $THERMAL_ABS_THRESH_OFF ]]; then + THERMAL_ABS_THRESH_OFF=0 + fi + if [[ -z $THERMAL_ABS_THRESH_ON ]]; then + THERMAL_ABS_THRESH_ON=1 + fi if [[ -z $DC_PERCENT_MIN ]]; then DC_PERCENT_MIN=25 fi @@ -174,28 +181,36 @@ fan_run_thermal () { if [[ ${#TEMPS[@]} -gt $TEMPS_SIZE ]]; then TEMPS=(${TEMPS[@]:1}) fi - if [[ ${TEMPS[-1]} -le ${THERMAL_ABS_THRESH[0]} ]]; then - echo ${DC_ABS_THRESH[0]} 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' - elif [[ ${TEMPS[-1]} -ge ${THERMAL_ABS_THRESH[-1]} ]]; then - echo ${DC_ABS_THRESH[-1]} 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' - elif [[ ${#TEMPS[@]} -gt 1 ]]; then - TEMPS_SUM=0 - for TEMP in ${TEMPS[@]}; do - let TEMPS_SUM+=$TEMP - done - # moving mid-point - MEAN_TEMP=$((TEMPS_SUM/${#TEMPS[@]})) - DEV_MEAN_CRITICAL=$((MEAN_TEMP-100)) - X0=${DEV_MEAN_CRITICAL#-} - # args: x, x0, L, a, b (k=a/b) - MODEL=$(function_logistic ${TEMPS[-1]} $X0 ${DC_ABS_THRESH[-1]} 1 10) - if [[ $MODEL -lt ${DC_ABS_THRESH[0]} ]]; then - echo ${DC_ABS_THRESH[0]} 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' - elif [[ $MODEL -gt ${DC_ABS_THRESH[-1]} ]]; then - echo ${DC_ABS_THRESH[-1]} 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' - else - echo $MODEL 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' - fi + if [[ ${TEMPS[-1]} -le ${THERMAL_ABS_THRESH_OFF} ]]; then + echo "0" 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' + ON_OFF_STATE=0 + elif [[ ${TEMPS[-1]} -ge ${THERMAL_ABS_THRESH_ON} ]]; then + ON_OFF_STATE=1 + fi + if [[ $ON_OFF_STATE -eq 1 ]]; then + if [[ ${TEMPS[-1]} -le ${THERMAL_ABS_THRESH[0]} ]]; then + echo ${DC_ABS_THRESH[0]} 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' + elif [[ ${TEMPS[-1]} -ge ${THERMAL_ABS_THRESH[-1]} ]]; then + echo ${DC_ABS_THRESH[-1]} 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' + elif [[ ${#TEMPS[@]} -gt 1 ]]; then + TEMPS_SUM=0 + for TEMP in ${TEMPS[@]}; do + let TEMPS_SUM+=$TEMP + done + # moving mid-point + MEAN_TEMP=$((TEMPS_SUM/${#TEMPS[@]})) + DEV_MEAN_CRITICAL=$((MEAN_TEMP-100)) + X0=${DEV_MEAN_CRITICAL#-} + # args: x, x0, L, a, b (k=a/b) + MODEL=$(function_logistic ${TEMPS[-1]} $X0 ${DC_ABS_THRESH[-1]} 1 10) + if [[ $MODEL -lt ${DC_ABS_THRESH[0]} ]]; then + echo ${DC_ABS_THRESH[0]} 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' + elif [[ $MODEL -gt ${DC_ABS_THRESH[-1]} ]]; then + echo ${DC_ABS_THRESH[-1]} 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' + else + echo $MODEL 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' + fi + fi fi sleep $TIME_LOOP done @@ -370,6 +385,8 @@ usage() { echo ' -s int The MAX SIZE of the TEMPERATURE ARRAY. Interval between data points is set by -l. Default (store last 1min data): 6.' echo ' -t int Lowest TEMPERATURE threshold (in Celsius). Lower temps set the fan speed to min. Default: 25' echo ' -T int Highest TEMPERATURE threshold (in Celsius). Higher temps set the fan speed to max. Default: 75' + echo ' -u int Fan-off TEMPERATURE threshold (in Celsius). Shuts off fan under the specified temperature. Default: 0' + echo ' -U int Fan-On TEMPERATURE threshold (in Celsius). Trun on fan control above the specified temperature. Default: 1' echo '' echo ' If no options are provided, the script will run with default values.' echo ' Defaults have been tested and optimized for the following hardware:' @@ -389,7 +406,7 @@ usage() { echo '' } -while getopts 'c:C:d:D:fF:hl:m:p:s:t:T:' OPT; do +while getopts 'c:C:d:D:fF:hl:m:p:s:t:T:u:U:' OPT; do case ${OPT} in c) CHANNEL="$OPTARG" @@ -471,6 +488,20 @@ while getopts 'c:C:d:D:fF:hl:m:p:s:t:T:' OPT; do exit 1 fi ;; + u) + THERMAL_ABS_THRESH_OFF="$OPTARG" + if [[ ! $THERMAL_ABS_THRESH_OFF =~ ^[0-4][0-9]?$ ]]; then + echo 'The OFF temperature threshold must be an integer between 0 and 49.' + exit 1 + fi + ;; + U) + THERMAL_ABS_THRESH_ON="$OPTARG" + if [[ $THERMAL_ABS_THRESH_ON -le $THERMAL_ABS_THRESH_OFF ]]; then + echo 'The ON temperature threshold must be an integer strictly greater than the OFF temperature threshold.' + exit 1 + fi + ;; \?) echo '!! ATTENTION !!' echo '................................' From 6589b9914551fe48e76ff5ce2361651cb43b3409 Mon Sep 17 00:00:00 2001 From: Antoine RAYNARD Date: Sun, 23 Jan 2022 18:21:21 +0100 Subject: [PATCH 2/2] Fix argument checks for -t, -T and -u, as well as other review comments 1. Fix the regex check for -t, -T and -u to behave has expected 2. Change variable name to a more descriptive one: FAN_ON_OFF_STATE and declare it in fan_run_thermal () 3. Revert TIME_STARTUP to 60 seconds 4. Fix typo in help message --- pwm-fan.sh | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pwm-fan.sh b/pwm-fan.sh index 57c8c82..3bd5705 100644 --- a/pwm-fan.sh +++ b/pwm-fan.sh @@ -106,7 +106,7 @@ export_pwmchip_channel () { fan_initialization () { if [[ -z "$TIME_STARTUP" ]]; then - TIME_STARTUP=10 + TIME_STARTUP=60 fi cache 'test_fan' local READ_MAX_DUTY_CYCLE=$(cat $CHANNEL_FOLDER'period') @@ -125,7 +125,6 @@ fan_initialization () { echo 1 > $CHANNEL_FOLDER'enable' sleep $TIME_STARTUP echo $((MAX_DUTY_CYCLE/2)) > $CHANNEL_FOLDER'duty_cycle' - ON_OFF_STATE=1 echo '[pwm-fan] Initialization done. Duty cycle at 50% now: '$((MAX_DUTY_CYCLE/2))' ns.' sleep 1 } @@ -162,6 +161,9 @@ fan_run_thermal () { if [[ -z $THERMAL_ABS_THRESH_ON ]]; then THERMAL_ABS_THRESH_ON=1 fi + if [[ -z $FAN_ON_OFF_STATE ]]; then + FAN_ON_OFF_STATE=1 + fi if [[ -z $DC_PERCENT_MIN ]]; then DC_PERCENT_MIN=25 fi @@ -183,11 +185,11 @@ fan_run_thermal () { fi if [[ ${TEMPS[-1]} -le ${THERMAL_ABS_THRESH_OFF} ]]; then echo "0" 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' - ON_OFF_STATE=0 + FAN_ON_OFF_STATE=0 elif [[ ${TEMPS[-1]} -ge ${THERMAL_ABS_THRESH_ON} ]]; then - ON_OFF_STATE=1 + FAN_ON_OFF_STATE=1 fi - if [[ $ON_OFF_STATE -eq 1 ]]; then + if [[ $FAN_ON_OFF_STATE -eq 1 ]]; then if [[ ${TEMPS[-1]} -le ${THERMAL_ABS_THRESH[0]} ]]; then echo ${DC_ABS_THRESH[0]} 2> /dev/null > $CHANNEL_FOLDER'duty_cycle' elif [[ ${TEMPS[-1]} -ge ${THERMAL_ABS_THRESH[-1]} ]]; then @@ -386,7 +388,7 @@ usage() { echo ' -t int Lowest TEMPERATURE threshold (in Celsius). Lower temps set the fan speed to min. Default: 25' echo ' -T int Highest TEMPERATURE threshold (in Celsius). Higher temps set the fan speed to max. Default: 75' echo ' -u int Fan-off TEMPERATURE threshold (in Celsius). Shuts off fan under the specified temperature. Default: 0' - echo ' -U int Fan-On TEMPERATURE threshold (in Celsius). Trun on fan control above the specified temperature. Default: 1' + echo ' -U int Fan-On TEMPERATURE threshold (in Celsius). Turn on fan control above the specified temperature. Default: 1' echo '' echo ' If no options are provided, the script will run with default values.' echo ' Defaults have been tested and optimized for the following hardware:' @@ -476,21 +478,21 @@ while getopts 'c:C:d:D:fF:hl:m:p:s:t:T:u:U:' OPT; do ;; t) THERMAL_ABS_THRESH_LOW="$OPTARG" - if [[ ! $THERMAL_ABS_THRESH_LOW =~ ^[0-4][0-9]?$ ]]; then + if [[ ! $THERMAL_ABS_THRESH_LOW =~ ^[0-4]?[0-9]$ ]]; then echo 'The lowest temperature threshold must be an integer between 0 and 49.' exit 1 fi ;; T) THERMAL_ABS_THRESH_HIGH="$OPTARG" - if [[ ! $THERMAL_ABS_THRESH_HIGH =~ ^([5-9][0-9]?|1[0-1][0-9]?|120)$ ]]; then + if [[ ! $THERMAL_ABS_THRESH_HIGH =~ ^([5-9][0-9]|1[0-1][0-9]|120)$ ]]; then echo 'The highest temperature threshold must be an integer between 50 and 120.' exit 1 fi ;; u) THERMAL_ABS_THRESH_OFF="$OPTARG" - if [[ ! $THERMAL_ABS_THRESH_OFF =~ ^[0-4][0-9]?$ ]]; then + if [[ ! $THERMAL_ABS_THRESH_OFF =~ ^[0-4]?[0-9]$ ]]; then echo 'The OFF temperature threshold must be an integer between 0 and 49.' exit 1 fi