Mercurial > hg > minimon
annotate frame-ctrl.py @ 3:e8957085fe8a
working
author | Peter Meerwald <pmeerw@pmeerw.net> |
---|---|
date | Sun, 08 May 2011 23:08:45 +0200 |
parents | bac8ed8d6eb9 |
children |
rev | line source |
---|---|
1 | 1 #!/usr/bin/env python |
2 | |
3 import os | |
4 import struct | |
5 import sys | |
6 import time | |
7 import usb.core | |
8 from usb.util import * | |
9 | |
10 vendorId = 0x04e8 | |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
1
diff
changeset
|
11 models = {'SPF-87H': (0x2033, 0x2034), |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
1
diff
changeset
|
12 'SPF-107H': (0x2027, 0x2028), |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
1
diff
changeset
|
13 'SPF-107H': (0x2035, 0x2036), |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
1
diff
changeset
|
14 } |
1 | 15 |
16 chunkSize = 0x4000 | |
17 bufferSize = 0x20000 | |
18 | |
19 def expect(result, verifyList): | |
20 resultList = result.tolist() | |
21 if resultList != verifyList: | |
22 print >> sys.stderr, "Warning: Expected " + str(verifyList) + " but got " + str(resultList) | |
23 | |
24 def storageToDisplay(dev): | |
25 print "Setting device to display mode" | |
26 try: | |
27 dev.ctrl_transfer(CTRL_TYPE_STANDARD | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x06, 0xfe, 0xfe, 254) | |
28 except usb.core.USBError as e: | |
29 errorStr = str(e) | |
30 if errorStr != 'No such device (it may have been disconnected)': | |
31 raise e | |
32 | |
33 def displayModeSetup(dev): | |
34 print "Sending setup commands to device" | |
35 result = dev.ctrl_transfer(CTRL_TYPE_VENDOR | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x04, 0x00, 0x00, 1) | |
36 expect(result, [ 0x03 ]) | |
37 # result = dev.ctrl_transfer(CTRL_TYPE_VENDOR | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x01, 0x00, 0x00, 2) | |
38 # expect(result, [ 0x09, 0x04 ]) | |
39 # result = dev.ctrl_transfer(CTRL_TYPE_VENDOR | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x02, 0x00, 0x00, 1) | |
40 # expect(result, [ 0x46 ]) | |
41 | |
42 def paddedBytes(buf, size): | |
43 diff = size - len(buf) | |
44 return buf + bytes(b'\x00') * diff | |
45 | |
46 def chunkyWrite(dev, buf): | |
47 pos = 0 | |
48 while pos < bufferSize: | |
49 dev.write(0x02, buf[pos:pos+chunkSize]) | |
50 pos += chunkSize | |
51 | |
52 def writeImage(dev): | |
53 # result = dev.ctrl_transfer(CTRL_TYPE_STANDARD | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x06, 0x0300, 0x00, 255) | |
54 # expect(result, [ 0x04, 0x03, 0x09, 0x04 ]) | |
55 | |
56 # result = dev.ctrl_transfer(CTRL_TYPE_STANDARD | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x06, 0x0301, 0x0409, 255) | |
57 | |
58 if len(sys.argv) < 2 or sys.argv[1] == "-": | |
59 content = sys.stdin.read() | |
60 else: | |
61 f = open(sys.argv[1]) | |
62 content = f.read() | |
63 f.close() | |
64 | |
65 size = struct.pack('I', len(content)) | |
66 header = b'\xa5\x5a\x09\x04' + size + b'\x46\x00\x00\x00' | |
67 | |
68 content = header + content | |
69 | |
70 pos = 0 | |
71 while pos < len(content): | |
72 buf = paddedBytes(content[pos:pos+bufferSize], bufferSize) | |
73 chunkyWrite(dev, buf) | |
74 pos += bufferSize | |
75 | |
76 | |
77 # result = dev.ctrl_transfer(CTRL_TYPE_VENDOR | CTRL_IN | CTRL_RECIPIENT_DEVICE, 0x06, 0x00, 0x00, 2) | |
78 # expect(result, [ 0x00, 0x00 ]) | |
79 | |
80 found = False | |
81 | |
82 for k, v in models.iteritems(): | |
83 dev = usb.core.find(idVendor=vendorId, idProduct=v[0]) | |
84 if dev: | |
85 print "Found " + k + " in storage mode" | |
86 storageToDisplay(dev) | |
87 time.sleep(1) | |
88 dev = usb.core.find(idVendor=vendorId, idProduct=v[1]) | |
89 displayModeSetup(dev) | |
90 found = True | |
91 if not dev: | |
92 dev = usb.core.find(idVendor=vendorId, idProduct=v[1]) | |
93 if dev: | |
94 print "Found " + k + " in display mode" | |
95 dev.set_configuration() | |
96 writeImage(dev) | |
97 found = True | |
98 | |
99 if not found: | |
100 print >> sys.stderr, "No supported devices found" | |
101 sys.exit(-1) |