don't crash on empty configlist lines
[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 import NavigationInstance
7 from enigma import iRecordableService
8
9 class FanControl:
10         # ATM there's only support for one fan
11         def __init__(self):
12                 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"):
13                         self.fancount = 1
14                 else:
15                         self.fancount = 0
16                 self.createConfig()
17                 config.misc.standbyCounter.addNotifier(self.standbyCounterChanged, initial_call = False)
18
19         def setVoltage_PWM(self):
20                 for fanid in range(self.getFanCount()):
21                         cfg = self.getConfig(fanid)
22                         self.setVoltage(fanid, cfg.vlt.value)
23                         self.setPWM(fanid, cfg.pwm.value)
24                         print "[FanControl]: setting fan values: fanid = %d, voltage = %d, pwm = %d" % (fanid, cfg.vlt.value, cfg.pwm.value)
25
26         def setVoltage_PWM_Standby(self):
27                 for fanid in range(self.getFanCount()):
28                         cfg = self.getConfig(fanid)
29                         self.setVoltage(fanid, cfg.vlt_standby.value)
30                         self.setPWM(fanid, cfg.pwm_standby.value)
31                         print "[FanControl]: setting fan values (standby mode): fanid = %d, voltage = %d, pwm = %d" % (fanid, cfg.vlt_standby.value, cfg.pwm_standby.value)
32
33         def getRecordEvent(self, recservice, event):
34                 recordings = len(NavigationInstance.instance.getRecordings())
35                 if event == iRecordableService.evEnd:
36                         if recordings == 0:
37                                 self.setVoltage_PWM_Standby()
38                 elif event == iRecordableService.evStart:
39                         if recordings == 1:
40                                 self.setVoltage_PWM()
41
42         def leaveStandby(self):
43                 NavigationInstance.instance.record_event.remove(self.getRecordEvent)
44                 recordings = NavigationInstance.instance.getRecordings()
45                 if not recordings:
46                         self.setVoltage_PWM()
47
48         def standbyCounterChanged(self, configElement):
49                 from Screens.Standby import inStandby
50                 inStandby.onClose.append(self.leaveStandby)
51                 recordings = NavigationInstance.instance.getRecordings()
52                 NavigationInstance.instance.record_event.append(self.getRecordEvent)
53                 if not recordings:
54                         self.setVoltage_PWM_Standby()
55
56         def createConfig(self):
57                 def setVlt(fancontrol, fanid, configElement):
58                         fancontrol.setVoltage(fanid, configElement.value)
59                 def setPWM(fancontrol, fanid, configElement):
60                         fancontrol.setPWM(fanid, configElement.value)
61                 
62                 config.fans = ConfigSubList()
63                 for fanid in range(self.getFanCount()):
64                         fan = ConfigSubsection()
65                         fan.vlt = ConfigSlider(default = 15, increment = 5, limits = (0, 255))
66                         fan.pwm = ConfigSlider(default = 0, increment = 5, limits = (0, 255))
67                         fan.vlt_standby = ConfigSlider(default = 5, increment = 5, limits = (0, 255))
68                         fan.pwm_standby = ConfigSlider(default = 0, increment = 5, limits = (0, 255))
69                         fan.vlt.addNotifier(boundFunction(setVlt, self, fanid))
70                         fan.pwm.addNotifier(boundFunction(setPWM, self, fanid))
71                         config.fans.append(fan)
72         
73         def getConfig(self, fanid):
74                 return config.fans[fanid]
75         
76         def getFanCount(self):
77                 return self.fancount
78         
79         def hasRPMSensor(self, fanid):
80                 return os.path.exists("/proc/stb/fp/fan_speed")
81         
82         def hasFanControl(self, fanid):
83                 return os.path.exists("/proc/stb/fp/fan_vlt") or os.path.exists("/proc/stb/fp/fan_pwm")
84         
85         def getFanSpeed(self, fanid):
86                 f = open("/proc/stb/fp/fan_speed", "r")
87                 value = int(f.readline().strip()[:-4])
88                 f.close()
89                 return value
90         
91         def getVoltage(self, fanid):
92                 f = open("/proc/stb/fp/fan_vlt", "r")
93                 value = int(f.readline().strip(), 16)
94                 f.close()
95                 return value
96         
97         def setVoltage(self, fanid, value):
98                 if value > 255:
99                         return
100                 f = open("/proc/stb/fp/fan_vlt", "w")
101                 f.write("%x" % value)
102                 f.close()
103                 
104         def getPWM(self, fanid):
105                 f = open("/proc/stb/fp/fan_pwm", "r")
106                 value = int(f.readline().strip(), 16)
107                 f.close()
108                 return value
109         
110         def setPWM(self, fanid, value):
111                 if value > 255:
112                         return
113                 f = open("/proc/stb/fp/fan_pwm", "w")
114                 f.write("%x" % value)
115                 f.close()
116         
117 fancontrol = FanControl()