Skip to content

Commit 27f4352

Browse files
committed
Code cleanup
1 parent 79f4ad5 commit 27f4352

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

main.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
#!/usr/bin/env python3
12
import struct
23
from time import sleep
3-
44
import serial # Install pyserial : pip install pyserial
55
from PIL import Image # Install PIL or Pillow
66

77
# Set your COM port e.g. COM3 for Windows, /dev/ttyACM0 for Linux...
88
COM_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+
1121
def 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

2232
def Reset(ser):
23-
SendReg(ser, 101, 0, 0, 0, 0)
33+
SendReg(ser, Command.RESET, 0, 0, 0, 0)
2434

2535

2636
def Clear(ser):
27-
SendReg(ser, 102, 0, 0, 0, 0)
37+
SendReg(ser, Command.CLEAR, 0, 0, 0, 0)
2838

2939

3040
def ScreenOff(ser):
31-
SendReg(ser, 108, 0, 0, 0, 0)
41+
SendReg(ser, Command.SCREEN_OFF, 0, 0, 0, 0)
3242

3343

3444
def ScreenOn(ser):
35-
SendReg(ser, 109, 0, 0, 0, 0)
45+
SendReg(ser, Command.SCREEN_ON, 0, 0, 0, 0)
3646

3747

3848
def 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

4353
def 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

6676
if __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

Comments
 (0)