Adds mqtt example (#68)
[EVA-2020-02-2.git] / examples / lcd.py
... / ...
CommitLineData
1#!/usr/bin/env python3
2
3import ST7735
4from PIL import Image, ImageDraw, ImageFont
5from fonts.ttf import RobotoMedium as UserFont
6import logging
7
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.
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
42font = ImageFont.truetype(UserFont, font_size)
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)