Skip to content

Commit 2d34b26

Browse files
authored
Merge pull request #30 from makermelissa/master
Added PIL/Pillow Example that does the equivalent of displayio
2 parents bf99c6f + 86d3cb9 commit 2d34b26

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

examples/ssd1306_pillow_demo.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
This demo will fill the screen with white, draw a black box on top
3+
and then print Hello World! in the center of the display
4+
"""
5+
6+
import board
7+
import digitalio
8+
from PIL import Image, ImageDraw, ImageFont
9+
import adafruit_ssd1306
10+
11+
# Define the Reset Pin
12+
oled_reset = digitalio.DigitalInOut(board.D4)
13+
14+
# Change these
15+
# to the right size for your display!
16+
WIDTH = 128
17+
HEIGHT = 32 # Change to 64 if needed
18+
BORDER = 5
19+
20+
# Use for I2C.
21+
i2c = board.I2C()
22+
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3c, reset=oled_reset)
23+
24+
# Use for SPI
25+
spi = board.SPI()
26+
oled_cs = digitalio.DigitalInOut(board.D5)
27+
oled_dc = digitalio.DigitalInOut(board.D6)
28+
oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, oled_dc, oled_reset, oled_cs)
29+
30+
# Clear display.
31+
oled.fill(0)
32+
oled.show()
33+
34+
# Create blank image for drawing.
35+
# Make sure to create image with mode '1' for 1-bit color.
36+
image = Image.new('1', (oled.width, oled.height))
37+
38+
# Get drawing object to draw on image.
39+
draw = ImageDraw.Draw(image)
40+
41+
# Draw a white background
42+
draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)
43+
44+
# Draw a smaller inner rectangle
45+
draw.rectangle((BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1),
46+
outline=0, fill=0)
47+
48+
# Load default font.
49+
font = ImageFont.load_default()
50+
51+
# Draw Some Text
52+
text = "Hello World!"
53+
(font_width, font_height) = font.getsize(text)
54+
draw.text((oled.width//2 - font_width//2, oled.height//2 - font_height//2),
55+
text, font=font, fill=255)
56+
57+
# Display image
58+
oled.image(image)
59+
oled.show()

0 commit comments

Comments
 (0)