fixes bug #429
[enigma2.git] / lib / python / Components / FanControl.py
1 import os
2
3 from Components.config import config, ConfigSubList, ConfigSubsection, ConfigSlider
4 from Tools.BoundFunction import boundFunction
5
6 class FanControl:
7         # ATM there's only support for one fan
8         def __init__(self):
9                 if os.path.exists("/proc/stb/fp/fan_vlt") or os.path.exists("/proc/stb/fp/fan_pwm") or os.path.exists("/proc/stb/fp/fan_speed"):
10                         self.fancount = 1
11                 else:
12                         self.fancount = 0
13                 self.createConfig()
14                 config.misc.standbyCounter.addNotifier(self.standbyCounterChanged, initial_call = False)
15
16         def leaveStandby(self):
17                 for fanid in range(self.getFanCount()):
18                         cfg = self.getConfig(fanid)
19                         self.setVoltage(fanid, cfg.vlt.value)
20                         self.setPWM(fanid, cfg.pwm.value)
21
22         def standbyCounterChanged(self, configElement):
23                 from Screens.Standby import inStandby
24                 inStandby.onClose.append(self.leaveStandby)
25                 for fanid in range(self.getFanCount()):
26                         self.setVoltage(fanid, 0)
27                         self.setPWM(fanid, 0)
28
29         def createConfig(self):
30                 def setVlt(fancontrol, fanid, configElement):
31                         fancontrol.setVoltage(fanid, configElement.value)
32                 def setPWM(fancontrol, fanid, configElement):
33                         fancontrol.setPWM(fanid, configElement.value)
34                 
35                 config.fans = ConfigSubList()
36                 for fanid in range(self.getFanCount()):
37                         fan = ConfigSubsection()
38                         fan.vlt = ConfigSlider(default = 16, increment = 5, limits = (0, 255))
39                         fan.pwm = ConfigSlider(default = 0, increment = 5, limits = (0, 255))
40                         fan.vlt.addNotifier(boundFunction(setVlt, self, fanid))
41                         fan.pwm.addNotifier(boundFunction(setPWM, self, fanid))
42                         config.fans.append(fan)
43                         
44         def getConfig(self, fanid):
45                 return config.fans[fanid]
46         
47         def getFanCount(self):
48                 return self.fancount
49         
50         def hasRPMSensor(self, fanid):
51                 return os.path.exists("/proc/stb/fp/fan_speed")
52         
53         def hasFanControl(self, fanid):
54                 return os.path.exists("/proc/stb/fp/fan_vlt") or os.path.exists("/proc/stb/fp/fan_pwm")
55         
56         def getFanSpeed(self, fanid):
57                 f = open("/proc/stb/fp/fan_speed", "r")
58                 value = int(f.readline().strip()[:-4])
59                 f.close()
60                 return value
61         
62         def getVoltage(self, fanid):
63                 f = open("/proc/stb/fp/fan_vlt", "r")
64                 value = int(f.readline().strip(), 16)
65                 f.close()
66                 return value
67         
68         def setVoltage(self, fanid, value):
69                 if value > 255:
70                         return
71                 f = open("/proc/stb/fp/fan_vlt", "w")
72                 f.write("%x" % value)
73                 f.close()
74                 
75         def getPWM(self, fanid):
76                 f = open("/proc/stb/fp/fan_pwm", "r")
77                 value = int(f.readline().strip(), 16)
78                 f.close()
79                 return value
80         
81         def setPWM(self, fanid, value):
82                 if value > 255:
83                         return
84                 f = open("/proc/stb/fp/fan_pwm", "w")
85                 f.write("%x" % value)
86                 f.close()
87         
88 fancontrol = FanControl()