Skip to content

Commit 8116fb7

Browse files
committed
Fix communication with LCD, add a clock example
1 parent 59508b7 commit 8116fb7

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

main.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/usr/bin/env python3
22
# A simple Python manager for "Turing Smart Screen" 3.5" IPS USB-C display
33
# https://github.com/mathoudebine/turing-smart-screen-python
4-
4+
import signal
5+
from datetime import datetime
56
import struct
67
from time import sleep
78
import serial # Install pyserial : pip install pyserial
89
from PIL import Image, ImageDraw, ImageFont # Install PIL or Pillow
910

1011
# Set your COM port e.g. COM3 for Windows, /dev/ttyACM0 for Linux...
11-
COM_PORT = "/dev/ttyACM1"
12+
COM_PORT = "/dev/ttyACM0"
1213
# COM_PORT = "COM5"
1314

1415
DISPLAY_WIDTH = 320
@@ -60,19 +61,26 @@ def DisplayPILImage(ser, image, x, y):
6061
image_height = image.size[1]
6162
image_width = image.size[0]
6263

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)
6465

6566
pix = image.load()
67+
line = bytes()
6668
for h in range(image_height):
67-
line = bytes()
6869
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)
7376

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:
7684
ser.write(line)
7785

7886
sleep(0.01) # Wait 10 ms after picture display
@@ -111,7 +119,19 @@ def DisplayText(ser, text, x=0, y=0,
111119
DisplayPILImage(ser, text_image, x, y)
112120

113121

122+
stop = False
123+
114124
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+
115135
# Do not change COM port settings unless you know what you are doing
116136
lcd_comm = serial.Serial(COM_PORT, 115200, timeout=1, rtscts=1)
117137

@@ -142,10 +162,18 @@ def DisplayText(ser, text, x=0, y=0,
142162
background_image="res/example.png")
143163

144164
# Display text that overflows
145-
DisplayText(lcd_comm, "Text overflow!", 5, 450,
165+
DisplayText(lcd_comm, "Text overflow!", 5, 430,
146166
font="roboto/Roboto-Bold.ttf",
147167
font_size=60,
148168
font_color=(255, 255, 255),
149169
background_image="res/example.png")
150170

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+
151179
lcd_comm.close()

0 commit comments

Comments
 (0)