|
| 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_sharpmemorydisplay |
| 15 | + |
| 16 | +# Colors |
| 17 | +BLACK = 0 |
| 18 | +WHITE = 255 |
| 19 | + |
| 20 | +# Parameters to Change |
| 21 | +BORDER = 5 |
| 22 | +FONTSIZE = 10 |
| 23 | + |
| 24 | +spi = busio.SPI(board.SCK, MOSI=board.MOSI) |
| 25 | +scs = digitalio.DigitalInOut(board.D6) # inverted chip select |
| 26 | + |
| 27 | +#display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 96, 96) |
| 28 | +display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 144, 168) |
| 29 | + |
| 30 | +# Clear display. |
| 31 | +display.fill(1) |
| 32 | +display.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', (display.width, display.height)) |
| 37 | + |
| 38 | +# Get drawing object to draw on image. |
| 39 | +draw = ImageDraw.Draw(image) |
| 40 | + |
| 41 | +# Draw a black background |
| 42 | +draw.rectangle((0, 0, display.width, display.height), outline=BLACK, fill=BLACK) |
| 43 | + |
| 44 | +# Draw a smaller inner rectangle |
| 45 | +draw.rectangle((BORDER, BORDER, display.width - BORDER - 1, display.height - BORDER - 1), |
| 46 | + outline=WHITE, fill=WHITE) |
| 47 | + |
| 48 | +# Load a TTF font. |
| 49 | +font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', FONTSIZE) |
| 50 | + |
| 51 | +# Draw Some Text |
| 52 | +text = "Hello World!" |
| 53 | +(font_width, font_height) = font.getsize(text) |
| 54 | +draw.text((display.width//2 - font_width//2, display.height//2 - font_height//2), |
| 55 | + text, font=font, fill=BLACK) |
| 56 | + |
| 57 | +# Display image |
| 58 | +display.image(image) |
| 59 | +display.show() |
0 commit comments