11#!/usr/bin/env python3
2+ # A simple Python manager for "Turing Smart Screen" 3.5" IPS USB-C display
3+ # https://github.com/mathoudebine/turing-smart-screen-python
4+
25import struct
36from time import sleep
47import serial # Install pyserial : pip install pyserial
1114DISPLAY_WIDTH = 320
1215DISPLAY_HEIGHT = 480
1316
17+
1418class Command :
1519 RESET = 101
1620 CLEAR = 102
@@ -83,21 +87,26 @@ def DisplayText(ser, text, x=0, y=0,
8387 font = "roboto/Roboto-Regular.ttf" ,
8488 font_size = 20 ,
8589 font_color = (0 , 0 , 0 ),
86- background_color = (255 , 255 , 255 )):
90+ background_color = (255 , 255 , 255 ),
91+ background_image = None ):
8792 # Convert text to bitmap using PIL and display it
93+ # Provide the background image path to display text with transparent background
8894
89- # The text bitmap is created with max width/height by default
90- # Note : alpha component from RGBA is not supported by the display
91- text_image = Image .new ('RGB' , (DISPLAY_WIDTH , DISPLAY_HEIGHT ), background_color )
95+ if background_image is None :
96+ # A text bitmap is created with max width/height by default : text with solid background
97+ text_image = Image .new ('RGB' , (DISPLAY_WIDTH , DISPLAY_HEIGHT ), background_color )
98+ else :
99+ # The text bitmap is created from provided background image : text with transparent background
100+ text_image = Image .open (background_image )
92101
93102 # Draw text with specified color & font
94103 font = ImageFont .truetype ("./res/fonts/" + font , font_size )
95104 d = ImageDraw .Draw (text_image )
96- d .text ((0 , 0 ), text , font = font , fill = font_color )
105+ d .text ((x , y ), text , font = font , fill = font_color )
97106
98- # Crop text bitmap to the size of the text
107+ # Crop text bitmap to keep only the text
99108 text_width , text_height = d .textsize (text , font = font )
100- text_image = text_image .crop ((0 , 0 , text_width , text_height ))
109+ text_image = text_image .crop ((x , y , x + text_width , y + text_height ))
101110
102111 DisplayPILImage (ser , text_image , x , y )
103112
@@ -117,11 +126,17 @@ def DisplayText(ser, text, x=0, y=0,
117126
118127 # Display sample text
119128 DisplayText (lcd_comm , "Basic text" , 50 , 100 )
120-
121- DisplayText (lcd_comm , "Custom text" , 5 , 150 ,
122- font = "roboto/Roboto-BoldItalic .ttf" ,
123- font_size = 40 ,
129+
130+ DisplayText (lcd_comm , "Custom italic text" , 5 , 150 ,
131+ font = "roboto/Roboto-Italic .ttf" ,
132+ font_size = 30 ,
124133 font_color = (0 , 0 , 255 ),
125134 background_color = (255 , 255 , 0 ))
126135
136+ DisplayText (lcd_comm , "Transparent bold text" , 5 , 300 ,
137+ font = "roboto/Roboto-Bold.ttf" ,
138+ font_size = 30 ,
139+ font_color = (255 , 255 , 255 ),
140+ background_image = "res/example.png" )
141+
127142 lcd_comm .close ()
0 commit comments