Skip to content

Commit 131ba44

Browse files
committed
Initial commit, direct port to current Adafruit MicroPython API.
0 parents  commit 131ba44

19 files changed

+1445
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Radomir Dopieralski and 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 all
13+
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 THE
21+
SOFTWARE.

README.rst

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Display drivers for Adafruit MicroPython.
2+
=========================================
3+
4+
Port of display drivers from https://github.com/adafruit/micropython-adafruit-rgb-display
5+
to Adafruit MicroPython for use on Adafruit's SAMD21-based and other MicroPython
6+
boards.
7+
8+
Note that this driver currently won't work on micropython.org firmware, instead
9+
you want the micropython-adafruit-rgb-display driver linked above!

adafruit_rgb_display/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from adafruit_rgb_display.rgb import color565

adafruit_rgb_display/hx8353.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from adafruit_rgb_display.rgb import DisplaySPI
2+
3+
_SWRESET = const(0x01)
4+
_NORON = const(0x13)
5+
_INVOFF = const(0x20)
6+
_INVON = const(0x21)
7+
_DISPOFF = const(0x28)
8+
_DISPON = const(0x29)
9+
_CASET = const(0x2a)
10+
_PASET = const(0x2b)
11+
_RAMWR = const(0x2c)
12+
_RAMRD = const(0x2e)
13+
_MADCTL = const(0x36)
14+
_COLMOD = const(0x3a)
15+
16+
class HX8353(DisplaySPI):
17+
"""
18+
A simple driver for the ST7735-based displays.
19+
20+
>>> import nativeio
21+
>>> import board
22+
>>> from adafruit_rgb_display import color565
23+
>>> import adafruit_rgb_display.hx8353 as hx8353
24+
>>> spi = nativeio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
25+
>>> display = hx8353.HX8383(spi, cs=nativeio.DigitalInOut(board.GPIO0), dc=nativeio.DigitalInOut(board.GPIO15))
26+
>>> display.fill(0x7521)
27+
>>> display.pixel(64, 64, 0)
28+
"""
29+
_COLUMN_SET = _CASET
30+
_PAGE_SET = _PASET
31+
_RAM_WRITE = _RAMWR
32+
_RAM_READ = _RAMRD
33+
_INIT = (
34+
(_SWRESET, None),
35+
(_DISPON, None),
36+
)
37+
_ENCODE_PIXEL = ">H"
38+
_ENCODE_POS = ">HH"
39+
40+
def __init__(self, spi, dc, cs, rst=None, width=128, height=128):
41+
super().__init__(spi, dc, cs, rst, width, height)

adafruit_rgb_display/ili9341.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import ustruct
2+
from adafruit_rgb_display.rgb import DisplaySPI
3+
4+
5+
class ILI9341(DisplaySPI):
6+
"""
7+
A simple driver for the ILI9341/ILI9340-based displays.
8+
9+
10+
>>> import nativeio
11+
>>> import board
12+
>>> from adafruit_rgb_display import color565
13+
>>> import adafruit_rgb_display.ili9341 as ili9341
14+
>>> spi = nativeio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
15+
>>> display = ili9341.ILI9341(spi, cs=nativeio.DigitalInOut(board.GPIO0), dc=nativeio.DigitalInOut(board.GPIO15))
16+
>>> display.fill(color565(0xff, 0x11, 0x22))
17+
>>> display.pixel(120, 160, 0)
18+
"""
19+
20+
_COLUMN_SET = 0x2a
21+
_PAGE_SET = 0x2b
22+
_RAM_WRITE = 0x2c
23+
_RAM_READ = 0x2e
24+
_INIT = (
25+
(0xef, b'\x03\x80\x02'),
26+
(0xcf, b'\x00\xc1\x30'),
27+
(0xed, b'\x64\x03\x12\x81'),
28+
(0xe8, b'\x85\x00\x78'),
29+
(0xcb, b'\x39\x2c\x00\x34\x02'),
30+
(0xf7, b'\x20'),
31+
(0xea, b'\x00\x00'),
32+
(0xc0, b'\x23'), # Power Control 1, VRH[5:0]
33+
(0xc1, b'\x10'), # Power Control 2, SAP[2:0], BT[3:0]
34+
(0xc5, b'\x3e\x28'), # VCM Control 1
35+
(0xc7, b'\x86'), # VCM Control 2
36+
(0x36, b'\x48'), # Memory Access Control
37+
(0x3a, b'\x55'), # Pixel Format
38+
(0xb1, b'\x00\x18'), # FRMCTR1
39+
(0xb6, b'\x08\x82\x27'), # Display Function Control
40+
(0xf2, b'\x00'), # 3Gamma Function Disable
41+
(0x26, b'\x01'), # Gamma Curve Selected
42+
(0xe0, # Set Gamma
43+
b'\x0f\x31\x2b\x0c\x0e\x08\x4e\xf1\x37\x07\x10\x03\x0e\x09\x00'),
44+
(0xe1, # Set Gamma
45+
b'\x00\x0e\x14\x03\x11\x07\x31\xc1\x48\x08\x0f\x0c\x31\x36\x0f'),
46+
(0x11, None),
47+
(0x29, None),
48+
)
49+
_ENCODE_PIXEL = ">H"
50+
_ENCODE_POS = ">HH"
51+
_DECODE_PIXEL = ">BBB"
52+
53+
def __init__(self, spi, dc, cs, rst=None, width=240, height=320,
54+
baudrate=16000000, polarity=0, phase=0):
55+
super().__init__(spi, dc, cs, rst=rst, width=width, height=height,
56+
baudrate=baudrate, polarity=polarity, phase=phase)
57+
"""Test doc string."""
58+
self._scroll = 0
59+
60+
def scroll(self, dy=None):
61+
"""Test doc string 2."""
62+
if dy is None:
63+
return self._scroll
64+
self._scroll = (self._scroll + dy) % self.height
65+
self._write(0x37, ustruct.pack('>H', self._scroll))

adafruit_rgb_display/rgb.py

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import time
2+
import ustruct
3+
4+
import adafruit_busdevice.spi_device as spi_device
5+
6+
7+
def color565(r, g, b):
8+
return (r & 0xf8) << 8 | (g & 0xfc) << 3 | b >> 3
9+
10+
11+
class DummyPin:
12+
"""A fake gpio pin for when you want to skip pins."""
13+
def init(self, *args, **kwargs):
14+
pass
15+
16+
def low(self):
17+
pass
18+
19+
def high(self):
20+
pass
21+
22+
23+
class Display:
24+
_PAGE_SET = None
25+
_COLUMN_SET = None
26+
_RAM_WRITE = None
27+
_RAM_READ = None
28+
_INIT = ()
29+
_ENCODE_PIXEL = ">H"
30+
_ENCODE_POS = ">HH"
31+
_DECODE_PIXEL = ">BBB"
32+
33+
def __init__(self, width, height):
34+
self.width = width
35+
self.height = height
36+
self.init()
37+
38+
def init(self):
39+
"""Run the initialization commands."""
40+
for command, data in self._INIT:
41+
self._write(command, data)
42+
43+
def _block(self, x0, y0, x1, y1, data=None):
44+
"""Read or write a block of data."""
45+
self._write(self._COLUMN_SET, self._encode_pos(x0, x1))
46+
self._write(self._PAGE_SET, self._encode_pos(y0, y1))
47+
if data is None:
48+
size = ustruct.calcsize(self._DECODE_PIXEL)
49+
return self._read(self._RAM_READ,
50+
(x1 - x0 + 1) * (y1 - y0 + 1) * size)
51+
self._write(self._RAM_WRITE, data)
52+
53+
def _encode_pos(self, a, b):
54+
"""Encode a postion into bytes."""
55+
return ustruct.pack(self._ENCODE_POS, a, b)
56+
57+
def _encode_pixel(self, color):
58+
"""Encode a pixel color into bytes."""
59+
return ustruct.pack(self._ENCODE_PIXEL, color)
60+
61+
def _decode_pixel(self, data):
62+
"""Decode bytes into a pixel color."""
63+
return color565(*ustruct.unpack(self._DECODE_PIXEL, data))
64+
65+
def pixel(self, x, y, color=None):
66+
"""Read or write a pixel."""
67+
if color is None:
68+
return self._decode_pixel(self._block(x, y, x, y))
69+
if not 0 <= x < self.width or not 0 <= y < self.height:
70+
return
71+
self._block(x, y, x, y, self._encode_pixel(color))
72+
73+
def fill_rectangle(self, x, y, width, height, color):
74+
"""Draw a filled rectangle."""
75+
x = min(self.width - 1, max(0, x))
76+
y = min(self.height - 1, max(0, y))
77+
w = min(self.width - x, max(1, width))
78+
h = min(self.height - y, max(1, height))
79+
self._block(x, y, x + w - 1, y + h - 1, b'')
80+
chunks, rest = divmod(w * h, 512)
81+
pixel = self._encode_pixel(color)
82+
if chunks:
83+
data = pixel * 512
84+
for count in range(chunks):
85+
self._write(None, data)
86+
self._write(None, pixel * rest)
87+
88+
def fill(self, color=0):
89+
"""Fill whole screen."""
90+
self.fill_rectangle(0, 0, self.width, self.height, color)
91+
92+
def hline(self, x, y, width, color):
93+
"""Draw a horizontal line."""
94+
self.fill_rectangle(x, y, width, 1, color)
95+
96+
def vline(self, x, y, height, color):
97+
"""Draw a vertical line."""
98+
self.fill_rectangle(x, y, 1, height, color)
99+
100+
101+
class DisplaySPI(Display):
102+
def __init__(self, spi, dc, cs, rst=None, width=1, height=1, baudrate=1000000,
103+
polarity=0, phase=0):
104+
self.spi_device = spi_device.SPIDevice(spi, cs, baudrate=baudrate,
105+
polarity=polarity, phase=phase)
106+
self.dc = dc
107+
self.rst = rst
108+
self.dc.switch_to_output(value=0)
109+
if self.rst:
110+
self.rst.switch_to_output(value=0)
111+
self.reset()
112+
super().__init__(width, height)
113+
114+
def reset(self):
115+
self.rst.value = 0
116+
time.sleep(0.050) # 50 milliseconds
117+
self.rst.value = 1
118+
time.sleep(0.050) # 50 milliseconds
119+
120+
def _write(self, command=None, data=None):
121+
if command is not None:
122+
self.dc.value = 0
123+
with self.spi_device as spi:
124+
spi.write(bytearray([command]))
125+
if data is not None:
126+
self.dc.value = 1
127+
with self.spi_device as spi:
128+
spi.write(data)
129+
130+
def _read(self, command=None, count=0):
131+
self.dc.value = 0
132+
with self.spi_device as spi:
133+
if command is not None:
134+
spi.write(bytearray([command]))
135+
if count:
136+
data = spi.read(count)
137+
return data

adafruit_rgb_display/s6d02a1.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from adafruit_rgb_display.rgb import DisplaySPI
2+
3+
_SWRESET = const(0x01)
4+
_DISPON = const(0x29)
5+
_SLEEPOUT = const(0x11)
6+
_CASET = const(0x2a)
7+
_PASET = const(0x2b)
8+
_RAMWR = const(0x2c)
9+
_RAMRD = const(0x2e)
10+
_COLMOD = const(0x3a)
11+
_MADCTL = const(0x36)
12+
13+
class S6D02A1(DisplaySPI):
14+
"""
15+
A simple driver for the ST7735-based displays.
16+
17+
>>> import nativeio
18+
>>> import board
19+
>>> from adafruit_rgb_display import color565
20+
>>> import adafruit_rgb_display.s6d02a1 as s6d02a1
21+
>>> spi = nativeio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
22+
>>> display = s6d02a1.S6D02A1(spi, cs=nativeio.DigitalInOut(board.GPIO0), dc=nativeio.DigitalInOut(board.GPIO15), rst=nativeio.DigitalInOut(board.GPIO16))
23+
>>> display.fill(0x7521)
24+
>>> display.pixel(64, 64, 0)
25+
"""
26+
_COLUMN_SET = _CASET
27+
_PAGE_SET = _PASET
28+
_RAM_WRITE = _RAMWR
29+
_RAM_READ = _RAMRD
30+
_INIT = (
31+
(_SWRESET, None),
32+
(_SLEEPOUT, None),
33+
(_MADCTL, b'\x10'), # bottom-top
34+
(_COLMOD, b'\x05'), # RGB565 pixel format
35+
(_DISPON, None),
36+
)
37+
_ENCODE_PIXEL = ">H"
38+
_ENCODE_POS = ">HH"
39+
40+
def __init__(self, spi, dc, cs, rst=None, width=128, height=160):
41+
super().__init__(spi, dc, cs, rst, width, height)

0 commit comments

Comments
 (0)