-
Notifications
You must be signed in to change notification settings - Fork 63
/
tft.py
234 lines (208 loc) · 5.92 KB
/
tft.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
#testing code for pyb.TFT
import pyb
from sysfont import sysfont
from seriffont import seriffont
from terminalfont import terminalfont
def randcolor( ) :
r = pyb.rng() & 0xFF
g = pyb.rng() & 0xFF
b = pyb.rng() & 0xFF
return pyb.TFT.color(r, g, b)
def testpixel( display ) :
print('testing pixels')
displaysize = display.size()
r = 255
x = -10
g = 0
b = 0
for y in range(-10, displaysize[1] + 10) :
display.pixel((x, y), pyb.TFT.color(r, g, b))
x += 1
g += 2
b += 1
for i in range(100):
x = pyb.rng() % displaysize[0]
y = pyb.rng() % displaysize[1]
display.pixel((x,y), randcolor())
pyb.delay(2000)
def testline( display ) :
print('testing line')
displaysize = display.size()
start = (int(displaysize[0] / 2), int(displaysize[1] / 2))
px = 0
py = 0
def draw(x, y) :
display.line(start, (x, y), randcolor())
for x in range(displaysize[0]) :
draw(px, py)
px += 1
for y in range(displaysize[1]) :
draw(px, py)
py += 1
for x in range(displaysize[0]) :
draw(px, py)
px -= 1
for y in range(displaysize[1]) :
draw(px, py)
py -= 1
pyb.delay(2000)
def testrect( display ) :
print('testing rect')
displaysize = display.size()
size = (20, 10)
p0 = (0, 0)
p1 = (displaysize[0] - size[0], p0[1])
p2 = (p1[0], displaysize[1] - size[1])
p3 = (p0[0], p2[1])
#fillrect at center, top left and bottom right
display.fillrect(p0, size, display.BLUE)
display.fillrect(p1, size, display.GRAY)
display.fillrect(p2, size, display.PURPLE)
display.fillrect(p3, size, display.NAVY)
#now do border rect as well
display.rect(p0, size, display.CYAN)
display.rect(p1, size, display.WHITE)
display.rect(p2, size, display.YELLOW)
display.rect(p3, size, display.FOREST)
#try negative sizes
size = (-10, -10)
center = (int((displaysize[0] / 2) - (size[0] / 2)), int((displaysize[1] / 2) - (size[1] / 2)))
display.fillrect(center, size, display.WHITE)
pyb.delay(1000)
display.rect(center, size, display.RED)
pyb.delay(1000)
size = (displaysize[0] * 2, 50)
pos = (-displaysize[0], center[1])
display.fillrect(pos, size, display.GREEN)
display.rect(pos, size, display.WHITE)
pyb.delay(2000)
def testcircle( display ) :
print('testing circle')
displaysize = display.size()
radius = 20
p0 = (radius, radius)
p1 = (displaysize[0] - radius, p0[1])
p2 = (p1[0], displaysize[1] - radius)
p3 = (p0[0], p2[1])
#draw filled circle win upper right, center and lower left
display.fillcircle(p0, radius, display.BLUE)
display.fillcircle(p1, radius, display.GRAY)
display.fillcircle(p2, radius, display.PURPLE)
display.fillcircle(p3, radius, display.NAVY)
#Now do border.
display.circle(p0, radius, display.CYAN)
display.circle(p1, radius, display.MAROON)
display.circle(p2, radius, display.YELLOW)
display.circle(p3, radius, display.FOREST)
pyb.delay(2000)
center = ((displaysize[0] >> 1), (displaysize[1] >> 1))
#try negative radius
display.fillcircle(center, -radius, display.WHITE)
pyb.delay(2000)
display.circle(center, -radius, display.GREEN)
#tedt big circle.
display.fillcircle(center, 90, display.WHITE)
pyb.delay(2000)
display.circle(center, 90, display.GREEN)
display.fill(0)
#draw near edge to test clipping.
pos = (center[0], 0)
display.fillcircle(pos, 30, display.RED)
display.circle(pos, 30, display.PURPLE)
pos = (center[0], displaysize[1] - 1)
display.fillcircle(pos, 30, display.RED)
display.circle(pos, 30, display.PURPLE)
pos = (0, center[1])
display.fillcircle(pos, 30, display.RED)
display.circle(pos, 30, display.PURPLE)
pos = (displaysize[0] - 1, center[1])
display.fillcircle(pos, 30, display.RED)
display.circle(pos, 30, display.PURPLE)
pyb.delay(2000)
def testtext( display ) :
print('testing text')
displaysize = display.size()
txt = "Testing Text"
fontA = [None, sysfont, seriffont, terminalfont]
def draw( ) :
x = 0
f = 0
for y in range(0, display.size()[1], 10) :
display.text((x, y), txt, pyb.TFT.CYAN, fontA[f])
x += 2
f = (f + 1) % len(fontA)
#draw text
draw()
pyb.delay(2000)
display.rotation(1)
display.fill(0)
draw()
pyb.delay(2000)
#try passing bogus font dict
bogus = { "Hello": 1, "Width": 8 }
try:
display.text((0, 0), txt, pyb.TFT.GREEN, bogus)
except Exception as e :
print("Bogus font failed with:")
print(e)
pyb.delay(2000)
display.fill(0)
display.rotation(3)
#try different scales
display.text((0, 0), txt, pyb.TFT.GREEN, fontA[0], 2)
display.text((0, 20), txt, pyb.TFT.BLUE, fontA[1], (2, 1))
display.text((0, 30), txt, pyb.TFT.PURPLE, fontA[2], (1, 2))
display.rotation(2)
#try negative scales
display.text((50, 50), txt, pyb.TFT.YELLOW, fontA[1], -1)
display.rotation(0)
pyb.delay(2000)
def testfill( display ) :
print("testing fill")
display.fill(pyb.TFT.GREEN)
pyb.delay(2000)
display.rotation(1)
display.fill(pyb.TFT.RED)
pyb.delay(2000)
display.rotation(2)
display.fill(pyb.TFT.BLACK)
#left at rotation 2.
def testrgb( display ) :
print("bgr")
display.rgb(False) #bgr
pyb.delay(2000)
print("rgb")
display.rgb(True) #rgb
pyb.delay(1000)
def testinvert( display ) :
print("Invert Color")
display.invertcolor(True) #invert color
pyb.delay(2000)
display.invertcolor(False)
pyb.delay(2000)
def testonoff( display ) :
print("Display on/off")
display.on(False)
pyb.delay(2000)
display.on(True)
pyb.delay(2000)
def run( display ) :
testdisplay = display
#inits?
# display.initg()
displaysize = display.size()
#draw pixels all over, try out of range values as well.
testpixel(display)
#draw lines in 360 deg at center, top left and bottom right
testline(display)
#fill using difference colors and different rotations.
testfill(display)
testtext(display)
display.fill(0)
testrect(display)
testcircle(display)
testinvert(display)
testrgb(display)
#off/on
testonoff(display)
print("Test Done")