Skip to content

Commit d971036

Browse files
authored
Merge pull request #2 from vitasam/master
New API parameters: w(idth), h(eight), r(otation)
2 parents 3d22ee1 + dab9d15 commit d971036

File tree

4 files changed

+74
-16
lines changed

4 files changed

+74
-16
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Micropython Driver for ILI9341 display
22

3-
43
This has been tested on an M5Stack module using the standard esp32 micropython port. The default font is the Adafruit glcdfont and additional fonts can be generated by a very slightly modified version of Peter Hinch's font-to-py program which includes a function in font file to return the pixel width of a string of characters.
54

65
![m5stack](image/m5stack.jpg)
76

7+
# Rotation parameters
8+
9+
![m5stack](image/rotations.png)
10+
11+
812

ili934xnew.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This is an adapted version of the ILI934X driver as below.
22
# It works with multiple fonts and also works with the esp32 H/W SPI implementation
33
# 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
55
# MIT License; Copyright (c) 2017 Jeffrey N. Magee
66

77
# This file is part of MicroPython ILI934X driver
@@ -54,14 +54,16 @@ def color565(r, g, b):
5454

5555
class ILI9341:
5656

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):
6158
self.spi = spi
6259
self.cs = cs
6360
self.dc = dc
6461
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
6567
self.cs.init(self.cs.OUT, value=1)
6668
self.dc.init(self.dc.OUT, value=0)
6769
self.rst.init(self.rst.OUT, value=0)
@@ -74,25 +76,25 @@ def __init__(self, spi, cs, dc, rst):
7476
self._y = 0
7577
self._font = glcdfont
7678
self.scrolling = False
77-
79+
7880
def set_color(self,fg,bg):
7981
self._colormap[0] = bg>>8
8082
self._colormap[1] = bg & 255
8183
self._colormap[2] = fg>>8
8284
self._colormap[3] = fg & 255
83-
85+
8486
def set_pos(self,x,y):
8587
self._x = x
8688
self._y = y
87-
89+
8890
def reset_scroll(self):
8991
self.scrolling = False
9092
self._scroll = 0
9193
self.scroll(0)
92-
94+
9395
def set_font(self, font):
9496
self._font = font
95-
97+
9698
def init(self):
9799
for command, data in (
98100
(_RDDSDR, b"\x03\x80\x02"),
@@ -105,9 +107,45 @@ def init(self):
105107
(_PWCTRL1, b"\x23"),
106108
(_PWCTRL2, b"\x10"),
107109
(_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 (
111149
(_PIXSET, b"\x55"),
112150
(_FRMCTR1, b"\x00\x18"),
113151
(_DISCTRL, b"\x08\x82\x27"),
@@ -189,7 +227,7 @@ def fill_rectangle(self, x, y, w, h, color=None):
189227

190228
def erase(self):
191229
self.fill_rectangle(0, 0, self.width, self.height)
192-
230+
193231
def blit(self, bitbuff, x, y, w, h):
194232
x = min(self.width - 1, max(0, x))
195233
y = min(self.height - 1, max(0, y))
@@ -212,7 +250,7 @@ def blit(self, bitbuff, x, y, w, h):
212250
if rest != 0:
213251
mv = memoryview(self._buf)
214252
self._data(mv[:rest*2])
215-
253+
216254
def chars(self, str, x, y):
217255
str_w = self._font.get_width(str)
218256
div, rem = divmod(self._font.height(),8)

image/rotations.png

122 KB
Loading

rotations_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from ili934xnew import ILI9341, color565
2+
from machine import Pin, SPI
3+
import tt14
4+
5+
text = 'F'
6+
spi = SPI(2, baudrate=20000000, miso=Pin(19),mosi=Pin(23), sck=Pin(18))
7+
display = ILI9341(spi, cs=Pin(2), dc=Pin(27), rst=Pin(33), w=320, h=240, r=0)
8+
display.erase()
9+
display.set_font(tt14)
10+
display.set_pos(0,0)
11+
display.print(text)
12+
display.set_pos(0,20)
13+
display.print(text)
14+
display.set_pos(40,20)
15+
display.print(text)
16+

0 commit comments

Comments
 (0)