1
1
# This is an adapted version of the ILI934X driver as below.
2
2
# It works with multiple fonts and also works with the esp32 H/W SPI implementation
3
3
# Also includes a word wrap print function
4
- # Proportional fonts are generated by Peter Hinch's Font-to-py
4
+ # Proportional fonts are generated by Peter Hinch's Font-to-py
5
5
# MIT License; Copyright (c) 2017 Jeffrey N. Magee
6
6
7
7
# This file is part of MicroPython ILI934X driver
@@ -54,14 +54,16 @@ def color565(r, g, b):
54
54
55
55
class ILI9341 :
56
56
57
- width = 320
58
- height = 240
59
-
60
- def __init__ (self , spi , cs , dc , rst ):
57
+ def __init__ (self , spi , cs , dc , rst , w , h , r ):
61
58
self .spi = spi
62
59
self .cs = cs
63
60
self .dc = dc
64
61
self .rst = rst
62
+ self ._init_width = w
63
+ self ._init_height = h
64
+ self .width = w
65
+ self .height = h
66
+ self .rotation = r
65
67
self .cs .init (self .cs .OUT , value = 1 )
66
68
self .dc .init (self .dc .OUT , value = 0 )
67
69
self .rst .init (self .rst .OUT , value = 0 )
@@ -74,25 +76,25 @@ def __init__(self, spi, cs, dc, rst):
74
76
self ._y = 0
75
77
self ._font = glcdfont
76
78
self .scrolling = False
77
-
79
+
78
80
def set_color (self ,fg ,bg ):
79
81
self ._colormap [0 ] = bg >> 8
80
82
self ._colormap [1 ] = bg & 255
81
83
self ._colormap [2 ] = fg >> 8
82
84
self ._colormap [3 ] = fg & 255
83
-
85
+
84
86
def set_pos (self ,x ,y ):
85
87
self ._x = x
86
88
self ._y = y
87
-
89
+
88
90
def reset_scroll (self ):
89
91
self .scrolling = False
90
92
self ._scroll = 0
91
93
self .scroll (0 )
92
-
94
+
93
95
def set_font (self , font ):
94
96
self ._font = font
95
-
97
+
96
98
def init (self ):
97
99
for command , data in (
98
100
(_RDDSDR , b"\x03 \x80 \x02 " ),
@@ -105,9 +107,45 @@ def init(self):
105
107
(_PWCTRL1 , b"\x23 " ),
106
108
(_PWCTRL2 , b"\x10 " ),
107
109
(_VMCTRL1 , b"\x3e \x28 " ),
108
- (_VMCTRL2 , b"\x86 " ),
109
- #(_MADCTL, b"\x48"),
110
- (_MADCTL , b"\x08 " ),
110
+ (_VMCTRL2 , b"\x86 " )):
111
+ self ._write (command , data )
112
+
113
+ if self .rotation == 0 : # 0 deg
114
+ self ._write (_MADCTL , b"\x48 " )
115
+ self .width = self ._init_height
116
+ self .height = self ._init_width
117
+ elif self .rotation == 1 : # 90 deg
118
+ self ._write (_MADCTL , b"\x28 " )
119
+ self .width = self ._init_width
120
+ self .height = self ._init_height
121
+ elif self .rotation == 2 : # 180 deg
122
+ self ._write (_MADCTL , b"\x88 " )
123
+ self .width = self ._init_height
124
+ self .height = self ._init_width
125
+ elif self .rotation == 3 : # 270 deg
126
+ self ._write (_MADCTL , b"\xE8 " )
127
+ self .width = self ._init_width
128
+ self .height = self ._init_height
129
+ elif self .rotation == 4 : # Mirrored + 0 deg
130
+ self ._write (_MADCTL , b"\xC8 " )
131
+ self .width = self ._init_height
132
+ self .height = self ._init_width
133
+ elif self .rotation == 5 : # Mirrored + 90 deg
134
+ self ._write (_MADCTL , b"\x68 " )
135
+ self .width = self ._init_width
136
+ self .height = self ._init_height
137
+ elif self .rotation == 6 : # Mirrored + 180 deg
138
+ self ._write (_MADCTL , b"\x08 " )
139
+ self .width = self ._init_height
140
+ self .height = self ._init_width
141
+ elif self .rotation == 7 : # Mirrored + 270 deg
142
+ self ._write (_MADCTL , b"\xA8 " )
143
+ self .width = self ._init_width
144
+ self .height = self ._init_height
145
+ else :
146
+ self ._write (_MADCTL , b"\x08 " )
147
+
148
+ for command , data in (
111
149
(_PIXSET , b"\x55 " ),
112
150
(_FRMCTR1 , b"\x00 \x18 " ),
113
151
(_DISCTRL , b"\x08 \x82 \x27 " ),
@@ -189,7 +227,7 @@ def fill_rectangle(self, x, y, w, h, color=None):
189
227
190
228
def erase (self ):
191
229
self .fill_rectangle (0 , 0 , self .width , self .height )
192
-
230
+
193
231
def blit (self , bitbuff , x , y , w , h ):
194
232
x = min (self .width - 1 , max (0 , x ))
195
233
y = min (self .height - 1 , max (0 , y ))
@@ -212,7 +250,7 @@ def blit(self, bitbuff, x, y, w, h):
212
250
if rest != 0 :
213
251
mv = memoryview (self ._buf )
214
252
self ._data (mv [:rest * 2 ])
215
-
253
+
216
254
def chars (self , str , x , y ):
217
255
str_w = self ._font .get_width (str )
218
256
div , rem = divmod (self ._font .height (),8 )
0 commit comments