[filestreamproxy] some bugs fixed.
[vuplus_openvuplus] / meta-openvuplus / recipes-vuplus / duo2lcd4linux / lcd4linuxsupport / plugin.py
1
2 import os
3 from time import time
4 from datetime import datetime
5
6 from enigma import eTimer
7 from Components.config import ConfigSelection, ConfigSelectionNumber, ConfigSlider, ConfigYesNo
8 from Plugins.Plugin import PluginDescriptor
9 from fcntl import ioctl
10
11 from pngutil import png_util
12 pngutil = png_util.PNGUtil()
13 lcd4linuxPluginPath = "/usr/lib/enigma2/python/Plugins/Extensions/LCD4linux"
14
15 LCDType1_id_lver = "150"
16 LCDType1_id = "350"
17
18 from Plugins.Extensions.LCD4linux import plugin
19
20 def getResolution(t, r="0"):
21         if t[:1] == "5":
22                 ttt = LCD4linux.xmlLCDType.value.split("x")
23                 MAX_W,MAX_H = int(ttt[0]),int(ttt[1])
24         elif t[1:] == "1":
25                 MAX_W,MAX_H = 320,240
26         elif t[1:] == "2":
27                 MAX_W,MAX_H = 240,320
28         elif t[1:] in ["3","4","5","10"]:
29                 MAX_W,MAX_H = 800,480
30         elif t[1:] in ["6","9","11","12"]:
31                 MAX_W,MAX_H = 800,600
32         elif t[1:] in ["7","8","13"]:
33                 MAX_W,MAX_H = 1024,600
34         elif t[1:] == "17":
35                 MAX_W,MAX_H = 220,176
36         elif t[1:] == "18":
37                 MAX_W,MAX_H = 255,64
38         elif t[1:] == "30":
39                 MAX_W,MAX_H = 400,240
40         elif t[1:] == "20":
41                 MAX_W,MAX_H = LCD4linux.SizeW.value,LCD4linux.SizeH.value
42         elif t[1:] == "50": # vuduo2
43                 MAX_W,MAX_H = 400,240
44         if r in ["90","270"]:
45                 MAX_W,MAX_H = MAX_H,MAX_W
46         return MAX_W,MAX_H
47
48 plugin_writeHelligkeit = None
49 g_min = 0
50 g_max = None
51 def writeHelligkeit_lver(hell):
52         global g_min
53         global g_max
54         global plugin_writeHelligkeit
55         global LCDType1_id_lver
56         plugin_writeHelligkeit(hell)
57         if plugin.config.plugins.LCD4linux.LCDType1.value == LCDType1_id_lver:
58                 if g_max is None:
59                         g_min, g_max = getHellRange(plugin.config.plugins.LCD4linux.Helligkeit)
60                 updateBrightness(int(hell), g_min, g_max)
61
62 def writeHelligkeit(hell,hell2,STOP = False):
63         global g_min
64         global g_max
65         global old_hell
66         global plugin_writeHelligkeit
67         global LCDType1_id
68         plugin_writeHelligkeit(hell, hell2, STOP)
69         if plugin.LCD4linux.LCDType1.value == LCDType1_id:
70                 if g_max is None:
71                         g_min, g_max = getHellRange(plugin.LCD4linux.Helligkeit)
72                 updateBrightness(int(hell), g_min, g_max)
73
74 def getHellRange(ins):
75         try:
76                 if isinstance(ins, ConfigSelectionNumber):
77                         choices = []
78                         for s in ins.choices.choices:
79                                 choices.append(int(s))
80                         return (min(choices), max(choices))
81                 elif isinstance(ins, ConfigSlider):
82                         return (ins.min, ins.max)
83                 else:
84                         return (0,10)
85         except:
86                 return (0,10)
87
88 old_hell = 40
89 def updateBrightness(hell, _min, _max):
90         try:
91                 if _min == _max: return
92                 hell = int( 255*hell/(_max - _min) )
93                 if hell >= 250:
94                         hell = 255
95                 global old_hell
96                 if hell == old_hell:
97                         return
98                 else:
99                         old_hell = hell
100                 print "[LCD4linux Support] update Brightness : ",hell
101                 led_fd = open("/dev/lcd2",'rw')
102                 ioctl(led_fd, 0x10, hell)
103                 led_fd.close()
104         except:
105                 pass
106
107 class pngUtilTimer:
108         def __init__(self):
109                 self.PIC = "/tmp/dpf"
110                 self.updateTimer = eTimer()
111                 self.updateTimer.callback.append(self.updateLCD)
112                 self.pngutilconnect = pngutil.connect()
113                 self.last_mod_time = 0
114                 self.oldPluginVer = False
115
116         def startUpdateTimer(self):
117                 if self.pngutilconnect :
118                         self.updateTimer.start(1000, True)
119
120         def updateLCD(self):
121                 self.updateTimer.stop()
122                 if self.oldPluginVer:
123                         lcd = plugin.config.plugins.LCD4linux.LCDType1.value
124                 else:
125                         lcd = plugin.LCD4linux.LCDType1.value
126                 if lcd[1:] == "50":
127                         if os.path.exists(self.PIC):
128                                 modify_time = os.stat(self.PIC).st_mtime
129                                 if modify_time != self.last_mod_time:
130                                         self.last_mod_time = modify_time
131                                         pngutiltime = time()
132                                         pngutil.send(self.PIC)
133                                         print "[LCD4linux Support] time (write to lcd) : ",time()-pngutiltime
134                 self.updateTimer.start(500, True)
135
136         def setLcd4linuxDuo2(self):
137                 global plugin_writeHelligkeit
138                 try:
139                         lcdtype = plugin.LCDType
140                         print "[LCD4linux Support] set LCD4linux for DUO2 (ver %s)"%plugin.Version
141                         if plugin.Version == "V0.8r3":
142                                 global LCDType1_id_lver
143                                 self.oldPluginVer = True
144                                 lcdtype.append((LCDType1_id_lver, _("Vu+ Duo2 LCD 400x240")))
145                                 plugin.config.plugins.LCD4linux.LCDType1 = ConfigSelection(choices = lcdtype, default=LCDType1_id_lver)
146                                 plugin.config.plugins.LCD4linux.Standby = ConfigSelection(choices = [("0", _("off")), ("1", _("on"))], default="0")
147                                 plugin.config.plugins.LCD4linux.Thread = ConfigYesNo(default = True)
148                                 plugin.config.plugins.LCD4linux.Helligkeit = ConfigSlider(default = 1,  limits = (0, 7))
149                                 plugin_writeHelligkeit = plugin.writeHelligkeit
150                                 plugin.writeHelligkeit = writeHelligkeit_lver
151                         else:
152                                 global LCDType1_id
153                                 lcdtype.append((LCDType1_id, _("Vu+ Duo2 LCD 400x240")))
154                                 plugin.LCD4linux.LCDType1 = ConfigSelection(choices = lcdtype, default=LCDType1_id)
155                                 plugin.LCD4linux.Standby = ConfigSelection(choices = [("0", _("off")), ("1", _("on"))], default="0")
156                                 plugin.LCD4linux.Helligkeit = ConfigSelectionNumber(0, 10, 1, default = 2)
157                                 plugin_writeHelligkeit = plugin.writeHelligkeit
158                                 plugin.writeHelligkeit = writeHelligkeit
159                         plugin.getResolution = getResolution
160                         self.PIC = plugin.PIC+".png"
161                         return True
162                 except:
163                         return False
164
165 pngutiltimer = pngUtilTimer()
166
167 def lcdtimer(reason, **kwargs):
168         if pngutiltimer.setLcd4linuxDuo2():
169                 pngutiltimer.startUpdateTimer()
170
171 def Plugins(**kwargs):
172         list = []
173         global lcd4linuxPluginPath
174         if os.path.exists(lcd4linuxPluginPath+"/plugin.py"):
175                 list.append(
176                         PluginDescriptor(name="LCD4linuxSupport",
177                         description="LCD4linuxSupport",
178                         where = [PluginDescriptor.WHERE_SESSIONSTART],
179                         fnc = lcdtimer))
180         return list
181