From c6cd49890846f967bb2af237292a867a092ec6bb Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Sat, 13 Feb 2021 08:03:35 -0800 Subject: [PATCH] Fixing terminal --- ueforth/Makefile | 2 +- ueforth/arduino/arduino.fs | 5 +++-- ueforth/common/boot.fs | 2 ++ ueforth/posix/terminal.fs | 7 +++---- ueforth/posix/termios.fs | 16 ++++++++++++++++ ueforth/windows/terminal.fs | 1 + ueforth/windows/windows.fs | 3 ++- 7 files changed, 28 insertions(+), 8 deletions(-) diff --git a/ueforth/Makefile b/ueforth/Makefile index 0bad4ec..a56f4f5 100644 --- a/ueforth/Makefile +++ b/ueforth/Makefile @@ -120,7 +120,7 @@ $(GEN): mkdir -p $@ POSIX_BOOT = common/boot.fs common/vocabulary.fs common/hide_calls.fs common/ansi.fs \ - posix/posix.fs posix/posix_highlevel.fs \ + posix/posix.fs posix/posix_highlevel.fs posix/termios.fs \ common/utils.fs common/highlevel.fs common/filetools.fs posix/posix_desktop.fs \ common/tasks.fs common/streams.fs common/blocks.fs posix/args.fs $(GEN)/posix_boot.h: common/source_to_string.js $(POSIX_BOOT) | $(GEN) diff --git a/ueforth/arduino/arduino.fs b/ueforth/arduino/arduino.fs index 5cab2d6..3b3fb2b 100644 --- a/ueforth/arduino/arduino.fs +++ b/ueforth/arduino/arduino.fs @@ -5,10 +5,11 @@ internals definitions : arduino-type ( a n -- ) Serial.write drop ; ' arduino-type is type : arduino-key ( -- n ) - begin Serial.available until 0 >r rp@ 1 Serial.readBytes drop r> ; + begin Serial.available if 0 >r rp@ 1 Serial.readBytes drop r> dup 13 <> if exit then then again ; ' arduino-key is key +: arduino-key? ( -- n ) Serial.available ; +' arduino-key? is key? forth definitions -: key? ( -- n ) Serial.available ; ( Map Arduino / ESP32 things to shorter names. ) : pin ( n pin# -- ) swap digitalWrite ; diff --git a/ueforth/common/boot.fs b/ueforth/common/boot.fs index 533c2b3..695a6f0 100644 --- a/ueforth/common/boot.fs +++ b/ueforth/common/boot.fs @@ -19,6 +19,7 @@ : > ( a b -- a>b ) swap - 0< ; : = ( a b -- a!=b ) - 0= ; : <> ( a b -- a!=b ) = 0= ; +: 0<> ( n -- n) 0= 0= ; : bl 32 ; : nl 10 ; : 1+ 1 + ; : 1- 1 - ; : 2* 2 * ; : 2/ 2 / ; @@ -140,6 +141,7 @@ variable handler ( Defer I/O to platform specific ) defer type defer key +defer key? defer bye : emit ( n -- ) >r rp@ 1 type rdrop ; : space bl emit ; : cr nl emit ; diff --git a/ueforth/posix/terminal.fs b/ueforth/posix/terminal.fs index ca2e5a4..5c0c98c 100644 --- a/ueforth/posix/terminal.fs +++ b/ueforth/posix/terminal.fs @@ -1,10 +1,9 @@ -include posix/termios.fs - +also posix s" /dev/ttyS3" r/w O_NONBLOCK or open-file throw constant remote : remote-type ( a n -- ) remote write-file throw ; : remote-emit ( ch -- ) >r rp@ 1 remote-type rdrop ; : remote-key ( -- ch|0 ) 0 >r rp@ 1 remote read-file dup EAGAIN = if rdrop 2drop 0 exit then throw if r> else rdrop 0 then ; -: terminal nodelay-mode begin key dup if remote-emit else drop then - remote-key dup if emit else drop then again ; +: terminal begin remote-key dup if emit else drop then + key? if key remote-emit then again ; diff --git a/ueforth/posix/termios.fs b/ueforth/posix/termios.fs index babfc57..6a0b1e6 100644 --- a/ueforth/posix/termios.fs +++ b/ueforth/posix/termios.fs @@ -1,5 +1,7 @@ ( Terminal Handling ) +vocabulary termios termios definitions also posix + z" tcgetattr" 2 sysfunc tcgetattr z" tcsetattr" 3 sysfunc tcsetattr z" fcntl" 3 sysfunc fcntl @@ -38,3 +40,17 @@ $5413 constant TIOCGWINSZ create winsize sizeof(winsize) allot : form ( -- h w ) stdin TIOCGWINSZ winsize ioctl throw winsize l@ dup $ffff and swap $10000 / ; + +0 value pending +: termios-key? ( -- f ) pending if -1 else stdin-key to pending pending 0<> then ; +: termios-key ( -- n ) begin termios-key? 0= while repeat pending 0 to pending ; + +nodelay-mode +' termios-key is key +' termios-key? is key? + +forth definitions + +: form form ; + +only forth definitions diff --git a/ueforth/windows/terminal.fs b/ueforth/windows/terminal.fs index e16ebbf..c128506 100644 --- a/ueforth/windows/terminal.fs +++ b/ueforth/windows/terminal.fs @@ -1,3 +1,4 @@ +also windows s" \\.\COM3" r/w open-file throw constant remote : remote-type ( a n -- ) remote write-file throw ; : remote-emit ( ch -- ) >r rp@ 1 remote-type rdrop ; diff --git a/ueforth/windows/windows.fs b/ueforth/windows/windows.fs index fb4c1a1..22787e1 100644 --- a/ueforth/windows/windows.fs +++ b/ueforth/windows/windows.fs @@ -80,7 +80,8 @@ stdout console-mode @ ENABLE_VIRTUAL_TERMINAL_PROCESSING or SetConsoleMode drop : win-type ( a n -- ) stdout -rot NULL NULL WriteFile drop ; ' win-type is type : raw-key ( -- n ) 0 >r stdin rp@ 1 NULL NULL ReadFile drop r> ; -: key? ( -- f ) stdin 0 WaitForSingleObject 0= ; +: win-key? ( -- f ) stdin 0 WaitForSingleObject 0= ; +' win-key? is key? : win-key ( -- n ) raw-key dup 13 = if drop nl then ; ' win-key is key : win-bye ( -- ) 0 ExitProcess drop ;