1
0
mirror of https://github.com/mnaberez/py65.git synced 2025-04-10 02:39:34 +00:00

Move linux termios changes into Monitor._run

This commit is contained in:
Sam Colwell 2018-11-04 11:50:48 -05:00
parent cd673d50d1
commit 8a0471e45d
2 changed files with 42 additions and 29 deletions

@ -467,6 +467,25 @@ class Monitor(cmd.Cmd):
mpu = self._mpu
mem = self._mpu.memory
if sys.platform[:3] != "win":
# For non-windows systems, switch to non-canonical
# and no-echo non-blocking-read mode.
import termios
# Save the current terminal setup.
fd = self.stdin.fileno()
self.oldattr = termios.tcgetattr(fd)
# Switch to noncanonical (instant) mode with no echo.
newattr = self.oldattr[:]
newattr[3] &= ~termios.ICANON & ~termios.ECHO
# Switch to non-blocking reads.
newattr[6][termios.VMIN] = 0
newattr[6][termios.VTIME] = 0
termios.tcsetattr(fd, termios.TCSANOW, newattr)
if not breakpoints:
while True:
mpu.step()
@ -483,6 +502,15 @@ class Monitor(cmd.Cmd):
self._output(msg % self._breakpoints.index(pc))
break
if sys.platform[:3] != "win":
# For non-windows systems, switch back to canonical/echo
# mode.
import termios
# Restore the old input settings.
fd = self.stdin.fileno()
termios.tcsetattr(fd, termios.TCSANOW, self.oldattr)
def help_radix(self):
self._output("radix [H|D|O|B]")
self._output("Set default radix to hex, decimal, octal, or binary.")

@ -1,4 +1,5 @@
import sys
import time
if sys.platform[:3] == "win":
import msvcrt
@ -33,42 +34,26 @@ else:
""" Read one character from stdin, blocking until one is available.
Does not echo the character.
"""
fd = stdin.fileno()
oldattr = termios.tcgetattr(fd)
newattr = oldattr[:]
newattr[3] &= ~termios.ICANON & ~termios.ECHO
try:
termios.tcsetattr(fd, termios.TCSANOW, newattr)
# Try to get a character with a non-blocking read.
char = stdin.read(1)
while char == '':
# If we don't get one right away, wait a bit and try again.
time.sleep(0.001)
char = stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldattr)
return char
def getch_noblock(stdin):
""" Read one character from stdin without blocking. Does not echo the
character. If no character is available, an empty string is returned.
"""
fd = stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = oldterm[:]
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
try:
char = ''
r, w, e = select.select([fd], [], [], 0.1)
if r:
char = stdin.read(1)
if char == "\n":
char = "\r"
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
char = ''
# Using non-blocking read as set up in Monitor._run
char = stdin.read(1)
if char == '':
# Don't use 100% CPU while waiting for input.
time.sleep(0.001)
if char == "\n":
char = "\r"
return char