Skip to content

Commit 72754ee

Browse files
authored
Merge pull request #9 from Hains/python3
Update LCD4Linux
2 parents c51ef42 + 891216e commit 72754ee

File tree

4 files changed

+172
-139
lines changed

4 files changed

+172
-139
lines changed

Diff for: lcd4linux/src/Photoframe.py

+71-57
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
from usb.util import get_string
66
from PIL import Image
77
from struct import pack
8+
from six import ensure_binary
89
from six.moves import cStringIO as StringIO
910

1011

1112
def write_jpg2frame(dev, pic):
12-
"""Attach header to picture, pad with zeros if necessary, and send to frame"""
13+
# Attach header to picture, pad with zeros if necessary, and send to frame
1314
# create header and stack before picture
1415
# middle 4 bytes have size of picture
1516
rawdata = b"\xa5\x5a\x18\x04" + pack('<I', len(pic) + 14) + b"\x48\x00\x00\x00" + pic
@@ -22,95 +23,96 @@ def write_jpg2frame(dev, pic):
2223

2324

2425
def get_known_devices():
25-
"""Return a dict of photo frames"""
26-
dList = []
26+
# Return a dict of photo frames
27+
dlist = []
2728
# listed as: Name, idVendor, idProduct, [width , height - in pixel if applicable]
28-
#
29+
2930
#0,1 Samsung SPF-75H/76H (23)
30-
dList.append({'name': "SPF75H/76H Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x200f, 'width': 800, 'height': 480})
31-
dList.append({'name': "SPF75H/76H Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x200e})
31+
dlist.append({'name': "SPF75H/76H Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x200f, 'width': 800, 'height': 480})
32+
dlist.append({'name': "SPF75H/76H Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x200e})
3233

3334
#2,3 Samsung SPF-87H (24)
34-
dList.append({'name': "SPF87H Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2034, 'width': 800, 'height': 480})
35-
dList.append({'name': "SPF87H Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2033})
35+
dlist.append({'name': "SPF87H Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2034, 'width': 800, 'height': 480})
36+
dlist.append({'name': "SPF87H Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2033})
3637

3738
#4,5 Samsung SPF-87Hold (25)
38-
dList.append({'name': "SPF87Hold Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2026, 'width': 800, 'height': 480})
39-
dList.append({'name': "SPF87Hold Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2025})
39+
dlist.append({'name': "SPF87Hold Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2026, 'width': 800, 'height': 480})
40+
dlist.append({'name': "SPF87Hold Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2025})
4041

4142
#6,7 Samsung SPF-83H (26)
42-
dList.append({'name': "SPF83H Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x200d, 'width': 800, 'height': 600})
43-
dList.append({'name': "SPF83H Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x200c})
43+
dlist.append({'name': "SPF83H Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x200d, 'width': 800, 'height': 600})
44+
dlist.append({'name': "SPF83H Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x200c})
4445

4546
#8,9 Samsung SPF-107H (27)
46-
dList.append({'name': "SPF107H Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2036, 'width': 1024, 'height': 600})
47-
dList.append({'name': "SPF107H Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2035})
47+
dlist.append({'name': "SPF107H Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2036, 'width': 1024, 'height': 600})
48+
dlist.append({'name': "SPF107H Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2035})
4849

4950
#10,11 Samsung SPF-105P (28)
50-
dList.append({'name': "SPF105P Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x201b, 'width': 1024, 'height': 600})
51-
dList.append({'name': "SPF105P Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x201c})
51+
dlist.append({'name': "SPF105P Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x201b, 'width': 1024, 'height': 600})
52+
dlist.append({'name': "SPF105P Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x201c})
5253

5354
#12,13 Samsung SPF-85H/86H (29)
54-
dList.append({'name': "SPF85H/86H Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2013, 'width': 800, 'height': 600})
55-
dList.append({'name': "SPF85H/86H Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2012})
55+
dlist.append({'name': "SPF85H/86H Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2013, 'width': 800, 'height': 600})
56+
dlist.append({'name': "SPF85H/86H Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2012})
5657

5758
#14,15 Samsung SPF-72H (210)
58-
dList.append({'name': "SPF72H Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x200b, 'width': 800, 'height': 480})
59-
dList.append({'name': "SPF72H Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x200a})
59+
dlist.append({'name': "SPF72H Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x200b, 'width': 800, 'height': 480})
60+
dlist.append({'name': "SPF72H Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x200a})
6061

6162
#16,17 Samsung SPF-700T (211)
62-
dList.append({'name': "SPF700T Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2050, 'width': 800, 'height': 600})
63-
dList.append({'name': "SPF700T Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x204f})
63+
dlist.append({'name': "SPF700T Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2050, 'width': 800, 'height': 600})
64+
dlist.append({'name': "SPF700T Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x204f})
6465

6566
#18,19 Samsung SPF-85P/86P (212)
66-
dList.append({'name': "SPF85P/86P Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2017, 'width': 800, 'height': 600})
67-
dList.append({'name': "SPF85P/86P Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2016})
67+
dlist.append({'name': "SPF85P/86P Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2017, 'width': 800, 'height': 600})
68+
dlist.append({'name': "SPF85P/86P Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2016})
6869

6970
#20,21 Samsung SPF-107Hold (213)
70-
dList.append({'name': "SPF107Hold Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2028, 'width': 1024, 'height': 600})
71-
dList.append({'name': "SPF107Hold Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2027})
71+
dlist.append({'name': "SPF107Hold Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2028, 'width': 1024, 'height': 600})
72+
dlist.append({'name': "SPF107Hold Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2027})
7273

7374
#22,23 Samsung SPF-1000P (214)
74-
dList.append({'name': "SPF1000P Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2040, 'width': 1024, 'height': 600})
75-
dList.append({'name': "SPF1000P Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2039})
75+
dlist.append({'name': "SPF1000P Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2040, 'width': 1024, 'height': 600})
76+
dlist.append({'name': "SPF1000P Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2039})
7677

7778
#24,25 Samsung SPF-800P (215)
78-
dList.append({'name': "SPF800P Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2038, 'width': 800, 'height': 480})
79-
dList.append({'name': "SPF800P Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2037})
79+
dlist.append({'name': "SPF800P Mini Monitor", 'idVendor': 0x04e8, 'idProduct': 0x2038, 'width': 800, 'height': 480})
80+
dlist.append({'name': "SPF800P Mass Storage", 'idVendor': 0x04e8, 'idProduct': 0x2037})
81+
82+
# Amazon Fire 7 (9th Generation 2019)
83+
dlist.append({'name': "Amazon Fire 7 Mini Monitor", 'idVendor': 0x1949, 'idProduct': 0x03C3, 'width': 1024, 'height': 600})
84+
dlist.append({'name': "Amazon Fire 7 Mass Storage", 'idVendor': 0x1949, 'idProduct': 0x03C1})
8085

8186
# Pearl DPF for Testing
82-
dList.append({'name': "Pearl DPF", 'idVendor': 0x1908, 'idProduct': 0x0102, 'width': 320, 'height': 240})
83-
dList.append({'name': "Pearl DPF", 'idVendor': 0x1908, 'idProduct': 0x0102, 'width': 320, 'height': 240})
87+
dlist.append({'name': "Pearl DPF", 'idVendor': 0x1908, 'idProduct': 0x0102, 'width': 320, 'height': 240})
88+
dlist.append({'name': "Pearl DPF", 'idVendor': 0x1908, 'idProduct': 0x0102, 'width': 320, 'height': 240})
8489

85-
return dList
90+
return dlist
8691

8792

88-
def find_device(Anzahl, device, device2):
89-
"""Try to find device on USB bus."""
93+
def find_device(anzahl, device, device2):
94+
# Try to find device on USB bus.
9095
try:
91-
print("[LCD4linux] looking for frame", Anzahl, device['name'], device['idVendor'], device['idProduct'], device2['idProduct'])
92-
if Anzahl == 2:
96+
print("[LCD4linux] looking for frame", anzahl, device['name'], device['idVendor'], device['idProduct'], device2['idProduct'])
97+
if anzahl == 2:
9398
d = list(find(idVendor=device['idVendor'], idProduct=device['idProduct'], find_all=True)) + list(find(idVendor=device2['idVendor'], idProduct=device2['idProduct'], find_all=True))
9499
if isinstance(d, list):
95-
if len(d) >= 2:
96-
d = d[1]
97-
else:
98-
d = None
100+
d = d[1] if len(d) >= 2 else None
99101
else:
100102
d = None
101103
else:
102104
d = list(list(find(idVendor=device['idVendor'], idProduct=device['idProduct'], find_all=True)) + list(find(idVendor=device2['idVendor'], idProduct=device2['idProduct'], find_all=True)))[0]
103-
except:
105+
except Exception:
104106
from traceback import format_exc
105107
print("[LCD4linux] find exception")
106108
print("Error: %s" % format_exc())
107109
d = None
108110
return d
109111

110112

111-
def init_device(Anzahl, device0, device1):
112-
"""First try Mini Monitor mode, then Mass storage mode"""
113-
dev = find_device(Anzahl, device0, device1)
113+
def init_device(anzahl, device0, device1):
114+
# First try Mini Monitor mode, then Mass storage mode
115+
dev = find_device(anzahl, device0, device1)
114116

115117
if dev is not None:
116118
## found it, trying to init it
@@ -124,7 +126,7 @@ def init_device(Anzahl, device0, device1):
124126
ts = time()
125127
while True:
126128
# may need to burn some time
127-
dev = find_device(Anzahl, device0, device1)
129+
dev = find_device(anzahl, device0, device1)
128130
if dev is not None and dev.idProduct == device0["idProduct"]:
129131
#switching successful
130132
break
@@ -140,46 +142,58 @@ def init_device(Anzahl, device0, device1):
140142

141143

142144
def frame_init(dev):
143-
"""Init device so it stays in Mini Monitor mode"""
145+
# Init device so it stays in Mini Monitor mode
144146
# this is the minimum required to keep the frame in Mini Monitor mode!!!
145-
# dev.ctrl_transfer(0xc0, 4 )
146-
# dev.ctrl_transfer(0xc0, 0x01, 0x00, 0x00, 0x09, 0x04 )
147+
# dev.ctrl_transfer(0xc0, 4 )
148+
# dev.ctrl_transfer(0xc0, 0x01, 0x00, 0x00, 0x09, 0x04 )
147149
dev.ctrl_transfer(0xc0, 0x01, 0x00, 0x00, 0x02)
148150

149151

150152
def frame_switch(dev):
151-
"""Switch device from Mass Storage to Mini Monitor"""
153+
# Switch device from Mass Storage to Mini Monitor
154+
CTRL_TYPE_VENDOR = (2 << 5)
155+
CTRL_IN = 0x80
156+
CTRL_RECIPIENT_DEVICE = 0
152157
try:
153158
sleep(0.5)
159+
s = "\x00" * 251
154160
dev.ctrl_transfer(0x00 | 0x80, 0x06, 0xfe, 0xfe, 0xfe)
155-
except:
161+
# dev.ctrl_transfer(0x00 | 0x80, 0x06, 0xfe, 0xfe, s, 0xfe )
162+
# dev.ctrl_transfer(CTRL_TYPE_VENDOR | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x04, 0x00, 0x00, 1)
163+
# result = dev.ctrl_transfer(CTRL_TYPE_VENDOR | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x04, 0x00, 0x00, 1)
164+
# expect(result, [ 0x03 ])
165+
# result = dev.ctrl_transfer(CTRL_TYPE_VENDOR | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x01, 0x00, 0x00, 2)
166+
# expect(result, [ 0x09, 0x04 ])
167+
# result = dev.ctrl_transfer(CTRL_TYPE_VENDOR | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x02, 0x00, 0x00, 1)
168+
# expect(result, [ 0x46 ])
169+
# settling of the bus and frame takes about 0.42 sec
170+
# give it some extra time, but then still make sure it has settled
171+
except Exception:
156172
print("[LCD4linux] switching ERROR")
173+
# from traceback import format_exc
174+
# print format_exc()
157175
finally:
158176
sleep(2)
159177

160178

161179
def name(dev):
162180
try:
163181
return get_string(dev, 1)
164-
except:
182+
except Exception:
165183
try:
166184
return get_string(dev, 256, 2)
167-
except:
185+
except Exception:
168186
return None
169187

170188

171189
def main():
172190
global dev, known_devices_list
173-
174191
known_devices_list = get_known_devices()
175-
176192
# define which frame to use, here use Samsung SPF-87H
177193
device0 = known_devices_list[0] # Mini Monitor mode
178194
device1 = known_devices_list[1] # Mass Storage mode
179-
180195
dev = init_device(1, device0, device1)
181196
print("Frame is in Mini Monitor mode and initialized. Sending pictures now")
182-
183197
image = Image.open("mypicture.jpg")
184198
#manipulations to consider:
185199
# convert

Diff for: lcd4linux/src/dpf.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from six import PY2
33
try:
44
from . import dpflib
5-
except:
5+
except Exception:
66
print("[LCD4linux] dpflib-Error")
77

88
PROPERTY_BRIGHTNESS = 0x01
@@ -15,7 +15,7 @@ def setBacklight(dev, value):
1515
try:
1616
dev.setProperty(PROPERTY_BRIGHTNESS, value)
1717
return True
18-
except:
18+
except Exception:
1919
print("[LCD4linux] Error set Backlight")
2020
return False
2121

@@ -29,17 +29,17 @@ def showImage(dev, image):
2929
else:
3030
dev.showRGBAImage(0, 0, x, y, ir.tobytes())
3131
return True
32-
except:
32+
except Exception:
3333
print("[LCD4linux] Error writing DPF Device")
3434
return False
3535

3636

3737
def open(usb):
3838
try:
3939
d = dpflib.open(usb)
40-
d.setProperty(PROPERTY_ORIENTATION, 1)
40+
# d.setProperty(PROPERTY_ORIENTATION, 1)
4141
print("[LCD4linux] open %s" % usb)
42-
except:
42+
except Exception:
4343
d = None
4444
print("[LCD4linux] open Error: %s" % usb)
4545
return d
@@ -49,5 +49,5 @@ def close(dev):
4949
try:
5050
if dev is not None:
5151
dev.close()
52-
except:
52+
except Exception:
5353
pass

Diff for: lcd4linux/src/module.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ def getHold(self):
7979
return L4Lelement.Hold
8080

8181
def setHold(self, H):
82-
print("[LCD4linuxE] Hold: %s" % H)
82+
print("[LCD4linuxE] hold: %s" % H)
8383
L4Lelement.Hold = H
8484

8585
def getHoldKey(self):
8686
return L4Lelement.HoldKey
8787

8888
def setHoldKey(self, H=False):
89-
print("[LCD4linuxE] HoldKey: %s" % H)
89+
print("[LCD4linuxE] holdkey: %s" % H)
9090
L4Lelement.HoldKey = H
9191

9292
def getFont(self, F="0"):
@@ -102,29 +102,27 @@ def getScreen(self):
102102
return L4Lelement.Screen
103103

104104
def setScreen(self, S, Lcd="", Hold=False):
105-
if Lcd != "":
106-
if len(str(Lcd)) > 1 or int(Lcd) > 3:
107-
Lcd = "1"
105+
if Lcd != "" and (len(str(Lcd)) > 1 or int(Lcd) > 3):
106+
Lcd = "1"
108107
L4Lelement.Screen = str(S)
109108
L4Lelement.LCD = str(Lcd)
110109
L4Lelement.Hold = Hold
111110
L4Lelement.Refresh = True
112111

113-
def resetBrightness(self, AKT=""):
112+
def resetBrightness(self, AKT=[]):
114113
if len(AKT) == 3:
115114
L4Lelement.BrightAkt = AKT
116115
else:
117116
L4Lelement.Bright = [-1, -1, -1]
118117

119118
def setBrightness(self, LCD, BRI=-1):
120-
if int(LCD) < 1 or int(LCD) > 3:
121-
return
122-
L4Lelement.Bright[int(LCD) - 1] = int(BRI)
123-
L4Lelement.Refresh = True
119+
if int(LCD) > 0 and int(LCD) < 4:
120+
L4Lelement.Bright[int(LCD) - 1] = int(BRI)
121+
L4Lelement.Refresh = True
124122

125123
def getBrightness(self, LCD=0, ORG=True):
126124
if int(LCD) > 0 and int(LCD) < 4:
127-
return L4Lelement.Bright[int(LCD) - 1] if ORG == False else L4Lelement.BrightAkt[int(LCD) - 1]
125+
return [L4Lelement.Bright[int(LCD) - 1]] if ORG == False else [L4Lelement.BrightAkt[int(LCD) - 1]]
128126
else:
129127
return L4Lelement.Bright if ORG == False else L4Lelement.BrightAkt
130128

@@ -145,14 +143,13 @@ def getstatusoutput(cmd):
145143
sts = pipe.close()
146144
if sts is None:
147145
sts = 0
148-
if text[-1:] == '\n':
146+
if text.endswith == '\n':
149147
text = text[:-1]
150148
except Exception:
151149
sts = 1
152150
text = "- -"
153151
print("[LCD4linux] Error on os-call")
154-
finally:
155-
return sts, text
152+
return sts, text
156153

157154

158155
def L4LVtest(VV):
@@ -172,6 +169,7 @@ def L4LVtest(VV):
172169
if O != "":
173170
try:
174171
f = open(L4Linfo % (O, P))
172+
B = f.readline()
175173
OO = f.readline().strip().split()[1].startswith(VV[1:])
176174
f.close()
177175
except Exception:

0 commit comments

Comments
 (0)