-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlcd_lib.py
221 lines (183 loc) · 6.18 KB
/
lcd_lib.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
from machine import Pin,SPI,PWM
import framebuf
import time
import os
BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9
class LCD_1inch3(framebuf.FrameBuffer):
def __init__(self):
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)
self.spi = SPI(1,1000_000)
self.spi = SPI(1,100000_000,polarity=0, phase=0,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()
# B-R-G
self.red = 0x07E0
self.green = 0x001f
self.blue = 0xf800
self.white = 0xffff
self.black = 0x0000
def write_cmd(self, cmd):
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
def write_data(self, buf):
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(bytearray([buf]))
self.cs(1)
def init_display(self):
"""Initialize dispaly"""
self.rst(1)
self.rst(0)
self.rst(1)
self.write_cmd(0x36)
self.write_data(0x70)
self.write_cmd(0x3A)
self.write_data(0x05)
self.write_cmd(0xB2)
self.write_data(0x0C)
self.write_data(0x0C)
self.write_data(0x00)
self.write_data(0x33)
self.write_data(0x33)
self.write_cmd(0xB7)
self.write_data(0x35)
self.write_cmd(0xBB)
self.write_data(0x19)
self.write_cmd(0xC0)
self.write_data(0x2C)
self.write_cmd(0xC2)
self.write_data(0x01)
self.write_cmd(0xC3)
self.write_data(0x12)
self.write_cmd(0xC4)
self.write_data(0x20)
self.write_cmd(0xC6)
self.write_data(0x0F)
self.write_cmd(0xD0)
self.write_data(0xA4)
self.write_data(0xA1)
self.write_cmd(0xE0)
self.write_data(0xD0)
self.write_data(0x04)
self.write_data(0x0D)
self.write_data(0x11)
self.write_data(0x13)
self.write_data(0x2B)
self.write_data(0x3F)
self.write_data(0x54)
self.write_data(0x4C)
self.write_data(0x18)
self.write_data(0x0D)
self.write_data(0x0B)
self.write_data(0x1F)
self.write_data(0x23)
self.write_cmd(0xE1)
self.write_data(0xD0)
self.write_data(0x04)
self.write_data(0x0C)
self.write_data(0x11)
self.write_data(0x13)
self.write_data(0x2C)
self.write_data(0x3F)
self.write_data(0x44)
self.write_data(0x51)
self.write_data(0x2F)
self.write_data(0x1F)
self.write_data(0x1F)
self.write_data(0x20)
self.write_data(0x23)
self.write_cmd(0x21)
self.write_cmd(0x11)
self.write_cmd(0x29)
def show(self):
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0xef)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0xEF)
self.write_cmd(0x2C)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(self.buffer)
self.cs(1)
def render(self,image_name,offset_x=0,offset_y=0,background=None,show_rendering=True):
'''Method to render 16-Bit images on the LCD panel
Args:
image_name: path of the encoded image
offset_x: x co-ordinate of starting position
offset_y: y co-ordinate of starting position
background: color of the background
show_rendering: if True, the process of rendering is shown, else a loading screen is shown
'''
self.fill(background)
f = open(image_name,'r')
row_count = 0
while True:
data = f.readline()
if not data:
break
px_ptr = 0
# All Even Positions will be Pixel Counts and
# odd positions will be Pixel Color Values
data = data.split(',')
for i in range(len(data)):
# Reading Count of Homogenous Pixels
if i%2 == 0:
px_count = int(data[i])
# Reading the Color of the Homogenous Pixels
else:
color = int('0x'+data[i])
if color != background:
self.hline(px_ptr+offset_x,row_count+offset_y,px_count,color)
px_ptr += px_count
row_count += 1
if show_rendering:
self.show()
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])