Merge pull request #57 from pimoroni/test-tweaks-and-linting
[EVA-2020-02-2.git] / examples / lcd.py
CommitLineData
20442c9a 1#!/usr/bin/env python3
a155e335
SM
2
3import ST7735
4from PIL import Image, ImageDraw, ImageFont
20442c9a 5from fonts.ttf import RobotoMedium as UserFont
10b73e18 6import logging
a155e335 7
10b73e18
CM
8logging.basicConfig(
9 format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
10 level=logging.INFO,
11 datefmt='%Y-%m-%d %H:%M:%S')
12
13logging.info("""lcd.py - Hello, World! example on the 0.96" LCD.
a155e335
SM
14
15Press Ctrl+C to exit!
16
17""")
18
19# Create LCD class instance.
20disp = ST7735.ST7735(
21 port=0,
22 cs=1,
23 dc=9,
24 backlight=12,
25 rotation=270,
26 spi_speed_hz=10000000
27)
28
29# Initialize display.
30disp.begin()
31
32# Width and height to calculate text position.
33WIDTH = disp.width
34HEIGHT = disp.height
35
36# New canvas to draw on.
37img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0))
38draw = ImageDraw.Draw(img)
39
40# Text settings.
41font_size = 25
20442c9a 42font = ImageFont.truetype(UserFont, font_size)
a155e335
SM
43text_colour = (255, 255, 255)
44back_colour = (0, 170, 170)
45
46message = "Hello, World!"
47size_x, size_y = draw.textsize(message, font)
48
49# Calculate text position
50x = (WIDTH - size_x) / 2
51y = (HEIGHT / 2) - (size_y / 2)
52
53# Draw background rectangle and write text.
54draw.rectangle((0, 0, 160, 80), back_colour)
55draw.text((x, y), message, font=font, fill=text_colour)
56disp.display(img)
57
58# Keep running.
59try:
60 while True:
61 pass
62
63# Turn off backlight on control-c
64except KeyboardInterrupt:
65 disp.set_backlight(0)