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