From ff4823b3ad9af50bebbc02e25f08d3a1876eb12a Mon Sep 17 00:00:00 2001 From: "Daniele Verducci (ZenPenguin)" Date: Sat, 2 Jan 2021 07:56:11 +0100 Subject: [PATCH] WIP Terminal interface with commands --- arduino/arduino_terminal/arduino_terminal.ino | 3 +- python/terminal_emulator.py | 29 ++++++++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/arduino/arduino_terminal/arduino_terminal.ino b/arduino/arduino_terminal/arduino_terminal.ino index 97b4ab5..05fc428 100644 --- a/arduino/arduino_terminal/arduino_terminal.ino +++ b/arduino/arduino_terminal/arduino_terminal.ino @@ -41,9 +41,10 @@ void setup() { } void loop() { - if (Serial.available() > 0) { + if (Serial.available() > 1) { switch (Serial.read()) { case COMMAND_WRITE: + while (Serial.available() < 1) {} // Waits for the second byte incomingBuffer = Serial.read(); availableBytes = 1; // TODO: Implement a 256 byte buffer and store the avail bytes number in this var break; diff --git a/python/terminal_emulator.py b/python/terminal_emulator.py index 18ed302..81034d4 100755 --- a/python/terminal_emulator.py +++ b/python/terminal_emulator.py @@ -31,7 +31,6 @@ import os class TerminalEmulator: - SYNC_SLEEP = 0.001 def __init__(self, w, ser): w.clear() @@ -48,8 +47,7 @@ class TerminalEmulator: key = w.getch() if key == 10 or (key > 31 and key < 256): # Is a character - time.sleep(self.SYNC_SLEEP) - ser.write(bytes([key])) + self.sendByte(bytes([key])) elif int(key) == 1: # CTRL+A, enter ADB mode # Save cursor position (xPos, yPos) = w.getyx() @@ -82,18 +80,15 @@ class TerminalEmulator: 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) + self.sendByte(header[0:1]) + self.sendByte(header[1:2]) # Send the actual binary stream with open(path, "rb") as f: byte = f.read(1) while byte: - ser.write(byte) + self.sendByte(byte) byte = f.read(1) - time.sleep(self.SYNC_SLEEP) except IOError as e: w.move(0,0) w.clrtoeol() @@ -102,7 +97,21 @@ class TerminalEmulator: curses.noecho() stdscr.nodelay(True) - + + # Sends a byte checking if the interface is busy + def sendByte(self, b): + busy = True + while busy: + # check if busy + ser.write(b'\x01') # send COMMAND_BUFFER + while not ser.inWaiting(): + # wait for answer from interface + pass + busy = ser.read() != 0 + # interface is free: write byte + ser.write(b'\x00') # send COMMAND_WRITE + ser.write(b) # send byte + if __name__ == '__main__': import argparse