This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplay.py
101 lines (80 loc) · 2.51 KB
/
display.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
This demo will fill the screen with white, draw a black box on top
and then print Hello World! in the center of the display
This example is for use on (Linux) computers that are using CPython with
Adafruit Blinka to support CircuitPython libraries. CircuitPython does
not support PIL/pillow (python imaging library)!
"""
import sys
import time
import board
import busio
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_pcd8544
# Parameters to Change
BORDER = 5
FONTSIZE = 10
spi = busio.SPI(board.SCK, MOSI=board.MOSI)
# Check pinout.xyz
# D6 = GPIO6
# dc = digitalio.DigitalInOut(board.D6) # data/command
dc = digitalio.DigitalInOut(board.D12) # data/command
cs = digitalio.DigitalInOut(board.CE0) # Chip select
# reset = digitalio.DigitalInOut(board.D5) # reset
reset = digitalio.DigitalInOut(board.D24) # reset
display = adafruit_pcd8544.PCD8544(spi, dc, cs, reset)
# Contrast and Brightness Settings
display.bias = 4
display.contrast = 60
# Turn on the Backlight LED
# backlight = digitalio.DigitalInOut(board.D13) # backlight
# backlight.switch_to_output()
# backlight.value = True
# Clear display.
display.fill(0)
display.show()
time.sleep(1)
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
def make_image(text):
text = str(text)
len_ = len(text.split())
if len_ != 5:
text += ' err'
image = Image.new("1", (display.width, display.height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black background
draw.rectangle((0, 0, display.width-1, display.height-1), outline=255, fill=0)
# Draw a smaller inner rectangle
draw.rectangle(
(BORDER, BORDER, display.width - BORDER - 1, display.height - BORDER - 1),
outline=0,
fill=0,
)
# Load a TTF font.
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FONTSIZE)
# Draw Some Text
(font_width, font_height) = font.getsize(text)
draw.text(
(display.width // 2 - font_width // 2, display.height // 2 - font_height // 2),
text,
font=font,
fill=255,
)
return image
def show(text):
print(f'\n{text}')
# reset display
display.fill(0)
display.show()
time.sleep(1)
# print text on display
image = make_image(text)
display.image(image)
display.show()
if __name__ == '__main__':
show('5 4 3 2 1')