diff --git a/adafruit_rgb_display/hx8353.py b/adafruit_rgb_display/hx8353.py index cef40b1..3d6592a 100644 --- a/adafruit_rgb_display/hx8353.py +++ b/adafruit_rgb_display/hx8353.py @@ -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) diff --git a/adafruit_rgb_display/hx8357.py b/adafruit_rgb_display/hx8357.py index d835e2a..a2cea8b 100755 --- a/adafruit_rgb_display/hx8357.py +++ b/adafruit_rgb_display/hx8357.py @@ -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) diff --git a/adafruit_rgb_display/ili9341.py b/adafruit_rgb_display/ili9341.py index d06342e..9b52adf 100644 --- a/adafruit_rgb_display/ili9341.py +++ b/adafruit_rgb_display/ili9341.py @@ -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 diff --git a/adafruit_rgb_display/rgb.py b/adafruit_rgb_display/rgb.py index bbdc4fa..7026212 100644 --- a/adafruit_rgb_display/rgb.py +++ b/adafruit_rgb_display/rgb.py @@ -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): @@ -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): @@ -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 @@ -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): diff --git a/adafruit_rgb_display/s6d02a1.py b/adafruit_rgb_display/s6d02a1.py index 7beb0f8..b23e04f 100644 --- a/adafruit_rgb_display/s6d02a1.py +++ b/adafruit_rgb_display/s6d02a1.py @@ -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) diff --git a/adafruit_rgb_display/ssd1331.py b/adafruit_rgb_display/ssd1331.py index 5c087f3..d771e83 100644 --- a/adafruit_rgb_display/ssd1331.py +++ b/adafruit_rgb_display/ssd1331.py @@ -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): diff --git a/adafruit_rgb_display/ssd1351.py b/adafruit_rgb_display/ssd1351.py index ba6153d..adbb896 100644 --- a/adafruit_rgb_display/ssd1351.py +++ b/adafruit_rgb_display/ssd1351.py @@ -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) diff --git a/adafruit_rgb_display/st7735.py b/adafruit_rgb_display/st7735.py index 2cb4073..bd55ce5 100644 --- a/adafruit_rgb_display/st7735.py +++ b/adafruit_rgb_display/st7735.py @@ -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): @@ -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() diff --git a/adafruit_rgb_display/st7789.py b/adafruit_rgb_display/st7789.py index e735aef..463f0be 100644 --- a/adafruit_rgb_display/st7789.py +++ b/adafruit_rgb_display/st7789.py @@ -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() diff --git a/examples/rgb_display_pillow_demo.py b/examples/rgb_display_pillow_demo.py index fd4e7dd..dad4e84 100644 --- a/examples/rgb_display_pillow_demo.py +++ b/examples/rgb_display_pillow_demo.py @@ -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 @@ -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), @@ -54,4 +59,4 @@ text, font=font, fill=(255, 255, 0)) # Display image. -disp.image(image, rotation) +disp.image(image) diff --git a/examples/rgb_display_pillow_image.py b/examples/rgb_display_pillow_image.py index a858bfc..04fbcd5 100644 --- a/examples/rgb_display_pillow_image.py +++ b/examples/rgb_display_pillow_image.py @@ -3,6 +3,7 @@ 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) @@ -10,30 +11,34 @@ 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") @@ -54,4 +59,4 @@ image = image.crop((x, y, x + width, y + height)) # Display image. -disp.image(image, rotation) +disp.image(image) diff --git a/examples/rgb_display_pillow_stats.py b/examples/rgb_display_pillow_stats.py index 8d4693a..fc9fb4c 100644 --- a/examples/rgb_display_pillow_stats.py +++ b/examples/rgb_display_pillow_stats.py @@ -5,6 +5,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 # Configuration for CS and DC pins (these are PiTFT defaults): cs_pin = digitalio.DigitalInOut(board.CE0) @@ -12,30 +13,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 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 @@ -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)