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