-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpicopythonhub75.py
More file actions
270 lines (212 loc) · 6.92 KB
/
Copy pathpicopythonhub75.py
File metadata and controls
270 lines (212 loc) · 6.92 KB
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
import rp2
import time
from machine import Pin
import array
import math
import _thread
import random
#TODO
#* work out how to place pixels in different places
#* buffering??
clock_pin = 16
data_pin_start = 0
latch_pin_start = 12
row_pin_start = 6
row_bits = 3
data_bits = 192 # note must be divisible by 32
row_ar_len = 50
@rp2.asm_pio(out_shiftdir=1, autopull=True, pull_thresh=24, out_init=(rp2.PIO.OUT_HIGH, rp2.PIO.OUT_LOW, rp2.PIO.OUT_HIGH,
rp2.PIO.OUT_HIGH, rp2.PIO.OUT_HIGH, rp2.PIO.OUT_HIGH), sideset_init=(rp2.PIO.OUT_LOW))
def data_hub75():
#pull # actually, can I use autopull?
#is this a bit weird? better to use nops with side set?
#really simple clocking out of pins?
#not 100% sure how this will work as pulling in 32 bits at a time, but out won't line up with this?
#do I need some dummy bits?
#wrap_target()
#nop() .side(0)
out(pins, 6)
nop() .side(1)
nop() .side(0)
out(pins, 6)
nop() .side(1)
nop() .side(0)
out(pins, 6)
nop() .side(1)
nop() .side(0)
out(pins, 6)
nop() .side(1)
nop() .side(0)
#wrap()
@rp2.asm_pio(out_shiftdir=1, autopull=False,out_init=(rp2.PIO.OUT_LOW,rp2.PIO.OUT_LOW,rp2.PIO.OUT_LOW,
rp2.PIO.OUT_LOW,rp2.PIO.OUT_LOW), sideset_init=(rp2.PIO.OUT_LOW, rp2.PIO.OUT_LOW))
def row_hub75():
wrap_target()
pull()
nop() .side(2)
out(pins, 5) [2]#note 3 as only three pins needed for 32x32 screen. This might be wrong. Might needs all five still
nop() .side(3) #this triggers the latch
nop() .side(2) #this disables the latch
nop() .side(0)
#setup state machines
#can I run them at full tilt, or do they need slowing down?
sm_data = rp2.StateMachine(0,data_hub75, out_base=Pin(data_pin_start), sideset_base=Pin(clock_pin), freq=55000000)
sm_row = rp2.StateMachine(1, row_hub75, out_base=Pin(row_pin_start),sideset_base=Pin(latch_pin_start), freq=1000000)
sm_row.active(1)
sm_data.active(1)
counter = 0
toggle = False
rows = []
blocks_per_row = const(8) #each block has 24 bits, so 4 pixels x 2 scanlines
num_rows = const(16)
fast_buffer1 = array.array("I")
fast_buffer2 = array.array("I")
for i in range(blocks_per_row * num_rows):
fast_buffer1.append(0)
fast_buffer2.append(0)
drawBuffer = fast_buffer1
frameBuffer = fast_buffer2
#fill with white
'''
for j in range(num_rows):
rows.append([])
for i in range(blocks_per_row):
rows[j].append(0xffffffff)
'''
#fill with black
for j in range(num_rows):
rows.append([])
for i in range(blocks_per_row):
rows[j].append(0x00000000)
# There are 32x32 3-bit values for each panel.
# Pins r1g1b1 control scanlines 0-15 while pins r2g2b2 control scanlines 16-31
# We want to write 24-bits at a time so we can write out 4 pixels per scanline at a time
# 1st pixel 0th scanline would be the lowest bits (0-2)
# 1st pixel 16th scanline would be the next bits (3-5)
# 2nd pixel 0th scanline would be the next lowest bits (6-8)
# 2nd pixel 16th scanline would be the next lowest bits (9-11)
# continue this pattern to fill our 24-bits
@micropython.viper
def set_pixel(x:int, y:int, rgb:uint):
bit_posn = ((x % 4) * 6)
if y > 15:
y = y - 17
bit_posn += 3
else:
y = y - 1
# a mystery but rows 0/16 need to use y = 15 to be drawn correctly
if y == -1:
y = 15
index = y * blocks_per_row + int(x >> 2)
val = uint(drawBuffer[index])
drawBuffer[index] = val | uint(rgb << (bit_posn))
def light_xy(x,y, r, g, b):
rgb = (r << 0) | (g << 1) | (b << 2);
set_pixel(x, y, rgb)
#p-shape
#should these really be stored as datapoints?
def p_draw(init_x, init_y, r, g, b):
#line 10 pixels high
for i in range(10):
light_xy(init_x, init_y+i, r, g, b)
#line 4 pixesl across
for i in range(4):
light_xy(init_x+i, init_y, r, g, b)
for i in range(4):
light_xy(init_x+i, init_y+4, r, g, b)
for i in range(3):
light_xy(init_x+4, init_y+i+1, r, g, b)
def i_draw(init_x, init_y, r, g, b):
for i in range(4):
light_xy(init_x, init_y+i+2, r, g, b)
light_xy(init_x, init_y, r, g, b)
def c_draw(init_x, init_y, r, g, b):
for i in range(4):
light_xy(init_x, init_y+i+1, r, g, b)
for i in range(3):
light_xy(init_x+1+i, init_y, r, g, b)
light_xy(init_x+1+i, init_y+5, r, g, b)
def o_draw(init_x, init_y, r, g, b):
for i in range(4):
light_xy(init_x, init_y+i+1, r, g, b)
light_xy(init_x+4, init_y+i+1, r, g, b)
for i in range(3):
light_xy(init_x+1+i, init_y, r, g, b)
light_xy(init_x+1+i, init_y+5, r, g, b)
def clearBuffer():
# reusing should be same or faster than reallocations
#for j in range(num_rows):
# for i in range(blocks_per_row):
# rows[j][i] = 0
for i in range(num_rows * blocks_per_row):
drawBuffer[i] = 0
text_y = 14
direction = 1
def draw_text():
global text_y
global direction
global writing
global current_rows
global rows
writing = True
text_y = text_y + direction
if text_y > 20: direction = -1
if text_y < 5: direction = 1
clearBuffer()
p_draw(3, text_y-4, 1, 1, 1)
i_draw(9, text_y, 1, 1, 0)
c_draw(11, text_y, 0, 1, 1)
o_draw(16, text_y, 1, 0, 1)
writing = False
#draw_text()
draw_counter = 0
writing = False
out_rows = rows
def draw_performance():
global writing
global rows
writing = True
start = time.ticks_us()
counter = random.randint(1, 7);
clearBuffer()
for j in range(32):
for i in range(32):
set_pixel(i, j, 1 + counter % 6)
counter += 1
end = time.ticks_us()
usecs = time.ticks_diff(end, start)
print(usecs / 1000.0)
writing = False
def draw_test_pattern():
global writing
global rows
writing = True
clearBuffer()
for i in range(0,32):
# draw random selection of rows/colums using all colors
light_xy(i, 31, 0, 0, 1)
light_xy(i, 0, 0, 1, 0)
light_xy(i, 16, 0, 1, 1)
light_xy(0, i, 1, 0, 0)
light_xy(5, i, 1, 0, 1)
light_xy(15, i, 1, 1, 0)
light_xy(25, i, 1, 1, 1)
light_xy(30, i, 1, 0, 0)
writing = False
while(True):
sm_row.put(counter)
# Write out 8 integers that hold 8 pixels worth of 3-bit RGB (two scanlines x four pixels)
baseIndex = counter * 8
for i in range(8):
val = frameBuffer[baseIndex + i]
sm_data.put(val)
#sm_data.put(out_rows[counter][i])
counter = counter +1
if (counter > 15):
counter = 0
if writing == False:
# perform our double buffering
tempBuffer = frameBuffer
frameBuffer = drawBuffer
drawBuffer = tempBuffer
_thread.start_new_thread(draw_text, ())