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