Add BGR and gamma options.

master
rdagger 10 months ago
parent 8f3d531023
commit 53a945e211

@ -0,0 +1,28 @@
"""ILI9341 demo (inversion)."""
from time import sleep
from ili9341 import Display, color565
from machine import Pin, SPI # type: ignore
def test():
"""Test code."""
# Baud rate of 40000000 seems about the max
spi = SPI(1, baudrate=40000000, sck=Pin(14), mosi=Pin(13))
display = Display(spi, dc=Pin(4), cs=Pin(16), rst=Pin(17))
display.clear()
display.fill_rectangle(0, 0, 100, 100, color565(255, 0, 0))
display.fill_polygon(3, 140, 140, 70, color565(0, 255, 0), rotate=15)
display.fill_circle(170, 240, 50, color565(0, 0, 255))
sleep(2)
display.invert()
sleep(5)
display.invert(False)
sleep(2)
display.cleanup()
test()

@ -3,6 +3,7 @@ from time import sleep
from math import cos, sin, pi, radians from math import cos, sin, pi, radians
from sys import implementation from sys import implementation
from framebuf import FrameBuffer, RGB565 # type: ignore from framebuf import FrameBuffer, RGB565 # type: ignore
from micropython import const # type: ignore
def color565(r, g, b): def color565(r, g, b):
@ -88,8 +89,8 @@ class Display(object):
270: 0x28 270: 0x28
} }
def __init__(self, spi, cs, dc, rst, def __init__(self, spi, cs, dc, rst, width=240, height=320, rotation=0,
width=240, height=320, rotation=0): bgr=True, gamma=True):
"""Initialize OLED. """Initialize OLED.
Args: Args:
@ -100,6 +101,8 @@ class Display(object):
width (Optional int): Screen width (default 240) width (Optional int): Screen width (default 240)
height (Optional int): Screen height (default 320) height (Optional int): Screen height (default 320)
rotation (Optional int): Rotation must be 0 default, 90. 180 or 270 rotation (Optional int): Rotation must be 0 default, 90. 180 or 270
bgr (Optional bool): Swaps red and blue colors (default True)
gamma (Optional bool): Custom gamma correction (default True)
""" """
self.spi = spi self.spi = spi
self.cs = cs self.cs = cs
@ -111,6 +114,8 @@ class Display(object):
raise RuntimeError('Rotation must be 0, 90, 180 or 270.') raise RuntimeError('Rotation must be 0, 90, 180 or 270.')
else: else:
self.rotation = self.ROTATE[rotation] self.rotation = self.ROTATE[rotation]
if not bgr: # Clear BGR bit
self.rotation &= 0b11110111
# Initialize GPIO pins and set implementation specific methods # Initialize GPIO pins and set implementation specific methods
if implementation.name == 'circuitpython': if implementation.name == 'circuitpython':
@ -148,10 +153,13 @@ class Display(object):
self.write_cmd(self.DFUNCTR, 0x08, 0x82, 0x27) self.write_cmd(self.DFUNCTR, 0x08, 0x82, 0x27)
self.write_cmd(self.ENABLE3G, 0x00) # Enable 3 gamma ctrl self.write_cmd(self.ENABLE3G, 0x00) # Enable 3 gamma ctrl
self.write_cmd(self.GAMMASET, 0x01) # Gamma curve selected self.write_cmd(self.GAMMASET, 0x01) # Gamma curve selected
self.write_cmd(self.GMCTRP1, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, if gamma: # Use custom gamma correction values
0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00) self.write_cmd(self.GMCTRP1, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08,
self.write_cmd(self.GMCTRN1, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09,
0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F) 0x00)
self.write_cmd(self.GMCTRN1, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07,
0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36,
0x0F)
self.write_cmd(self.SLPOUT) # Exit sleep self.write_cmd(self.SLPOUT) # Exit sleep
sleep(.1) sleep(.1)
self.write_cmd(self.DISPLAY_ON) # Display on self.write_cmd(self.DISPLAY_ON) # Display on
@ -897,6 +905,17 @@ class Display(object):
chunk_x + remainder - 1, y + h - 1, chunk_x + remainder - 1, y + h - 1,
buf) buf)
def invert(self, enable=True):
"""Enables or disables inversion of display colors.
Args:
enable (Optional bool): True=enable, False=disable
"""
if enable:
self.write_cmd(self.INVON)
else:
self.write_cmd(self.INVOFF)
def is_off_grid(self, xmin, ymin, xmax, ymax): def is_off_grid(self, xmin, ymin, xmax, ymax):
"""Check if coordinates extend past display boundaries. """Check if coordinates extend past display boundaries.

Loading…
Cancel
Save