| 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import ST7735 |
| 4 | from PIL import Image, ImageDraw, ImageFont |
| 5 | |
| 6 | print("""lcd.py - Hello, World! example on the 0.96" LCD. |
| 7 | |
| 8 | Press Ctrl+C to exit! |
| 9 | |
| 10 | """) |
| 11 | |
| 12 | # Create LCD class instance. |
| 13 | disp = 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. |
| 23 | disp.begin() |
| 24 | |
| 25 | # Width and height to calculate text position. |
| 26 | WIDTH = disp.width |
| 27 | HEIGHT = disp.height |
| 28 | |
| 29 | # New canvas to draw on. |
| 30 | img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0)) |
| 31 | draw = ImageDraw.Draw(img) |
| 32 | |
| 33 | # Text settings. |
| 34 | font_size = 25 |
| 35 | font = ImageFont.truetype("fonts/Asap/Asap-Bold.ttf", font_size) |
| 36 | text_colour = (255, 255, 255) |
| 37 | back_colour = (0, 170, 170) |
| 38 | |
| 39 | message = "Hello, World!" |
| 40 | size_x, size_y = draw.textsize(message, font) |
| 41 | |
| 42 | # Calculate text position |
| 43 | x = (WIDTH - size_x) / 2 |
| 44 | y = (HEIGHT / 2) - (size_y / 2) |
| 45 | |
| 46 | # Draw background rectangle and write text. |
| 47 | draw.rectangle((0, 0, 160, 80), back_colour) |
| 48 | draw.text((x, y), message, font=font, fill=text_colour) |
| 49 | disp.display(img) |
| 50 | |
| 51 | # Keep running. |
| 52 | try: |
| 53 | while True: |
| 54 | pass |
| 55 | |
| 56 | # Turn off backlight on control-c |
| 57 | except KeyboardInterrupt: |
| 58 | disp.set_backlight(0) |