/ examples / ssd1306_pillow_demo.py
ssd1306_pillow_demo.py
 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  This example is for use on (Linux) computers that are using CPython with
 6  Adafruit Blinka to support CircuitPython libraries. CircuitPython does
 7  not support PIL/pillow (python imaging library)!
 8  """
 9  
10  import board
11  import digitalio
12  from PIL import Image, ImageDraw, ImageFont
13  import adafruit_ssd1306
14  
15  # Define the Reset Pin
16  oled_reset = digitalio.DigitalInOut(board.D4)
17  
18  # Change these
19  # to the right size for your display!
20  WIDTH = 128
21  HEIGHT = 32  # Change to 64 if needed
22  BORDER = 5
23  
24  # Use for I2C.
25  i2c = board.I2C()
26  oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset)
27  
28  # Use for SPI
29  # spi = board.SPI()
30  # oled_cs = digitalio.DigitalInOut(board.D5)
31  # oled_dc = digitalio.DigitalInOut(board.D6)
32  # oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, oled_dc, oled_reset, oled_cs)
33  
34  # Clear display.
35  oled.fill(0)
36  oled.show()
37  
38  # Create blank image for drawing.
39  # Make sure to create image with mode '1' for 1-bit color.
40  image = Image.new("1", (oled.width, oled.height))
41  
42  # Get drawing object to draw on image.
43  draw = ImageDraw.Draw(image)
44  
45  # Draw a white background
46  draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)
47  
48  # Draw a smaller inner rectangle
49  draw.rectangle(
50      (BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1),
51      outline=0,
52      fill=0,
53  )
54  
55  # Load default font.
56  font = ImageFont.load_default()
57  
58  # Draw Some Text
59  text = "Hello World!"
60  (font_width, font_height) = font.getsize(text)
61  draw.text(
62      (oled.width // 2 - font_width // 2, oled.height // 2 - font_height // 2),
63      text,
64      font=font,
65      fill=255,
66  )
67  
68  # Display image
69  oled.image(image)
70  oled.show()