-
Notifications
You must be signed in to change notification settings - Fork 1
/
serial_iface.py
executable file
·60 lines (48 loc) · 1.19 KB
/
serial_iface.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
48
49
50
51
52
53
54
55
56
57
58
59
import serial
import config
import time
import sys
import traceback
STATE_IDLE = 0
STATE_READY = 1
STATE_PRINTING = 2
EVT_NONE = 0
EVT_PRINT = 1
EVT_RESET = 2
m_state = STATE_IDLE
m_evt = EVT_NONE
def set_state(st):
global m_state
m_state = st
def get_pressed():
global m_evt
_evt = m_evt
m_evt = EVT_NONE
return _evt
def serial_loop():
global m_state, m_evt
if config.SERIAL_PORT is None:
while True:
m_evt = EVT_PRINT
time.sleep(0.5)
"""
Software -> Button: sends state every 0.2s
Button -> Software: sends event when triggered
"""
try:
with serial.Serial(config.SERIAL_PORT, 115200, timeout=0) as ser:
while True:
time.sleep(0.2)
ser.write(str(m_state).encode())
ser.flush()
dat = ser.read(9999).decode()
for c in dat:
if c == "1": m_evt = EVT_PRINT
if c == "2": m_evt = EVT_RESET
if not ser.is_open:
break
except:
print("Serial Error")
traceback.print_exc()
# If serial fails, kill the whole app
sys.exit(1)