Skip to content

Commit 1145977

Browse files
authored
Merge pull request #25 from makermelissa/hx8357
Added HX8357 Display Support
2 parents 6666a00 + 3f53d3d commit 1145977

File tree

6 files changed

+341
-4
lines changed

6 files changed

+341
-4
lines changed

adafruit_rgb_display/hx8357.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2019 Melissa LeBlanc-Williams for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_rgb_display.hx8357`
24+
====================================================
25+
26+
A simple driver for the HX8357-based displays.
27+
28+
* Author(s): Melissa LeBlanc-Williams
29+
"""
30+
from micropython import const
31+
from adafruit_rgb_display.rgb import DisplaySPI
32+
33+
__version__ = "0.0.0-auto.0"
34+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RGB_Display.git"
35+
36+
_SWRESET = const(0x01)
37+
_SLPOUT = const(0x11)
38+
_NORON = const(0x13)
39+
_INVOFF = const(0x20)
40+
_INVON = const(0x21)
41+
_DISPOFF = const(0x28)
42+
_DISPON = const(0x29)
43+
_CASET = const(0x2a)
44+
_PASET = const(0x2b)
45+
_RAMWR = const(0x2c)
46+
_RAMRD = const(0x2e)
47+
_TEON = const(0x35)
48+
_MADCTL = const(0x36)
49+
_COLMOD = const(0x3a)
50+
_TEARLINE = const(0x44)
51+
_SETOSC = const(0xb0)
52+
_SETPWR1 = const(0xb1)
53+
_SETRGB = const(0xb3)
54+
_SETCYC = const(0xb4)
55+
_SETCOM = const(0xb6)
56+
_SETC = const(0xb9)
57+
_SETSTBA = const(0xc0)
58+
_SETPANEL = const(0xcc)
59+
_SETGAMMA = const(0xe0)
60+
61+
class HX8357(DisplaySPI):
62+
"""
63+
A simple driver for the HX8357-based displays.
64+
65+
>>> import busio
66+
>>> import digitalio
67+
>>> import board
68+
>>> from adafruit_rgb_display import color565
69+
>>> import adafruit_rgb_display.hx8357 as hx8357
70+
>>> spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
71+
>>> display = hx8357.HX8357(spi, cs=digitalio.DigitalInOut(board.GPIO0),
72+
... dc=digitalio.DigitalInOut(board.GPIO15))
73+
>>> display.fill(0x7521)
74+
>>> display.pixel(64, 64, 0)
75+
"""
76+
_COLUMN_SET = _CASET
77+
_PAGE_SET = _PASET
78+
_RAM_WRITE = _RAMWR
79+
_RAM_READ = _RAMRD
80+
_INIT = (
81+
(_SWRESET, None),
82+
(_SETC, b'\xFF\x83\x57'),
83+
(_SETRGB, b'\x80\x00\x06\x06'), # 0x80 enables SDO pin (0x00 disables)
84+
(_SETCOM, b'\x25'), # -1.52V
85+
(_SETOSC, b'\x68'), # Normal mode 70Hz, Idle mode 55 Hz
86+
(_SETPANEL, b'\x05'), # BGR, Gate direction swapped
87+
(_SETPWR1, b'\x00\x15\x1C\x1C\x83\xAA'), # Not deep standby BT VSPR VSNR AP
88+
(_SETSTBA, b'\x50\x50\x01\x3C\x1E\x08'), # OPON normal OPON idle STBA GEN
89+
(_SETCYC, b'\x02\x40\x00\x2A\x2A\x0D\x78'), # NW 0x02 RTN DIV DUM DUM GDON GDOFF
90+
(_SETGAMMA, b'\x02\x0A\x11\x1d\x23\x35\x41\x4b\x4b\x42\x3A\x27\x1B\x08\x09\x03\x02' \
91+
b'\x0A\x11\x1d\x23\x35\x41\x4b\x4b\x42\x3A\x27\x1B\x08\x09\x03\x00\x01'),
92+
(_COLMOD, b'\x55'), # 16 bit
93+
(_MADCTL, b'\xc0'),
94+
(_TEON, b'\x00'),
95+
(_TEARLINE, b'\x00\x02'), # TW off
96+
(_SLPOUT, None),
97+
(_MADCTL, b'\xa0'),
98+
(_DISPON, None),
99+
)
100+
_ENCODE_PIXEL = ">H"
101+
_ENCODE_POS = ">HH"
102+
103+
#pylint: disable-msg=useless-super-delegation, too-many-arguments
104+
def __init__(self, spi, dc, cs, rst=None, width=480, height=320,
105+
baudrate=16000000, polarity=0, phase=0):
106+
super().__init__(spi, dc, cs, rst, width, height,
107+
baudrate=baudrate, polarity=polarity, phase=phase)

adafruit_rgb_display/rgb.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class Display: #pylint: disable-msg=no-member
8080
_COLUMN_SET = None
8181
_RAM_WRITE = None
8282
_RAM_READ = None
83+
_X_START = 0
84+
_Y_START = 0
8385
_INIT = ()
8486
_ENCODE_PIXEL = ">H"
8587
_ENCODE_POS = ">HH"
@@ -98,8 +100,8 @@ def init(self):
98100
#pylint: disable-msg=invalid-name,too-many-arguments
99101
def _block(self, x0, y0, x1, y1, data=None):
100102
"""Read or write a block of data."""
101-
self.write(self._COLUMN_SET, self._encode_pos(x0, x1))
102-
self.write(self._PAGE_SET, self._encode_pos(y0, y1))
103+
self.write(self._COLUMN_SET, self._encode_pos(x0 + self._X_START, x1 + self._X_START))
104+
self.write(self._PAGE_SET, self._encode_pos(y0 + self._Y_START, y1 + self._Y_START))
103105
if data is None:
104106
size = struct.calcsize(self._DECODE_PIXEL)
105107
return self.read(self._RAM_READ,

adafruit_rgb_display/st7789.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2019 Melissa LeBlanc-Williams for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_rgb_display.st7789`
24+
====================================================
25+
26+
A simple driver for the ST7789-based displays.
27+
28+
* Author(s): Melissa LeBlanc-Williams
29+
"""
30+
31+
try:
32+
import struct
33+
except ImportError:
34+
import ustruct as struct
35+
from micropython import const
36+
from adafruit_rgb_display.rgb import DisplaySPI
37+
38+
__version__ = "0.0.0-auto.0"
39+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RGB_Display.git"
40+
41+
_NOP = const(0x00)
42+
_SWRESET = const(0x01)
43+
_RDDID = const(0x04)
44+
_RDDST = const(0x09)
45+
46+
_SLPIN = const(0x10)
47+
_SLPOUT = const(0x11)
48+
_PTLON = const(0x12)
49+
_NORON = const(0x13)
50+
51+
_INVOFF = const(0x20)
52+
_INVON = const(0x21)
53+
_DISPOFF = const(0x28)
54+
_DISPON = const(0x29)
55+
_CASET = const(0x2A)
56+
_RASET = const(0x2B)
57+
_RAMWR = const(0x2C)
58+
_RAMRD = const(0x2E)
59+
60+
_PTLAR = const(0x30)
61+
_COLMOD = const(0x3A)
62+
_MADCTL = const(0x36)
63+
64+
_FRMCTR1 = const(0xB1)
65+
_FRMCTR2 = const(0xB2)
66+
_FRMCTR3 = const(0xB3)
67+
_INVCTR = const(0xB4)
68+
_DISSET5 = const(0xB6)
69+
70+
_PWCTR1 = const(0xC0)
71+
_PWCTR2 = const(0xC1)
72+
_PWCTR3 = const(0xC2)
73+
_PWCTR4 = const(0xC3)
74+
_PWCTR5 = const(0xC4)
75+
_VMCTR1 = const(0xC5)
76+
77+
_RDID1 = const(0xDA)
78+
_RDID2 = const(0xDB)
79+
_RDID3 = const(0xDC)
80+
_RDID4 = const(0xDD)
81+
82+
_PWCTR6 = const(0xFC)
83+
84+
_GMCTRP1 = const(0xE0)
85+
_GMCTRN1 = const(0xE1)
86+
87+
88+
class ST7789(DisplaySPI):
89+
"""
90+
A simple driver for the ST7789-based displays.
91+
92+
>>> import busio
93+
>>> import digitalio
94+
>>> import board
95+
>>> from adafruit_rgb_display import color565
96+
>>> import adafruit_rgb_display.st7789 as st7789
97+
>>> spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
98+
>>> display = st7789.ST7789(spi, cs=digitalio.DigitalInOut(board.GPIO0),
99+
... dc=digitalio.DigitalInOut(board.GPIO15), rst=digitalio.DigitalInOut(board.GPIO16))
100+
>>> display.fill(0x7521)
101+
>>> display.pixel(64, 64, 0)
102+
"""
103+
_COLUMN_SET = _CASET
104+
_PAGE_SET = _RASET
105+
_RAM_WRITE = _RAMWR
106+
_RAM_READ = _RAMRD
107+
_Y_START = 80
108+
_INIT = (
109+
(_SWRESET, None),
110+
(_SLPOUT, None),
111+
(_COLMOD, b'\x55'), # 16bit color
112+
(_MADCTL, b'\x08'),
113+
)
114+
115+
#pylint: disable-msg=useless-super-delegation, too-many-arguments
116+
def __init__(self, spi, dc, cs, rst=None, width=240, height=240,
117+
baudrate=16000000, polarity=0, phase=0):
118+
super().__init__(spi, dc, cs, rst, width, height,
119+
baudrate=baudrate, polarity=polarity, phase=phase)
120+
121+
def init(self):
122+
super().init()
123+
cols = struct.pack('>HH', 0, self.width)
124+
rows = struct.pack('>HH', self._Y_START, self.height + self._Y_START)
125+
for command, data in (
126+
(_CASET, cols),
127+
(_RASET, rows),
128+
(_INVON, None),
129+
(_NORON, None),
130+
(_DISPON, None),
131+
(_MADCTL, b'\xc0'), #Set rotation to 0 and use RGB
132+
):
133+
self.write(command, data)

docs/examples.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Simple test
33

44
Ensure your device works with this simple test.
55

6-
.. literalinclude:: ../examples/esp8266_tft_featherwing.py
7-
:caption: examples/esp8266_tft_featherwing.py
6+
.. literalinclude:: ../examples/rgbdisplay_simpletest.py
7+
:caption: examples/rgbdisplay_simpletest.py
8+
:linenos:
9+
10+
.. literalinclude:: ../examples/rgbdisplay_ili9341test.py
11+
:caption: examples/rgbdisplay_ili9341test.py
812
:linenos:

examples/rgbdisplay_hx8357test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Quick test of 3.5" TFT FeatherWing (HX8357) with Feather M0 or M4
2+
# This will work even on a device running displayio
3+
# Will fill the TFT black and put a red pixel in the center, wait 2 seconds,
4+
# then fill the screen blue (with no pixel), wait 2 seconds, and repeat.
5+
import time
6+
import random
7+
import digitalio
8+
import board
9+
import displayio
10+
11+
from adafruit_rgb_display.rgb import color565
12+
import adafruit_rgb_display.hx8357 as hx8357
13+
14+
displayio.release_displays()
15+
16+
# Configuration for CS and DC pins (these are TFT FeatherWing defaults):
17+
cs_pin = digitalio.DigitalInOut(board.D9)
18+
dc_pin = digitalio.DigitalInOut(board.D10)
19+
20+
# Config for display baudrate (default max is 24mhz):
21+
BAUDRATE = 24000000
22+
23+
# Setup SPI bus using hardware SPI:
24+
spi = board.SPI()
25+
26+
# Create the HX8357 display:
27+
display = hx8357.HX8357(spi, cs=cs_pin, dc=dc_pin, baudrate=BAUDRATE)
28+
29+
# Main loop:
30+
while True:
31+
# Fill the screen red, green, blue, then black:
32+
for color in ((255, 0, 0), (0, 255, 0), (0, 0, 255)):
33+
display.fill(color565(color))
34+
# Clear the display
35+
display.fill(0)
36+
# Draw a red pixel in the center.
37+
display.pixel(display.width//2, display.height//2, color565(255, 0, 0))
38+
# Pause 2 seconds.
39+
time.sleep(2)
40+
# Clear the screen a random color
41+
display.fill(color565(random.randint(0, 255),
42+
random.randint(0, 255),
43+
random.randint(0, 255)))
44+
# Pause 2 seconds.
45+
time.sleep(2)

examples/rgbdisplay_simpletest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Quick test of TFT FeatherWing (ST7789) with Feather M0 or M4
2+
# This will work even on a device running displayio
3+
# Will fill the TFT black and put a red pixel in the center, wait 2 seconds,
4+
# then fill the screen blue (with no pixel), wait 2 seconds, and repeat.
5+
import time
6+
import random
7+
import digitalio
8+
import board
9+
import displayio
10+
11+
from adafruit_rgb_display.rgb import color565
12+
import adafruit_rgb_display.st7789 as st7789
13+
14+
displayio.release_displays()
15+
16+
# Configuratoin for CS and DC pins (these are FeatherWing defaults on M0/M4):
17+
cs_pin = digitalio.DigitalInOut(board.D5)
18+
dc_pin = digitalio.DigitalInOut(board.D6)
19+
reset_pin = digitalio.DigitalInOut(board.D9)
20+
21+
# Config for display baudrate (default max is 24mhz):
22+
BAUDRATE = 24000000
23+
24+
# Setup SPI bus using hardware SPI:
25+
spi = board.SPI()
26+
27+
# Create the ST7789 display:
28+
display = st7789.ST7789(spi, cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE)
29+
30+
# Main loop:
31+
while True:
32+
# Fill the screen red, green, blue, then black:
33+
for color in ((255, 0, 0), (0, 255, 0), (0, 0, 255)):
34+
display.fill(color565(color))
35+
# Clear the display
36+
display.fill(0)
37+
# Draw a red pixel in the center.
38+
display.pixel(display.width//2, display.height//2, color565(255, 0, 0))
39+
# Pause 2 seconds.
40+
time.sleep(2)
41+
# Clear the screen a random color
42+
display.fill(color565(random.randint(0, 255),
43+
random.randint(0, 255),
44+
random.randint(0, 255)))
45+
# Pause 2 seconds.
46+
time.sleep(2)

0 commit comments

Comments
 (0)