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