-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfan.py
executable file
·58 lines (44 loc) · 1.52 KB
/
fan.py
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
#!/usr/bin/env python3
import sys
from types import ModuleType
from i2c_pkg import i2c as i2c_module
from i2c_pkg.emc2301_pkg import emc2301
class RaspiCM4IOBoardFanSensor(emc2301.EMC2301):
def __init__(self,
address: int = 0x2f,
busnum: int = 10,
i2c: ModuleType = i2c_module,
**kwargs):
super().__init__(address=address,
busnum=busnum,
i2c=i2c,
**kwargs)
def fan_speed(self) -> int:
return self.speed()[0]
def set_fan_speed_percentage(self, percentage: int):
"""Percentage should be 0 - 100"""
converted_value = int(percentage / 100 * 255)
self.write_register(register='FAN_SETTING', value=converted_value)
def print_help():
print(f'Supported commands:')
print(f'{sys.argv[0]} set (speed in percentage)')
print(f'{sys.argv[0]} get')
if __name__ == '__main__':
if len(sys.argv) <= 1:
print_help()
sys.exit(1)
fan = RaspiCM4IOBoardFanSensor()
if sys.argv[1] == 'set':
print(f'Setting fan speed to {sys.argv[2]}%')
try:
fan.set_fan_speed_percentage(int(sys.argv[2]))
except ValueError:
print(f'Incorrect speed percentage value: {sys.argv[2]}')
sys.exit(1)
sys.exit(0)
if sys.argv[1] == 'get':
print(f'Current fan speed: {fan.fan_speed()} RPM')
sys.exit(0)
print(f'Unknown command.')
print_help()
sys.exit(1)