From 2568fc10734265c7e35c269fe2368a3639cf17c1 Mon Sep 17 00:00:00 2001 From: Maciej Grela Date: Sun, 26 Mar 2023 17:02:22 +0200 Subject: [PATCH] Allow configurations with MINPWM > MAXPWM These configurations would drive the PWM outputs in an "inverted mode" where the 0 value is full fan speed and 255 stops the fan. The PWM output adjustment algorithm works correctly in this situation. --- prog/pwm/fancontrol | 97 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 73 insertions(+), 24 deletions(-) diff --git a/prog/pwm/fancontrol b/prog/pwm/fancontrol index 886a7367..94e3bc48 100755 --- a/prog/pwm/fancontrol +++ b/prog/pwm/fancontrol @@ -118,30 +118,79 @@ function LoadConfig echo "MINTEMP must be less than MAXTEMP" >&2 exit 1 fi - if [ "${AFCMAXPWM[$fcvcount]}" -gt 255 ] - then - echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 - echo "MAXPWM must be at most 255" >&2 - exit 1 - fi - if [ "${AFCMINSTOP[$fcvcount]}" -ge "${AFCMAXPWM[$fcvcount]}" ] - then - echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 - echo "MINSTOP must be less than MAXPWM" >&2 - exit 1 - fi - if [ "${AFCMINSTOP[$fcvcount]}" -lt "${AFCMINPWM[$fcvcount]}" ] - then - echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 - echo "MINSTOP must be greater than or equal to MINPWM" >&2 - exit 1 - fi - if [ "${AFCMINPWM[$fcvcount]}" -lt 0 ] - then - echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 - echo "MINPWM must be at least 0" >&2 - exit 1 - fi + + if [ "${AFCMINPWM[$fvcount]}" -lt "${AFCMAXPWM[$fvcount]}" ]; then + # Normal drive: + # PWM value 0 -> Fan not spinning + # PWM value 255 -> Fan spinning at full speed + + if [ "${AFCMAXPWM[$fcvcount]}" -gt $MAX ] + then + echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 + echo "For normal drive MAXPWM must be at most $MAX" >&2 + exit 1 + fi + + if [ "${AFCMINSTOP[$fcvcount]}" -ge "${AFCMAXPWM[$fcvcount]}" ] + then + echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 + echo "For normal drive MINSTOP must be less than MAXPWM" >&2 + exit 1 + fi + + if [ "${AFCMINSTOP[$fcvcount]}" -lt "${AFCMINPWM[$fcvcount]}" ] + then + echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 + echo "For normal drive MINSTOP must be greater than or equal to MINPWM" >&2 + exit 1 + fi + + if [ "${AFCMINPWM[$fcvcount]}" -lt 0 ] + then + echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 + echo "For normal drive MINPWM must be at least 0" >&2 + exit 1 + fi + + elif [ "${AFCMINPWM[$fvcount]}" -gt "${AFCMAXPWM[$fvcount]}" ]; then + # Inverted drive: + # PWM value 255 -> Fan not spinning + # PWM value 0 -> Fan spinning at full speed + + if [ "${AFCMINPWM[$fcvcount]}" -gt $MAX ] + then + echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 + echo "For inverted drive MINPWM must be at most $MAX" >&2 + exit 1 + fi + + if [ "${AFCMINSTOP[$fcvcount]}" -lt "${AFCMAXPWM[$fcvcount]}" ] + then + echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 + echo "For inverted drive MINSTOP must be larger than MAXPWM" >&2 + exit 1 + fi + + if [ "${AFCMINSTOP[$fcvcount]}" -ge "${AFCMINPWM[$fcvcount]}" ] + then + echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 + echo "For inverted drive MINSTOP must be less than MINPWM" >&2 + exit 1 + fi + + if [ "${AFCMAXPWM[$fcvcount]}" -lt 0 ] + then + echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 + echo "For inverted drive MAXPWM must be at least 0" >&2 + exit 1 + fi + + else + echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2 + echo "MINPWM and MAXPWM cannot be equal" >&2 + exit 1 + fi + if [ "${AFCAVERAGE[$fcvcount]}" -lt 1 ] then echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2