annotate frame-ctrl.py @ 8:66c77f9ba9b9

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

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.