-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcdapi.py
47 lines (39 loc) · 1.16 KB
/
lcdapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import serial
import time
import sys
import glob
def get_port():
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result[0]
def init(port):
global ser
ser = serial.Serial(port,115200)
time.sleep(0.05) #Wait for things to settle
print("Using port "+str(ser.portstr))
def print_lcd(string):
ser.write(string.encode())
def switch_line(line):
if line == 1:
ser.write(b">")
elif line == 0:
ser.write(b"<")
def clear_screen():
ser.write(b"%")
def deinit():
ser.close()