|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | # A simple Python manager for "Turing Smart Screen" 3.5" IPS USB-C display |
3 | 3 | # https://github.com/mathoudebine/turing-smart-screen-python |
4 | | - |
| 4 | +import signal |
| 5 | +from datetime import datetime |
5 | 6 | import struct |
6 | 7 | from time import sleep |
7 | 8 | import serial # Install pyserial : pip install pyserial |
8 | 9 | from PIL import Image, ImageDraw, ImageFont # Install PIL or Pillow |
9 | 10 |
|
10 | 11 | # Set your COM port e.g. COM3 for Windows, /dev/ttyACM0 for Linux... |
11 | | -COM_PORT = "/dev/ttyACM1" |
| 12 | +COM_PORT = "/dev/ttyACM0" |
12 | 13 | # COM_PORT = "COM5" |
13 | 14 |
|
14 | 15 | DISPLAY_WIDTH = 320 |
@@ -60,19 +61,26 @@ def DisplayPILImage(ser, image, x, y): |
60 | 61 | image_height = image.size[1] |
61 | 62 | image_width = image.size[0] |
62 | 63 |
|
63 | | - SendReg(ser, Command.DISPLAY_BITMAP, x, y, image_width - 1, image_height - 1) |
| 64 | + SendReg(ser, Command.DISPLAY_BITMAP, x, y, x + image_width - 1, y + image_height - 1) |
64 | 65 |
|
65 | 66 | pix = image.load() |
| 67 | + line = bytes() |
66 | 68 | for h in range(image_height): |
67 | | - line = bytes() |
68 | 69 | for w in range(image_width): |
69 | | - if w < image_width: |
70 | | - R = pix[w, h][0] >> 3 |
71 | | - G = pix[w, h][1] >> 2 |
72 | | - B = pix[w, h][2] >> 3 |
| 70 | + R = pix[w, h][0] >> 3 |
| 71 | + G = pix[w, h][1] >> 2 |
| 72 | + B = pix[w, h][2] >> 3 |
| 73 | + |
| 74 | + rgb = (R << 11) | (G << 5) | B |
| 75 | + line += struct.pack('H', rgb) |
73 | 76 |
|
74 | | - rgb = (R << 11) | (G << 5) | B |
75 | | - line += struct.pack('H', rgb) |
| 77 | + # Send image data by multiple of DISPLAY_WIDTH bytes |
| 78 | + if len(line) >= DISPLAY_WIDTH*4: |
| 79 | + ser.write(line) |
| 80 | + line = bytes() |
| 81 | + |
| 82 | + # Write last line if needed |
| 83 | + if len(line) > 0: |
76 | 84 | ser.write(line) |
77 | 85 |
|
78 | 86 | sleep(0.01) # Wait 10 ms after picture display |
@@ -111,7 +119,19 @@ def DisplayText(ser, text, x=0, y=0, |
111 | 119 | DisplayPILImage(ser, text_image, x, y) |
112 | 120 |
|
113 | 121 |
|
| 122 | +stop = False |
| 123 | + |
114 | 124 | if __name__ == "__main__": |
| 125 | + |
| 126 | + def sighandler(signum, frame): |
| 127 | + global stop |
| 128 | + stop = True |
| 129 | + |
| 130 | + # Set the signal handlers, to send a complete frame to the LCD before exit |
| 131 | + signal.signal(signal.SIGINT, sighandler) |
| 132 | + signal.signal(signal.SIGQUIT, sighandler) |
| 133 | + signal.signal(signal.SIGTERM, sighandler) |
| 134 | + |
115 | 135 | # Do not change COM port settings unless you know what you are doing |
116 | 136 | lcd_comm = serial.Serial(COM_PORT, 115200, timeout=1, rtscts=1) |
117 | 137 |
|
@@ -142,10 +162,18 @@ def DisplayText(ser, text, x=0, y=0, |
142 | 162 | background_image="res/example.png") |
143 | 163 |
|
144 | 164 | # Display text that overflows |
145 | | - DisplayText(lcd_comm, "Text overflow!", 5, 450, |
| 165 | + DisplayText(lcd_comm, "Text overflow!", 5, 430, |
146 | 166 | font="roboto/Roboto-Bold.ttf", |
147 | 167 | font_size=60, |
148 | 168 | font_color=(255, 255, 255), |
149 | 169 | background_image="res/example.png") |
150 | 170 |
|
| 171 | + # Display the current time as fast as possible |
| 172 | + while not stop: |
| 173 | + DisplayText(lcd_comm, str(datetime.now().time()), 160, 2, |
| 174 | + font="roboto/Roboto-Bold.ttf", |
| 175 | + font_size=20, |
| 176 | + font_color=(255, 0, 0), |
| 177 | + background_image="res/example.png") |
| 178 | + |
151 | 179 | lcd_comm.close() |
0 commit comments