87 lines
1.5 KiB
Bash
Executable file
87 lines
1.5 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
set -e
|
|
|
|
DEVICE="$1" # eg: /sys/class/drm/card1/device
|
|
HWMON=$(echo "$DEVICE"/hwmon/hwmon*)
|
|
|
|
exit() {
|
|
echo "Setting controll to auto" >&2
|
|
echo 2 > "$HWMON/pwm1_enable"
|
|
}
|
|
|
|
trap exit EXIT INT
|
|
|
|
bail() {
|
|
echo "Error: $@" >&2
|
|
echo "Exiting..." >&2
|
|
exit 1
|
|
}
|
|
|
|
if ! [ -d "$HWMON" ]; then
|
|
bail "Invalid HWMON"
|
|
fi
|
|
|
|
TEMP_INPUT="$HWMON/temp2_input"
|
|
|
|
if ! [ -f $TEMP_INPUT ]; then
|
|
bail "Invalid TEMP_INPUT"
|
|
fi
|
|
|
|
TEMP_MIN="$2"
|
|
TEMP_MAX="$3"
|
|
|
|
if [ -z "$TEMP_MIN" ];then
|
|
bail "No minimum temperature provided"
|
|
fi
|
|
|
|
if [ -z "$TEMP_MAX" ];then
|
|
bail "No maximum temperature provided"
|
|
fi
|
|
|
|
PWM_MIN_PCT="$4"
|
|
PWM_MAX_PCT="$5"
|
|
|
|
if [ -z "$PWM_MIN_PCT" ];then
|
|
bail "No minimum fan speed % not provided"
|
|
fi
|
|
|
|
if [ -z "$PWM_MAX_PCT" ];then
|
|
bail "No maximum fan speed % not provided"
|
|
fi
|
|
|
|
PWM_MIN="$(( $PWM_MIN_PCT * 255 / 100))"
|
|
PWM_MAX="$(( $PWM_MAX_PCT * 255 / 100))"
|
|
|
|
echo "Running..." >&2
|
|
|
|
echo "TEMP_MIN=$TEMP_MIN°C"
|
|
echo "TEMP_MAX=$TEMP_MAX°C"
|
|
echo "FAN_MIN=$PWM_MIN_PCT%"
|
|
echo "FAN_MAX=$PWM_MAX_PCT%"
|
|
|
|
echo 1 > "$HWMON/pwm1_enable"
|
|
|
|
PREV=0
|
|
|
|
while true; do
|
|
TEMPERATURE_RAW=$(cat "$TEMP_INPUT")
|
|
TEMPERATURE="$(( $TEMPERATURE_RAW / 1000 ))"
|
|
# Remap from a number between 60_000..90_000 to 0..255
|
|
PWM=$(( ($TEMPERATURE - $TEMP_MIN) * $PWM_MAX / ($TEMP_MAX - $TEMP_MIN) ))
|
|
|
|
if [ "$PWM" -gt $PWM_MAX ]; then
|
|
PWM=$PWM_MAX
|
|
elif [ "$PWM" -lt $PWM_MIN ]; then
|
|
PWM=$PWM_MIN
|
|
fi
|
|
|
|
AVG="$(( ($PWM * 20 + $PREV * 80) / 100 ))"
|
|
|
|
echo "$AVG"
|
|
|
|
echo "$AVG" > "$HWMON/pwm1"
|
|
PREV="$AVG"
|
|
sleep .1s
|
|
done
|