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