honor horizontal alignment also when a mark is present
[enigma2.git] / lib / python / Plugins / SystemPlugins / PositionerSetup / plugin.py
1 from enigma import eTimer, eDVBSatelliteEquipmentControl, eDVBResourceManager, eDVBDiseqcCommand, eDVBResourceManagerPtr, iDVBChannelPtr, iDVBFrontendPtr, iDVBFrontend, eDVBFrontendParametersSatellite, eDVBFrontendParameters
2 from Screens.Screen import Screen
3 from Screens.ScanSetup import ScanSetup
4 from Screens.MessageBox import MessageBox
5 from Plugins.Plugin import PluginDescriptor
6
7 from Components.Label import Label
8 from Components.ConfigList import ConfigList
9 from Components.TunerInfo import TunerInfo
10 from Components.ActionMap import ActionMap
11 from Components.NimManager import nimmanager
12 from Components.MenuList import MenuList
13 from Components.config import config, ConfigSubsection, configElement_nonSave, configNothing, getConfigListEntry, configSelection, currentConfigSelectionElement, configSatlist
14
15 class PositionerSetup(Screen):
16         skin = """
17                 <screen position="100,100" size="560,400" title="Positioner setup..." >
18                         <widget name="list" position="100,0" size="350,155" />
19
20                         <widget name="red" position="0,155" size="140,80" backgroundColor="red" halign="center" valign="center" font="Regular;21" />
21                         <widget name="green" position="140,155" size="140,80" backgroundColor="green" halign="center" valign="center" font="Regular;21" />
22                         <widget name="yellow" position="280,155" size="140,80" backgroundColor="yellow" halign="center" valign="center" font="Regular;21" />
23                         <widget name="blue" position="420,155" size="140,80" backgroundColor="blue" halign="center" valign="center" font="Regular;21" />
24                         
25                         <widget name="snr" text="SNR:" position="0,245" size="60,22" font="Regular;21" />
26                         <widget name="agc" text="AGC:" position="0,270" size="60,22" font="Regular;21" />
27                         <widget name="ber" text="BER:" position="0,295" size="60,22" font="Regular;21" />
28                         <widget name="lock" text="Lock:" position="0,320" size="60,22" font="Regular;21" />
29                         <widget name="snr_percentage" position="220,245" size="60,22" font="Regular;21" />
30                         <widget name="agc_percentage" position="220,270" size="60,22" font="Regular;21" />
31                         <widget name="ber_value" position="220,295" size="60,22" font="Regular;21" />
32                         <widget name="lock_state" position="60,320" size="150,22" font="Regular;21" />
33                         <widget name="snr_bar" position="60,245" size="150,22" />
34                         <widget name="agc_bar" position="60,270" size="150,22" />
35                         <widget name="ber_bar" position="60,295" size="150,22" />
36
37                         <widget name="frequency" text="Frequency:" position="300,245" size="120,22" font="Regular;21" />
38                         <widget name="symbolrate" text="Symbolrate:" position="300,270" size="120,22" font="Regular;21" />
39                         <widget name="fec" text="FEC:" position="300,295" size="120,22" font="Regular;21" />
40                         <widget name="frequency_value" position="420,245" size="120,22" font="Regular;21" />
41                         <widget name="symbolrate_value" position="420,270" size="120,22" font="Regular;21" />
42                         <widget name="fec_value" position="420,295" size="120,22" font="Regular;21" />
43                 </screen>"""
44         def __init__(self, session, feid):
45                 self.skin = PositionerSetup.skin
46                 Screen.__init__(self, session)
47                 
48                 self.session.nav.stopService()
49                 
50                 self.feid = feid
51                 
52                 self.diseqc = Diseqc(self.feid)
53                 self.tuner = Tuner(self.diseqc.getFrontend())
54                 self.tuner.tune((0,0,0,0,0,0,0,0,0))
55                 
56                 #self.session.nav.stopService()
57                 
58                 self.createConfig()
59                 
60                 self.isMoving = False
61                 self.stopOnLock = False
62                 
63                 self.red = Label("")
64                 self["red"] = self.red
65                 self.green = Label("")
66                 self["green"] = self.green
67                 self.yellow = Label("")
68                 self["yellow"] = self.yellow
69                 self.blue = Label("")
70                 self["blue"] = self.blue
71                 
72                 self.list = []
73                 self["list"] = ConfigList(self.list)
74                 self.createSetup()
75                 
76                 self["snr"] = Label()
77                 self["agc"] = Label()
78                 self["ber"] = Label()
79                 self["lock"] = Label()
80                 self["snr_percentage"] = TunerInfo(TunerInfo.SNR_PERCENTAGE, frontendfkt = self.diseqc.getFrontend)
81                 self["agc_percentage"] = TunerInfo(TunerInfo.AGC_PERCENTAGE, frontendfkt = self.diseqc.getFrontend)
82                 self["ber_value"] = TunerInfo(TunerInfo.BER_VALUE, frontendfkt = self.diseqc.getFrontend)
83                 self["snr_bar"] = TunerInfo(TunerInfo.SNR_BAR, frontendfkt = self.diseqc.getFrontend)
84                 self["agc_bar"] = TunerInfo(TunerInfo.AGC_BAR, frontendfkt = self.diseqc.getFrontend)
85                 self["ber_bar"] = TunerInfo(TunerInfo.BER_BAR, frontendfkt = self.diseqc.getFrontend)
86                 self["lock_state"] = TunerInfo(TunerInfo.LOCK_STATE, frontendfkt = self.diseqc.getFrontend)
87
88                 self["frequency"] = Label()
89                 self["symbolrate"] = Label()
90                 self["fec"] = Label()
91
92                 self["frequency_value"] = Label("")
93                 self["symbolrate_value"] = Label("")
94                 self["fec_value"] = Label("")
95                 
96                 self["actions"] = ActionMap(["DirectionActions", "OkCancelActions", "ColorActions"],
97                 {
98                         "ok": self.go,
99                         "cancel": self.close,
100                         "up": self.up,
101                         "down": self.down,
102                         "left": self.left,
103                         "right": self.right,
104                         "red": self.redKey,
105                         "green": self.greenKey,
106                         "yellow": self.yellowKey,
107                         "blue": self.blueKey,
108                 }, -1)
109                 
110                 self.updateColors("tune")
111                 
112                 self.statusTimer = eTimer()
113                 self.statusTimer.timeout.get().append(self.updateStatus)
114                 self.statusTimer.start(50, False)
115                 
116         def createConfig(self):
117                 config.positioner = ConfigSubsection()
118                 config.positioner.tune = configElement_nonSave("tune", configNothing, 0, None)
119                 config.positioner.move = configElement_nonSave("move", configNothing, 0, None)
120                 config.positioner.finemove = configElement_nonSave("finemove", configNothing, 0, None)
121                 config.positioner.limits = configElement_nonSave("limits", configNothing, 0, None)
122                 config.positioner.goto0 = configElement_nonSave("goto0", configNothing, 0, None)
123                 storepos = []
124                 for x in range(1,255):
125                         storepos.append(str(x))
126                 config.positioner.storage = configElement_nonSave("storage", configSelection, 0, storepos)
127         
128         def createSetup(self):
129                 self.list.append(getConfigListEntry(_("Tune"), config.positioner.tune))
130                 self.list.append(getConfigListEntry(_("Positioner movement"), config.positioner.move))
131                 self.list.append(getConfigListEntry(_("Positioner fine movement"), config.positioner.finemove))
132                 self.list.append(getConfigListEntry(_("Set limits"), config.positioner.limits))
133                 self.list.append(getConfigListEntry(_("Positioner storage"), config.positioner.storage))
134                 self.list.append(getConfigListEntry(_("Goto 0"), config.positioner.goto0))
135                 self["list"].l.setList(self.list)
136                 
137         def go(self):
138                 pass
139         
140         def getCurrentConfigPath(self):
141                 return self["list"].getCurrent()[1].parent.configPath
142         
143         def up(self):
144                 if not self.isMoving:
145                         self["list"].instance.moveSelection(self["list"].instance.moveUp)
146                         self.updateColors(self.getCurrentConfigPath())
147         
148         def down(self):
149                 if not self.isMoving:
150                         self["list"].instance.moveSelection(self["list"].instance.moveDown)
151                         self.updateColors(self.getCurrentConfigPath())
152         
153         def left(self):
154                 self["list"].handleKey(config.key["prevElement"])
155         
156         def right(self):
157                 self["list"].handleKey(config.key["nextElement"])
158         
159         def updateColors(self, entry):
160                 if entry == "tune":
161                         self.red.setText(_("Tune"))
162                         self.green.setText("")
163                         self.yellow.setText("")
164                         self.blue.setText("")
165                 elif entry == "move":
166                         if self.isMoving:
167                                 self.red.setText(_("Stop"))
168                                 self.green.setText(_("Stop"))
169                                 self.yellow.setText(_("Stop"))
170                                 self.blue.setText(_("Stop"))
171                         else:
172                                 self.red.setText(_("Move west"))
173                                 self.green.setText(_("Search west"))
174                                 self.yellow.setText(_("Search east"))
175                                 self.blue.setText(_("Move east"))
176                 elif entry == "finemove":
177                         self.red.setText("")
178                         self.green.setText(_("Step west"))
179                         self.yellow.setText(_("Step east"))
180                         self.blue.setText("")
181                 elif entry == "limits":
182                         self.red.setText(_("Limits off"))
183                         self.green.setText(_("Limit west"))
184                         self.yellow.setText(_("Limit east"))
185                         self.blue.setText(_("Limits on"))
186                 elif entry == "storage":
187                         self.red.setText("")
188                         self.green.setText(_("Store position"))
189                         self.yellow.setText(_("Goto position"))
190                         self.blue.setText("")
191                 elif entry == "goto0":
192                         self.red.setText(_("Goto 0"))
193                         self.green.setText("")
194                         self.yellow.setText("")
195                         self.blue.setText("")
196                 else:
197                         self.red.setText("")
198                         self.green.setText("")
199                         self.yellow.setText("")
200                         self.blue.setText("")
201         
202         def redKey(self):
203                 entry = self.getCurrentConfigPath()
204                 if entry == "move":
205                         if self.isMoving:
206                                 self.diseqccommand("stop")
207                                 self.isMoving = False
208                                 self.stopOnLock = False
209                         else:
210                                 self.diseqccommand("moveWest", 0)
211                                 self.isMoving = True
212                         self.updateColors("move")
213                 elif entry == "limits":
214                         self.diseqccommand("limitOff")
215                 elif entry == "tune":
216                         self.session.openWithCallback(self.tune, TunerScreen, self.feid)
217                 elif entry == "goto0":
218                         print "move to position 0"
219                         self.diseqccommand("moveTo", 0)
220
221         def greenKey(self):
222                 entry = self.getCurrentConfigPath()
223                 if entry == "move":
224                         if self.isMoving:
225                                 self.diseqccommand("stop")
226                                 self.isMoving = False
227                                 self.stopOnLock = False
228                         else:
229                                 self.isMoving = True
230                                 self.stopOnLock = True
231                                 self.diseqccommand("moveWest", 0)
232                         self.updateColors("move")
233                 elif entry == "finemove":
234                         print "stepping west"
235                         self.diseqccommand("moveWest", 0xFF) # one step
236                 elif entry == "storage":
237                         print "store at position", (config.positioner.storage.value + 1)
238                         self.diseqccommand("store", config.positioner.storage.value + 1)
239                 elif entry == "limits":
240                         self.diseqccommand("limitWest")
241         
242         def yellowKey(self):
243                 entry = self.getCurrentConfigPath()
244                 if entry == "move":
245                         if self.isMoving:
246                                 self.diseqccommand("stop")
247                                 self.isMoving = False
248                                 self.stopOnLock = False
249                         else:
250                                 self.isMoving = True
251                                 self.stopOnLock = True
252                                 self.diseqccommand("moveEast", 0)
253                         self.updateColors("move")
254                 elif entry == "finemove":
255                         print "stepping east"
256                         self.diseqccommand("moveEast", 0xFF) # one step
257                 elif entry == "storage":
258                         print "move to position", (config.positioner.storage.value + 1)
259                         self.diseqccommand("moveTo", config.positioner.storage.value + 1)
260                 elif entry == "limits":
261                         self.diseqccommand("limitEast")
262 #       
263         def blueKey(self):
264                 entry = self.getCurrentConfigPath()
265                 if entry == "move":
266                         if self.isMoving:
267                                 self.diseqccommand("stop")
268                                 self.isMoving = False
269                                 self.stopOnLock = False
270                         else:
271                                 self.diseqccommand("moveEast", 0)
272                                 self.isMoving = True
273                         self.updateColors("move")
274                         print "moving east"
275                 elif entry == "limits":
276                         self.diseqccommand("limitOn")
277
278         def diseqccommand(self, cmd, param = 0):
279                 self.diseqc.command(cmd, param)
280                 self.tuner.retune()
281
282         def updateStatus(self):
283                 self["snr_percentage"].update()
284                 self["agc_percentage"].update()
285                 self["ber_value"].update()
286                 self["snr_bar"].update()
287                 self["agc_bar"].update()
288                 self["ber_bar"].update()
289                 self["lock_state"].update()
290                 transponderdata = self.tuner.getTransponderData()
291                 self["frequency_value"].setText(str(transponderdata["frequency"]))
292                 self["symbolrate_value"].setText(str(transponderdata["symbol_rate"]))
293                 self["fec_value"].setText(str(transponderdata["fec_inner"]))
294                 if transponderdata["tuner_locked"] == 1 and self.isMoving and self.stopOnLock:
295                         self.diseqccommand("stop")
296                         self.isMoving = False
297                         self.stopOnLock = False
298                         self.updateColors(self.getCurrentConfigPath())
299
300         def tune(self, transponder):
301                 if transponder is not None:
302                         self.tuner.tune(transponder)
303                         
304 class Diseqc:
305         def __init__(self, feid = 0):
306                 self.ready = False
307                 self.feid = feid
308                 res_mgr = eDVBResourceManagerPtr()
309                 if eDVBResourceManager.getInstance(res_mgr) == 0:
310                         self.raw_channel = iDVBChannelPtr()
311                         if res_mgr.allocateRawChannel(self.raw_channel, self.feid) == 0:
312                                 self.frontend = iDVBFrontendPtr()
313                                 if self.raw_channel.getFrontend(self.frontend) == 0:
314                                         self.ready = True
315                                 else:
316                                         print "getFrontend failed"
317                         else:
318                                 print "getRawChannel failed"
319                 else:
320                         print "getResourceManager instance failed"
321         
322         def getFrontend(self):
323                 return self.frontend
324                 
325         def command(self, what, param = 0):
326                 if self.ready:
327                         cmd = eDVBDiseqcCommand()
328                         if what == "moveWest":
329                                 string = 'e03169' + ("%02x" % param)
330                         elif what == "moveEast":
331                                 string = 'e03168' + ("%02x" % param)
332                         elif what == "moveTo":
333                                 string = 'e0316b' + ("%02x" % param)
334                         elif what == "store":
335                                 string = 'e0316a' + ("%02x" % param)
336                         elif what == "limitOn":
337                                 string = 'e0316a00'
338                         elif what == "limitOff":
339                                 string = 'e03163'
340                         elif what == "limitEast":
341                                 string = 'e03166'
342                         elif what == "limitWest":
343                                 string = 'e03167'
344                         else:
345                                 string = 'e03160' #positioner stop
346                         print "diseqc command:",
347                         print string
348                         
349                         cmd.setCommandString(string)
350                         self.frontend.sendDiseqc(cmd)
351                         
352 class Tuner:
353         def __init__(self, frontend):
354                 self.frontend = frontend
355                 
356         def tune(self, transponder):
357                 print "tuning to transponder with data", transponder
358                 parm = eDVBFrontendParametersSatellite()
359                 parm.frequency = transponder[0] * 1000
360                 parm.symbol_rate = transponder[1] * 1000
361                 parm.polarisation = transponder[2]
362                 parm.fec = transponder[3]
363                 parm.inversion = transponder[4]
364                 parm.orbital_position = transponder[5]
365                 parm.system = 0  # FIXMEE !! HARDCODED DVB-S (add support for DVB-S2)
366                 parm.modulation = 1 # FIXMEE !! HARDCODED QPSK 
367                 feparm = eDVBFrontendParameters()
368                 feparm.setDVBS(parm, True)
369                 self.lastparm = feparm
370                 self.frontend.tune(feparm)
371         
372         def retune(self):
373                 self.frontend.tune(self.lastparm)
374         
375         def getTransponderData(self):
376                 return self.frontend.readTransponderData(True)
377
378 class TunerScreen(ScanSetup):
379         skin = """
380                 <screen position="90,100" size="520,400" title="Tune">
381                         <widget name="config" position="20,10" size="460,350" scrollbarMode="showOnDemand" />
382                         <widget name="introduction" position="20,360" size="350,30" font="Regular;23" />
383                 </screen>"""
384
385         def __init__(self, session, feid):
386                 self.feid = feid
387                 ScanSetup.__init__(self, session)
388
389                 self["introduction"].setText("")
390                 
391         def createSetup(self):
392                 self.typeOfTuningEntry = None
393                 self.satEntry = None
394
395                 self.list = []
396                 self.typeOfTuningEntry = getConfigListEntry(_('Tune'), config.tuning.type)
397                 self.list.append(self.typeOfTuningEntry)
398                 self.satEntry = getConfigListEntry(_('Satellite'), config.tuning.sat)
399                 self.list.append(self.satEntry)
400                 if currentConfigSelectionElement(config.tuning.type) == "manual_transponder":
401                         self.list.append(getConfigListEntry(_('Frequency'), config.scan.sat.frequency))
402                         self.list.append(getConfigListEntry(_('Inversion'), config.scan.sat.inversion))
403                         self.list.append(getConfigListEntry(_('Symbol Rate'), config.scan.sat.symbolrate))
404                         self.list.append(getConfigListEntry(_("Polarity"), config.scan.sat.polarization))
405                         self.list.append(getConfigListEntry(_("FEC"), config.scan.sat.fec))
406                 elif currentConfigSelectionElement(config.tuning.type) == "predefined_transponder":
407                         self.list.append(getConfigListEntry(_("Transponder"), config.tuning.transponder))
408                 self["config"].list = self.list
409                 self["config"].l.setList(self.list)
410
411         def newConfig(self):
412                 if self["config"].getCurrent() == self.typeOfTuningEntry:
413                         self.createSetup()
414                 elif self["config"].getCurrent() == self.satEntry:
415                         self.updateSats()
416                         self.createSetup()
417
418         def createConfig(self, foo):
419                 config.tuning = ConfigSubsection()
420                 
421                 config.tuning.type = configElement_nonSave("config.tuning.type", configSelection, 0, (("manual_transponder", _("Manual transponder")), ("predefined_transponder", _("Predefined satellite"))))
422                 
423                 config.tuning.sat = configElement_nonSave("config.tuning.sat", configSatlist, 192, nimmanager.getRotorSatListForNim(self.feid))
424                 ScanSetup.createConfig(self, None)
425                 self.updateSats()
426                 
427         def updateSats(self):
428                 satnum = config.tuning.sat.value
429                 satlist = config.tuning.sat.vals
430                 if len(satlist):
431                         transponderlist = nimmanager.getTransponders(satlist[satnum][1])
432                         list = []
433                         for x in transponderlist:
434                                 if x[3] == 0:
435                                         pol = "H"
436                                 elif x[3] == 1:
437                                         pol = "V"
438                                 elif x[3] == 2:
439                                         pol = "CL"
440                                 elif x[3] == 3:
441                                         pol = "CR"
442                                 if x[4] == 0:
443                                         fec = "FEC_AUTO"
444                                 elif x[4] == 1:
445                                         fec = "FEC_1_2"
446                                 elif x[4] == 2:
447                                         fec = "FEC_2_3"
448                                 elif x[4] == 3:
449                                         fec = "FEC_3_4"
450                                 elif x[4] == 4:
451                                         fec = "FEC_5_6"
452                                 elif x[4] == 5:
453                                         fec = "FEC_7_8"
454                                 elif x[4] == 5:
455                                         fec = "FEC_8_9"
456                                 elif x[4] == 6:
457                                         fec = "FEC_None"
458                                 list.append(str(x[1]) + "," + str(x[2]) + "," + pol + "," + fec)
459                         config.tuning.transponder = configElement_nonSave("config.tuning.transponder", configSelection, 0, list)
460         
461         def keyGo(self):
462                 returnvalue = (0, 0, 0, 0, 0, 0, 0)
463                 satpos = config.tuning.sat.vals[config.tuning.sat.value][1]
464                 if currentConfigSelectionElement(config.tuning.type) == "manual_transponder":
465                         returnvalue = (config.scan.sat.frequency.value[0], config.scan.sat.symbolrate.value[0], config.scan.sat.polarization.value, config.scan.sat.fec.value, config.scan.sat.inversion.value, satpos)
466                 elif currentConfigSelectionElement(config.tuning.type) == "predefined_transponder":
467                         transponder = nimmanager.getTransponders(config.tuning.sat.vals[config.tuning.sat.value][1])[config.tuning.transponder.value]
468                         returnvalue = (int(transponder[1] / 1000), int(transponder[2] / 1000), transponder[3], transponder[4], 2, config.tuning.sat.vals[config.tuning.sat.value][1], satpos)
469                 self.close(returnvalue)
470
471         def keyCancel(self):
472                 self.close(None)
473
474 class NimSelection(Screen):
475         skin = """
476                 <screen position="140,165" size="400,100" title="select Slot">
477                         <widget name="nimlist" position="20,10" size="360,75" />
478                 </screen>"""
479                 
480         def __init__(self, session):
481                 Screen.__init__(self, session)
482
483                 nimlist = nimmanager.getNimListOfType(nimmanager.nimType["DVB-S"])
484                 nimMenuList = []
485                 for x in nimlist:
486                         nimMenuList.append((_("NIM ") + (["A", "B", "C", "D"][x]) + ": " + nimmanager.getNimName(x) + " (" + nimmanager.getNimTypeName(x) + ")", x))
487                 
488                 self["nimlist"] = MenuList(nimMenuList)
489
490                 self["actions"] = ActionMap(["OkCancelActions"],
491                 {
492                         "ok": self.okbuttonClick ,
493                         "cancel": self.close
494                 }, -1)
495
496         def okbuttonClick(self):
497                 selection = self["nimlist"].getCurrent()
498                 self.session.open(PositionerSetup, selection[1])
499
500 def PositionerMain(session, **kwargs):
501         nimList = nimmanager.getNimListOfType(nimmanager.nimType["DVB-S"])
502         if len(nimList) == 0:
503                 session.open(MessageBox, _("No positioner capable frontend found."), MessageBox.TYPE_ERROR)
504         else:
505                 if session.nav.RecordTimer.isRecording():
506                         session.open(MessageBox, _("A recording is currently running. Please stop the recording before trying to configure the positioner."), MessageBox.TYPE_ERROR)
507                 else:
508                         usableNims = []
509                         for x in nimList:
510                                 configured_rotor_sats = nimmanager.getRotorSatListForNim(x)
511                                 if len(configured_rotor_sats) != 0:
512                                         usableNims.append(x)
513                         if len(usableNims) == 1:
514                                 session.open(PositionerSetup, usableNims[0])
515                         elif len(usableNims) > 1:
516                                 session.open(NimSelection)
517                         else:
518                                 session.open(MessageBox, _("No tuner is configured for use with a diseqc positioner!"), MessageBox.TYPE_ERROR)
519
520 def Plugins(**kwargs):
521         return PluginDescriptor(name="Positioner setup", description="Setup your positioner", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=PositionerMain)