-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
514 lines (408 loc) Β· 12.9 KB
/
Copy pathtest.py
File metadata and controls
514 lines (408 loc) Β· 12.9 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# TinyPICO -- TP IPS shield
# gnd ------- gnd
# 3v3 ------- vcc
# 18 sck ---- sck
# 23 mosi --- mosi
# n/c ------- res (no hw rst)
# 15 -------- dc
# 5 --------- cs
# 14 -------- blk
from machine import Pin, SPI
from st7735r import ST7735R
import seriffont
# 6x8 (doesnt look very good scaled)
import sysfont
# 5x8
import terminalfont
# 6x8
dc = Pin(15, Pin.OUT)
cs = Pin(5, Pin.OUT)
bl = Pin(14, Pin.OUT, value=1)
spi = SPI(1, baudrate=8000000, polarity=1, phase=1) # blue and green tab work
# spi = SPI(1, baudrate=8000000, polarity=1, phase=0) # green tab works, blue does not
# spi = SPI(1, baudrate=8000000, polarity=0, phase=0) # both blue and green tab work
# spi = SPI(1, baudrate=8000000, polarity=0, phase=1) # blue works, green does not
# works with green and blue tab UM IPS LCDs
tft = ST7735R(spi, dc, cs, None, w=80, h=160, x=26, y=1, rot=0, inv=True, bgr=True)
tft.init()
# --------------------
# colours
# some colours generated by tft.color565() used in many examples below
black = 0x0000
red = 0xF800
green = 0x07E0
blue = 0x001F
cyan = 0x07FF
magenta = 0xF81F
yellow = 0xFFE0
white = 0xFFFF
tft.fill(red)
tft.fill(green)
tft.fill(blue)
tft.fill(black)
tft.fill_slow(red)
tft.fill_slow(green)
tft.fill_slow(blue)
tft.fill_slow(black)
# show refresh rate / flicker
for i in range(5):
tft.fill(red)
tft.fill(green)
tft.fill(blue)
tft.fill(black)
# --------------------
# rotation
# when you call tft.rotate(n) whats already on screen doesnt change
# the next time you draw something, it will use the new x/y coords / inc directions
# r0 portrait, green/blue tab at bottom
tft.rotate(0)
tft.fill(black)
tft.line(0, 0, 79, 159, red)
tft.line(0, 159, 79, 0, green)
tft.fill(black)
tft.rect(0, 0, 10, 10, green) # near IO23
tft.fill(black)
tft.text(0, 0, '0', sysfont, white, 2)
tft.text(58, 0, '79', sysfont, white, 2)
tft.text(0, 144, '159', sysfont, white, 2)
# r1 landscape, green/blue tab at bottom
tft.rotate(1)
tft.fill(black)
tft.line(0, 0, 159, 79, blue)
tft.line(0, 79, 159, 0, white)
tft.fill(black)
tft.rect(0, 0, 10, 10, green) # near BAT
tft.fill(black)
tft.text(0, 0, '0', sysfont, white, 2)
tft.text(0, 58, '79', sysfont, white, 2)
tft.text(127, 0, '159', sysfont, white, 2)
# r2 portait, green/blue tab at top
tft.rotate(2)
tft.fill(black)
tft.line(0, 0, 79, 159, red)
tft.line(0, 159, 79, 0, blue)
tft.fill(black)
tft.rect(0, 0, 10, 10, green) # near IO25
tft.fill(black)
tft.text(0, 0, '0', sysfont, white, 2)
tft.text(58, 0, '79', sysfont, white, 2)
tft.text(0, 144, '159', sysfont, white, 2)
# r3 landscape, green/blue tab at top
tft.rotate(3)
tft.fill(black)
tft.line(0, 0, 159, 79, green)
tft.line(0, 79, 159, 0, blue)
tft.fill(black)
tft.rect(0, 0, 10, 10, green) # near RESET
tft.fill(black)
tft.text(0, 0, '0', sysfont, white, 2)
tft.text(0, 58, '79', sysfont, white, 2)
tft.text(127, 0, '159', sysfont, white, 2)
# --------------------
# circle (filled)
tft.rotate(0)
tft.fill(black)
tft.circle(20,20,20,red)
tft.circle(40,20,20,green)
tft.circle(60,20,20,blue)
tft.fill(black)
tft.circle(40,40,40,red)
tft.circle(40,120,40,green)
tft.fill(black)
tft.circle(40,40,40,red)
tft.circle(40,40,30,green)
tft.circle(40,40,20,blue)
tft.circle(40,40,10,white)
# --------------------
# circle_outline
tft.rotate(0)
tft.fill(black)
tft.circle_outline(20,20,20,red)
tft.circle_outline(40,20,20,green)
tft.circle_outline(60,20,20,blue)
tft.fill(black)
tft.circle_outline(40,40,40,red)
tft.circle_outline(40,120,40,green)
tft.fill(black)
tft.circle_outline(40,40,40,red)
tft.circle_outline(40,40,30,green)
tft.circle_outline(40,40,20,blue)
tft.circle_outline(40,40,10,white)
# --------------------
# triangle
tft.rotate(0)
tft.fill(black)
tft.triangle_outline(0,0,0,30,30,30,red)
tft.triangle_outline(0,70,40,60,50,30,green)
tft.triangle_outline(40,80,79,0,60,120,blue)
tft.fill(black)
tft.triangle_outline(0,79,79,0,60,120,red)
tft.triangle_outline(10,89,59,10,70,100,green)
tft.triangle_outline(20,99,39,20,80,80,blue)
# --------------------
# pixel
tft.rotate(0)
tft.fill(black)
tft.pixel(0, 0, red)
tft.pixel(1, 1, red)
tft.pixel(2, 2, red)
tft.pixel(3, 3, red)
tft.pixel(4, 4, red)
tft.fill(black)
tft.pixel(0, 0, red)
tft.pixel(79, 0, green)
tft.pixel(79, 159, blue)
tft.pixel(0, 159, white)
# --------------------
# random static pixels (slow)
import random
import time
tft.rotate(0)
tft._set_window(0, 0, 79, 159)
# 160*80 = 12800
for i in range(12800):
color = random.getrandbits(5) << 11 | random.getrandbits(6) << 5 | random.getrandbits(5)
tft.data(bytearray([color >> 8, color]))
# --------------------
# rect
tft.fill(black)
tft.rect(0, 0, 20, 20, red)
tft.rect(59, 0, 20, 20, green)
tft.rect(59, 139, 20, 20, blue)
tft.rect(0, 139, 20, 20, white)
tft.fill(black)
tft.rect(0, 0, 50, 50, red)
tft.rect(10, 10, 50, 50, green)
tft.rect(20, 20, 50, 50, blue)
# --------------------
# rect_outline
# basically just 4 lines
tft.fill(black)
tft.rect_outline(0, 0, 20, 20, red)
tft.rect_outline(59, 0, 20, 20, green)
tft.rect_outline(59, 139, 20, 20, blue)
tft.rect_outline(0, 139, 20, 20, white)
tft.fill(black)
tft.rect_outline(0, 0, 50, 50, red)
tft.rect_outline(10, 10, 50, 50, green)
tft.rect_outline(20, 20, 50, 50, blue)
# --------------------
# line
tft.rotate(0)
tft.fill(black)
tft.line(0, 0, 79, 159, red)
tft.line(0, 159, 79, 0, green)
tft.rotate(1)
tft.fill(black)
tft.line(0, 0, 159, 79, blue)
tft.line(0, 79, 159, 0, white)
tft.rotate(0)
# --------------------
# hline + vline
tft.fill(black)
tft.hline(20, 20, 40, green)
tft.hline(20, 60, 40, green)
tft.vline(20, 20, 40, green)
tft.vline(60, 20, 40, green)
# note: these call _set_window()
# if you're writing data() later, you will need to re-set the window dimensions
# --------------------
# framebuf landscape
import framebuf
buf = bytearray(160 * 80 * 2)
fbuf = framebuf.FrameBuffer(buf, 160, 80, framebuf.RGB565)
fbuf.fill(0)
fbuf.text('Red', 5, 5, red) # shows as blue
fbuf.text('Green', 5, 18, green) # shows as red
fbuf.text('Blue', 5, 31, blue) # shows as green
tft.fill(black)
tft.rotate(1)
tft._set_window(0, 0, 159, 79)
tft.data(buf)
tft.fill(black)
tft.rotate(3)
tft._set_window(0, 0, 159, 79)
tft.data(buf)
# it seems the byte order is reversed in the framebuf
# rgb656 red = 0xF800
# framebuf red = b'\x00\xF8'
# --------------------
# framebuf portrait
import framebuf
buf = bytearray(80 * 160 * 2)
fbuf = framebuf.FrameBuffer(buf, 80, 160, framebuf.RGB565)
fbuf.fill(0)
fbuf.text('Red', 5, 5, red) # shows as blue
fbuf.text('Green', 5, 18, green) # shows as red
fbuf.text('Blue', 5, 31, blue) # shows as green
tft.fill(black)
tft.rotate(0)
tft._set_window(0, 0, 79, 159)
tft.data(buf)
tft.fill(black)
tft.rotate(2)
tft._set_window(0, 0, 79, 159)
tft.data(buf)
# --------------------
# fonts
# r3 landscape green/blue tab at top
# demo: just like the UM printed card
tft.rotate(3)
tft.fill(black)
tft.text(16, 2, '160 x 80', sysfont, white, 3)
tft.text(30, 29, 'CO', sysfont, red, 3)
tft.text(32+32, 29, 'LO', sysfont, green, 3)
tft.text(33+64, 29, 'UR', sysfont, blue, 3)
tft.text(0, 56, 'ST7735 LCD', sysfont, white, 3)
# same, but looks shit at 2x, reduced scale from 3x to fit
tft.rotate(3)
tft.fill(black)
tft.text(15+16+8, 2, '160x80', seriffont, white, 2)
tft.text(15+30-4, 29, 'CO', seriffont, red, 2)
tft.text(15+32+24, 29, 'LO', seriffont, green, 2)
tft.text(15+33+48+4, 29, 'UR', seriffont, blue, 2)
tft.text(15+0, 56, 'ST7735 LCD', seriffont, white, 2)
# same again, looks better, scaled to 2x to fit
tft.rotate(3)
tft.fill(black)
tft.text(15+16+8, 2, '160x80', terminalfont, white, 2)
tft.text(15+30-4, 29, 'CO', terminalfont, red, 2)
tft.text(15+32+24-4, 29, 'LO', terminalfont, green, 2)
tft.text(15+33+48-4, 29, 'UR', terminalfont, blue, 2)
tft.text(15+0, 56, 'ST7735 LCD', terminalfont, white, 2)
# --------------------
# draw 3735928559 in white
tft.fill(black)
tft.text(0, 0, str(0xDEADBEEF), sysfont, white, 2)
# --------------------
# lots of small text in white, wraps, some text offscreen
tft.rotate(3)
tft.fill(black)
tft.text(0, 0, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere.', terminalfont, white, 1)
# text clipped here ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------>|
# --------------------
# fits 198 chars on screen
tft.rotate(3)
tft.fill(black)
tft.text(0, 0, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed porttit', terminalfont, white, 1)
# --------------------
# fits 10 lines (80/8=10)
tft.rotate(3)
tft.fill(black)
for i in range(10):
tft.text(0, 8*i, f'Line {i+1}', terminalfont, white, 1)
# fits 22 chars across (160/7=22.8571428571)
tft.fill(black)
tft.text(0, 0, '0123456789', terminalfont, red, 1)
tft.text(9*8, 8, '0123456789', terminalfont, green, 1)
tft.text(18*8, 0, '01', terminalfont, blue, 1)
# --------------------
# when you draw text and it wraps, x doesnt reset to 0
tft.fill(black)
tft.text(18*8, 0, '0123456789', terminalfont, blue, 1)
# ...unless you tell it to with that last arg (x reset pos on next line)
tft.fill(black)
tft.text(18*8, 0, 'Hello World', terminalfont, blue, 1, 0)
tft.fill(black)
tft.text(18*8, 0, 'Hello World', terminalfont, blue, 1, 20)
# --------------------
# seriffont @ 1x
tft.rotate(3)
tft.fill(black)
tft.text(0, 0, '01234567890123456789012', seriffont, green, 1) # fits 22 chars landscape
tft.fill(black)
tft.text(0, 0, 'Hello', seriffont, green, 1)
tft.text(0, 1*8, 'World', seriffont, green, 1)
# seriffont @ 2x
tft.fill(black)
tft.text(0, 0, '01234567890123456789012', seriffont, green, 2) # fits 12 chars landscape
tft.fill(black)
tft.text(0, 0, 'Hello', seriffont, green, 2)
tft.text(0, 2*8, 'World', seriffont, green, 2)
# seriffont @ 3x
tft.fill(black)
tft.text(0, 0, '01234567890123456789012', seriffont, green, 3) # fits 8 chars landscape
tft.fill(black)
tft.text(0, 0, 'Hello', seriffont, green, 3)
tft.text(0, 3*8, 'World', seriffont, green, 3)
# --------------------
# sysfont @ 1x
tft.fill(black)
tft.text(0, 0, '012345678901234567890123456789', sysfont, green, 1) # fits 26 chars landscape
tft.fill(black)
tft.text(0, 0, 'Hello', sysfont, green, 1)
tft.text(0, 1*8, 'World', sysfont, green, 1)
# sysfont @ 2x
tft.fill(black)
tft.text(0, 0, '01234567890123456789012', sysfont, green, 2) # fits 14 chars landscape
tft.fill(black)
tft.text(0, 0, 'Hello', sysfont, green, 2)
tft.text(0, 2*8, 'World', sysfont, green, 2)
# sysfont @ 3x
tft.fill(black)
tft.text(0, 0, '01234567890123456789012', sysfont, green, 3) # fits 10 chars landscape
tft.fill(black)
tft.text(0, 0, 'Hello', sysfont, green, 3)
tft.text(0, 3*8, 'World', sysfont, green, 3)
# --------------------
# terminalfont @ 1x
tft.fill(black)
tft.text(0, 0, '01234567890123456789012', terminalfont, green, 1) # fits 22 chars landscape
# terminalfont @ 2x
tft.fill(black)
tft.text(0, 0, '01234567890123456789012', terminalfont, green, 2) # fits 12 chars landscape
# terminalfont @ 3x
tft.fill(black)
tft.text(0, 0, '01234567890123456789012', terminalfont, green, 3) # fits 8 chars landscape
# --------------------
# invert
tft.invert(0) # colors look wrong
tft.invert(1) # colors look good
# --------------------
# rgb565 (5 red bits, 6 green bits, 5 blue bits) 0b_RRRR_RGGG_GGGB_BBBB
tft.color565(0,0,0) # black 0x0000 0 0b_0000_0000_0000_0000
tft.color565(255,0,0) # red 0xF800 63488 0b_1111_1000_0000_0000
tft.color565(0,255,0) # green 0x07E0 2016 0b_0000_0111_1110_0000
tft.color565(0,0,255) # blue 0x001F 31 0b_0000_0000_0001_1111
tft.color565(0,255,255) # cyan 0x07FF 2047 0b_0000_0111_1111_1111
tft.color565(255,0,255) # magenta 0xF81F 63519 0b_1111_1000_0001_1111
tft.color565(255,255,0) # yellow 0xFFE0 65504 0b_1111_1111_1110_0000
tft.color565(255,255,255) # white 0xFFFF 65535 0b_1111_1111_1111_1111
# --------------------
# compare the fill methods
# tft.fill()
# tft.fill_slow()
# tft.fill_bulk
import time
def speed_test_fill():
start = time.ticks_ms()
tft.fill(red)
tft.fill(green)
tft.fill(blue)
tft.fill(black)
print(time.ticks_ms() - start)
def speed_test_fill_slow():
start = time.ticks_ms()
tft.fill_slow(red)
tft.fill_slow(green)
tft.fill_slow(blue)
tft.fill_slow(black)
print(time.ticks_ms() - start)
def speed_test_fill_bulk():
start = time.ticks_ms()
tft.fill_bulk(red)
tft.fill_bulk(green)
tft.fill_bulk(blue)
tft.fill_bulk(black)
print(time.ticks_ms() - start)
# tested on TinyPICO USB-C V2C2 - MicroPython v1.22.1
speed_test_fill()
# 189
# 193
speed_test_fill_slow()
# 2596
# 2563
speed_test_fill_bulk()
# 133
# 141
# --------------------