Skip to content

Added rotation property and added HX8357 support #38

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 2 commits into from
Oct 11, 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
5 changes: 3 additions & 2 deletions adafruit_rgb_display/hx8353.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ class HX8353(DisplaySPI):
_ENCODE_POS = ">HH"

#pylint: disable-msg=useless-super-delegation, too-many-arguments
def __init__(self, spi, dc, cs, rst=None, width=128, height=128):
super().__init__(spi, dc, cs, rst, width, height)
def __init__(self, spi, dc, cs, rst=None, width=128, height=128,
rotation=0):
super().__init__(spi, dc, cs, rst, width, height, rotation)
5 changes: 3 additions & 2 deletions adafruit_rgb_display/hx8357.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class HX8357(DisplaySPI):

#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):
baudrate=16000000, polarity=0, phase=0, rotation=0):
super().__init__(spi, dc, cs, rst, width, height,
baudrate=baudrate, polarity=polarity, phase=phase)
baudrate=baudrate, polarity=polarity, phase=phase,
rotation=rotation)
5 changes: 3 additions & 2 deletions adafruit_rgb_display/ili9341.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ class ILI9341(DisplaySPI):

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

Expand Down
23 changes: 19 additions & 4 deletions adafruit_rgb_display/rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,12 @@ class Display: #pylint: disable-msg=no-member
_ENCODE_POS = ">HH"
_DECODE_PIXEL = ">BBB"

def __init__(self, width, height):
def __init__(self, width, height, rotation):
self.width = width
self.height = height
if rotation not in (0, 90, 180, 270):
raise ValueError('Rotation must be 0/90/180/270')
self._rotation = rotation
self.init()

def init(self):
Expand Down Expand Up @@ -161,9 +164,11 @@ def pixel(self, x, y, color=None):
self._block(x, y, x, y, self._encode_pixel(color))
return None

def image(self, img, rotation=0):
def image(self, img, rotation=None):
"""Set buffer to value of Python Imaging Library image. The image should
be in 1 bit mode and a size equal to the display size."""
if rotation is None:
rotation = self.rotation
if not img.mode in ('RGB', 'RGBA'):
raise ValueError('Image must be in mode RGB or RGBA')
if rotation not in (0, 90, 180, 270):
Expand Down Expand Up @@ -215,13 +220,23 @@ def vline(self, x, y, height, color):
"""Draw a vertical line."""
self.fill_rectangle(x, y, 1, height, color)

@property
def rotation(self):
"""Set the default rotation"""
return self._rotation

@rotation.setter
def rotation(self, val):
if val not in (0, 90, 180, 270):
raise ValueError('Rotation must be 0/90/180/270')
self._rotation = val

class DisplaySPI(Display):
"""Base class for SPI type devices"""
#pylint: disable-msg=too-many-arguments
def __init__(self, spi, dc, cs, rst=None, width=1, height=1,
baudrate=12000000, polarity=0, phase=0, *,
x_offset=0, y_offset=0):
x_offset=0, y_offset=0, rotation=0):
self.spi_device = spi_device.SPIDevice(spi, cs, baudrate=baudrate,
polarity=polarity, phase=phase)
self.dc_pin = dc
Expand All @@ -232,7 +247,7 @@ def __init__(self, spi, dc, cs, rst=None, width=1, height=1,
self.reset()
self._X_START = x_offset # pylint: disable=invalid-name
self._Y_START = y_offset # pylint: disable=invalid-name
super().__init__(width, height)
super().__init__(width, height, rotation)
#pylint: enable-msg=too-many-arguments

def reset(self):
Expand Down
4 changes: 2 additions & 2 deletions adafruit_rgb_display/s6d02a1.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ class S6D02A1(DisplaySPI):
_ENCODE_POS = ">HH"

#pylint: disable-msg=useless-super-delegation, too-many-arguments
def __init__(self, spi, dc, cs, rst=None, width=128, height=160):
super().__init__(spi, dc, cs, rst, width, height)
def __init__(self, spi, dc, cs, rst=None, width=128, height=160, rotation=0):
super().__init__(spi, dc, cs, rst, width, height, rotation)
4 changes: 2 additions & 2 deletions adafruit_rgb_display/ssd1331.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ class SSD1331(DisplaySPI):
# pylint: disable-msg=useless-super-delegation, too-many-arguments
# super required to allow override of default values
# All arguments needed due to driver requiring all the given data to function
def __init__(self, spi, dc, cs, rst=None, width=96, height=64):
super().__init__(spi, dc, cs, rst, width, height)
def __init__(self, spi, dc, cs, rst=None, width=96, height=64, rotation=0):
super().__init__(spi, dc, cs, rst, width, height, rotation)

# pylint: disable=no-member
def write(self, command=None, data=None):
Expand Down
5 changes: 3 additions & 2 deletions adafruit_rgb_display/ssd1351.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,6 @@ class SSD1351(DisplaySPI):
_ENCODE_POS = ">BB"

#pylint: disable-msg=useless-super-delegation, too-many-arguments
def __init__(self, spi, dc, cs, rst=None, width=128, height=128):
super().__init__(spi, dc, cs, rst, width, height)
def __init__(self, spi, dc, cs, rst=None, width=128, height=128,
rotation=0):
super().__init__(spi, dc, cs, rst, width, height, rotation)
8 changes: 4 additions & 4 deletions adafruit_rgb_display/st7735.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ class ST7735(DisplaySPI):
_ENCODE_POS = ">HH"

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


class ST7735R(ST7735):
Expand Down Expand Up @@ -167,8 +167,8 @@ class ST7735R(ST7735):
)

#pylint: disable-msg=useless-super-delegation, too-many-arguments
def __init__(self, spi, dc, cs, rst=None, width=128, height=160):
super().__init__(spi, dc, cs, rst, width, height)
def __init__(self, spi, dc, cs, rst=None, width=128, height=160, rotation=0):
super().__init__(spi, dc, cs, rst, width, height, rotation)

def init(self):
super().init()
Expand Down
4 changes: 2 additions & 2 deletions adafruit_rgb_display/st7789.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ class ST7789(DisplaySPI):
#pylint: disable-msg=useless-super-delegation, too-many-arguments
def __init__(self, spi, dc, cs, rst=None, width=240, height=320,
baudrate=16000000, polarity=0, phase=0, *,
x_offset=0, y_offset=0):
x_offset=0, y_offset=0, rotation=0):
super().__init__(spi, dc, cs, rst, width, height,
baudrate=baudrate, polarity=polarity, phase=phase,
x_offset=x_offset, y_offset=y_offset)
x_offset=x_offset, y_offset=y_offset, rotation=rotation)
def init(self):

super().init()
Expand Down
25 changes: 15 additions & 10 deletions examples/rgb_display_pillow_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from PIL import Image, ImageDraw, ImageFont
import adafruit_rgb_display.ili9341 as ili9341
import adafruit_rgb_display.st7789 as st7789 # pylint: disable=unused-import
import adafruit_rgb_display.hx8357 as hx8357 # pylint: disable=unused-import

# First define some constants to allow easy resizing of shapes.
BORDER = 20
Expand All @@ -14,31 +15,35 @@
reset_pin = digitalio.DigitalInOut(board.D24)

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

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

# Create the display:
#disp = st7789.ST7789(spi, # 2.0" ST7789
#disp = st7789.ST7789(spi, width=240, height=240, y_offset=80, # 1.3", 1.54" ST7789
disp = ili9341.ILI9341(spi, # 2.2", 2.4", 2.8", 3.2" ILI9341
#disp = st7789.ST7789(spi, rotation=90 # 2.0" ST7789
#disp = st7789.ST7789(spi, height=240, y_offset=80, rotation=90 # 1.3", 1.54" ST7789
#disp = hx8357.HX8357(spi, rotation=180, # 3.5" HX8357
disp = ili9341.ILI9341(spi, rotation=90, # 2.2", 2.4", 2.8", 3.2" ILI9341
cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE)


# Create blank image for drawing.
# Make sure to create image with mode 'RGB' for full color.
height = disp.width # we swap height/width to rotate it to landscape!
width = disp.height
if disp.rotation % 180 == 90:
height = disp.width # we swap height/width to rotate it to landscape!
width = disp.height
else:
width = disp.width # we swap height/width to rotate it to landscape!
height = disp.height

image = Image.new('RGB', (width, height))
rotation = 90

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a green filled box as the background
draw.rectangle((0, 0, width, height), fill=(0, 255, 0))
disp.image(image, rotation)
disp.image(image)

# Draw a smaller inner purple rectangle
draw.rectangle((BORDER, BORDER, width - BORDER - 1, height - BORDER - 1),
Expand All @@ -54,4 +59,4 @@
text, font=font, fill=(255, 255, 0))

# Display image.
disp.image(image, rotation)
disp.image(image)
23 changes: 14 additions & 9 deletions examples/rgb_display_pillow_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,42 @@
from PIL import Image, ImageDraw
import adafruit_rgb_display.ili9341 as ili9341
import adafruit_rgb_display.st7789 as st7789 # pylint: disable=unused-import
import adafruit_rgb_display.hx8357 as hx8357 # pylint: disable=unused-import

# Configuration for CS and DC pins (these are PiTFT defaults):
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = digitalio.DigitalInOut(board.D24)

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

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

# Create the display:
#disp = st7789.ST7789(spi, # 2.0" ST7789
#disp = st7789.ST7789(spi, width=240, height=240, y_offset=80, # 1.3", 1.54" ST7789
disp = ili9341.ILI9341(spi, # 2.2", 2.4", 2.8", 3.2" ILI9341
#disp = st7789.ST7789(spi, rotation=90 # 2.0" ST7789
#disp = st7789.ST7789(spi, height=240, y_offset=80, rotation=90 # 1.3", 1.54" ST7789
#disp = hx8357.HX8357(spi, rotation=180, # 3.5" HX8357
disp = ili9341.ILI9341(spi, rotation=90, # 2.2", 2.4", 2.8", 3.2" ILI9341
cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE)

# Create blank image for drawing.
# Make sure to create image with mode 'RGB' for full color.
height = disp.width # we swap height/width to rotate it to landscape!
width = disp.height
if disp.rotation % 180 == 90:
height = disp.width # we swap height/width to rotate it to landscape!
width = disp.height
else:
width = disp.width # we swap height/width to rotate it to landscape!
height = disp.height
image = Image.new('RGB', (width, height))
rotation = 90

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
disp.image(image, rotation)
disp.image(image)

image = Image.open("blinka.jpg")

Expand All @@ -54,4 +59,4 @@
image = image.crop((x, y, x + width, y + height))

# Display image.
disp.image(image, rotation)
disp.image(image)
24 changes: 15 additions & 9 deletions examples/rgb_display_pillow_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,43 @@
from PIL import Image, ImageDraw, ImageFont
import adafruit_rgb_display.ili9341 as ili9341
import adafruit_rgb_display.st7789 as st7789 # pylint: disable=unused-import
import adafruit_rgb_display.hx8357 as hx8357 # pylint: disable=unused-import

# Configuration for CS and DC pins (these are PiTFT defaults):
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = digitalio.DigitalInOut(board.D24)

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

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

# Create the display:
#disp = st7789.ST7789(spi, # 2.0" ST7789
#disp = st7789.ST7789(spi, width=240, height=240, y_offset=80, # 1.3", 1.54" ST7789
disp = ili9341.ILI9341(spi, # 2.2", 2.4", 2.8", 3.2" ILI9341
#disp = st7789.ST7789(spi, rotation=90 # 2.0" ST7789
#disp = st7789.ST7789(spi, height=240, y_offset=80, rotation=90 # 1.3", 1.54" ST7789
#disp = hx8357.HX8357(spi, rotation=180, # 3.5" HX8357
disp = ili9341.ILI9341(spi, rotation=90, # 2.2", 2.4", 2.8", 3.2" ILI9341
cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE)

# Create blank image for drawing.
# Make sure to create image with mode 'RGB' for full color.
height = disp.width # we swap height/width to rotate it to landscape!
width = disp.height
if disp.rotation % 180 == 90:
height = disp.width # we swap height/width to rotate it to landscape!
width = disp.height
else:
width = disp.width # we swap height/width to rotate it to landscape!
height = disp.height

image = Image.new('RGB', (width, height))
rotation = 90

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
disp.image(image, rotation)
disp.image(image)

# First define some constants to allow easy positioning of text.
padding = -2
Expand Down Expand Up @@ -76,5 +82,5 @@
draw.text((x, y), Temp, font=font, fill="#FF00FF")

# Display image.
disp.image(image, rotation)
disp.image(image)
time.sleep(.1)