From e492527399f8ea66c1ef541c7251aa41d645cca6 Mon Sep 17 00:00:00 2001 From: Terence Boldt Date: Mon, 11 Oct 2021 21:19:40 -0400 Subject: [PATCH] Update exec command to stream text --- .../apple2driver/a2io/communication.go | 2 + RaspberryPi/apple2driver/handlers/exec.go | 38 +++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/RaspberryPi/apple2driver/a2io/communication.go b/RaspberryPi/apple2driver/a2io/communication.go index 72a1b9d..46d62cc 100644 --- a/RaspberryPi/apple2driver/a2io/communication.go +++ b/RaspberryPi/apple2driver/a2io/communication.go @@ -118,6 +118,7 @@ func ReadByte() (byte, error) { // wait for the Apple II to write for in_write.Read() == gpio.High { if !in_write.WaitForEdge(edgeTimeout) { + out_read.Out(gpio.High) return 0, errors.New("Timed out reading byte -- write stuck high\n") } } @@ -238,6 +239,7 @@ func WriteByte(data byte) error { //fmt.Printf("wait for the Apple II to finsih reading\n") for in_read.Read() == gpio.Low { if !in_read.WaitForEdge(edgeTimeout) { + out_write.Out(gpio.High) return errors.New("Timed out writing byte -- read stuck low") } } diff --git a/RaspberryPi/apple2driver/handlers/exec.go b/RaspberryPi/apple2driver/handlers/exec.go index 9bb21d8..49b8cc5 100644 --- a/RaspberryPi/apple2driver/handlers/exec.go +++ b/RaspberryPi/apple2driver/handlers/exec.go @@ -5,6 +5,7 @@ import ( "os" "os/exec" "strings" + "bufio" "github.com/tjboldt/Apple2-IO-RPi/RaspberryPi/apple2driver/a2io" ) @@ -80,17 +81,40 @@ func ExecCommand() { } cmd := exec.Command("bash", "-c", linuxCommand) cmd.Dir = workingDirectory - cmdOut, err := cmd.Output() + stdout, err := cmd.StdoutPipe() if err != nil { - fmt.Printf("Failed to execute command\n") - a2io.WriteString("Failed to execute command\r") + fmt.Printf("Failed to set stdout\n") + a2io.WriteString("Failed to set stdout\r") return } - fmt.Printf("Command output: %s\n", cmdOut) - apple2string := strings.Replace(string(cmdOut), "\n", "\r", -1) - err = a2io.WriteString(apple2string) + fmt.Printf("Command output:\n") + err = cmd.Start() if err != nil { - fmt.Printf("Failed to send command output\n") + fmt.Printf("Failed to start command\n") + a2io.WriteString("Failed to start command\r") return } + + reader := bufio.NewReader(stdout) + + for err == nil { + var b byte + b, err = reader.ReadByte() + if err == nil { + fmt.Print(string(b)) + if b == 10 { // convert LF to CR for Apple II compatiblity + b = 13 + } + b |= 128 + err = a2io.WriteByte(b) + if err != nil { + fmt.Printf("\nFailed to write byte\n") + cmd.Process.Kill() + return + } + } + } + + cmd.Wait() + a2io.WriteByte(0) }