-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd_1inch28.py
407 lines (332 loc) · 10.5 KB
/
lcd_1inch28.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
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
#LCD_1inch28
#Version 3.1
from machine import Pin,I2C,SPI,PWM,Timer,ADC
import framebuf
import time
Vbat_Pin = 29
#Pin definition
I2C_SDA = 6
I2C_SDL = 7
I2C_INT = 17
I2C_RST = 16
DC = 8
CS = 9
SCK = 10
MOSI = 11
MISO = 12
RST = 13
BL = 25
#LCD Driver LCD
class LCD_1inch28(framebuf.FrameBuffer):
def __init__(self): #SPI initialization SPI
self.width = 240
self.height = 240
self.cs = Pin(CS,Pin.OUT)
self.rst = Pin(RST,Pin.OUT)
self.cs(1)
self.spi = SPI(1,100_000_000,polarity=0, phase=0,bits= 8,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
self.dc = Pin(DC,Pin.OUT)
self.dc(1)
self.buffer = bytearray(self.height * self.width * 2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
self.init_display()
#Define color, Micropython fixed to BRG format
self.red = 0x07E0
self.lilac = 0x3ffb
self.green = 0x001f
self.palegreen = 0xeb1c
self.blue = 0xf800
self.paleblue = 2058
self.white = 0xffff
self.black = 0x0000
self.brown = 0X8430
self.yellow = 630
self.washoutyellow = 0x76ff
self.brightyellow = 0xA0FF
self.salmon = 2251
self.orange = 130
self.testcolour = 0x00FF
self.fill(self.white) #Clear screen
self.show()#Show
self.pwm = PWM(Pin(BL))
self.pwm.freq(5000) #Turn on the backlight
def write_cmd(self, cmd): #Write command
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
def write_data(self, buf): #Write data
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(bytearray([buf]))
self.cs(1)
def set_bl_pwm(self,duty): #Set screen brightness
self.pwm.duty_u16(duty)#max 65535
def init_display(self): #LCD initialization LCD
"""Initialize display"""
self.rst(1)
time.sleep(0.01)
self.rst(0)
time.sleep(0.01)
self.rst(1)
time.sleep(0.05)
self.write_cmd(0xEF)
self.write_cmd(0xEB)
self.write_data(0x14)
self.write_cmd(0xFE)
self.write_cmd(0xEF)
self.write_cmd(0xEB)
self.write_data(0x14)
self.write_cmd(0x84)
self.write_data(0x40)
self.write_cmd(0x85)
self.write_data(0xFF)
self.write_cmd(0x86)
self.write_data(0xFF)
self.write_cmd(0x87)
self.write_data(0xFF)
self.write_cmd(0x88)
self.write_data(0x0A)
self.write_cmd(0x89)
self.write_data(0x21)
self.write_cmd(0x8A)
self.write_data(0x00)
self.write_cmd(0x8B)
self.write_data(0x80)
self.write_cmd(0x8C)
self.write_data(0x01)
self.write_cmd(0x8D)
self.write_data(0x01)
self.write_cmd(0x8E)
self.write_data(0xFF)
self.write_cmd(0x8F)
self.write_data(0xFF)
self.write_cmd(0xB6)
self.write_data(0x00)
self.write_data(0x20)
self.write_cmd(0x36)
self.write_data(0x98)
self.write_cmd(0x3A)
self.write_data(0x05)
self.write_cmd(0x90)
self.write_data(0x08)
self.write_data(0x08)
self.write_data(0x08)
self.write_data(0x08)
self.write_cmd(0xBD)
self.write_data(0x06)
self.write_cmd(0xBC)
self.write_data(0x00)
self.write_cmd(0xFF)
self.write_data(0x60)
self.write_data(0x01)
self.write_data(0x04)
self.write_cmd(0xC3)
self.write_data(0x13)
self.write_cmd(0xC4)
self.write_data(0x13)
self.write_cmd(0xC9)
self.write_data(0x22)
self.write_cmd(0xBE)
self.write_data(0x11)
self.write_cmd(0xE1)
self.write_data(0x10)
self.write_data(0x0E)
self.write_cmd(0xDF)
self.write_data(0x21)
self.write_data(0x0c)
self.write_data(0x02)
self.write_cmd(0xF0)
self.write_data(0x45)
self.write_data(0x09)
self.write_data(0x08)
self.write_data(0x08)
self.write_data(0x26)
self.write_data(0x2A)
self.write_cmd(0xF1)
self.write_data(0x43)
self.write_data(0x70)
self.write_data(0x72)
self.write_data(0x36)
self.write_data(0x37)
self.write_data(0x6F)
self.write_cmd(0xF2)
self.write_data(0x45)
self.write_data(0x09)
self.write_data(0x08)
self.write_data(0x08)
self.write_data(0x26)
self.write_data(0x2A)
self.write_cmd(0xF3)
self.write_data(0x43)
self.write_data(0x70)
self.write_data(0x72)
self.write_data(0x36)
self.write_data(0x37)
self.write_data(0x6F)
self.write_cmd(0xED)
self.write_data(0x1B)
self.write_data(0x0B)
self.write_cmd(0xAE)
self.write_data(0x77)
self.write_cmd(0xCD)
self.write_data(0x63)
self.write_cmd(0x70)
self.write_data(0x07)
self.write_data(0x07)
self.write_data(0x04)
self.write_data(0x0E)
self.write_data(0x0F)
self.write_data(0x09)
self.write_data(0x07)
self.write_data(0x08)
self.write_data(0x03)
self.write_cmd(0xE8)
self.write_data(0x34)
self.write_cmd(0x62)
self.write_data(0x18)
self.write_data(0x0D)
self.write_data(0x71)
self.write_data(0xED)
self.write_data(0x70)
self.write_data(0x70)
self.write_data(0x18)
self.write_data(0x0F)
self.write_data(0x71)
self.write_data(0xEF)
self.write_data(0x70)
self.write_data(0x70)
self.write_cmd(0x63)
self.write_data(0x18)
self.write_data(0x11)
self.write_data(0x71)
self.write_data(0xF1)
self.write_data(0x70)
self.write_data(0x70)
self.write_data(0x18)
self.write_data(0x13)
self.write_data(0x71)
self.write_data(0xF3)
self.write_data(0x70)
self.write_data(0x70)
self.write_cmd(0x64)
self.write_data(0x28)
self.write_data(0x29)
self.write_data(0xF1)
self.write_data(0x01)
self.write_data(0xF1)
self.write_data(0x00)
self.write_data(0x07)
self.write_cmd(0x66)
self.write_data(0x3C)
self.write_data(0x00)
self.write_data(0xCD)
self.write_data(0x67)
self.write_data(0x45)
self.write_data(0x45)
self.write_data(0x10)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x00)
self.write_cmd(0x67)
self.write_data(0x00)
self.write_data(0x3C)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x01)
self.write_data(0x54)
self.write_data(0x10)
self.write_data(0x32)
self.write_data(0x98)
self.write_cmd(0x74)
self.write_data(0x10)
self.write_data(0x85)
self.write_data(0x80)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x4E)
self.write_data(0x00)
self.write_cmd(0x98)
self.write_data(0x3e)
self.write_data(0x07)
self.write_cmd(0x35)
self.write_cmd(0x21)
self.write_cmd(0x11)
self.write_cmd(0x29)
def setWindows(self,Xstart,Ystart,Xend,Yend):
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(Xstart)
self.write_data(0x00)
self.write_data(Xend-1)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(Ystart)
self.write_data(0x00)
self.write_data(Yend-1)
self.write_cmd(0x2C)
def show(self):
self.setWindows(0,0,self.width,self.height)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(self.buffer)
self.cs(1)
'''
Partial display, the starting point of the local
display here is reduced by 10, and the end point
is increased by 10
'''
#Partial display, the starting point of the local display here is reduced by 10, and the end point is increased by 10
def Windows_show(self,Xstart,Ystart,Xend,Yend):
if Xstart > Xend:
data = Xstart
Xstart = Xend
Xend = data
if (Ystart > Yend):
data = Ystart
Ystart = Yend
Yend = data
if Xstart <= 10:
Xstart = 10
if Ystart <= 10:
Ystart = 10
Xstart -= 10;Xend += 10
Ystart -= 10;Yend += 10
self.setWindows(Xstart,Ystart,Xend,Yend)
self.cs(1)
self.dc(1)
self.cs(0)
for i in range (Ystart,Yend-1):
Addr = (Xstart * 2) + (i * 240 * 2)
self.spi.write(self.buffer[Addr : Addr+((Xend-Xstart)*2)])
self.cs(1)
#Write characters, size is the font size, the minimum is 1
def write_text(self,text,x,y,size,color):
''' Method to write Text on OLED/LCD Displays
with a variable font size
Args:
text: the string of chars to be displayed
x: x co-ordinate of starting position
y: y co-ordinate of starting position
size: font size of text
color: color of text to be displayed
'''
background = self.pixel(x,y)
info = []
# Creating reference charaters to read their values
self.text(text,x,y,color)
for i in range(x,x+(8*len(text))):
for j in range(y,y+8):
# Fetching amd saving details of pixels, such as
# x co-ordinate, y co-ordinate, and color of the pixel
px_color = self.pixel(i,j)
info.append((i,j,px_color)) if px_color == color else None
# Clearing the reference characters from the screen
self.text(text,x,y,background)
# Writing the custom-sized font characters on screen
for px_info in info:
self.fill_rect(size*px_info[0] - (size-1)*x , size*px_info[1] - (size-1)*y, size, size, px_info[2])