76 lines
1.7 KiB
Bash
76 lines
1.7 KiB
Bash
|
#!/bin/bash
|
||
|
|
||
|
#
|
||
|
# My Odroid fan control script
|
||
|
# (only tested with an Odroid HC4 running Manjaro)
|
||
|
#
|
||
|
|
||
|
# Information (don't touch unless you're me)
|
||
|
NAME="odroid-fancontrol.sh"
|
||
|
AUTHOR="david@socialnerds.org"
|
||
|
VERSION="0.2.1"
|
||
|
LICENSE="MIT"
|
||
|
DESCRIPTION="$NAME (v$VERSION) controls the fan speed in an Odroid HC4 (and possibly others) based on temperature. It is written by $AUTHOR and published under the $LICENSE license."
|
||
|
|
||
|
# Configuration
|
||
|
THRESHOLD=50000
|
||
|
DISTANCE=1000
|
||
|
INTERVAL=30
|
||
|
STEPS=(120 160 210 255)
|
||
|
FAN="/sys/class/hwmon/hwmon2/pwm1"
|
||
|
LOGFILE="/var/log/hc4_fancontrol.log"
|
||
|
|
||
|
# Get all temperature readings and return the highest value
|
||
|
function get_temp() {
|
||
|
local TEMPS=($(cat /sys/devices/virtual/thermal/thermal_zone*/temp))
|
||
|
local RESULT=${TEMPS[0]};
|
||
|
for TEMP in ${TEMPS[@]}; do
|
||
|
if [ $TEMP -gt $RESULT ]; then
|
||
|
RESULT=$TEMP
|
||
|
fi
|
||
|
done
|
||
|
echo $RESULT
|
||
|
}
|
||
|
|
||
|
# Get current fan speed
|
||
|
function get_speed() {
|
||
|
local SPEED=$(cat $FAN)
|
||
|
echo $SPEED
|
||
|
}
|
||
|
|
||
|
# Set new fan speed
|
||
|
function set_speed() {
|
||
|
local CURRENT_SPEED=$(get_speed)
|
||
|
local NEW_SPEED=$1
|
||
|
if [ $CURRENT_SPEED -ne $NEW_SPEED ]; then
|
||
|
echo $NEW_SPEED > $FAN
|
||
|
if [ -n $LOGFILE ]; then
|
||
|
echo "$(date +%Y%m%d-%H%M%S): Temp[$(get_temp)], Speed[$(get_speed)]" >> $LOGFILE
|
||
|
fi
|
||
|
sleep 3
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
# Set fan mode to manual
|
||
|
echo "disabled" | tee /sys/class/thermal/thermal_zone{0,1}/mode > /dev/null
|
||
|
|
||
|
# Run loop
|
||
|
while true; do
|
||
|
TEMP=$(get_temp)
|
||
|
SPEED=$(get_speed)
|
||
|
NEW_THRESHOLD=$THRESHOLD
|
||
|
if [ $TEMP -gt $THRESHOLD ]; then
|
||
|
for STEP in ${STEPS[@]}; do
|
||
|
if [ $TEMP -gt $NEW_THRESHOLD ]; then
|
||
|
NEW_THRESHOLD=$((NEW_THRESHOLD + DISTANCE))
|
||
|
if [ $SPEED -lt $STEP ]; then
|
||
|
set_speed $STEP
|
||
|
fi
|
||
|
fi
|
||
|
done
|
||
|
else
|
||
|
set_speed 0
|
||
|
fi
|
||
|
sleep $INTERVAL
|
||
|
done
|