Skip to content

Added HX8357 Display Support #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions adafruit_rgb_display/hx8357.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# The MIT License (MIT)
#
# Copyright (c) 2019 Melissa LeBlanc-Williams for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_rgb_display.hx8357`
====================================================

A simple driver for the HX8357-based displays.

* Author(s): Melissa LeBlanc-Williams
"""
from micropython import const
from adafruit_rgb_display.rgb import DisplaySPI

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RGB_Display.git"

_SWRESET = const(0x01)
_SLPOUT = const(0x11)
_NORON = const(0x13)
_INVOFF = const(0x20)
_INVON = const(0x21)
_DISPOFF = const(0x28)
_DISPON = const(0x29)
_CASET = const(0x2a)
_PASET = const(0x2b)
_RAMWR = const(0x2c)
_RAMRD = const(0x2e)
_TEON = const(0x35)
_MADCTL = const(0x36)
_COLMOD = const(0x3a)
_TEARLINE = const(0x44)
_SETOSC = const(0xb0)
_SETPWR1 = const(0xb1)
_SETRGB = const(0xb3)
_SETCYC = const(0xb4)
_SETCOM = const(0xb6)
_SETC = const(0xb9)
_SETSTBA = const(0xc0)
_SETPANEL = const(0xcc)
_SETGAMMA = const(0xe0)

class HX8357(DisplaySPI):
"""
A simple driver for the HX8357-based displays.

>>> import busio
>>> import digitalio
>>> import board
>>> from adafruit_rgb_display import color565
>>> import adafruit_rgb_display.hx8357 as hx8357
>>> spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
>>> display = hx8357.HX8357(spi, cs=digitalio.DigitalInOut(board.GPIO0),
... dc=digitalio.DigitalInOut(board.GPIO15))
>>> display.fill(0x7521)
>>> display.pixel(64, 64, 0)
"""
_COLUMN_SET = _CASET
_PAGE_SET = _PASET
_RAM_WRITE = _RAMWR
_RAM_READ = _RAMRD
_INIT = (
(_SWRESET, None),
(_SETC, b'\xFF\x83\x57'),
(_SETRGB, b'\x80\x00\x06\x06'), # 0x80 enables SDO pin (0x00 disables)
(_SETCOM, b'\x25'), # -1.52V
(_SETOSC, b'\x68'), # Normal mode 70Hz, Idle mode 55 Hz
(_SETPANEL, b'\x05'), # BGR, Gate direction swapped
(_SETPWR1, b'\x00\x15\x1C\x1C\x83\xAA'), # Not deep standby BT VSPR VSNR AP
(_SETSTBA, b'\x50\x50\x01\x3C\x1E\x08'), # OPON normal OPON idle STBA GEN
(_SETCYC, b'\x02\x40\x00\x2A\x2A\x0D\x78'), # NW 0x02 RTN DIV DUM DUM GDON GDOFF
(_SETGAMMA, b'\x02\x0A\x11\x1d\x23\x35\x41\x4b\x4b\x42\x3A\x27\x1B\x08\x09\x03\x02' \
b'\x0A\x11\x1d\x23\x35\x41\x4b\x4b\x42\x3A\x27\x1B\x08\x09\x03\x00\x01'),
(_COLMOD, b'\x55'), # 16 bit
(_MADCTL, b'\xc0'),
(_TEON, b'\x00'),
(_TEARLINE, b'\x00\x02'), # TW off
(_SLPOUT, None),
(_MADCTL, b'\xa0'),
(_DISPON, None),
)
_ENCODE_PIXEL = ">H"
_ENCODE_POS = ">HH"

#pylint: disable-msg=useless-super-delegation, too-many-arguments
def __init__(self, spi, dc, cs, rst=None, width=480, height=320,
baudrate=16000000, polarity=0, phase=0):
super().__init__(spi, dc, cs, rst, width, height,
baudrate=baudrate, polarity=polarity, phase=phase)
6 changes: 4 additions & 2 deletions adafruit_rgb_display/rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class Display: #pylint: disable-msg=no-member
_COLUMN_SET = None
_RAM_WRITE = None
_RAM_READ = None
_X_START = 0
_Y_START = 0
_INIT = ()
_ENCODE_PIXEL = ">H"
_ENCODE_POS = ">HH"
Expand All @@ -98,8 +100,8 @@ def init(self):
#pylint: disable-msg=invalid-name,too-many-arguments
def _block(self, x0, y0, x1, y1, data=None):
"""Read or write a block of data."""
self.write(self._COLUMN_SET, self._encode_pos(x0, x1))
self.write(self._PAGE_SET, self._encode_pos(y0, y1))
self.write(self._COLUMN_SET, self._encode_pos(x0 + self._X_START, x1 + self._X_START))
self.write(self._PAGE_SET, self._encode_pos(y0 + self._Y_START, y1 + self._Y_START))
if data is None:
size = struct.calcsize(self._DECODE_PIXEL)
return self.read(self._RAM_READ,
Expand Down
133 changes: 133 additions & 0 deletions adafruit_rgb_display/st7789.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# The MIT License (MIT)
#
# Copyright (c) 2019 Melissa LeBlanc-Williams for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_rgb_display.st7789`
====================================================

A simple driver for the ST7789-based displays.

* Author(s): Melissa LeBlanc-Williams
"""

try:
import struct
except ImportError:
import ustruct as struct
from micropython import const
from adafruit_rgb_display.rgb import DisplaySPI

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RGB_Display.git"

_NOP = const(0x00)
_SWRESET = const(0x01)
_RDDID = const(0x04)
_RDDST = const(0x09)

_SLPIN = const(0x10)
_SLPOUT = const(0x11)
_PTLON = const(0x12)
_NORON = const(0x13)

_INVOFF = const(0x20)
_INVON = const(0x21)
_DISPOFF = const(0x28)
_DISPON = const(0x29)
_CASET = const(0x2A)
_RASET = const(0x2B)
_RAMWR = const(0x2C)
_RAMRD = const(0x2E)

_PTLAR = const(0x30)
_COLMOD = const(0x3A)
_MADCTL = const(0x36)

_FRMCTR1 = const(0xB1)
_FRMCTR2 = const(0xB2)
_FRMCTR3 = const(0xB3)
_INVCTR = const(0xB4)
_DISSET5 = const(0xB6)

_PWCTR1 = const(0xC0)
_PWCTR2 = const(0xC1)
_PWCTR3 = const(0xC2)
_PWCTR4 = const(0xC3)
_PWCTR5 = const(0xC4)
_VMCTR1 = const(0xC5)

_RDID1 = const(0xDA)
_RDID2 = const(0xDB)
_RDID3 = const(0xDC)
_RDID4 = const(0xDD)

_PWCTR6 = const(0xFC)

_GMCTRP1 = const(0xE0)
_GMCTRN1 = const(0xE1)


class ST7789(DisplaySPI):
"""
A simple driver for the ST7789-based displays.

>>> import busio
>>> import digitalio
>>> import board
>>> from adafruit_rgb_display import color565
>>> import adafruit_rgb_display.st7789 as st7789
>>> spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
>>> display = st7789.ST7789(spi, cs=digitalio.DigitalInOut(board.GPIO0),
... dc=digitalio.DigitalInOut(board.GPIO15), rst=digitalio.DigitalInOut(board.GPIO16))
>>> display.fill(0x7521)
>>> display.pixel(64, 64, 0)
"""
_COLUMN_SET = _CASET
_PAGE_SET = _RASET
_RAM_WRITE = _RAMWR
_RAM_READ = _RAMRD
_Y_START = 80
_INIT = (
(_SWRESET, None),
(_SLPOUT, None),
(_COLMOD, b'\x55'), # 16bit color
(_MADCTL, b'\x08'),
)

#pylint: disable-msg=useless-super-delegation, too-many-arguments
def __init__(self, spi, dc, cs, rst=None, width=240, height=240,
baudrate=16000000, polarity=0, phase=0):
super().__init__(spi, dc, cs, rst, width, height,
baudrate=baudrate, polarity=polarity, phase=phase)

def init(self):
super().init()
cols = struct.pack('>HH', 0, self.width)
rows = struct.pack('>HH', self._Y_START, self.height + self._Y_START)
for command, data in (
(_CASET, cols),
(_RASET, rows),
(_INVON, None),
(_NORON, None),
(_DISPON, None),
(_MADCTL, b'\xc0'), #Set rotation to 0 and use RGB
):
self.write(command, data)
8 changes: 6 additions & 2 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Simple test

Ensure your device works with this simple test.

.. literalinclude:: ../examples/esp8266_tft_featherwing.py
:caption: examples/esp8266_tft_featherwing.py
.. literalinclude:: ../examples/rgbdisplay_simpletest.py
:caption: examples/rgbdisplay_simpletest.py
:linenos:

.. literalinclude:: ../examples/rgbdisplay_ili9341test.py
:caption: examples/rgbdisplay_ili9341test.py
:linenos:
45 changes: 45 additions & 0 deletions examples/rgbdisplay_hx8357test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Quick test of 3.5" TFT FeatherWing (HX8357) with Feather M0 or M4
# This will work even on a device running displayio
# Will fill the TFT black and put a red pixel in the center, wait 2 seconds,
# then fill the screen blue (with no pixel), wait 2 seconds, and repeat.
import time
import random
import digitalio
import board
import displayio

from adafruit_rgb_display.rgb import color565
import adafruit_rgb_display.hx8357 as hx8357

displayio.release_displays()

# Configuration for CS and DC pins (these are TFT FeatherWing defaults):
cs_pin = digitalio.DigitalInOut(board.D9)
dc_pin = digitalio.DigitalInOut(board.D10)

# Config for display baudrate (default max is 24mhz):
BAUDRATE = 24000000

# Setup SPI bus using hardware SPI:
spi = board.SPI()

# Create the HX8357 display:
display = hx8357.HX8357(spi, cs=cs_pin, dc=dc_pin, baudrate=BAUDRATE)

# Main loop:
while True:
# Fill the screen red, green, blue, then black:
for color in ((255, 0, 0), (0, 255, 0), (0, 0, 255)):
display.fill(color565(color))
# Clear the display
display.fill(0)
# Draw a red pixel in the center.
display.pixel(display.width//2, display.height//2, color565(255, 0, 0))
# Pause 2 seconds.
time.sleep(2)
# Clear the screen a random color
display.fill(color565(random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255)))
# Pause 2 seconds.
time.sleep(2)
46 changes: 46 additions & 0 deletions examples/rgbdisplay_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Quick test of TFT FeatherWing (ST7789) with Feather M0 or M4
# This will work even on a device running displayio
# Will fill the TFT black and put a red pixel in the center, wait 2 seconds,
# then fill the screen blue (with no pixel), wait 2 seconds, and repeat.
import time
import random
import digitalio
import board
import displayio

from adafruit_rgb_display.rgb import color565
import adafruit_rgb_display.st7789 as st7789

displayio.release_displays()

# Configuratoin for CS and DC pins (these are FeatherWing defaults on M0/M4):
cs_pin = digitalio.DigitalInOut(board.D5)
dc_pin = digitalio.DigitalInOut(board.D6)
reset_pin = digitalio.DigitalInOut(board.D9)

# Config for display baudrate (default max is 24mhz):
BAUDRATE = 24000000

# Setup SPI bus using hardware SPI:
spi = board.SPI()

# Create the ST7789 display:
display = st7789.ST7789(spi, cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE)

# Main loop:
while True:
# Fill the screen red, green, blue, then black:
for color in ((255, 0, 0), (0, 255, 0), (0, 0, 255)):
display.fill(color565(color))
# Clear the display
display.fill(0)
# Draw a red pixel in the center.
display.pixel(display.width//2, display.height//2, color565(255, 0, 0))
# Pause 2 seconds.
time.sleep(2)
# Clear the screen a random color
display.fill(color565(random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255)))
# Pause 2 seconds.
time.sleep(2)