Merge branch 'master' of github.com:pimoroni/enviroplus-python into patch-adc
[EVA-2020-02-2.git] / examples / lcd.py
CommitLineData
a155e335
SM
1#!/usr/bin/env python
2
3import ST7735
4from PIL import Image, ImageDraw, ImageFont
5
6print("""lcd.py - Hello, World! example on the 0.96" LCD.
7
8Press Ctrl+C to exit!
9
10""")
11
12# Create LCD class instance.
13disp = ST7735.ST7735(
14 port=0,
15 cs=1,
16 dc=9,
17 backlight=12,
18 rotation=270,
19 spi_speed_hz=10000000
20)
21
22# Initialize display.
23disp.begin()
24
25# Width and height to calculate text position.
26WIDTH = disp.width
27HEIGHT = disp.height
28
29# New canvas to draw on.
30img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0))
31draw = ImageDraw.Draw(img)
32
33# Text settings.
34font_size = 25
35font = ImageFont.truetype("fonts/Asap/Asap-Bold.ttf", font_size)
36text_colour = (255, 255, 255)
37back_colour = (0, 170, 170)
38
39message = "Hello, World!"
40size_x, size_y = draw.textsize(message, font)
41
42# Calculate text position
43x = (WIDTH - size_x) / 2
44y = (HEIGHT / 2) - (size_y / 2)
45
46# Draw background rectangle and write text.
47draw.rectangle((0, 0, 160, 80), back_colour)
48draw.text((x, y), message, font=font, fill=text_colour)
49disp.display(img)
50
51# Keep running.
52try:
53 while True:
54 pass
55
56# Turn off backlight on control-c
57except KeyboardInterrupt:
58 disp.set_backlight(0)