forked from TamtamHero/fw-fanctrl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfanctrl.py
More file actions
125 lines (106 loc) · 3.89 KB
/
fanctrl.py
File metadata and controls
125 lines (106 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import argparse
import subprocess
from time import sleep
import json
class FanController:
# state
speed = 0
temps = [0] * 100
_tempIndex = 0
def __init__(self, configPath, strategy):
with open(configPath, "r") as fp:
config = json.load(fp)
if strategy == "":
strategy = config["defaultStrategy"]
strategy = config["strategies"][strategy]
self.speedCurve = strategy["speedCurve"]
self.fanSpeedUpdateFrequency = strategy["fanSpeedUpdateFrequency"]
self.movingAverageInterval = strategy["movingAverageInterval"]
self.setSpeed(self.speedCurve[0]["speed"])
self.updateTemperature()
self.temps = [self.temps[self._tempIndex]] * 100
def setSpeed(self, speed):
self.speed = speed
bashCommand = f"ectool fanduty {speed}"
subprocess.run(bashCommand, stdout=subprocess.PIPE, shell=True)
def adaptSpeed(self):
currentTemp = self.temps[self._tempIndex]
currentTemp = min(
currentTemp, self.getMovingAverageTemperature(
self.movingAverageInterval)
)
minPoint = self.speedCurve[0]
maxPoint = self.speedCurve[-1]
for e in self.speedCurve:
if currentTemp > e["temp"]:
minPoint = e
else:
maxPoint = e
break
if minPoint == maxPoint:
newSpeed = minPoint["speed"]
else:
slope = (maxPoint["speed"] - minPoint["speed"]) / (
maxPoint["temp"] - minPoint["temp"]
)
newSpeed = int(minPoint["speed"] +
(currentTemp - minPoint["temp"]) * slope)
self.setSpeed(newSpeed)
def updateTemperature(self):
sumCoreTemps = 0
sensorsOutput = json.loads(
subprocess.run(
"sensors -j",
stdout=subprocess.PIPE,
shell=True,
text=True,
executable="/bin/bash",
).stdout
)
cores = 0
for k, v in sensorsOutput["coretemp-isa-0000"].items():
if k.startswith("Core "):
i = int(k.split(" ")[1])
cores += 1
sumCoreTemps += float(v["temp" + str(i + 2) + "_input"])
self._tempIndex = (self._tempIndex + 1) % len(self.temps)
self.temps[self._tempIndex] = sumCoreTemps / cores
# return mean temperature over a given time interval (in seconds)
def getMovingAverageTemperature(self, timeInterval):
tempSum = 0
for i in range(0, timeInterval):
tempSum += self.temps[self._tempIndex - i]
return tempSum / timeInterval
def printState(self):
print(
f"speed: {self.speed}% temp: {self.temps[self._tempIndex]}°C movingAverage: {self.getMovingAverageTemperature(self.movingAverageInterval)}°C"
)
def run(self, debug=True):
while True:
self.updateTemperature()
# update fan speed every "fanSpeedUpdateFrequency" seconds
if self._tempIndex % self.fanSpeedUpdateFrequency == 0:
self.adaptSpeed()
if debug:
self.printState()
sleep(1)
def main():
parser = argparse.ArgumentParser(
description="Emulate Ledger Nano/Blue apps.")
parser.add_argument(
"--config", type=str, help="Path to config file", default="./config.json"
)
parser.add_argument(
"--strategy",
type=str,
help='Name of the strategy to use e.g: "lazy" (check config.json for others)',
default="",
)
parser.add_argument(
"--no-log", help="Print speed/temp/meanTemp to stdout", action="store_true"
)
args = parser.parse_args()
fan = FanController(configPath=args.config, strategy=args.strategy)
fan.run(debug=not args.no_log)
if __name__ == "__main__":
main()