Add mirror option

master
rdagger 7 months ago
parent 672997ca35
commit 06abf62718

@ -0,0 +1,82 @@
"""ILI9341 demo (mirror)."""
from time import sleep
from ili9341 import Display, color565
from machine import Pin, SPI # type: ignore
# Define colors
COLOR_RED = color565(255, 0, 0)
COLOR_BLUE = color565(0, 0, 255)
COLOR_GREEN = color565(0, 255, 0)
COLOR_YELLOW = color565(255, 255, 0)
COLOR_PURPLE = color565(128, 0, 128)
COLOR_CYAN = color565(0, 255, 255)
COLOR_MAGENTA = color565(255, 0, 255)
COLOR_ORANGE = color565(255, 165, 0)
COLOR_WHITE = color565(255, 255, 255)
COLOR_LAVENDER = color565(255, 165, 255)
MIRROR_ROTATE = ((False, 0),
(False, 90),
(False, 180),
(False, 270),
(True, 0),
(True, 90),
(True, 180),
(True, 270))
def test():
"""Test code."""
for mirror, rotation in MIRROR_ROTATE:
# Baud rate of 40000000 seems about the max
spi = SPI(1, baudrate=40000000, sck=Pin(14), mosi=Pin(13))
# Set width & height based on rotation
if rotation == 0 or rotation == 180:
width, height = 240, 320
else:
width, height = 320, 240
display = Display(spi, dc=Pin(4), cs=Pin(16), rst=Pin(17),
width=width, height=height,
rotation=rotation, mirror=mirror)
display.clear()
# Outer Vertical Line
display.draw_line(41, 21, 41, 239, COLOR_RED)
# Inner Vertical Line
display.draw_line(61, 41, 61, 239, COLOR_BLUE)
# Outer Top Horizontal Line
display.draw_line(41, 21, 181, 21, COLOR_GREEN)
# Inner Top Horizontal Line
display.draw_line(61, 41, 181, 41, COLOR_YELLOW)
# Outer Middle Horizontal Line
display.draw_line(62, 130, 161, 130, COLOR_PURPLE)
# Inner Middle Horizontal Line
display.draw_line(62, 111, 161, 111, COLOR_CYAN)
# End Cap on Outer Vertical Line (Bottom)
display.draw_line(41, 239, 61, 239, COLOR_MAGENTA)
# End Cap on Outer Top Horizontal Line (Right)
display.draw_line(181, 21, 181, 41, COLOR_ORANGE)
# End Caps on Inner Lines (Middle)
display.draw_line(162, 111, 162, 130, COLOR_WHITE)
# Display rotation and mirror values at bottom of display
text = f"Rotation: {rotation}, Mirror: {mirror}"
display.draw_text8x8(
(width - len(text) * 8) // 2 if width < 320 else 90,
height - 50 if width < 320 else height - 9,
text,
COLOR_LAVENDER)
sleep(5)
display.cleanup()
test()

@ -82,15 +82,19 @@ class Display(object):
ENABLE3G = const(0xF2) # Enable 3 gamma control
PUMPRC = const(0xF7) # Pump ratio control
ROTATE = {
0: 0x88,
90: 0xE8,
180: 0x48,
270: 0x28
MIRROR_ROTATE = { # MADCTL configurations for rotation and mirroring
(False, 0): 0x80, # 1000 0000
(False, 90): 0xE0, # 1110 0000
(False, 180): 0x40, # 0100 0000
(False, 270): 0x20, # 0010 0000
(True, 0): 0xC0, # 1100 0000
(True, 90): 0x60, # 0110 0000
(True, 180): 0x00, # 0000 0000
(True, 270): 0xA0 # 1010 0000
}
def __init__(self, spi, cs, dc, rst, width=240, height=320, rotation=0,
bgr=True, gamma=True):
mirror=False, bgr=True, gamma=True):
"""Initialize OLED.
Args:
@ -101,6 +105,7 @@ class Display(object):
width (Optional int): Screen width (default 240)
height (Optional int): Screen height (default 320)
rotation (Optional int): Rotation must be 0 default, 90. 180 or 270
mirror (Optional bool): Mirror display (default False)
bgr (Optional bool): Swaps red and blue colors (default True)
gamma (Optional bool): Custom gamma correction (default True)
"""
@ -110,12 +115,12 @@ class Display(object):
self.rst = rst
self.width = width
self.height = height
if rotation not in self.ROTATE.keys():
raise RuntimeError('Rotation must be 0, 90, 180 or 270.')
if (mirror, rotation) not in self.MIRROR_ROTATE:
raise ValueError('Rotation must be 0, 90, 180 or 270.')
else:
self.rotation = self.ROTATE[rotation]
if not bgr: # Clear BGR bit
self.rotation &= 0b11110111
self.rotation = self.MIRROR_ROTATE[mirror, rotation]
if bgr: # Set BGR bit
self.rotation |= 0b00001000
# Initialize GPIO pins and set implementation specific methods
if implementation.name == 'circuitpython':
@ -994,7 +999,6 @@ class Display(object):
"""
if top + bottom <= self.height:
middle = self.height - (top + bottom)
print(top, middle, bottom)
self.write_cmd(self.VSCRDEF,
top >> 8,
top & 0xFF,

Loading…
Cancel
Save