needed update for avahi stuff
[enigma2.git] / lib / python / Components / TunerInfo.py
1 from GUIComponent import GUIComponent
2
3 from enigma import eLabel, eSlider, iFrontendInformation
4
5 from math import log
6
7 class TunerInfo(GUIComponent):
8         SNR = 0
9         AGC = 1
10         BER = 2
11         LOCK = 3
12         SNR_PERCENTAGE = 0
13         AGC_PERCENTAGE = 1
14         BER_VALUE = 2
15         SNR_BAR = 3
16         AGC_BAR = 4
17         BER_BAR = 5
18         LOCK_STATE = 6
19         SYNC_STATE = 7
20
21         def __init__(self, type, servicefkt = None, frontendfkt = None, statusDict = None):
22                 GUIComponent.__init__(self)
23                 self.instance = None
24                 self.message = None
25                 self.value = None
26                 self.servicefkt = servicefkt
27                 self.frontendfkt = frontendfkt
28                 self.statusDict = statusDict
29                 self.type = type
30                 self.update()
31
32         def setText(self, text):
33                 self.message = text
34                 if self.instance:
35                         self.instance.setText(self.message)
36
37         def setValue(self, value):
38                 self.value = value
39                 if self.instance:
40                         self.instance.setValue(self.value)              
41
42         def calc(self,val):
43                 if not val:
44                         return 0
45                 if val < 2500:
46                         return (long)(log(val)/log(2))
47                 return val*100/65535
48
49         def update(self):
50                 if self.type == self.SNR_PERCENTAGE or self.type == self.SNR_BAR:
51                         value = self.getValue(self.SNR) * 100 / 65536
52                 elif self.type == self.AGC_PERCENTAGE or self.type == self.AGC_BAR:
53                         value = self.getValue(self.AGC) * 100 / 65536
54                 elif self.type == self.BER_VALUE or self.type == self.BER_BAR:
55                         value = self.getValue(self.BER)
56                 elif self.type == self.LOCK_STATE:
57                         value = self.getValue(self.LOCK)
58                 
59                 if self.type == self.SNR_PERCENTAGE or self.type == self.AGC_PERCENTAGE:
60                         self.setText("%d%%" % (value))
61                 elif self.type == self.BER_VALUE:
62                         self.setText("%d" % (value))
63                 elif self.type == self.SNR_BAR or self.type == self.AGC_BAR:
64                         self.setValue(value)
65                 elif self.type == self.BER_BAR:
66                         self.setValue(self.calc(value))
67                 elif self.type == self.LOCK_STATE:
68                         if value == 1:
69                                 self.setText(_("locked"))
70                         else:
71                                 self.setText(_("not locked"))
72
73         def getValue(self, what):
74                 if self.statusDict:
75                         if what == self.SNR:
76                                 return self.statusDict.get("tuner_signal_quality", 0)
77                         elif what == self.AGC:
78                                 return self.statusDict.get("tuner_signal_power", 0)
79                         elif what == self.BER:
80                                 return self.statusDict.get("tuner_bit_error_rate", 0)
81                         elif what == self.LOCK:
82                                 return self.statusDict.get("tuner_locked", 0)
83                 elif self.servicefkt:
84                         service = self.servicefkt()
85                         if service is not None:
86                                 feinfo = service.frontendInfo()
87                                 if feinfo is not None:
88                                         if what == self.SNR:
89                                                 return feinfo.getFrontendInfo(iFrontendInformation.signalQuality)
90                                         elif what == self.AGC:
91                                                 return feinfo.getFrontendInfo(iFrontendInformation.signalPower)
92                                         elif what == self.BER:
93                                                 return feinfo.getFrontendInfo(iFrontendInformation.bitErrorRate)
94                                         elif what == self.LOCK:
95                                                 return feinfo.getFrontendInfo(iFrontendInformation.lockState)
96                 elif self.frontendfkt:
97                         frontend = self.frontendfkt()
98                         if frontend:
99                                 if what == self.SNR:
100                                         return frontend.readFrontendData(iFrontendInformation.signalQuality)
101                                 elif what == self.AGC:
102                                         return frontend.readFrontendData(iFrontendInformation.signalPower)
103                                 elif what == self.BER:
104                                         return frontend.readFrontendData(iFrontendInformation.bitErrorRate)
105                                 elif what == self.LOCK:
106                                         return frontend.readFrontendData(iFrontendInformation.lockState)
107                 return 0
108
109         def createWidget(self, parent):
110                 if self.SNR_PERCENTAGE <= self.type <= self.BER_VALUE or self.type == self.LOCK_STATE:
111                         return eLabel(parent)
112                 elif self.SNR_BAR <= self.type <= self.BER_BAR:
113                         self.g = eSlider(parent)
114                         self.g.setRange(0, 100)
115                         return self.g
116
117         def postWidgetCreate(self, instance):
118                 if self.message is not None:
119                         instance.setText(self.message)
120                 elif self.value is not None:
121                         instance.setValue(self.value)