From c60e0334bd41af2a4d9f0f95659d9c5b0285275f Mon Sep 17 00:00:00 2001 From: Yeo Kheng Meng Date: Sat, 29 Oct 2016 18:49:24 +0800 Subject: [PATCH] refactor stdin prep --- Examples/SwiftSerialIM/Sources/main.swift | 29 ++++++++++++----------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Examples/SwiftSerialIM/Sources/main.swift b/Examples/SwiftSerialIM/Sources/main.swift index 6f9ad25..c4ebed8 100644 --- a/Examples/SwiftSerialIM/Sources/main.swift +++ b/Examples/SwiftSerialIM/Sources/main.swift @@ -15,6 +15,20 @@ let serialPort: SerialPort = SerialPort(path: portName) var myturn = true +// Prepares the stdin so we can getchar() without echoing +func prepareStdin() { + + // Set up the control structure + var settings = termios() + + // Get options structure for stdin + tcgetattr(STDIN_FILENO, &settings) + + //Turn off ICANON and ECHO + settings.c_lflag &= ~tcflag_t(ICANON | ECHO) + + tcsetattr(STDIN_FILENO, TCSANOW, &settings) +} func getKeyPress () -> UnicodeScalar { let valueRead: Int = Int(getchar()) @@ -55,20 +69,7 @@ do { transmitRate: .baud9600, minimumBytesToRead: 1) - - /* These prepares the stdin so we can getchar() without echoing */ - // Set up the control structure - var settings = termios() - - // Get options structure for stdin - tcgetattr(STDIN_FILENO, &settings) - - //Turn off ICANON and ECHO - settings.c_lflag &= ~tcflag_t(ICANON | ECHO) - - tcsetattr(STDIN_FILENO, TCSANOW, &settings) - - + prepareStdin() //Turn off output buffering if not multiple threads will have problems printing