pato-z80-home-computer/pat80-io-devices/parallel-terminal/python/terminal_emulator.py

171 lines
5.3 KiB
Python
Raw Permalink Normal View History

#! /usr/bin/env python3
2020-12-10 18:59:14 +01:00
# -*- coding: utf-8 -*-
""" @package docstring
ARDUINO PARALLEL TERMINAL EMULATOR
2021-07-11 09:49:42 +02:00
* This file is part of Pat80 IO Devices.
*
* Pat80 IO Devices is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pat80 IO Devices is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY * without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Pat80 IO Devices. If not, see <http://www.gnu.org/licenses/>.
2020-12-10 18:59:14 +01:00
USAGE:
Connect the arduino to a Pat80 I/O port.
Flash /arduino/arduino_terminal firmware into the Arduino.
Connect the Arduino to a PC via USB and power on Pat80
Run this program providing the Arduino USB port
DISCLAIMER:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import serial
2020-12-10 23:47:19 +01:00
import curses
2020-12-10 18:59:14 +01:00
import time
2020-12-12 09:22:38 +01:00
import textwrap
import os
2020-12-12 09:22:38 +01:00
2020-12-10 18:59:14 +01:00
class TerminalEmulator:
SYNC_SLEEP = 0.001
2020-12-11 08:48:30 +01:00
def __init__(self, w, ser):
2020-12-10 23:47:19 +01:00
w.clear()
2020-12-11 08:48:30 +01:00
w.move(0,0)
2020-12-10 18:59:14 +01:00
while True:
# read serial port and write to curses
if ser.inWaiting():
b = ser.read(1)
if ord(b) > 31 or ord(b) == 10:
w.addch(b)
2020-12-12 09:22:38 +01:00
# read key and write to serial port
key = w.getch()
if key == 10 or (key > 31 and key < 256):
# Is a character
time.sleep(self.SYNC_SLEEP)
ser.write(bytes([key]))
2020-12-12 09:22:38 +01:00
elif int(key) == 1: # CTRL+A, enter ADB mode
# Save cursor position
(xPos, yPos) = w.getyx()
2020-12-12 09:22:38 +01:00
self.adbMode(w, ser)
# Restore cursor position
w.move(xPos, yPos)
2020-12-11 08:48:30 +01:00
2020-12-12 09:22:38 +01:00
w.refresh()
2020-12-12 09:22:38 +01:00
def adbMode(self, w, ser):
stdscr.nodelay(False)
curses.echo()
# Clear first line
w.move(0,0)
w.clrtoeol()
# Ask for file path
w.addstr(0, 0, '[ADB MODE] file to load:', curses.A_REVERSE)
path = w.getstr()
try:
header = bytearray(2)
size = os.path.getsize(path)
# Compute the two header bytes needed to declare the length of the stream
header[1] = size & 255 # get lower 8 bits
size >>= 8 # shift by 8 bits
header[0] = size & 255 # get second lower 8 bits
# Log size for debug
w.addstr("Header bytes: {}".format(header.hex()))
w.refresh()
# Send the two heading bytes (most significant first)
ser.write(header[0:1])
time.sleep(self.SYNC_SLEEP)
ser.write(header[1:2])
time.sleep(self.SYNC_SLEEP)
# Send the actual binary stream
2020-12-12 09:22:38 +01:00
with open(path, "rb") as f:
byte = f.read(1)
while byte:
# Check if terminal interface (Arduino) is busy
ser.write(b'\x01') # COMMAND_BUFFER
ser.read()
2020-12-12 09:22:38 +01:00
ser.write(byte)
byte = f.read(1)
except IOError as e:
w.move(0,0)
w.clrtoeol()
w.addstr(" {}".format(str(e)), curses.A_REVERSE)
w.refresh()
2020-12-12 09:22:38 +01:00
curses.noecho()
stdscr.nodelay(True)
2020-12-10 23:47:19 +01:00
2020-12-10 18:59:14 +01:00
if __name__ == '__main__':
import argparse
2020-12-12 09:22:38 +01:00
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
epilog=textwrap.dedent('''\
Pat80 Terminal Emulator with ADB (Assembly Deploy Bridge) support.
CTRL+C Exits
CTRL+A ADB mode: sends binary file
''')
)
2020-12-10 18:59:14 +01:00
parser.add_argument('port', help="arduino parallel monitor USB port")
parser.add_argument('baudrate', help="arduino parallel monitor USB baudrate")
args = parser.parse_args()
2020-12-10 23:47:19 +01:00
# Init curses
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
2020-12-11 08:48:30 +01:00
stdscr.idlok(True)
stdscr.scrollok(True)
2020-12-12 09:22:38 +01:00
stdscr.nodelay(True)
2020-12-10 23:47:19 +01:00
2020-12-12 09:22:38 +01:00
exitMessage = None
exitSuccess = True
2020-12-10 23:47:19 +01:00
try:
2020-12-11 08:48:30 +01:00
ser = serial.Serial(args.port, args.baudrate, timeout=0)
2020-12-12 09:22:38 +01:00
td = TerminalEmulator(stdscr, ser)
except KeyboardInterrupt:
exitMessage = 'Bye!'
exitSuccess = True
2020-12-10 23:47:19 +01:00
except Exception as e:
2020-12-12 09:22:38 +01:00
exitMessage = str(e)
exitSuccess = False
2020-12-10 23:47:19 +01:00
finally:
2020-12-11 08:48:30 +01:00
# Close serial
ser.close()
2020-12-10 23:47:19 +01:00
# Stop curses
curses.nocbreak()
curses.echo()
curses.endwin()
2020-12-12 09:22:38 +01:00
# Print exit message
print(exitMessage)
exit(0 if exitSuccess else 1)
2020-12-10 23:47:19 +01:00