Commit 72164e16 authored by Mart Lubbers's avatar Mart Lubbers
Browse files

add TTYavailable function

parent 1432dcd3
...@@ -5,4 +5,4 @@ Clean\ System\ Files/tty.o: tty.c ...@@ -5,4 +5,4 @@ Clean\ System\ Files/tty.o: tty.c
gcc -g -c $< -o "$@" gcc -g -c $< -o "$@"
clean: clean:
$(RM) -r Clean\ System\ Files/* test sapl $(RM) -r Clean\ System\ Files/* test
...@@ -20,12 +20,9 @@ from StdClass import class zero ...@@ -20,12 +20,9 @@ from StdClass import class zero
instance zero TTYSettings instance zero TTYSettings
TTYopen :: !String !TTYSettings !*env -> (!Bool,!*TTY,!*env)
TTYclose :: !*TTY !*env -> (!Bool, !*env) TTYclose :: !*TTY !*env -> (!Bool, !*env)
TTYerror :: !*env -> (!String, !*env)
TTYopen :: !String !TTYSettings !*env -> (!Bool,!*TTY,!*env)
TTYreadline :: !*TTY -> (!String, !*TTY) TTYreadline :: !*TTY -> (!String, !*TTY)
TTYavailable :: !*TTY -> (!Bool, !*TTY)
TTYwrite :: !*TTY !String -> *TTY TTYwrite :: !*TTY !String -> *TTY
TTYerror :: !*env -> (!String, !*env)
...@@ -46,22 +46,27 @@ TTYopen fn ts w = TTYopen2 ...@@ -46,22 +46,27 @@ TTYopen fn ts w = TTYopen2
} }
TTYclose :: !*TTY !*env -> (!Bool, !*env) TTYclose :: !*TTY !*env -> (!Bool, !*env)
TTYclose f w = code { TTYclose _ _ = code {
ccall ttyclose "I:I:A" ccall ttyclose "I:I:A"
} }
TTYreadline :: !*TTY -> (!String, !*TTY) TTYreadline :: !*TTY -> (!String, !*TTY)
TTYreadline t = code { TTYreadline _ = code {
ccall ttyreadline "I:VSI" ccall ttyreadline "I:VSI"
} }
TTYwrite :: !*TTY !String -> *TTY TTYwrite :: !*TTY !String -> *TTY
TTYwrite s e = code { TTYwrite _ _ = code {
ccall ttywrite "IS:I" ccall ttywrite "IS:I"
} }
TTYavailable :: !*TTY -> (!Bool, !*TTY)
TTYavailable _ = code {
ccall ttyavailable "I:VII"
}
TTYerror :: !*env -> (!String, !*env) TTYerror :: !*env -> (!String, !*env)
TTYerror w = code { TTYerror _ = code {
ccall ttyerror ":S:A" ccall ttyerror ":S:A"
} }
......
...@@ -4,13 +4,23 @@ import StdEnv ...@@ -4,13 +4,23 @@ import StdEnv
import TTY import TTY
Start :: *World -> (!String, *World) TTYerrorclose :: !*File !*World -> *World
TTYerrorclose f w
# (err, w) = TTYerror w
# (ok, w) = fclose (f <<< err <<< "\n") w
| not ok = abort "Couldn't close file"
= w
Start :: *World -> *World
Start w Start w
#! (ok, tty, w) = TTYopen "/dev/ttyUSB0" zero w # (io, w) = stdio w
| not ok = TTYerror w # (ok, tty, w) = TTYopen "/dev/ttyUSB0" zero w
| not ok = TTYerrorclose io w
#! tty = TTYwrite tty "echo123\n" #! tty = TTYwrite tty "echo123\n"
#! (c, tty) = TTYreadline tty #! (av, tty) = TTYavailable tty
# io = io <<< ("Bytes available: " +++ toString av +++ "\n")
#! (l, tty) = TTYreadline tty
# io = io <<< ("Line read: " +++ l)
#! (ok, w) = TTYclose tty w #! (ok, w) = TTYclose tty w
| not ok = TTYerror w | not ok = TTYerrorclose io w
#! (s, w) = TTYerror w = snd (fclose io w)
= ("Read: " +++ c, w)
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "Clean.h" #include "Clean.h"
#define INITIAL_BUFFERSIZE 2 #define INITIAL_BUFFERSIZE 2
#define DEVICE_TIMEOUT {5, 0}
#define die(s) {perror(s);exit(EXIT_FAILURE);} #define die(s) {perror(s);exit(EXIT_FAILURE);}
static speed_t baudrates[] = {B0, B50, B75, B110, B134, B150, B200, B300, B600, static speed_t baudrates[] = {B0, B50, B75, B110, B134, B150, B200, B300, B600,
...@@ -168,6 +169,20 @@ void ttyreadline(int fd, CleanString *result, int *fdo) ...@@ -168,6 +169,20 @@ void ttyreadline(int fd, CleanString *result, int *fdo)
free(buf); free(buf);
} }
void ttyavailable(int fd, int *r, int *fdo)
{
fd_set fds;
struct timeval tv = DEVICE_TIMEOUT;
FD_ZERO(&fds);
FD_SET(fd, &fds);
*r = select(fd+1, &fds, NULL, NULL, NULL);
if(*r == -1)
die("select");
*fdo = fd;
}
int ttywrite(int fd, CleanString s) int ttywrite(int fd, CleanString s)
{ {
write(fd, CleanStringCharacters(s), CleanStringLength(s)); write(fd, CleanStringCharacters(s), CleanStringLength(s));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment