This repository was archived by the owner on May 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathili9341.py
186 lines (162 loc) · 5.85 KB
/
ili9341.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
import time
import ustruct
import framebuf
_COLUMN_SET = const(0x2a)
_PAGE_SET = const(0x2b)
_RAM_WRITE = const(0x2c)
_RAM_READ = const(0x2e)
_DISPLAY_ON = const(0x29)
_WAKE = const(0x11)
_LINE_SET = const(0x37)
def color565(r, g, b):
return (r & 0xf8) << 8 | (g & 0xfc) << 3 | b >> 3
class ILI9341:
"""
A simple driver for the ILI9341/ILI9340-based displays.
>>> import ili9341
>>> from machine import Pin, SPI
>>> spi = SPI(miso=Pin(12), mosi=Pin(13, Pin.OUT), sck=Pin(14, Pin.OUT))
>>> display = ili9341.ILI9341(spi, cs=Pin(0), dc=Pin(5), rst=Pin(4))
>>> display.fill(ili9341.color565(0xff, 0x11, 0x22))
>>> display.pixel(120, 160, 0)
"""
width = 240
height = 320
def __init__(self, spi, cs, dc, rst):
self.spi = spi
self.cs = cs
self.dc = dc
self.rst = rst
self.cs.init(self.cs.OUT, value=1)
self.dc.init(self.dc.OUT, value=0)
self.rst.init(self.rst.OUT, value=0)
self.reset()
self.init()
self._scroll = 0
def init(self):
for command, data in (
(0xef, b'\x03\x80\x02'),
(0xcf, b'\x00\xc1\x30'),
(0xed, b'\x64\x03\x12\x81'),
(0xe8, b'\x85\x00\x78'),
(0xcb, b'\x39\x2c\x00\x34\x02'),
(0xf7, b'\x20'),
(0xea, b'\x00\x00'),
(0xc0, b'\x23'), # Power Control 1, VRH[5:0]
(0xc1, b'\x10'), # Power Control 2, SAP[2:0], BT[3:0]
(0xc5, b'\x3e\x28'), # VCM Control 1
(0xc7, b'\x86'), # VCM Control 2
(0x36, b'\x48'), # Memory Access Control
(0x3a, b'\x55'), # Pixel Format
(0xb1, b'\x00\x18'), # FRMCTR1
(0xb6, b'\x08\x82\x27'), # Display Function Control
(0xf2, b'\x00'), # 3Gamma Function Disable
(0x26, b'\x01'), # Gamma Curve Selected
(0xe0, # Set Gamma
b'\x0f\x31\x2b\x0c\x0e\x08\x4e\xf1\x37\x07\x10\x03\x0e\x09\x00'),
(0xe1, # Set Gamma
b'\x00\x0e\x14\x03\x11\x07\x31\xc1\x48\x08\x0f\x0c\x31\x36\x0f'),
):
self._write(command, data)
self._write(_WAKE)
time.sleep_ms(120)
self._write(_DISPLAY_ON)
def reset(self):
self.rst.low()
time.sleep_ms(50)
self.rst.high()
time.sleep_ms(50)
def _write(self, command, data=None):
self.dc.low()
self.cs.low()
self.spi.write(bytearray([command]))
self.cs.high()
if data is not None:
self._data(data)
def _data(self, data):
self.dc.high()
self.cs.low()
self.spi.write(data)
self.cs.high()
def _block(self, x0, y0, x1, y1, data=None):
self._write(_COLUMN_SET, ustruct.pack(">HH", x0, x1))
self._write(_PAGE_SET, ustruct.pack(">HH", y0, y1))
if data is None:
return self._read(_RAM_READ, (x1 - x0 + 1) * (y1 - y0 + 1) * 3)
self._write(_RAM_WRITE, data)
def _read(self, command, count):
self.dc.low()
self.cs.low()
self.spi.write(bytearray([command]))
data = self.spi.read(count)
self.cs.high()
return data
def pixel(self, x, y, color=None):
if color is None:
r, b, g = self._block(x, y, x, y)
return color565(r, g, b)
if not 0 <= x < self.width or not 0 <= y < self.height:
return
self._block(x, y, x, y, ustruct.pack(">H", color))
def fill_rectangle(self, x, y, w, h, color):
x = min(self.width - 1, max(0, x))
y = min(self.height - 1, max(0, y))
w = min(self.width - x, max(1, w))
h = min(self.height - y, max(1, h))
self._block(x, y, x + w - 1, y + h - 1, b'')
chunks, rest = divmod(w * h, 512)
if chunks:
data = ustruct.pack(">H", color) * 512
for count in range(chunks):
self._data(data)
data = ustruct.pack(">H", color) * rest
self._data(data)
def fill(self, color):
self.fill_rectangle(0, 0, self.width, self.height, color)
def char(self, char, x, y, color=0xffff, background=0x0000):
buffer = bytearray(8)
framebuffer = framebuf.FrameBuffer1(buffer, 8, 8)
framebuffer.text(char, 0, 0)
color = ustruct.pack(">H", color)
background = ustruct.pack(">H", background)
data = bytearray(2 * 8 * 8)
for c, byte in enumerate(buffer):
for r in range(8):
if byte & (1 << r):
data[r * 8 * 2 + c * 2] = color[0]
data[r * 8 * 2 + c * 2 + 1] = color[1]
else:
data[r * 8 * 2 + c * 2] = background[0]
data[r * 8 * 2 + c * 2 + 1] = background[1]
self._block(x, y, x + 7, y + 7, data)
def text(self, text, x, y, color=0xffff, background=0x0000, wrap=None,
vwrap=None, clear_eol=False):
if wrap is None:
wrap = self.width - 8
if vwrap is None:
vwrap = self.height - 8
tx = x
ty = y
def new_line():
nonlocal tx, ty
tx = x
ty += 8
if ty >= vwrap:
ty = y
for char in text:
if char == '\n':
if clear_eol and tx < wrap:
self.fill_rectangle(tx, ty, wrap - tx + 7, 8, background)
new_line()
else:
if tx >= wrap:
new_line()
self.char(char, tx, ty, color, background)
tx += 8
if clear_eol and tx < wrap:
self.fill_rectangle(tx, ty, wrap - tx + 7, 8, background)
def scroll(self, dy=None):
if dy is None:
return self._scroll
self._scroll = (self._scroll + dy) % self.height
self._write(_LINE_SET, ustruct.pack('>H', self._scroll))