-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsh1107 demo v319 i2c 128x128.py
306 lines (268 loc) · 7.1 KB
/
sh1107 demo v319 i2c 128x128.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
# sh1107 driver demo code v319
# this code is intended for a 128*128 pixel display
print('starting test')
from machine import Pin, I2C, SoftI2C
import sh1107
print('dir sh1107: ', dir(sh1107))
import gc
import sys
import time #as time
import framebuf
import array
# # basic test code I2C
# i2c0 = SoftI2C(scl=Pin(5), sda=Pin(4), freq=400000)
i2c0 = I2C(0, scl=Pin(5), sda=Pin(4), freq=400000)
print('I2C scan: ',i2c0.scan())
# display = sh1107.SH1107_I2C(128, 128, i2c0, Pin(16), address=0x3d, rotate=90)
display = sh1107.SH1107_I2C(128, 128, i2c0, address=61, rotate=0)
time.sleep(0.5)
# display.sleep(False)
display.fill(0)
display.text('SH1107', 0, 0, 1)
display.text('driver', 0, 8, 1)
display.show()
time.sleep(1)
display.fill(0)
display.show()
# full test code
print('version ',sys.implementation)
print('Initial free memory: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
i2c0 = I2C(0, scl=Pin(5), sda=Pin(4), freq=400000)
print('I2C scan: ',i2c0.scan())
print('I2C created: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
display = sh1107.SH1107_I2C(128, 128, i2c0, address=61, rotate=90)
# display = sh1107.SH1107_I2C(128, 128, i2c0, Pin(16), address=0x3d, rotate=0)
print('display created: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
## common I2C and SPI test code below ##
display.sleep(False)
display.fill(0)
display.text('SH1107', 0, 0, 1)
display.text('driver', 0, 8, 1)
display.show()
time.sleep(1)
display.fill(0)
display.show()
if sh1107._fb_variant == 2:
for i in range(17):
display.large_text(str(i % 10), 0, 0, max(i,1), c=1, r=90*i)
display.show()
time.sleep(0.1)
display.fill(0)
for i in range(5):
for j in range(5):
display.large_text(str("big text"), (i) % 2 *56, (i+1) % 2 *56, 2, 1, 90*i, 90*j)
display.show()
time.sleep(.2)
display.fill(0)
print('framebuf2 framebuffer extension tests: triangles and circles')
for i in range (0, 32, 4):
display.triangle(0+3*i, i, 127-i, i, 127-i, 127-3*i, c=1)
display.show()
for i in range (0, 32, 4):
display.triangle(i, 0+3*i, i, 127-i, 127-3*i, 127-i, c=1)
display.show()
time.sleep(2)
display.fill(0)
# display.show()
# display.triangle(0, 0, 0, 127, 127, 127, c=1, f=True)
# display.show()
# time.sleep(2)
display.fill(0)
display.show()
for i in range (0, 64, 4):
display.circle(64, 64, 64-i , c=1)
display.show()
time.sleep(2)
display.fill(0)
display.show()
for i in range (0, 128, 32):
for j in range (0, 128, 32):
display.rect(i, j, 32, 32, c=(i+j)//32 % 2, f=True)
display.show()
display.circle(i+16, j+16, 15 , c=((i+j)//32 +1) % 2, f=True)
display.show()
time.sleep(2)
display.fill(0)
display.show()
#display.sleep(False)
display.fill(1)
display.show()
display.show()
time.sleep(1)
display.fill(0)
time.sleep(1)
full_update_flag = False
display.text('Testing 1', 0, 0, 1)
display.show(full_update_flag)
display.show(full_update_flag)
time.sleep(1)
display.text('Testing 16', 16, 16, 1)
display.show(full_update_flag)
time.sleep(1)
display.fill_rect(0, 26, 128, 4, 1)
# display.text('Testing 32', 16, 32, 1)
display.show(full_update_flag)
time.sleep(1)
display.text('Testing 120', 16, 120, 1)
display.show(full_update_flag)
time.sleep(1)
display.text('0----+----1----+-', 0, 88, 1)
display.text('01234567890123456', 0, 96, 1)
for i in range (16):
p=display.pixel(i,96)
# p=i // 10
display.text(str(p),i*8,104)
for j in range (4):
display.pixel(i*8+j*2,114,1)
display.show(full_update_flag)
print('bilt test')
smallbuffer=bytearray(8)
letter=framebuf.FrameBuffer(smallbuffer,8,8,framebuf.MONO_HMSB)
print('letter dir', dir(letter))
letter.text('K',0,0,1)
display.blit(letter,0,56)
display.show(full_update_flag)
print('large text test')
try:
display.large_text('QuiteBIG', 0, 32, 2, 1)
except:
display.text('framebuf2', 0, 40 ,1)
display.text('not loaded', 0, 48, 1)
display.show(True)
# sys.exit(0)
time.sleep(2)
print('contrast demo')
print('free memory' , gc.mem_free())
for i in range (256):
display.contrast(i)
contrast_text='contrast '+str(i)
display.fill_rect(16,64,96,8,0)
display.text(contrast_text, 16, 64, 1)
display.show(full_update_flag)
time.sleep_ms(25)
display.contrast(0x5F)
print('scroll test (start line)')
print('free memory' , gc.mem_free())
for i in range (128):
display.display_start_line(i)
time.sleep_ms(4)
for i in range (128):
display.display_start_line(127-i)
time.sleep_ms(4)
display.display_start_line(0)
time.sleep_ms(500)
print('free memory' , gc.mem_free())
print('invert test')
display.flip()
time.sleep(2)
display.invert(1)
time.sleep(2)
display.invert(0)
time.sleep(2)
display.flip()
time.sleep(2)
display.invert(1)
time.sleep(2)
display.invert(0)
time.sleep(1)
display.fill(0)
display.show(full_update_flag)
print('large text test 3')
try:
display.large_text('BIGx3', 0, 0, 3, 1)
except:
display.text('large_text', 0, 0, 1)
display.text('method from', 0, 8, 1)
display.text('framebuf2', 0, 16 ,1)
display.text('not loaded', 0, 24, 1)
display.show(full_update_flag)
print('large text test 4')
try:
display.large_text('HUGE', 0, 64, 4, 1)
except:
pass
display.show(full_update_flag)
time.sleep(2)
display.fill(0)
display.show()
print('large text test 16')
try:
display.large_text('1', 0, 0, 16, 1)
display.show()
time.sleep(1)
display.fill(0)
display.large_text('2', 0, 0, 16, 1)
display.show()
time.sleep(1)
display.fill(0)
display.large_text('3', 0, 0, 16, 1)
display.show()
time.sleep(1)
display.fill(0)
display.large_text('!', 0, 0, 16, 1)
display.show()
time.sleep(1)
display.invert(1)
time.sleep(1)
display.invert(0)
time.sleep(1)
display.invert(1)
time.sleep(1)
display.invert(0)
time.sleep(1)
except:
pass
time.sleep(1)
for i in range(64):
display.rect(i, i, 128-i*2, 128-i*2, c = i % 2)
display.show()
# time.sleep_ms(10)
time.sleep(1)
display.fill(0)
display.show()
for i in range(64):
display.rect(i, i, 128-i*2, 128-i*2, c = i % 2, f=False)
# display.sleep(i % 2)
display.show()
time.sleep(1)
display.fill(0)
display.show()
for i in range(64):
display.rect(i, i, 128-i*2, 128-i*2, c = i % 2, f=True)
# display.sleep(i % 2)
display.show()
# time.sleep(1)
# display.fill(0)
# display.show()
#
# for i in range(64):
# display.fill_rect(i, i, 128-i*2, 128-i*2, c = i % 2)
# # display.sleep(i % 2)
# display.show()
# for i in range(64):
# display.sleep(i % 2)
time.sleep(2)
display.fill(0)
display.show()
display.show()
display.show()
display.show()
display.show()
display.show()
display.show()
display.show()
display.show()
display.show()
display.ellipse(64, 48, 56, 32, 1)
display.show()
time.sleep(1)
display.ellipse(64, 48, 56, 32, 1, 1, 0xD)
display.show()
time.sleep(1)
coords=array.array('h', [0,0,0,40,40,40 ])
print(coords)
display.fill(0)
display.poly(10,10,coords, 1,1)
display.show()
time.sleep(3)
display.poweroff()