55from usb .util import get_string
66from PIL import Image
77from struct import pack
8+ from six import ensure_binary
89from six .moves import cStringIO as StringIO
910
1011
1112def 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
2425def 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
142144def 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
150152def 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
161179def 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
171189def 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
0 commit comments