Merge branch 'bug_293_indicate_unhandled_key' into experimental
[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         SNR_DB = 1
14         AGC_PERCENTAGE = 2
15         BER_VALUE = 3
16         SNR_BAR = 4
17         AGC_BAR = 5
18         BER_BAR = 6
19         LOCK_STATE = 7
20         SYNC_STATE = 8
21
22         def __init__(self, type, servicefkt = None, frontendfkt = None, statusDict = None):
23                 GUIComponent.__init__(self)
24                 self.instance = None
25                 self.message = None
26                 self.value = None
27                 self.servicefkt = servicefkt
28                 self.frontendfkt = frontendfkt
29                 self.statusDict = statusDict
30                 self.type = type
31                 self.update()
32
33         def setText(self, text):
34                 self.message = text
35                 if self.instance:
36                         self.instance.setText(self.message)
37
38         def setValue(self, value):
39                 self.value = value
40                 if self.instance:
41                         self.instance.setValue(self.value)              
42
43         def calc(self,val):
44                 if not val:
45                         return 0
46                 if val < 2500:
47                         return (long)(log(val)/log(2))
48                 return val*100/65535
49
50         def update(self):
51                 if self.type == self.SNR_DB:
52                         value = self.getValue(self.SNR_DB)
53                 elif self.type == self.SNR_PERCENTAGE or self.type == self.SNR_BAR:
54                         value = self.getValue(self.SNR) * 100 / 65536
55                 elif self.type == self.AGC_PERCENTAGE or self.type == self.AGC_BAR:
56                         value = self.getValue(self.AGC) * 100 / 65536
57                 elif self.type == self.BER_VALUE or self.type == self.BER_BAR:
58                         value = self.getValue(self.BER)
59                 elif self.type == self.LOCK_STATE:
60                         value = self.getValue(self.LOCK)
61
62                 if self.type == self.SNR_DB:
63                         if value != 0x12345678:
64                                 self.setText("%3.02f dB" % (value / 100.0))
65                         else:
66                                 self.setText("")
67                 elif self.type == self.SNR_PERCENTAGE or self.type == self.AGC_PERCENTAGE:
68                         self.setText("%d%%" % (value))
69                 elif self.type == self.BER_VALUE:
70                         self.setText("%d" % (value))
71                 elif self.type == self.SNR_BAR or self.type == self.AGC_BAR:
72                         self.setValue(value)
73                 elif self.type == self.BER_BAR:
74                         self.setValue(self.calc(value))
75                 elif self.type == self.LOCK_STATE:
76                         if value == 1:
77                                 self.setText(_("locked"))
78                         else:
79                                 self.setText(_("not locked"))
80
81         def getValue(self, what):
82                 if self.statusDict:
83                         if what == self.SNR_DB:
84                                 return self.statusDict.get("tuner_signal_quality_db", 0x12345678)
85                         elif what == self.SNR:
86                                 return self.statusDict.get("tuner_signal_quality", 0)
87                         elif what == self.AGC:
88                                 return self.statusDict.get("tuner_signal_power", 0)
89                         elif what == self.BER:
90                                 return self.statusDict.get("tuner_bit_error_rate", 0)
91                         elif what == self.LOCK:
92                                 return self.statusDict.get("tuner_locked", 0)
93                 elif self.servicefkt:
94                         service = self.servicefkt()
95                         if service is not None:
96                                 feinfo = service.frontendInfo()
97                                 if feinfo is not None:
98                                         if what == self.SNR_DB:
99                                                 return feinfo.getFrontendInfo(iFrontendInformation.signalQualitydB)
100                                         elif what == self.SNR:
101                                                 return feinfo.getFrontendInfo(iFrontendInformation.signalQuality)
102                                         elif what == self.AGC:
103                                                 return feinfo.getFrontendInfo(iFrontendInformation.signalPower)
104                                         elif what == self.BER:
105                                                 return feinfo.getFrontendInfo(iFrontendInformation.bitErrorRate)
106                                         elif what == self.LOCK:
107                                                 return feinfo.getFrontendInfo(iFrontendInformation.lockState)
108                 elif self.frontendfkt:
109                         frontend = self.frontendfkt()
110                         if frontend:
111                                 if what == self.SNR_DB:
112                                         return frontend.readFrontendData(iFrontendInformation.signalQualitydB)
113                                 elif what == self.SNR:
114                                         return frontend.readFrontendData(iFrontendInformation.signalQuality)
115                                 elif what == self.AGC:
116                                         return frontend.readFrontendData(iFrontendInformation.signalPower)
117                                 elif what == self.BER:
118                                         return frontend.readFrontendData(iFrontendInformation.bitErrorRate)
119                                 elif what == self.LOCK:
120                                         return frontend.readFrontendData(iFrontendInformation.lockState)
121                 return 0
122
123         def createWidget(self, parent):
124                 if self.SNR_PERCENTAGE <= self.type <= self.BER_VALUE or self.type == self.LOCK_STATE:
125                         return eLabel(parent)
126                 elif self.SNR_BAR <= self.type <= self.BER_BAR:
127                         self.g = eSlider(parent)
128                         self.g.setRange(0, 100)
129                         return self.g
130
131         def postWidgetCreate(self, instance):
132                 if self.message is not None:
133                         instance.setText(self.message)
134                 elif self.value is not None:
135                         instance.setValue(self.value)