5
5
from usb .util import get_string
6
6
from PIL import Image
7
7
from struct import pack
8
- from six import ensure_binary
9
- from six .moves import cStringIO
8
+ from six .moves import cStringIO as StringIO
10
9
11
10
12
11
def write_jpg2frame (dev , pic ):
13
- # Attach header to picture, pad with zeros if necessary, and send to frame
12
+ """ Attach header to picture, pad with zeros if necessary, and send to frame"""
14
13
# create header and stack before picture
15
14
# middle 4 bytes have size of picture
16
15
rawdata = b"\xa5 \x5a \x18 \x04 " + pack ('<I' , len (pic ) + 14 ) + b"\x48 \x00 \x00 \x00 " + pic
@@ -23,96 +22,95 @@ def write_jpg2frame(dev, pic):
23
22
24
23
25
24
def get_known_devices ():
26
- # Return a dict of photo frames
27
- dlist = []
25
+ """ Return a dict of photo frames"""
26
+ dList = []
28
27
# listed as: Name, idVendor, idProduct, [width , height - in pixel if applicable]
29
-
28
+ #
30
29
#0,1 Samsung SPF-75H/76H (23)
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 })
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 })
33
32
34
33
#2,3 Samsung SPF-87H (24)
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 })
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 })
37
36
38
37
#4,5 Samsung SPF-87Hold (25)
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 })
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 })
41
40
42
41
#6,7 Samsung SPF-83H (26)
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 })
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 })
45
44
46
45
#8,9 Samsung SPF-107H (27)
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 })
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 })
49
48
50
49
#10,11 Samsung SPF-105P (28)
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 })
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 })
53
52
54
53
#12,13 Samsung SPF-85H/86H (29)
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 })
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 })
57
56
58
57
#14,15 Samsung SPF-72H (210)
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 })
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 })
61
60
62
61
#16,17 Samsung SPF-700T (211)
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 })
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 })
65
64
66
65
#18,19 Samsung SPF-85P/86P (212)
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 })
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 })
69
68
70
69
#20,21 Samsung SPF-107Hold (213)
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 })
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 })
73
72
74
73
#22,23 Samsung SPF-1000P (214)
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 })
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 })
77
76
78
77
#24,25 Samsung SPF-800P (215)
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 })
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 })
85
80
86
81
# Pearl DPF for Testing
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 })
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 })
89
84
90
- return dlist
85
+ return dList
91
86
92
87
93
- def find_device (anzahl , device , device2 ):
94
- # Try to find device on USB bus.
88
+ def find_device (Anzahl , device , device2 ):
89
+ """ Try to find device on USB bus."""
95
90
try :
96
- print ("[LCD4linux] looking for frame" , anzahl , device ['name' ], device ['idVendor' ], device ['idProduct' ], device2 ['idProduct' ])
97
- if anzahl == 2 :
91
+ print ("[LCD4linux] looking for frame" , Anzahl , device ['name' ], device ['idVendor' ], device ['idProduct' ], device2 ['idProduct' ])
92
+ if Anzahl == 2 :
98
93
d = list (find (idVendor = device ['idVendor' ], idProduct = device ['idProduct' ], find_all = True )) + list (find (idVendor = device2 ['idVendor' ], idProduct = device2 ['idProduct' ], find_all = True ))
99
94
if isinstance (d , list ):
100
- d = d [1 ] if len (d ) >= 2 else None
95
+ if len (d ) >= 2 :
96
+ d = d [1 ]
97
+ else :
98
+ d = None
101
99
else :
102
100
d = None
103
101
else :
104
102
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 ]
105
- except Exception :
103
+ except :
106
104
from traceback import format_exc
107
105
print ("[LCD4linux] find exception" )
108
106
print ("Error: %s" % format_exc ())
109
107
d = None
110
108
return d
111
109
112
110
113
- def init_device (anzahl , device0 , device1 ):
114
- # First try Mini Monitor mode, then Mass storage mode
115
- dev = find_device (anzahl , device0 , device1 )
111
+ def init_device (Anzahl , device0 , device1 ):
112
+ """ First try Mini Monitor mode, then Mass storage mode"""
113
+ dev = find_device (Anzahl , device0 , device1 )
116
114
117
115
if dev is not None :
118
116
## found it, trying to init it
@@ -126,7 +124,7 @@ def init_device(anzahl, device0, device1):
126
124
ts = time ()
127
125
while True :
128
126
# may need to burn some time
129
- dev = find_device (anzahl , device0 , device1 )
127
+ dev = find_device (Anzahl , device0 , device1 )
130
128
if dev is not None and dev .idProduct == device0 ["idProduct" ]:
131
129
#switching successful
132
130
break
@@ -142,66 +140,55 @@ def init_device(anzahl, device0, device1):
142
140
143
141
144
142
def frame_init (dev ):
145
- # Init device so it stays in Mini Monitor mode
143
+ """ Init device so it stays in Mini Monitor mode"""
146
144
# this is the minimum required to keep the frame in Mini Monitor mode!!!
147
- # dev.ctrl_transfer(0xc0, 4 )
148
- # dev.ctrl_transfer(0xc0, 0x01, 0x00, 0x00, 0x09, 0x04 )
145
+ # dev.ctrl_transfer(0xc0, 4 )
146
+ # dev.ctrl_transfer(0xc0, 0x01, 0x00, 0x00, 0x09, 0x04 )
149
147
dev .ctrl_transfer (0xc0 , 0x01 , 0x00 , 0x00 , 0x02 )
150
148
151
149
152
150
def frame_switch (dev ):
153
- CTRL_TYPE_VENDOR = (2 << 5 )
154
- CTRL_IN = 0x80
155
- CTRL_RECIPIENT_DEVICE = 0
151
+ """Switch device from Mass Storage to Mini Monitor"""
156
152
try :
157
- time .sleep (0.5 )
158
- s = "\x00 " * 251
153
+ sleep (0.5 )
159
154
dev .ctrl_transfer (0x00 | 0x80 , 0x06 , 0xfe , 0xfe , 0xfe )
160
- # dev.ctrl_transfer(0x00|0x80, 0x06, 0xfe, 0xfe, s, 0xfe )
161
- # dev.ctrl_transfer(CTRL_TYPE_VENDOR | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x04, 0x00, 0x00, 1)
162
- # result = dev.ctrl_transfer(CTRL_TYPE_VENDOR | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x04, 0x00, 0x00, 1)
163
- # expect(result, [ 0x03 ])
164
- # result = dev.ctrl_transfer(CTRL_TYPE_VENDOR | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x01, 0x00, 0x00, 2)
165
- # expect(result, [ 0x09, 0x04 ])
166
- # result = dev.ctrl_transfer(CTRL_TYPE_VENDOR | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x02, 0x00, 0x00, 1)
167
- # expect(result, [ 0x46 ])
168
- # settling of the bus and frame takes about 0.42 sec
169
- # give it some extra time, but then still make sure it has settled
170
- except Exception :
155
+ except :
171
156
print ("[LCD4linux] switching ERROR" )
172
- # from traceback import format_exc
173
- # print format_exc()
174
157
finally :
175
158
sleep (2 )
176
159
177
160
178
161
def name (dev ):
179
162
try :
180
163
return get_string (dev , 1 )
181
- except Exception :
164
+ except :
182
165
try :
183
166
return get_string (dev , 256 , 2 )
184
- except Exception :
167
+ except :
185
168
return None
186
169
187
170
188
171
def main ():
189
172
global dev , known_devices_list
173
+
190
174
known_devices_list = get_known_devices ()
175
+
191
176
# define which frame to use, here use Samsung SPF-87H
192
177
device0 = known_devices_list [0 ] # Mini Monitor mode
193
178
device1 = known_devices_list [1 ] # Mass Storage mode
179
+
194
180
dev = init_device (1 , device0 , device1 )
195
181
print ("Frame is in Mini Monitor mode and initialized. Sending pictures now" )
182
+
196
183
image = Image .open ("mypicture.jpg" )
197
184
#manipulations to consider:
198
185
# convert
199
186
# thumbnail
200
187
# rotate
201
188
# crop
202
189
image = image .resize ((800 , 480 ))
203
- output = cStringIO ()
204
- image .save (ensure_binary ( output ) , "JPEG" , quality = 94 )
190
+ output = StringIO . StringIO ()
191
+ image .save (output , "JPEG" , quality = 94 )
205
192
pic = output .getvalue ()
206
193
output .close ()
207
194
write_jpg2frame (dev , pic )
0 commit comments