add devterm_fan_daemon_cpi_a06 code

This commit is contained in:
cuu 2021-08-23 14:42:22 +08:00
parent e12dac0af2
commit 26ff216506
3 changed files with 166 additions and 0 deletions

View File

@ -0,0 +1,28 @@
# Fan control daemon for devterm A06()
## Install
**devterm is pre-installed this package, so devterm A06 users do not need to repeat the installation steps**
```
wget -O - https://raw.githubusercontent.com/clockworkpi/apt/main/debian/KEY.gpg | sudo apt-key add -
echo "deb https://raw.githubusercontent.com/clockworkpi/apt/main/debian/ stable main" | sudo tee -a /etc/apt/sources.list.d/clockworkpi.list
sudo apt update && apt install -y devterm-fan-daemon-cpi-a06
```
## Change the threshold temperature
Edit `/usr/local/bin/temp_fan_daemon_a06.py`
line starts with `MAX_TEMP=70000`
change the value of MAX_TEMP to be your target temperature value multiplied by 1000,eg 70000 for 70 degree,80000 for 80 degree
for A06 recommended reference temperature range 70-80
then restart systemd service to take effect
`sudo systemctl restart devterm-fan-temp-daemon-a06`

View File

@ -0,0 +1,10 @@
[Unit]
Description=devterm a06 fan control daemon
[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/local/bin/temp_fan_daemon_a06.py
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,128 @@
#!/usr/bin/python3
import glob
import os
import sys,getopt
import subprocess
import time
cpus = []
mid_freq = 0
max_freq = 0
MAX_TEMP=70000
ONCE_TIME=30
def init_fan_gpio():
os.popen("gpio mode 41 out")
def fan_on():
init_fan_gpio()
os.popen("gpio write 41 1")
time.sleep(ONCE_TIME)
def fan_off():
init_fan_gpio()
os.popen("gpio write 41 0")
def isDigit(x):
try:
float(x)
return True
except ValueError:
return False
def cpu_infos():
global cpus
global mid_freq
global max_freq
cpus = glob.glob('/sys/devices/system/cpu/cpu[0-9]')
cpus.sort()
#/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
#/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
scaling_available_freq = open(os.path.join(cpus[0],"cpufreq/scaling_available_frequencies"),"r").read()
scaling_available_freq = scaling_available_freq.strip("\n")
scaling_available_freqs = scaling_available_freq.split(" ")
for var in scaling_available_freqs:
if isDigit(var):
if(int(var) > 1000000):
if(mid_freq == 0):
mid_freq = int(var)
max_freq = int(var)
def set_gov(gov):
global cpus
for var in cpus:
gov_f = os.path.join(var,"cpufreq/scaling_governor")
#print(gov_f)
subprocess.run( "echo %s | sudo tee %s" %(gov,gov_f),shell=True)
def set_performance(scale):
global cpus
global mid_freq
global max_freq
freq = mid_freq
if scale =="mid":
freq = mid_freq
elif scale =="max":
freq = max_freq
for var in cpus:
_f = os.path.join(var,"cpufreq/scaling_max_freq")
#print(_f)
subprocess.run( "echo %s | sudo tee %s" %(freq,_f),shell=True)
def fan_loop():
while True:
temps = glob.glob('/sys/class/thermal/thermal_zone[0-9]/')
temps.sort()
for var in temps:
_f = os.path.join(var,"temp")
#print( open(_f).read().strip("\n") )
_t = open(_f).read().strip("\n")
if isDigit(_t):
if int(_t) > MAX_TEMP:
fan_on()
fan_off()
time.sleep(5)
def main(argv):
global cpus
scale = 'mid'
gov = 'powersave'
try:
opts, args = getopt.getopt(argv,"hs:g:",["scale=","governor="])
except getopt.GetoptError:
print ('test.py -s <scale> -g <governor>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('test.py -s <scale>')
sys.exit()
elif opt in ("-s", "--scale"):
scale = arg
elif opt in ("-g","--governor"):
gov = arg
print ('Scale is ', scale,"Gov is ",gov)
init_fan_gpio()
cpu_infos()
set_gov(gov)
set_performance(scale)
fan_loop()
if __name__ == "__main__":
main(sys.argv[1:])