-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpycrafter6500.py
326 lines (223 loc) · 7.81 KB
/
pycrafter6500.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import usb.core
import usb.util
import time
import numpy
import sys
from erle import encode
##function that converts a number into a bit string of given length
def convlen(a,l):
b=bin(a)[2:]
padding=l-len(b)
b='0'*padding+b
return b
##function that converts a bit string into a given number of bytes
def bitstobytes(a):
bytelist=[]
if len(a)%8!=0:
padding=8-len(a)%8
a='0'*padding+a
for i in range(len(a)//8):
bytelist.append(int(a[8*i:8*(i+1)],2))
bytelist.reverse()
return bytelist
##a dmd controller class
class dmd():
def __init__(self):
self.dev=usb.core.find(idVendor=0x0451 ,idProduct=0xc900 )
self.dev.set_configuration()
self.ans=[]
## standard usb command function
def command(self,mode,sequencebyte,com1,com2,data=None):
buffer = []
flagstring=''
if mode=='r':
flagstring+='1'
else:
flagstring+='0'
flagstring+='1000000'
buffer.append(bitstobytes(flagstring)[0])
buffer.append(sequencebyte)
temp=bitstobytes(convlen(len(data)+2,16))
buffer.append(temp[0])
buffer.append(temp[1])
buffer.append(com2)
buffer.append(com1)
if len(buffer)+len(data)<65:
for i in range(len(data)):
buffer.append(data[i])
for i in range(64-len(buffer)):
buffer.append(0x00)
self.dev.write(1, buffer)
else:
for i in range(64-len(buffer)):
buffer.append(data[i])
self.dev.write(1, buffer)
buffer = []
j=0
while j<len(data)-58:
buffer.append(data[j+58])
j=j+1
if j%64==0:
self.dev.write(1, buffer)
buffer = []
if j%64!=0:
while j%64!=0:
buffer.append(0x00)
j=j+1
self.dev.write(1, buffer)
self.ans=self.dev.read(0x81,64)
## functions for checking error reports in the dlp answer
def checkforerrors(self):
self.command('r',0x22,0x01,0x00,[])
if self.ans[6]!=0:
print (self.ans[6])
## function printing all of the dlp answer
def readreply(self):
for i in self.ans:
print (hex(i))
## functions for idle mode activation
def idle_on(self):
self.command('w',0x00,0x02,0x01,[int('00000001',2)])
self.checkforerrors()
def idle_off(self):
self.command('w',0x00,0x02,0x01,[int('00000000',2)])
self.checkforerrors()
## functions for power management
def standby(self):
self.command('w',0x00,0x02,0x00,[int('00000001',2)])
self.checkforerrors()
def wakeup(self):
self.command('w',0x00,0x02,0x00,[int('00000000',2)])
self.checkforerrors()
def reset(self):
self.command('w',0x00,0x02,0x00,[int('00000010',2)])
self.checkforerrors()
## test write and read operations, as reported in the dlpc900 programmer's guide
def testread(self):
self.command('r',0xff,0x11,0x00,[])
self.readreply()
def testwrite(self):
self.command('w',0x22,0x11,0x00,[0xff,0x01,0xff,0x01,0xff,0x01])
self.checkforerrors()
## some self explaining functions
def changemode(self,mode):
self.command('w',0x00,0x1a,0x1b,[mode])
self.checkforerrors()
def startsequence(self):
self.command('w',0x00,0x1a,0x24,[2])
self.checkforerrors()
def pausesequence(self):
self.command('w',0x00,0x1a,0x24,[1])
self.checkforerrors()
def stopsequence(self):
self.command('w',0x00,0x1a,0x24,[0])
self.checkforerrors()
def configurelut(self,imgnum,repeatnum):
img=convlen(imgnum,11)
repeat=convlen(repeatnum,32)
string=repeat+'00000'+img
bytes=bitstobytes(string)
self.command('w',0x00,0x1a,0x31,bytes)
self.checkforerrors()
def definepattern(self,index,exposure,bitdepth,color,triggerin,darktime,triggerout,patind,bitpos):
payload=[]
index=convlen(index,16)
index=bitstobytes(index)
for i in range(len(index)):
payload.append(index[i])
exposure=convlen(exposure,24)
exposure=bitstobytes(exposure)
for i in range(len(exposure)):
payload.append(exposure[i])
optionsbyte=''
optionsbyte+='1'
bitdepth=convlen(bitdepth-1,3)
optionsbyte=bitdepth+optionsbyte
optionsbyte=color+optionsbyte
if triggerin:
optionsbyte='1'+optionsbyte
else:
optionsbyte='0'+optionsbyte
payload.append(bitstobytes(optionsbyte)[0])
darktime=convlen(darktime,24)
darktime=bitstobytes(darktime)
for i in range(len(darktime)):
payload.append(darktime[i])
triggerout=convlen(triggerout,8)
triggerout=bitstobytes(triggerout)
payload.append(triggerout[0])
patind=convlen(patind,11)
bitpos=convlen(bitpos,5)
lastbits=bitpos+patind
lastbits=bitstobytes(lastbits)
for i in range(len(lastbits)):
payload.append(lastbits[i])
self.command('w',0x00,0x1a,0x34,payload)
self.checkforerrors()
def setbmp(self,index,size):
payload=[]
index=convlen(index,5)
index='0'*11+index
index=bitstobytes(index)
for i in range(len(index)):
payload.append(index[i])
total=convlen(size,32)
total=bitstobytes(total)
for i in range(len(total)):
payload.append(total[i])
self.command('w',0x00,0x1a,0x2a,payload)
self.checkforerrors()
## bmp loading function, divided in 56 bytes packages
## max hid package size=64, flag bytes=4, usb command bytes=2
## size of package description bytes=2. 64-4-2-2=56
def bmpload(self,image,size):
packnum=size//504+1
counter=0
for i in range(packnum):
if i %100==0:
print (i,packnum)
payload=[]
if i<packnum-1:
leng=convlen(504,16)
bits=504
else:
leng=convlen(size%504,16)
bits=size%504
leng=bitstobytes(leng)
for j in range(2):
payload.append(leng[j])
for j in range(bits):
payload.append(image[counter])
counter+=1
self.command('w',0x11,0x1a,0x2b,payload)
self.checkforerrors()
def defsequence(self,images,exp,ti,dt,to,rep):
self.stopsequence()
arr=[]
for i in images:
arr.append(i)
## arr.append(numpy.ones((1080,1920),dtype='uint8'))
num=len(arr)
encodedimages=[]
sizes=[]
for i in range((num-1)//24+1):
print ('merging...')
if i<((num-1)//24):
imagedata=arr[i*24:(i+1)*24]
else:
imagedata=arr[i*24:]
print ('encoding...')
imagedata,size=encode(imagedata)
encodedimages.append(imagedata)
sizes.append(size)
if i<((num-1)//24):
for j in range(i*24,(i+1)*24):
self.definepattern(j,exp[j],1,'111',ti[j],dt[j],to[j],i,j-i*24)
else:
for j in range(i*24,num):
self.definepattern(j,exp[j],1,'111',ti[j],dt[j],to[j],i,j-i*24)
self.configurelut(num,rep)
for i in range((num-1)//24+1):
self.setbmp((num-1)//24-i,sizes[(num-1)//24-i])
print ('uploading...')
self.bmpload(encodedimages[(num-1)//24-i],sizes[(num-1)//24-i])