Improve fan state detection and prepare for jupiter fan control

This commit is contained in:
NGnius 2022-04-29 20:30:09 -04:00
parent 65d0468aeb
commit 8f448b0306

30
main.py
View file

@ -1,11 +1,12 @@
import time import time
#import subprocess
class Plugin: class Plugin:
CPU_COUNT = 8 CPU_COUNT = 8
SCALING_FREQUENCIES = [1700000, 2400000, 2800000] SCALING_FREQUENCIES = [1700000, 2400000, 2800000]
FAN_VOLTAGES = [0, 1000, 2000, 3000, 4000, 5000, 6000] FAN_SPEEDS = [0, 1000, 2000, 3000, 4000, 5000, 6000]
set_fan_voltage = None auto_fan = True
# CPU stuff # CPU stuff
@ -74,36 +75,37 @@ class Plugin:
# Fan stuff # Fan stuff
async def set_fan_tick(self, tick: int): async def set_fan_tick(self, tick: int):
self.set_fan_voltage = tick # cache voltage set to echo back in get_fan_tick() if tick >= len(self.FAN_SPEEDS):
if tick >= len(self.FAN_VOLTAGES):
# automatic mode # automatic mode
self.auto_fan = True
write_to_sys("/sys/class/hwmon/hwmon5/recalculate", 0) write_to_sys("/sys/class/hwmon/hwmon5/recalculate", 0)
write_to_sys("/sys/class/hwmon/hwmon5/fan1_target", 4099) # 4099 is default write_to_sys("/sys/class/hwmon/hwmon5/fan1_target", 4099) # 4099 is default
#subprocess.run(["systemctl", "start", "jupiter-fan-control.service"])
else: else:
# manual voltage # manual voltage
self.auto_fan = False
write_to_sys("/sys/class/hwmon/hwmon5/recalculate", 1) write_to_sys("/sys/class/hwmon/hwmon5/recalculate", 1)
write_to_sys("/sys/class/hwmon/hwmon5/fan1_target", self.FAN_VOLTAGES[tick]) write_to_sys("/sys/class/hwmon/hwmon5/fan1_target", self.FAN_SPEEDS[tick])
#subprocess.run(["systemctl", "stop", "jupiter-fan-control.service"])
async def get_fan_tick(self) -> int: async def get_fan_tick(self) -> int:
fan_target = int(read_from_sys("/sys/class/hwmon/hwmon5/fan1_target", amount=-1).strip()) fan_target = int(read_from_sys("/sys/class/hwmon/hwmon5/fan1_target", amount=-1).strip())
fan_input = int(read_from_sys("/sys/class/hwmon/hwmon5/fan1_input", amount=-1).strip()) fan_input = int(read_from_sys("/sys/class/hwmon/hwmon5/fan1_input", amount=-1).strip())
fan_target_v = float(fan_target) / 1000 fan_target_v = float(fan_target) / 1000
fan_input_v = float(fan_input) / 1000 fan_input_v = float(fan_input) / 1000
if self.set_fan_voltage is not None: if self.auto_fan:
x = self.set_fan_voltage return len(self.FAN_SPEEDS)
self.set_fan_voltage = None elif fan_target == 4099 or (int(round(fan_target_v)) != int(round(fan_input_v)) and fan_target not in self.FAN_SPEEDS):
return x
elif fan_target == 4099 or (int(round(fan_target_v)) != int(round(fan_input_v))):
# cannot read /sys/class/hwmon/hwmon5/recalculate, so guess based on available fan info # cannot read /sys/class/hwmon/hwmon5/recalculate, so guess based on available fan info
# NOTE: the fan takes time to ramp up, so fan_target will never approximately equal fan_input # NOTE: the fan takes time to ramp up, so fan_target will never approximately equal fan_input
# when fan_target was changed recently (hence set voltage caching) # when fan_target was changed recently (hence set voltage caching)
return len(self.FAN_VOLTAGES) return len(self.FAN_SPEEDS)
else: else:
# quantize voltage to nearest tick (price is right rules; closest without going over) # quantize voltage to nearest tick (price is right rules; closest without going over)
for i in range(len(self.FAN_VOLTAGES)-1): for i in range(len(self.FAN_SPEEDS)-1):
if fan_target <= self.FAN_VOLTAGES[i]: if fan_target <= self.FAN_SPEEDS[i]:
return i return i
return len(self.FAN_VOLTAGES)-1 # any higher value is considered as highest manual setting return len(self.FAN_SPEEDS)-1 # any higher value is considered as highest manual setting
# Battery stuff # Battery stuff