avoid future redundant code by moving functionality for tuner status display to a...
[enigma2.git] / lib / python / Components / TunerInfo.py
1 from GUIComponent import GUIComponent
2
3 from enigma import eLabel, eSlider, iFrontendStatusInformation
4
5 from math import log
6
7 class TunerInfo(GUIComponent):
8         SNR_PERCENTAGE = 0
9         AGC_PERCENTAGE = 1
10         BER_VALUE = 2
11         SNR_BAR = 3
12         AGC_BAR = 4
13         BER_BAR = 5
14         def __init__(self, type, servicefkt):
15                 GUIComponent.__init__(self)
16                 self.instance = None
17                 self.message = None
18                 self.value = None
19                 
20                 self.servicefkt = servicefkt
21                 self.type = type
22                 self.update()
23         
24         def setText(self, text):
25                 self.message = text
26                 if self.instance:
27                         self.instance.setText(self.message)
28         
29         def setValue(self, value):
30                 self.value = value
31                 if self.instance:
32                         self.instance.setValue(self.value)              
33                         
34         def calc(self,val):
35                 if not val:
36                         return 0
37                 if val < 2500:
38                         return (long)(log(val)/log(2))
39                 return val*100/65535
40         
41         def update(self):
42                 service = self.servicefkt()
43                 value = 0
44                 if service is not None:
45                         feinfo = service.frontendStatusInfo()
46                         if feinfo is not None:
47                                 if self.type == self.SNR_PERCENTAGE or self.type == self.SNR_BAR:
48                                         value = feinfo.getFrontendInfo(iFrontendStatusInformation.signalPower) * 100 / 65536
49                                 elif self.type == self.AGC_PERCENTAGE or self.type == self.AGC_BAR:
50                                         value = feinfo.getFrontendInfo(iFrontendStatusInformation.signalQuality) * 100 / 65536
51                                 elif self.type == self.BER_VALUE or self.type == self.BER_BAR:
52                                         value = feinfo.getFrontendInfo(iFrontendStatusInformation.bitErrorRate)
53                 
54                 if self.type == self.SNR_PERCENTAGE or self.type == self.AGC_PERCENTAGE:
55                         self.setText("%d%%" % (value))
56                 elif self.type == self.BER_VALUE:
57                         self.setText("%d" % (value))
58                 elif self.type == self.SNR_BAR or self.type == self.AGC_BAR:
59                         self.setValue(value)
60                 elif self.type == self.BER_BAR:
61                         self.setValue(self.calc(value))
62
63         def createWidget(self, parent):
64                 if self.SNR_PERCENTAGE <= self.type <= self.BER_VALUE:
65                         return eLabel(parent)
66                 elif self.SNR_BAR <= self.type <= self.BER_BAR:
67                         self.g = eSlider(parent)
68                         self.g.setRange(0, 100)
69                         return self.g
70                 
71         def GUIcreate(self, parent):
72                 self.instance = self.createWidget(parent)
73                 if self.message is not None:
74                         self.instance.setText(self.message)
75                 elif self.value is not None:
76                         self.instance.setValue(self.value)      
77
78         def GUIdelete(self):
79                 self.removeWidget(self.instance)
80                 self.instance = None
81         
82         def removeWidget(self, instance):
83                 pass