|
| 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 busio |
| 12 | +import digitalio |
| 13 | +from PIL import Image, ImageDraw, ImageFont |
| 14 | +import adafruit_pcd8544 |
| 15 | + |
| 16 | +# Define the Reset Pin |
| 17 | +oled_reset = digitalio.DigitalInOut(board.D4) |
| 18 | + |
| 19 | +# Parameters to Change |
| 20 | +BORDER = 5 |
| 21 | +FONTSIZE = 10 |
| 22 | + |
| 23 | +spi = busio.SPI(board.SCK, MOSI=board.MOSI) |
| 24 | +dc = digitalio.DigitalInOut(board.D6) # data/command |
| 25 | +cs = digitalio.DigitalInOut(board.CE0) # Chip select |
| 26 | +reset = digitalio.DigitalInOut(board.D5) # reset |
| 27 | + |
| 28 | +display = adafruit_pcd8544.PCD8544(spi, dc, cs, reset) |
| 29 | + |
| 30 | +# Contrast and Brightness Settings |
| 31 | +display.bias = 4 |
| 32 | +display.contrast = 60 |
| 33 | + |
| 34 | +# Turn on the Backlight LED |
| 35 | +backlight = digitalio.DigitalInOut(board.D13) # backlight |
| 36 | +backlight.switch_to_output() |
| 37 | +backlight.value = True |
| 38 | + |
| 39 | +# Clear display. |
| 40 | +display.fill(0) |
| 41 | +display.show() |
| 42 | + |
| 43 | +# Create blank image for drawing. |
| 44 | +# Make sure to create image with mode '1' for 1-bit color. |
| 45 | +image = Image.new('1', (display.width, display.height)) |
| 46 | + |
| 47 | +# Get drawing object to draw on image. |
| 48 | +draw = ImageDraw.Draw(image) |
| 49 | + |
| 50 | +# Draw a white background |
| 51 | +draw.rectangle((0, 0, display.width, display.height), outline=255, fill=255) |
| 52 | + |
| 53 | +# Draw a smaller inner rectangle |
| 54 | +draw.rectangle((BORDER, BORDER, display.width - BORDER - 1, display.height - BORDER - 1), |
| 55 | + outline=0, fill=0) |
| 56 | + |
| 57 | +# Load a TTF font. |
| 58 | +font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', FONTSIZE) |
| 59 | + |
| 60 | +# Draw Some Text |
| 61 | +text = "Hello World!" |
| 62 | +(font_width, font_height) = font.getsize(text) |
| 63 | +draw.text((display.width//2 - font_width//2, display.height//2 - font_height//2), |
| 64 | + text, font=font, fill=255) |
| 65 | + |
| 66 | +# Display image |
| 67 | +display.image(image) |
| 68 | +display.show() |
0 commit comments