f15ca4056527f763146bd56641627d88ba1d5cbe
[enigma2.git] / lib / python / Plugins / SystemPlugins / PositionerSetup / plugin.py
1 from enigma import eTimer, eDVBSatelliteEquipmentControl, eDVBResourceManager, eDVBDiseqcCommand, eDVBResourceManagerPtr, iDVBChannelPtr, iDVBFrontendPtr, iDVBFrontend
2 from Screens.Screen import Screen
3 from Plugins.Plugin import PluginDescriptor
4
5 from Components.Label import Label
6 from Components.ConfigList import ConfigList
7 from Components.TunerInfo import TunerInfo
8 from Components.ActionMap import ActionMap
9 from Components.config import config, ConfigSubsection, configElement_nonSave, configNothing, getConfigListEntry, configSelection
10
11 class PositionerSetup(Screen):
12         skin = """
13                 <screen position="100,100" size="560,400" title="Positioner setup..." >
14                         <widget name="list" position="100,0" size="350,120" />
15
16                         <widget name="red" position="0,120" size="140,80" backgroundColor="red" halign="center" valign="center" font="Regular;21" />
17                         <widget name="green" position="140,120" size="140,80" backgroundColor="green" halign="center" valign="center" font="Regular;21" />
18                         <widget name="yellow" position="280,120" size="140,80" backgroundColor="yellow" halign="center" valign="center" font="Regular;21" />
19                         <widget name="blue" position="420,120" size="140,80" backgroundColor="blue" halign="center" valign="center" font="Regular;21" />
20                         
21                         <widget name="snr" text="SNR:" position="0,220" size="60,22" font="Regular;21" />
22                         <widget name="agc" text="AGC:" position="0,245" size="60,22" font="Regular;21" />
23                         <widget name="ber" text="BER:" position="0,270" size="60,22" font="Regular;21" />
24                         <widget name="lock" text="Lock:" position="0,295" size="60,22" font="Regular;21" />
25                         <widget name="snr_percentage" position="220,220" size="60,22" font="Regular;21" />
26                         <widget name="agc_percentage" position="220,245" size="60,22" font="Regular;21" />
27                         <widget name="ber_value" position="220,270" size="60,22" font="Regular;21" />
28                         <widget name="lock_state" position="60,295" size="150,22" font="Regular;21" />
29                         <widget name="snr_bar" position="60,220" size="150,22" />
30                         <widget name="agc_bar" position="60,245" size="150,22" />
31                         <widget name="ber_bar" position="60,270" size="150,22" />
32                 </screen>"""
33         def __init__(self, session):
34                 self.skin = PositionerSetup.skin
35                 Screen.__init__(self, session)
36                 
37                 self.session.nav.stopService()
38                 
39                 self.diseqc = Diseqc()
40                 
41                 #self.session.nav.stopService()
42                 
43                 self.createConfig()
44                 
45                 self.isMoving = False
46                 
47                 self.red = Label("")
48                 self["red"] = self.red
49                 self.green = Label("")
50                 self["green"] = self.green
51                 self.yellow = Label("")
52                 self["yellow"] = self.yellow
53                 self.blue = Label("")
54                 self["blue"] = self.blue
55                 
56                 self.feid = 0
57
58                 self.list = []
59                 self["list"] = ConfigList(self.list)
60                 self.createSetup()
61                 
62                 self["snr"] = Label()
63                 self["agc"] = Label()
64                 self["ber"] = Label()
65                 self["lock"] = Label()
66                 self["snr_percentage"] = TunerInfo(TunerInfo.SNR_PERCENTAGE, self.session.nav.getCurrentService)
67                 self["agc_percentage"] = TunerInfo(TunerInfo.AGC_PERCENTAGE, self.session.nav.getCurrentService)
68                 self["ber_value"] = TunerInfo(TunerInfo.BER_VALUE, self.session.nav.getCurrentService)
69                 self["snr_bar"] = TunerInfo(TunerInfo.SNR_BAR, self.session.nav.getCurrentService)
70                 self["agc_bar"] = TunerInfo(TunerInfo.AGC_BAR, self.session.nav.getCurrentService)
71                 self["ber_bar"] = TunerInfo(TunerInfo.BER_BAR, self.session.nav.getCurrentService)
72                 self["lock_state"] = TunerInfo(TunerInfo.LOCK_STATE, self.session.nav.getCurrentService)
73                 
74                 self["actions"] = ActionMap(["DirectionActions", "OkCancelActions", "ColorActions"],
75                 {
76                         "ok": self.go,
77                         "cancel": self.close,
78                         "up": self.up,
79                         "down": self.down,
80                         "left": self.left,
81                         "right": self.right,
82                         "red": self.redKey,
83                         "green": self.greenKey,
84                         "yellow": self.yellowKey,
85                         "blue": self.blueKey,
86                 }, -1)
87                 
88                 self.updateColors("tune")
89                 
90                 self.statusTimer = eTimer()
91                 self.statusTimer.timeout.get().append(self.updateStatus)
92                 self.statusTimer.start(200, False)
93                 
94         def createConfig(self):
95                 config.positioner = ConfigSubsection()
96                 config.positioner.tune = configElement_nonSave("tune", configNothing, 0, None)
97                 config.positioner.move = configElement_nonSave("move", configNothing, 0, None)
98                 config.positioner.limits = configElement_nonSave("limits", configNothing, 0, None)
99                 storepos = []
100                 for x in range(255):
101                         storepos.append(str(x))
102                 config.positioner.storage = configElement_nonSave("storage", configSelection, 0, storepos)
103         
104         def createSetup(self):
105                 self.list.append(getConfigListEntry(_("Tune"), config.positioner.tune))
106                 self.list.append(getConfigListEntry(_("Positioner movement"), config.positioner.move))
107                 self.list.append(getConfigListEntry(_("Set limits"), config.positioner.limits))
108                 self.list.append(getConfigListEntry(_("Positioner storage"), config.positioner.storage))
109                 
110                 self["list"].l.setList(self.list)
111                 
112         def go(self):
113                 pass
114         
115         def getCurrentConfigPath(self):
116                 return self["list"].getCurrent()[1].parent.configPath
117         
118         def up(self):
119                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
120                 self.updateColors(self.getCurrentConfigPath())
121         
122         def down(self):
123                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
124                 self.updateColors(self.getCurrentConfigPath())
125         
126         def left(self):
127                 self["list"].handleKey(config.key["prevElement"])
128         
129         def right(self):
130                 self["list"].handleKey(config.key["nextElement"])
131         
132         def updateColors(self, entry):
133                 if entry == "tune":
134                         self.red.setText("tune manually")
135                         self.green.setText("predefined transponder")
136                         self.yellow.setText("")
137                         self.blue.setText("")
138                 elif entry == "move":
139                         if self.isMoving:
140                                 self.red.setText(_("Stop"))
141                                 self.green.setText(_("Stop"))
142                                 self.yellow.setText(_("Stop"))
143                                 self.blue.setText(_("Stop"))
144                         else:
145                                 self.red.setText(_("Move west"))
146                                 self.green.setText(_("Step west"))
147                                 self.yellow.setText(_("Step east"))
148                                 self.blue.setText(_("Move east"))
149                 elif entry == "limits":
150                         self.red.setText(_("Limits off"))
151                         self.green.setText(_("Limit west"))
152                         self.yellow.setText(_("Limit east"))
153                         self.blue.setText("")
154                 elif entry == "storage":
155                         self.red.setText()
156                         self.green.setText(_("Store position"))
157                         self.yellow.setText(_("Goto position"))
158                         self.blue.setText("")
159                 else:
160                         self.red.setText("")
161                         self.green.setText("")
162                         self.yellow.setText("")
163                         self.blue.setText("")
164         
165         def redKey(self):
166                 entry = self.getCurrentConfigPath()
167                 if entry == "move":
168                         if self.isMoving:
169                                 self.diseqc.command("stop")
170                                 self.isMoving = False
171                         else:
172                                 self.diseqc.command("moveWest", 0)
173                                 self.isMoving = True
174                         self.updateColors("move")
175                         print "moving west"
176                 elif entry == "limits":
177                         self.diseqc.command("limitOff")
178                                 
179         def greenKey(self):
180                 entry = self.getCurrentConfigPath()
181                 if entry == "move":
182                         print "stepping west"
183                         self.diseqc.command("moveWest", 1)
184                 elif entry == "storage":
185                         print "store at position", (config.positioner.storage.value + 1)
186                         self.diseqc.command("store", config.positioner.storage.value + 1)
187                 elif entry == "limits":
188                         self.diseqc.command("limitWest")
189         
190         def yellowKey(self):
191                 entry = self.getCurrentConfigPath()
192                 if entry == "move":
193                         print "stepping east"
194                         self.diseqc.command("moveEast", 1)
195                 elif entry == "storage":
196                         print "move to position", (config.positioner.storage.value + 1)
197                         self.diseqc.command("moveTo", config.positioner.storage.value + 1)
198                 elif entry == "limits":
199                         self.diseqc.command("limitEast")
200 #       
201         def blueKey(self):
202                 entry = self.getCurrentConfigPath()
203                 if entry == "move":
204                         if self.isMoving:
205                                 self.diseqc.command("stop")
206                                 self.isMoving = False
207                         else:
208                                 self.diseqc.command("moveEast", 0)
209                                 self.isMoving = True
210                         self.updateColors("move")
211                         print "moving east"
212
213         def updateStatus(self):
214                 self["snr_percentage"].update()
215                 self["agc_percentage"].update()
216                 self["ber_value"].update()
217                 self["snr_bar"].update()
218                 self["agc_bar"].update()
219                 self["ber_bar"].update()
220
221 class Diseqc:
222         def __init__(self, feid = 0):
223                 self.ready = False
224                 self.feid = feid
225                 res_mgr = eDVBResourceManagerPtr()
226                 if eDVBResourceManager.getInstance(res_mgr) == 0:
227                         raw_channel = iDVBChannelPtr()
228                         if res_mgr.allocateRawChannel(raw_channel, self.feid) == 0:
229                                 self.frontend = iDVBFrontendPtr()
230                                 if raw_channel.getFrontend(self.frontend) == 0:
231                                         self.frontend.setVoltage(iDVBFrontend.voltage18)
232                                         self.ready = True
233                                 else:
234                                         print "getFrontend failed"
235                         else:
236                                 print "getRawChannel failed"
237                 else:
238                                 print "getResourceManager instance failed"
239                 
240         def command(self, what, param = 0):
241                 if self.ready:
242                         cmd = eDVBDiseqcCommand()
243                         if what == "moveWest":
244                                 string = '\xe1\x31\x69' + chr(param)
245                         elif what == "moveEast":
246                                 string = '\xe1\x31\x68' + chr(param)
247                         elif what == "moveTo":
248                                 string = '\xe1\x31\x6b' + chr(param)
249                         elif what == "store":
250                                 string = '\xe1\x31\x6a' + chr(param)
251                         elif what == "limitOff":
252                                 string = '\xe1\x31\x63'
253                         elif what == "limitEast":
254                                 string = '\xe1\x31\x66'
255                         elif what == "limitWest":
256                                 string = '\xe1\x31\x67'
257                         else:
258                                 string = '\xe0\x31\x60' #positioner stop
259                         print "diseqc command:",
260                         for x in string:
261                                 print hex(ord(x)),
262                         print
263                         
264                         cmd.setCommandString(string)
265                         self.frontend.sendDiseqc(cmd)
266                                         
267 def PositionerMain(session, **kwargs):
268         session.open(PositionerSetup)
269
270 def Plugins(**kwargs):
271         return PluginDescriptor(name="Positioner setup", description="Setup your positioner", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=PositionerMain)