litex_term: cleanup getkey and revert default settings on KeyboardInterrupt
This commit is contained in:
parent
06162b61cb
commit
e7d1683e34
|
@ -7,27 +7,37 @@ import serial
|
||||||
import threading
|
import threading
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
# TODO: cleanup getkey function
|
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
def getkey():
|
|
||||||
import msvcrt
|
import msvcrt
|
||||||
|
class Console:
|
||||||
|
def configure(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def unconfigure(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def getkey(self):
|
||||||
return msvcrt.getch()
|
return msvcrt.getch()
|
||||||
else:
|
else:
|
||||||
def getkey():
|
|
||||||
import termios
|
import termios
|
||||||
fd = sys.stdin.fileno()
|
class Console:
|
||||||
old = termios.tcgetattr(fd)
|
def __init__(self):
|
||||||
new = termios.tcgetattr(fd)
|
self.fd = sys.stdin.fileno()
|
||||||
new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
|
self.default_settings = termios.tcgetattr(self.fd)
|
||||||
new[6][termios.VMIN] = 1
|
|
||||||
new[6][termios.VTIME] = 0
|
def configure(self):
|
||||||
termios.tcsetattr(fd, termios.TCSANOW, new)
|
settings = termios.tcgetattr(self.fd)
|
||||||
c = None
|
settings[3] = settings[3] & ~termios.ICANON & ~termios.ECHO
|
||||||
try:
|
settings[6][termios.VMIN] = 1
|
||||||
c = os.read(fd, 1)
|
settings[6][termios.VTIME] = 0
|
||||||
finally:
|
termios.tcsetattr(self.fd, termios.TCSANOW, settings)
|
||||||
termios.tcsetattr(fd, termios.TCSAFLUSH, old)
|
|
||||||
return c
|
def unconfigure(self):
|
||||||
|
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.default_settings)
|
||||||
|
|
||||||
|
def getkey(self):
|
||||||
|
return os.read(self.fd, 1)
|
||||||
|
|
||||||
|
|
||||||
sfl_prompt_req = b"F7: boot from serial\n"
|
sfl_prompt_req = b"F7: boot from serial\n"
|
||||||
|
@ -119,6 +129,8 @@ class LiteXTerm:
|
||||||
self.prompt_detect_buffer = bytes(len(sfl_prompt_req))
|
self.prompt_detect_buffer = bytes(len(sfl_prompt_req))
|
||||||
self.magic_detect_buffer = bytes(len(sfl_magic_req))
|
self.magic_detect_buffer = bytes(len(sfl_magic_req))
|
||||||
|
|
||||||
|
self.console = Console()
|
||||||
|
|
||||||
def open(self, port, baudrate):
|
def open(self, port, baudrate):
|
||||||
if hasattr(self, "port"):
|
if hasattr(self, "port"):
|
||||||
return
|
return
|
||||||
|
@ -242,7 +254,7 @@ class LiteXTerm:
|
||||||
def writer(self):
|
def writer(self):
|
||||||
try:
|
try:
|
||||||
while self.writer_alive:
|
while self.writer_alive:
|
||||||
b = getkey()
|
b = self.console.getkey()
|
||||||
if b == b"\x03":
|
if b == b"\x03":
|
||||||
self.stop()
|
self.stop()
|
||||||
elif b == b"\n":
|
elif b == b"\n":
|
||||||
|
@ -292,13 +304,16 @@ def _get_args():
|
||||||
def main():
|
def main():
|
||||||
args = _get_args()
|
args = _get_args()
|
||||||
term = LiteXTerm(args.serial_boot, args.kernel, args.kernel_adr)
|
term = LiteXTerm(args.serial_boot, args.kernel, args.kernel_adr)
|
||||||
|
term.console.configure()
|
||||||
try:
|
try:
|
||||||
term.open(args.port, args.speed)
|
term.open(args.port, args.speed)
|
||||||
term.start()
|
term.start()
|
||||||
term.join(True)
|
term.join(True)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
term.console.unconfigure()
|
||||||
finally:
|
finally:
|
||||||
|
term.console.unconfigure()
|
||||||
term.close()
|
term.close()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue