1+ #!/usr/bin/env python3
12import struct
23from time import sleep
3-
44import serial # Install pyserial : pip install pyserial
55from PIL import Image # 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+
12+ class Command :
13+ RESET = 101
14+ CLEAR = 102
15+ SCREEN_OFF = 108
16+ SCREEN_ON = 109
17+ SET_BRIGHTNESS = 110
18+ DISPLAY_BITMAP = 197
19+
20+
1121def SendReg (ser , reg , x , y , ex , ey ):
1222 byteBuffer = bytearray (6 )
1323 byteBuffer [0 ] = (x >> 2 )
@@ -20,32 +30,32 @@ def SendReg(ser, reg, x, y, ex, ey):
2030
2131
2232def Reset (ser ):
23- SendReg (ser , 101 , 0 , 0 , 0 , 0 )
33+ SendReg (ser , Command . RESET , 0 , 0 , 0 , 0 )
2434
2535
2636def Clear (ser ):
27- SendReg (ser , 102 , 0 , 0 , 0 , 0 )
37+ SendReg (ser , Command . CLEAR , 0 , 0 , 0 , 0 )
2838
2939
3040def ScreenOff (ser ):
31- SendReg (ser , 108 , 0 , 0 , 0 , 0 )
41+ SendReg (ser , Command . SCREEN_OFF , 0 , 0 , 0 , 0 )
3242
3343
3444def ScreenOn (ser ):
35- SendReg (ser , 109 , 0 , 0 , 0 , 0 )
45+ SendReg (ser , Command . SCREEN_ON , 0 , 0 , 0 , 0 )
3646
3747
3848def SetBrightness (ser , level ):
39- # Level : 0 (bright ) - 255 (darkest)
40- SendReg (ser , 110 , level , 0 , 0 , 0 )
49+ # Level : 0 (brightest ) - 255 (darkest)
50+ SendReg (ser , Command . SET_BRIGHTNESS , level , 0 , 0 , 0 )
4151
4252
4353def PrintImage (ser , image ):
4454 im = Image .open (image )
4555 image_height = im .size [1 ]
4656 image_width = im .size [0 ]
4757
48- SendReg (ser , 197 , 0 , 0 , image_width - 1 , image_height - 1 )
58+ SendReg (ser , Command . DISPLAY_BITMAP , 0 , 0 , image_width - 1 , image_height - 1 )
4959
5060 pix = im .load ()
5161 for h in range (image_height ):
@@ -64,15 +74,16 @@ def PrintImage(ser, image):
6474
6575
6676if __name__ == "__main__" :
67- ser = serial .Serial (COM_PORT , 115200 , timeout = 1 , rtscts = 1 )
77+ # Do not change COM port settings unless you know what you are doing
78+ lcd_comm = serial .Serial (COM_PORT , 115200 , timeout = 1 , rtscts = 1 )
6879
6980 # Clear screen (blank)
70- Clear (ser )
81+ Clear (lcd_comm )
7182
7283 # Set brightness to max value
73- SetBrightness (ser , 0 )
84+ SetBrightness (lcd_comm , 0 )
7485
7586 # Display sample picture
76- PrintImage (ser , "res/example.png" )
87+ PrintImage (lcd_comm , "res/example.png" )
7788
78- ser .close ()
89+ lcd_comm .close ()
0 commit comments