Skip to content

Commit 8bd7903

Browse files
committed
Added rotation property and added HX8357 support
1 parent 1d34cd3 commit 8bd7903

File tree

12 files changed

+83
-50
lines changed

12 files changed

+83
-50
lines changed

adafruit_rgb_display/hx8353.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,6 @@ class HX8353(DisplaySPI):
7474
_ENCODE_POS = ">HH"
7575

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

adafruit_rgb_display/hx8357.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class HX8357(DisplaySPI):
102102

103103
#pylint: disable-msg=useless-super-delegation, too-many-arguments
104104
def __init__(self, spi, dc, cs, rst=None, width=480, height=320,
105-
baudrate=16000000, polarity=0, phase=0):
105+
baudrate=16000000, polarity=0, phase=0, rotation=0):
106106
super().__init__(spi, dc, cs, rst, width, height,
107-
baudrate=baudrate, polarity=polarity, phase=phase)
107+
baudrate=baudrate, polarity=polarity, phase=phase,
108+
rotation=rotation)

adafruit_rgb_display/ili9341.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ class ILI9341(DisplaySPI):
9090

9191
#pylint: disable-msg=too-many-arguments
9292
def __init__(self, spi, dc, cs, rst=None, width=240, height=320,
93-
baudrate=16000000, polarity=0, phase=0):
93+
baudrate=16000000, polarity=0, phase=0, rotation=0):
9494
super().__init__(spi, dc, cs, rst=rst, width=width, height=height,
95-
baudrate=baudrate, polarity=polarity, phase=phase)
95+
baudrate=baudrate, polarity=polarity, phase=phase,
96+
rotation=rotation)
9697
self._scroll = 0
9798
#pylint: enable-msg=too-many-arguments
9899

adafruit_rgb_display/rgb.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ class Display: #pylint: disable-msg=no-member
117117
_ENCODE_POS = ">HH"
118118
_DECODE_PIXEL = ">BBB"
119119

120-
def __init__(self, width, height):
120+
def __init__(self, width, height, rotation):
121121
self.width = width
122122
self.height = height
123+
self.rotation = rotation
123124
self.init()
124125

125126
def init(self):
@@ -161,9 +162,11 @@ def pixel(self, x, y, color=None):
161162
self._block(x, y, x, y, self._encode_pixel(color))
162163
return None
163164

164-
def image(self, img, rotation=0):
165+
def image(self, img, rotation=None):
165166
"""Set buffer to value of Python Imaging Library image. The image should
166167
be in 1 bit mode and a size equal to the display size."""
168+
if rotation is None:
169+
rotation = self.rotation
167170
if not img.mode in ('RGB', 'RGBA'):
168171
raise ValueError('Image must be in mode RGB or RGBA')
169172
if rotation not in (0, 90, 180, 270):
@@ -215,13 +218,23 @@ def vline(self, x, y, height, color):
215218
"""Draw a vertical line."""
216219
self.fill_rectangle(x, y, 1, height, color)
217220

221+
@property
222+
def rotation(self):
223+
"""Set the default rotation"""
224+
return self._rotation
225+
226+
@rotation.setter
227+
def rotation(self, val):
228+
if val not in (0, 90, 180, 270):
229+
raise ValueError('Rotation must be 0/90/180/270')
230+
self._rotation = val
218231

219232
class DisplaySPI(Display):
220233
"""Base class for SPI type devices"""
221234
#pylint: disable-msg=too-many-arguments
222235
def __init__(self, spi, dc, cs, rst=None, width=1, height=1,
223236
baudrate=12000000, polarity=0, phase=0, *,
224-
x_offset=0, y_offset=0):
237+
x_offset=0, y_offset=0, rotation=0):
225238
self.spi_device = spi_device.SPIDevice(spi, cs, baudrate=baudrate,
226239
polarity=polarity, phase=phase)
227240
self.dc_pin = dc
@@ -232,7 +245,7 @@ def __init__(self, spi, dc, cs, rst=None, width=1, height=1,
232245
self.reset()
233246
self._X_START = x_offset # pylint: disable=invalid-name
234247
self._Y_START = y_offset # pylint: disable=invalid-name
235-
super().__init__(width, height)
248+
super().__init__(width, height, rotation)
236249
#pylint: enable-msg=too-many-arguments
237250

238251
def reset(self):

adafruit_rgb_display/s6d02a1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ class S6D02A1(DisplaySPI):
7474
_ENCODE_POS = ">HH"
7575

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

adafruit_rgb_display/ssd1331.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ class SSD1331(DisplaySPI):
123123
# pylint: disable-msg=useless-super-delegation, too-many-arguments
124124
# super required to allow override of default values
125125
# All arguments needed due to driver requiring all the given data to function
126-
def __init__(self, spi, dc, cs, rst=None, width=96, height=64):
127-
super().__init__(spi, dc, cs, rst, width, height)
126+
def __init__(self, spi, dc, cs, rst=None, width=96, height=64, rotation=0):
127+
super().__init__(spi, dc, cs, rst, width, height, rotation)
128128

129129
# pylint: disable=no-member
130130
def write(self, command=None, data=None):

adafruit_rgb_display/ssd1351.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,6 @@ class SSD1351(DisplaySPI):
114114
_ENCODE_POS = ">BB"
115115

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

adafruit_rgb_display/st7735.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ class ST7735(DisplaySPI):
133133
_ENCODE_POS = ">HH"
134134

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

139139

140140
class ST7735R(ST7735):
@@ -167,8 +167,8 @@ class ST7735R(ST7735):
167167
)
168168

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

173173
def init(self):
174174
super().init()

adafruit_rgb_display/st7789.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ class ST7789(DisplaySPI):
114114
#pylint: disable-msg=useless-super-delegation, too-many-arguments
115115
def __init__(self, spi, dc, cs, rst=None, width=240, height=320,
116116
baudrate=16000000, polarity=0, phase=0, *,
117-
x_offset=0, y_offset=0):
117+
x_offset=0, y_offset=0, rotation=0):
118118
super().__init__(spi, dc, cs, rst, width, height,
119119
baudrate=baudrate, polarity=polarity, phase=phase,
120-
x_offset=x_offset, y_offset=y_offset)
120+
x_offset=x_offset, y_offset=y_offset, rotation=rotation)
121121
def init(self):
122122

123123
super().init()

examples/rgb_display_pillow_demo.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from PIL import Image, ImageDraw, ImageFont
44
import adafruit_rgb_display.ili9341 as ili9341
55
import adafruit_rgb_display.st7789 as st7789 # pylint: disable=unused-import
6+
import adafruit_rgb_display.hx8357 as hx8357 # pylint: disable=unused-import
67

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

1617
# Config for display baudrate (default max is 24mhz):
17-
BAUDRATE = 64000000
18+
BAUDRATE = 24000000
1819

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

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

28-
2930
# Create blank image for drawing.
3031
# Make sure to create image with mode 'RGB' for full color.
31-
height = disp.width # we swap height/width to rotate it to landscape!
32-
width = disp.height
32+
if disp.rotation % 180 == 90:
33+
height = disp.width # we swap height/width to rotate it to landscape!
34+
width = disp.height
35+
else:
36+
width = disp.width # we swap height/width to rotate it to landscape!
37+
height = disp.height
38+
3339
image = Image.new('RGB', (width, height))
34-
rotation = 90
3540

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

3944
# Draw a green filled box as the background
4045
draw.rectangle((0, 0, width, height), fill=(0, 255, 0))
41-
disp.image(image, rotation)
46+
disp.image(image)
4247

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

5661
# Display image.
57-
disp.image(image, rotation)
62+
disp.image(image)

examples/rgb_display_pillow_image.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,42 @@
33
from PIL import Image, ImageDraw
44
import adafruit_rgb_display.ili9341 as ili9341
55
import adafruit_rgb_display.st7789 as st7789 # pylint: disable=unused-import
6+
import adafruit_rgb_display.hx8357 as hx8357 # pylint: disable=unused-import
67

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

1213
# Config for display baudrate (default max is 24mhz):
13-
BAUDRATE = 64000000
14+
BAUDRATE = 24000000
1415

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

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

2426
# Create blank image for drawing.
2527
# Make sure to create image with mode 'RGB' for full color.
26-
height = disp.width # we swap height/width to rotate it to landscape!
27-
width = disp.height
28+
if disp.rotation % 180 == 90:
29+
height = disp.width # we swap height/width to rotate it to landscape!
30+
width = disp.height
31+
else:
32+
width = disp.width # we swap height/width to rotate it to landscape!
33+
height = disp.height
2834
image = Image.new('RGB', (width, height))
29-
rotation = 90
3035

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

3439
# Draw a black filled box to clear the image.
3540
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
36-
disp.image(image, rotation)
41+
disp.image(image)
3742

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

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

5661
# Display image.
57-
disp.image(image, rotation)
62+
disp.image(image)

examples/rgb_display_pillow_stats.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,43 @@
55
from PIL import Image, ImageDraw, ImageFont
66
import adafruit_rgb_display.ili9341 as ili9341
77
import adafruit_rgb_display.st7789 as st7789 # pylint: disable=unused-import
8+
import adafruit_rgb_display.hx8357 as hx8357 # pylint: disable=unused-import
89

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

1415
# Config for display baudrate (default max is 24mhz):
15-
BAUDRATE = 64000000
16+
BAUDRATE = 24000000
1617

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

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

2628
# Create blank image for drawing.
2729
# Make sure to create image with mode 'RGB' for full color.
28-
height = disp.width # we swap height/width to rotate it to landscape!
29-
width = disp.height
30+
if disp.rotation % 180 == 90:
31+
height = disp.width # we swap height/width to rotate it to landscape!
32+
width = disp.height
33+
else:
34+
width = disp.width # we swap height/width to rotate it to landscape!
35+
height = disp.height
36+
3037
image = Image.new('RGB', (width, height))
31-
rotation = 90
3238

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

3642
# Draw a black filled box to clear the image.
3743
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
38-
disp.image(image, rotation)
44+
disp.image(image)
3945

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

7884
# Display image.
79-
disp.image(image, rotation)
85+
disp.image(image)
8086
time.sleep(.1)

0 commit comments

Comments
 (0)