22import struct
33from time import sleep
44import serial # Install pyserial : pip install pyserial
5- from PIL import Image # Install PIL or Pillow
5+ from PIL import Image , ImageDraw , ImageFont # Install PIL or Pillow
66
77# Set your COM port e.g. COM3 for Windows, /dev/ttyACM0 for Linux...
88COM_PORT = "/dev/ttyACM1"
99# COM_PORT = "COM5"
1010
11+ DISPLAY_WIDTH = 320
12+ DISPLAY_HEIGHT = 480
1113
1214class Command :
1315 RESET = 101
@@ -18,14 +20,14 @@ class Command:
1820 DISPLAY_BITMAP = 197
1921
2022
21- def SendReg (ser , reg , x , y , ex , ey ):
23+ def SendReg (ser , cmd , x , y , ex , ey ):
2224 byteBuffer = bytearray (6 )
2325 byteBuffer [0 ] = (x >> 2 )
2426 byteBuffer [1 ] = (((x & 3 ) << 6 ) + (y >> 4 ))
2527 byteBuffer [2 ] = (((y & 15 ) << 4 ) + (ex >> 6 ))
2628 byteBuffer [3 ] = (((ex & 63 ) << 2 ) + (ey >> 8 ))
2729 byteBuffer [4 ] = (ey & 255 )
28- byteBuffer [5 ] = reg
30+ byteBuffer [5 ] = cmd
2931 ser .write (bytes (byteBuffer ))
3032
3133
@@ -50,14 +52,13 @@ def SetBrightness(ser, level):
5052 SendReg (ser , Command .SET_BRIGHTNESS , level , 0 , 0 , 0 )
5153
5254
53- def PrintImage (ser , image ):
54- im = Image .open (image )
55- image_height = im .size [1 ]
56- image_width = im .size [0 ]
55+ def DisplayPILImage (ser , image , x , y ):
56+ image_height = image .size [1 ]
57+ image_width = image .size [0 ]
5758
58- SendReg (ser , Command .DISPLAY_BITMAP , 0 , 0 , image_width - 1 , image_height - 1 )
59+ SendReg (ser , Command .DISPLAY_BITMAP , x , y , image_width - 1 , image_height - 1 )
5960
60- pix = im .load ()
61+ pix = image .load ()
6162 for h in range (image_height ):
6263 line = bytes ()
6364 for w in range (image_width ):
@@ -73,6 +74,34 @@ def PrintImage(ser, image):
7374 sleep (0.01 ) # Wait 10 ms after picture display
7475
7576
77+ def DisplayBitmap (ser , bitmap_path , x = 0 , y = 0 ):
78+ image = Image .open (bitmap_path )
79+ DisplayPILImage (ser , image , x , y )
80+
81+
82+ def DisplayText (ser , text , x = 0 , y = 0 ,
83+ font = "roboto/Roboto-Regular.ttf" ,
84+ font_size = 20 ,
85+ font_color = (0 , 0 , 0 ),
86+ background_color = (255 , 255 , 255 )):
87+ # Convert text to bitmap using PIL and display it
88+
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 )
92+
93+ # Draw text with specified color & font
94+ font = ImageFont .truetype ("./res/fonts/" + font , font_size )
95+ d = ImageDraw .Draw (text_image )
96+ d .text ((0 , 0 ), text , font = font , fill = font_color )
97+
98+ # Crop text bitmap to the size of the text
99+ text_width , text_height = d .textsize (text , font = font )
100+ text_image = text_image .crop ((0 , 0 , text_width , text_height ))
101+
102+ DisplayPILImage (ser , text_image , x , y )
103+
104+
76105if __name__ == "__main__" :
77106 # Do not change COM port settings unless you know what you are doing
78107 lcd_comm = serial .Serial (COM_PORT , 115200 , timeout = 1 , rtscts = 1 )
@@ -84,6 +113,15 @@ def PrintImage(ser, image):
84113 SetBrightness (lcd_comm , 0 )
85114
86115 # Display sample picture
87- PrintImage (lcd_comm , "res/example.png" )
116+ DisplayBitmap (lcd_comm , "res/example.png" )
117+
118+ # Display sample text
119+ 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 ,
124+ font_color = (0 , 0 , 255 ),
125+ background_color = (255 , 255 , 0 ))
88126
89127 lcd_comm .close ()
0 commit comments