-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathili9341_parallel_pin_interface_display.py
More file actions
340 lines (279 loc) · 9.2 KB
/
ili9341_parallel_pin_interface_display.py
File metadata and controls
340 lines (279 loc) · 9.2 KB
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
"""
MicroPython ILI9341 Parallel Interface Driver for ESP32
"""
import time
from micropython import const
import machine
# ILI9341 Constants
_RDDSDR = const(0x0f) # Read Display Self-Diagnostic Result
_SLPOUT = const(0x11) # Sleep Out
_GAMSET = const(0x26) # Gamma Set
_DISPOFF = const(0x28) # Display Off
_DISPON = const(0x29) # Display On
_CASET = const(0x2a) # Column Address Set
_PASET = const(0x2b) # Page Address Set
_RAMWR = const(0x2c) # Memory Write
_RAMRD = const(0x2e) # Memory Read
_MADCTL = const(0x36) # Memory Access Control
_PIXFMT = const(0x3a) # COLMOD: Pixel Format Set
# Color constants
BLACK = const(0x0000)
BLUE = const(0x001F)
RED = const(0xF800)
GREEN = const(0x07E0)
CYAN = const(0x07FF)
MAGENTA = const(0xF81F)
YELLOW = const(0xFFE0)
WHITE = const(0xFFFF)
class ILI9341:
"""Driver for ILI9341 display with 8-bit parallel interface"""
def __init__(self, d0, d1, d2, d3, d4, d5, d6, d7, cs, dc, rst, wr, rd,
width=240, height=320, rotation=0):
"""Initialize display with parallel interface
Args:
d0-d7: The 8 data pins
cs: Chip select pin
dc: Data/command pin
rst: Reset pin
wr: Write pin
rd: Read pin
width, height: Display dimensions
rotation: Display rotation (0-3)
"""
# Store pin references
self.data_pins = [d0, d1, d2, d3, d4, d5, d6, d7]
self.cs = cs
self.dc = dc
self.rst = rst
self.wr = wr
self.rd = rd
# Set default pin states
self.cs.value(1)
self.dc.value(1)
self.wr.value(1)
self.rd.value(1)
# Store dimensions
self.width = width
self.height = height
# Reset and initialize the display
self.reset()
self._init()
# Set rotation
self.set_rotation(rotation)
# Clear the display
self.fill(BLACK)
def reset(self):
"""Reset the display"""
self.rst.value(1)
time.sleep_ms(50)
self.rst.value(0)
time.sleep_ms(50)
self.rst.value(1)
time.sleep_ms(150)
def _write_byte(self, b):
"""Write a byte to the data pins"""
# Set each data pin according to the bits in b
for i in range(8):
self.data_pins[i].value((b >> i) & 1)
# Pulse WR pin to write data
self.wr.value(0)
time.sleep_us(1) # Small delay for timing
self.wr.value(1)
def _write_cmd(self, cmd):
"""Write a command to the display"""
self.cs.value(0)
self.dc.value(0) # Command mode
self._write_byte(cmd)
self.cs.value(1)
def _write_data(self, data):
"""Write data to the display"""
self.cs.value(0)
self.dc.value(1) # Data mode
self._write_byte(data)
self.cs.value(1)
def _write_data_word(self, data):
"""Write a 16-bit word as two bytes"""
self._write_data(data >> 8) # High byte first
self._write_data(data & 0xFF) # Low byte
def _init(self):
"""Initialize the display"""
# Send initialization commands
commands = [
(_SLPOUT, None, 150), # Exit sleep mode, 150ms delay
(_PIXFMT, b'\x55', 10), # Set 16-bit color mode
(_MADCTL, b'\x48', 10), # Set memory access control (orientation)
(_DISPON, None, 100), # Turn on the display
]
for cmd, data, delay in commands:
self._write_cmd(cmd)
if data:
for b in data:
self._write_data(b)
if delay:
time.sleep_ms(delay)
def set_rotation(self, r):
"""Set the display rotation
Args:
r: Rotation (0-3, clockwise rotation)
"""
# MADCTL values for different rotations
rotations = [
0x48, # Portrait (0°)
0x28, # Landscape (90°)
0x88, # Portrait (180°)
0xE8, # Landscape (270°)
]
if r not in range(4):
r = 0
# Update width/height based on rotation
if r % 2:
self.width, self.height = self.height, self.width
# Send command
self._write_cmd(_MADCTL)
self._write_data(rotations[r])
def set_window(self, x0, y0, x1, y1):
"""Set the active drawing window
Args:
x0, y0: Start coordinates
x1, y1: End coordinates
"""
# Column address set
self._write_cmd(_CASET)
self._write_data_word(x0)
self._write_data_word(x1)
# Row address set
self._write_cmd(_PASET)
self._write_data_word(y0)
self._write_data_word(y1)
# Memory write
self._write_cmd(_RAMWR)
def pixel(self, x, y, color):
"""Draw a pixel
Args:
x, y: Coordinates
color: 16-bit RGB color
"""
# Check bounds
if x < 0 or x >= self.width or y < 0 or y >= self.height:
return
# Set window to single pixel
self.set_window(x, y, x, y)
# Write color data
self.dc.value(1) # Data mode
self.cs.value(0)
self._write_byte(color >> 8)
self._write_byte(color & 0xFF)
self.cs.value(1)
def fill_rect(self, x, y, w, h, color):
"""Fill a rectangle with a color
Args:
x, y: Start coordinates
w, h: Width and height
color: 16-bit RGB color
"""
# Check bounds and clip rectangle
if x >= self.width or y >= self.height or w <= 0 or h <= 0:
return
if x < 0:
w += x
x = 0
if y < 0:
h += y
y = 0
if x + w > self.width:
w = self.width - x
if y + h > self.height:
h = self.height - y
# Set window
self.set_window(x, y, x + w - 1, y + h - 1)
# Prepare color bytes
hi = color >> 8
lo = color & 0xFF
# Fill rectangle
self.dc.value(1) # Data mode
self.cs.value(0)
# Calculate total number of pixels
num_pixels = w * h
# Fill by writing the same color value repeatedly
for _ in range(num_pixels):
self._write_byte(hi)
self._write_byte(lo)
self.cs.value(1)
def fill(self, color):
"""Fill the entire display with a color
Args:
color: 16-bit RGB color
"""
self.fill_rect(0, 0, self.width, self.height, color)
def hline(self, x, y, w, color):
"""Draw a horizontal line
Args:
x, y: Start coordinates
w: Width
color: 16-bit RGB color
"""
self.fill_rect(x, y, w, 1, color)
def vline(self, x, y, h, color):
"""Draw a vertical line
Args:
x, y: Start coordinates
h: Height
color: 16-bit RGB color
"""
self.fill_rect(x, y, 1, h, color)
def line(self, x0, y0, x1, y1, color):
"""Draw a line using Bresenham's algorithm
Args:
x0, y0: Start coordinates
x1, y1: End coordinates
color: 16-bit RGB color
"""
# Bresenham's algorithm
steep = abs(y1 - y0) > abs(x1 - x0)
if steep:
x0, y0 = y0, x0
x1, y1 = y1, x1
if x0 > x1:
x0, x1 = x1, x0
y0, y1 = y1, y0
dx = x1 - x0
dy = abs(y1 - y0)
err = dx // 2
if y0 < y1:
ystep = 1
else:
ystep = -1
while x0 <= x1:
if steep:
self.pixel(y0, x0, color)
else:
self.pixel(x0, y0, color)
err -= dy
if err < 0:
y0 += ystep
err += dx
x0 += 1
def rect(self, x, y, w, h, color):
"""Draw a rectangle outline
Args:
x, y: Start coordinates
w, h: Width and height
color: 16-bit RGB color
"""
self.hline(x, y, w, color)
self.hline(x, y + h - 1, w, color)
self.vline(x, y, h, color)
self.vline(x + w - 1, y, h, color)
def text(self, text, x, y, color=WHITE):
"""Draw text (basic implementation)
Args:
text: Text string to display
x, y: Start coordinates
color: 16-bit RGB color
"""
# Basic 8x8 font - this is a simplified implementation
# For a full implementation, consider using a font module
for i, char in enumerate(text):
# Draw a simple representation - a rectangle for each character
# In a real implementation, you would draw the actual character
self.fill_rect(x + (i * 8), y, 6, 8, color)