fix bluescreen when try to store a position in rotor
[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 ConfigSatlist, ConfigNothing, ConfigSelection, ConfigSubsection, KEY_LEFT, KEY_RIGHT, getConfigListEntry
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))
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                 self.positioner_tune = ConfigNothing()
118                 self.positioner_move = ConfigNothing()
119                 self.positioner_finemove = ConfigNothing()
120                 self.positioner_limits = ConfigNothing()
121                 self.positioner_goto0 = ConfigNothing()
122                 storepos = []
123                 for x in range(1,255):
124                         storepos.append(str(x))
125                 self.positioner_storage = ConfigSelection(choices = storepos)
126
127         def createSetup(self):
128                 self.list.append((_("Tune"), self.positioner_tune, "tune"))
129                 self.list.append((_("Positioner movement"), self.positioner_move, "move"))
130                 self.list.append((_("Positioner fine movement"), self.positioner_finemove, "finemove"))
131                 self.list.append((_("Set limits"), self.positioner_limits, "limits"))
132                 self.list.append((_("Positioner storage"), self.positioner_storage, "storage"))
133                 self.list.append((_("Goto 0"), self.positioner_goto0, "goto0"))
134                 self["list"].l.setList(self.list)
135
136         def go(self):
137                 pass
138
139         def getCurrentConfigPath(self):
140                 return self["list"].getCurrent()[2]
141
142         def up(self):
143                 if not self.isMoving:
144                         self["list"].instance.moveSelection(self["list"].instance.moveUp)
145                         self.updateColors(self.getCurrentConfigPath())
146
147         def down(self):
148                 if not self.isMoving:
149                         self["list"].instance.moveSelection(self["list"].instance.moveDown)
150                         self.updateColors(self.getCurrentConfigPath())
151
152         def left(self):
153                 self["list"].handleKey(KEY_LEFT)
154
155         def right(self):
156                 self["list"].handleKey(KEY_RIGHT)
157
158         def updateColors(self, entry):
159                 if entry == "tune":
160                         self.red.setText(_("Tune"))
161                         self.green.setText("")
162                         self.yellow.setText("")
163                         self.blue.setText("")
164                 elif entry == "move":
165                         if self.isMoving:
166                                 self.red.setText(_("Stop"))
167                                 self.green.setText(_("Stop"))
168                                 self.yellow.setText(_("Stop"))
169                                 self.blue.setText(_("Stop"))
170                         else:
171                                 self.red.setText(_("Move west"))
172                                 self.green.setText(_("Search west"))
173                                 self.yellow.setText(_("Search east"))
174                                 self.blue.setText(_("Move east"))
175                 elif entry == "finemove":
176                         self.red.setText("")
177                         self.green.setText(_("Step west"))
178                         self.yellow.setText(_("Step east"))
179                         self.blue.setText("")
180                 elif entry == "limits":
181                         self.red.setText(_("Limits off"))
182                         self.green.setText(_("Limit west"))
183                         self.yellow.setText(_("Limit east"))
184                         self.blue.setText(_("Limits on"))
185                 elif entry == "storage":
186                         self.red.setText("")
187                         self.green.setText(_("Store position"))
188                         self.yellow.setText(_("Goto position"))
189                         self.blue.setText("")
190                 elif entry == "goto0":
191                         self.red.setText(_("Goto 0"))
192                         self.green.setText("")
193                         self.yellow.setText("")
194                         self.blue.setText("")
195                 else:
196                         self.red.setText("")
197                         self.green.setText("")
198                         self.yellow.setText("")
199                         self.blue.setText("")
200
201         def redKey(self):
202                 entry = self.getCurrentConfigPath()
203                 if entry == "move":
204                         if self.isMoving:
205                                 self.diseqccommand("stop")
206                                 self.isMoving = False
207                                 self.stopOnLock = False
208                         else:
209                                 self.diseqccommand("moveWest", 0)
210                                 self.isMoving = True
211                         self.updateColors("move")
212                 elif entry == "limits":
213                         self.diseqccommand("limitOff")
214                 elif entry == "tune":
215                         self.session.openWithCallback(self.tune, TunerScreen, self.feid)
216                 elif entry == "goto0":
217                         print "move to position 0"
218                         self.diseqccommand("moveTo", 0)
219
220         def greenKey(self):
221                 entry = self.getCurrentConfigPath()
222                 if entry == "move":
223                         if self.isMoving:
224                                 self.diseqccommand("stop")
225                                 self.isMoving = False
226                                 self.stopOnLock = False
227                         else:
228                                 self.isMoving = True
229                                 self.stopOnLock = True
230                                 self.diseqccommand("moveWest", 0)
231                         self.updateColors("move")
232                 elif entry == "finemove":
233                         print "stepping west"
234                         self.diseqccommand("moveWest", 0xFF) # one step
235                 elif entry == "storage":
236                         print "store at position", int(self.positioner_storage.value)
237                         self.diseqccommand("store", int(self.positioner_storage.value)
238                 elif entry == "limits":
239                         self.diseqccommand("limitWest")
240         
241         def yellowKey(self):
242                 entry = self.getCurrentConfigPath()
243                 if entry == "move":
244                         if self.isMoving:
245                                 self.diseqccommand("stop")
246                                 self.isMoving = False
247                                 self.stopOnLock = False
248                         else:
249                                 self.isMoving = True
250                                 self.stopOnLock = True
251                                 self.diseqccommand("moveEast", 0)
252                         self.updateColors("move")
253                 elif entry == "finemove":
254                         print "stepping east"
255                         self.diseqccommand("moveEast", 0xFF) # one step
256                 elif entry == "storage":
257                         print "move to position", (self.positioner_storage.value + 1)
258                         self.diseqccommand("moveTo", self.positioner_storage.value + 1)
259                 elif entry == "limits":
260                         self.diseqccommand("limitEast")
261 #       
262         def blueKey(self):
263                 entry = self.getCurrentConfigPath()
264                 if entry == "move":
265                         if self.isMoving:
266                                 self.diseqccommand("stop")
267                                 self.isMoving = False
268                                 self.stopOnLock = False
269                         else:
270                                 self.diseqccommand("moveEast", 0)
271                                 self.isMoving = True
272                         self.updateColors("move")
273                         print "moving east"
274                 elif entry == "limits":
275                         self.diseqccommand("limitOn")
276
277         def diseqccommand(self, cmd, param = 0):
278                 self.diseqc.command(cmd, param)
279                 self.tuner.retune()
280
281         def updateStatus(self):
282                 self["snr_percentage"].update()
283                 self["agc_percentage"].update()
284                 self["ber_value"].update()
285                 self["snr_bar"].update()
286                 self["agc_bar"].update()
287                 self["ber_bar"].update()
288                 self["lock_state"].update()
289                 transponderdata = self.tuner.getTransponderData()
290                 self["frequency_value"].setText(str(transponderdata["frequency"]))
291                 self["symbolrate_value"].setText(str(transponderdata["symbol_rate"]))
292                 self["fec_value"].setText(str(transponderdata["fec_inner"]))
293                 if transponderdata["tuner_locked"] == 1 and self.isMoving and self.stopOnLock:
294                         self.diseqccommand("stop")
295                         self.isMoving = False
296                         self.stopOnLock = False
297                         self.updateColors(self.getCurrentConfigPath())
298
299         def tune(self, transponder):
300                 if transponder is not None:
301                         self.tuner.tune(transponder)
302                         
303 class Diseqc:
304         def __init__(self, feid = 0):
305                 self.ready = False
306                 self.feid = feid
307                 res_mgr = eDVBResourceManagerPtr()
308                 if eDVBResourceManager.getInstance(res_mgr) == 0:
309                         self.raw_channel = iDVBChannelPtr()
310                         if res_mgr.allocateRawChannel(self.raw_channel, self.feid) == 0:
311                                 self.frontend = iDVBFrontendPtr()
312                                 if self.raw_channel.getFrontend(self.frontend) == 0:
313                                         self.ready = True
314                                 else:
315                                         print "getFrontend failed"
316                         else:
317                                 print "getRawChannel failed"
318                 else:
319                         print "getResourceManager instance failed"
320         
321         def getFrontend(self):
322                 return self.frontend
323                 
324         def command(self, what, param = 0):
325                 if self.ready:
326                         cmd = eDVBDiseqcCommand()
327                         if what == "moveWest":
328                                 string = 'e03169' + ("%02x" % param)
329                         elif what == "moveEast":
330                                 string = 'e03168' + ("%02x" % param)
331                         elif what == "moveTo":
332                                 string = 'e0316b' + ("%02x" % param)
333                         elif what == "store":
334                                 string = 'e0316a' + ("%02x" % param)
335                         elif what == "limitOn":
336                                 string = 'e0316a00'
337                         elif what == "limitOff":
338                                 string = 'e03163'
339                         elif what == "limitEast":
340                                 string = 'e03166'
341                         elif what == "limitWest":
342                                 string = 'e03167'
343                         else:
344                                 string = 'e03160' #positioner stop
345                         print "diseqc command:",
346                         print string
347                         
348                         cmd.setCommandString(string)
349                         self.frontend.sendDiseqc(cmd)
350                         
351 class Tuner:
352         def __init__(self, frontend):
353                 self.frontend = frontend
354                 
355         def tune(self, transponder):
356                 print "tuning to transponder with data", transponder
357                 parm = eDVBFrontendParametersSatellite()
358                 parm.frequency = transponder[0] * 1000
359                 parm.symbol_rate = transponder[1] * 1000
360                 parm.polarisation = transponder[2]
361                 parm.fec = transponder[3]
362                 parm.inversion = transponder[4]
363                 parm.orbital_position = transponder[5]
364                 parm.system = 0  # FIXMEE !! HARDCODED DVB-S (add support for DVB-S2)
365                 parm.modulation = 1 # FIXMEE !! HARDCODED QPSK
366                 feparm = eDVBFrontendParameters()
367                 feparm.setDVBS(parm, True)
368                 self.lastparm = feparm
369                 self.frontend.tune(feparm)
370         
371         def retune(self):
372                 self.frontend.tune(self.lastparm)
373         
374         def getTransponderData(self):
375                 return self.frontend.readTransponderData(True)
376
377 class TunerScreen(ScanSetup):
378         skin = """
379                 <screen position="90,100" size="520,400" title="Tune">
380                         <widget name="config" position="20,10" size="460,350" scrollbarMode="showOnDemand" />
381                         <widget name="introduction" position="20,360" size="350,30" font="Regular;23" />
382                 </screen>"""
383
384         def __init__(self, session, feid):
385                 self.feid = feid
386                 ScanSetup.__init__(self, session)
387
388                 self["introduction"].setText("")
389                 
390         def createSetup(self):
391                 self.typeOfTuningEntry = None
392                 self.satEntry = None
393
394                 self.list = []
395                 self.typeOfTuningEntry = getConfigListEntry(_('Tune'), self.tuning.type)
396                 self.list.append(self.typeOfTuningEntry)
397                 self.satEntry = getConfigListEntry(_('Satellite'), self.tuning.sat)
398                 self.list.append(self.satEntry)
399                 if self.tuning.type.value == "manual_transponder":
400                         self.list.append(getConfigListEntry(_('Frequency'), self.scan_sat.frequency))
401                         self.list.append(getConfigListEntry(_('Inversion'), self.scan_sat.inversion))
402                         self.list.append(getConfigListEntry(_('Symbol Rate'), self.scan_sat.symbolrate))
403                         self.list.append(getConfigListEntry(_("Polarity"), self.scan_sat.polarization))
404                         self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec))
405                 elif self.tuning.type.value == "predefined_transponder":
406                         self.list.append(getConfigListEntry(_("Transponder"), self.tuning.transponder))
407                 self["config"].list = self.list
408                 self["config"].l.setList(self.list)
409
410         def newConfig(self):
411                 if self["config"].getCurrent() == self.typeOfTuningEntry:
412                         self.createSetup()
413                 elif self["config"].getCurrent() == self.satEntry:
414                         self.updateTransponders()
415                         self.createSetup()
416
417         def createConfig(self, foo):
418                 self.tuning = ConfigSubsection()
419                 self.tuning.type = ConfigSelection(
420                         default = "manual_transponder",
421                         choices = { "manual_transponder" : _("Manual transponder"),
422                                                 "predefined_transponder" : _("Predefined transponder") } )
423                 self.tuning.sat = ConfigSatlist(list=nimmanager.getRotorSatListForNim(self.feid))
424                 ScanSetup.createConfig(self, None)
425                 self.updateTransponders()
426
427         def updateTransponders(self):
428                 if len(self.tuning.sat.choices):
429                         transponderlist = nimmanager.getTransponders(int(self.tuning.sat.value))
430                         tps = []
431                         cnt=0
432                         for x in transponderlist:
433                                 if x[3] == 0:
434                                         pol = "H"
435                                 elif x[3] == 1:
436                                         pol = "V"
437                                 elif x[3] == 2:
438                                         pol = "CL"
439                                 elif x[3] == 3:
440                                         pol = "CR"
441                                 if x[4] == 0:
442                                         fec = "FEC_AUTO"
443                                 elif x[4] == 1:
444                                         fec = "FEC_1_2"
445                                 elif x[4] == 2:
446                                         fec = "FEC_2_3"
447                                 elif x[4] == 3:
448                                         fec = "FEC_3_4"
449                                 elif x[4] == 4:
450                                         fec = "FEC_5_6"
451                                 elif x[4] == 5:
452                                         fec = "FEC_7_8"
453                                 elif x[4] == 5:
454                                         fec = "FEC_8_9"
455                                 elif x[4] == 6:
456                                         fec = "FEC_None"
457                                 tps.append(str(x[1]) + "," + str(x[2]) + "," + pol + "," + fec)
458                         self.tuning.transponder = ConfigSelection(choices=tps)
459
460         def keyGo(self):
461                 returnvalue = (0, 0, 0, 0, 0, 0)
462                 satpos = int(self.tuning.sat.value)
463                 if self.tuning.type.value == "manual_transponder":
464                         returnvalue = (
465                                 self.scan_sat.frequency.value,
466                                 self.scan_sat.symbolrate.value,
467                                 self.scan_sat.polarization.index,
468                                 self.scan_sat.fec.index,
469                                 self.scan_sat.inversion.index,
470                                 satpos)
471                 elif self.tuning.type.value == "predefined_transponder":
472                         transponder = nimmanager.getTransponders(satpos)[self.tuning.transponder.index]
473                         returnvalue = (int(transponder[1] / 1000), int(transponder[2] / 1000), transponder[3], transponder[4], 2, satpos)
474                 self.close(returnvalue)
475
476         def keyCancel(self):
477                 self.close(None)
478
479 class NimSelection(Screen):
480         skin = """
481                 <screen position="140,165" size="400,100" title="select Slot">
482                         <widget name="nimlist" position="20,10" size="360,75" />
483                 </screen>"""
484                 
485         def __init__(self, session):
486                 Screen.__init__(self, session)
487
488                 nimlist = nimmanager.getNimListOfType(nimmanager.nimType["DVB-S"])
489                 nimMenuList = []
490                 for x in nimlist:
491                         nimMenuList.append((_("NIM ") + (["A", "B", "C", "D"][x]) + ": " + nimmanager.getNimName(x) + " (" + nimmanager.getNimTypeName(x) + ")", x))
492                 
493                 self["nimlist"] = MenuList(nimMenuList)
494
495                 self["actions"] = ActionMap(["OkCancelActions"],
496                 {
497                         "ok": self.okbuttonClick ,
498                         "cancel": self.close
499                 }, -1)
500
501         def okbuttonClick(self):
502                 selection = self["nimlist"].getCurrent()
503                 self.session.open(PositionerSetup, selection[1])
504
505 def PositionerMain(session, **kwargs):
506         nimList = nimmanager.getNimListOfType(nimmanager.nimType["DVB-S"])
507         if len(nimList) == 0:
508                 session.open(MessageBox, _("No positioner capable frontend found."), MessageBox.TYPE_ERROR)
509         else:
510                 if session.nav.RecordTimer.isRecording():
511                         session.open(MessageBox, _("A recording is currently running. Please stop the recording before trying to configure the positioner."), MessageBox.TYPE_ERROR)
512                 else:
513                         usableNims = []
514                         for x in nimList:
515                                 configured_rotor_sats = nimmanager.getRotorSatListForNim(x)
516                                 if len(configured_rotor_sats) != 0:
517                                         usableNims.append(x)
518                         if len(usableNims) == 1:
519                                 session.open(PositionerSetup, usableNims[0])
520                         elif len(usableNims) > 1:
521                                 session.open(NimSelection)
522                         else:
523                                 session.open(MessageBox, _("No tuner is configured for use with a diseqc positioner!"), MessageBox.TYPE_ERROR)
524
525 def Plugins(**kwargs):
526         return PluginDescriptor(name="Positioner setup", description="Setup your positioner", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=PositionerMain)