Update serial library so it's usable.
parent
95ef4d90a9
commit
97fa79d7fa
|
@ -2,10 +2,10 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors alien.c-types assocs combinators destructors
|
USING: accessors alien.c-types assocs combinators destructors
|
||||||
kernel math math.bitwise math.parser sequences summary system
|
kernel math math.bitwise math.parser sequences summary system
|
||||||
vocabs.loader ;
|
vocabs.loader io ;
|
||||||
IN: io.serial
|
IN: io.serial
|
||||||
|
|
||||||
TUPLE: serial stream path baud
|
TUPLE: serial-port < disposable stream path baud
|
||||||
termios iflag oflag cflag lflag ;
|
termios iflag oflag cflag lflag ;
|
||||||
|
|
||||||
ERROR: invalid-baud baud ;
|
ERROR: invalid-baud baud ;
|
||||||
|
@ -15,7 +15,20 @@ M: invalid-baud summary ( invalid-baud -- string )
|
||||||
|
|
||||||
HOOK: lookup-baud os ( m -- n )
|
HOOK: lookup-baud os ( m -- n )
|
||||||
HOOK: open-serial os ( serial -- serial' )
|
HOOK: open-serial os ( serial -- serial' )
|
||||||
M: serial dispose ( serial -- ) stream>> dispose ;
|
HOOK: default-serial-flags os ( m -- n )
|
||||||
|
M: serial-port dispose* ( serial -- ) stream>> dispose ;
|
||||||
|
|
||||||
|
: <serial-port> ( path baud -- obj )
|
||||||
|
serial-port new
|
||||||
|
swap >>baud
|
||||||
|
swap >>path
|
||||||
|
default-serial-flags ;
|
||||||
|
|
||||||
|
: with-duplex-stream ( duplex-stream quot -- )
|
||||||
|
[ [ in>> ] [ out>> ] bi ] dip with-streams ; inline
|
||||||
|
|
||||||
|
: with-serial-port ( serial-port quot -- )
|
||||||
|
[ open-serial ] dip with-duplex-stream ; inline
|
||||||
|
|
||||||
{
|
{
|
||||||
{ [ os unix? ] [ "io.serial.unix" ] }
|
{ [ os unix? ] [ "io.serial.unix" ] }
|
||||||
|
|
|
@ -1,25 +1,20 @@
|
||||||
! Copyright (C) 2008 Doug Coleman.
|
! Copyright (C) 2008 Doug Coleman.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors kernel math.bitwise io.serial io.serial.unix
|
USING: accessors io io.serial io.serial.unix kernel literals
|
||||||
literals ;
|
math.bitwise tools.test ;
|
||||||
IN: io.serial.unix
|
IN: io.serial.unix
|
||||||
|
|
||||||
: serial-obj ( -- obj )
|
! "/dev/ttyS0" ! netbsd
|
||||||
serial new
|
! "/dev/dty00" ! netbsd
|
||||||
"/dev/ttyS0" >>path ! linux
|
! "/dev/ttyd0" ! freebsd
|
||||||
! "/dev/dty00" >>path ! netbsd
|
! "/dev/ttyU0" ! openbsd
|
||||||
! "/dev/ttyd0" >>path ! freebsd
|
|
||||||
! "/dev/ttyU0" >>path ! openbsd
|
|
||||||
19200 >>baud
|
|
||||||
flags{ IGNPAR ICRNL } >>iflag
|
|
||||||
flags{ } >>oflag
|
|
||||||
flags{ CS8 CLOCAL CREAD } >>cflag
|
|
||||||
flags{ ICANON } >>lflag ;
|
|
||||||
|
|
||||||
: serial-test ( -- serial )
|
: <serial-port-test> ( -- serial-port )
|
||||||
serial-obj
|
"/dev/ttyS0" 19200 <serial-port> ;
|
||||||
open-serial
|
|
||||||
dup get-termios >>termios
|
: with-serial-port-test ( quot -- )
|
||||||
dup configure-termios
|
[ <serial-port-test> ] dip with-serial-port ; inline
|
||||||
dup tciflush
|
|
||||||
dup apply-termios ;
|
! [ ] [
|
||||||
|
! [ "hello" over stream-write stream-flush ] with-serial-port-test
|
||||||
|
! ] unit-test
|
||||||
|
|
|
@ -30,19 +30,23 @@ FUNCTION: int cfsetspeed ( termios* t, speed_t s ) ;
|
||||||
[ <input-port> ] [ <output-port> ] bi <duplex-stream> ;
|
[ <input-port> ] [ <output-port> ] bi <duplex-stream> ;
|
||||||
|
|
||||||
: open-rw ( path -- fd ) O_RDWR file-mode open-file ;
|
: open-rw ( path -- fd ) O_RDWR file-mode open-file ;
|
||||||
|
|
||||||
: <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
|
: <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
|
||||||
|
|
||||||
M: unix open-serial ( serial -- serial' )
|
: open-unix-serial-port ( serial-port -- )
|
||||||
dup
|
[
|
||||||
path>> flags{ O_RDWR O_NOCTTY O_NDELAY } file-mode open-file
|
path>> flags{ O_RDWR O_NOCTTY O_NDELAY } file-mode open-file
|
||||||
fd>duplex-stream >>stream ;
|
fd>duplex-stream
|
||||||
|
] keep stream<< ;
|
||||||
|
|
||||||
: serial-fd ( serial -- fd )
|
: serial-fd ( serial -- fd )
|
||||||
stream>> in>> handle>> fd>> ;
|
stream>> in>> handle>> fd>> ;
|
||||||
|
|
||||||
: get-termios ( serial -- termios )
|
: set-termios ( serial -- )
|
||||||
serial-fd
|
[
|
||||||
termios <struct> [ tcgetattr io-error ] keep ;
|
serial-fd
|
||||||
|
termios <struct> [ tcgetattr io-error ] keep
|
||||||
|
] keep termios<< ;
|
||||||
|
|
||||||
: configure-termios ( serial -- )
|
: configure-termios ( serial -- )
|
||||||
dup termios>>
|
dup termios>>
|
||||||
|
@ -63,3 +67,19 @@ M: unix open-serial ( serial -- serial' )
|
||||||
: apply-termios ( serial -- )
|
: apply-termios ( serial -- )
|
||||||
[ serial-fd TCSANOW ]
|
[ serial-fd TCSANOW ]
|
||||||
[ termios>> ] bi tcsetattr io-error ;
|
[ termios>> ] bi tcsetattr io-error ;
|
||||||
|
|
||||||
|
M: unix open-serial ( serial -- serial' )
|
||||||
|
{
|
||||||
|
[ open-unix-serial-port ]
|
||||||
|
[ set-termios ]
|
||||||
|
[ configure-termios ]
|
||||||
|
[ tciflush ]
|
||||||
|
[ apply-termios ]
|
||||||
|
[ ]
|
||||||
|
} cleave ;
|
||||||
|
|
||||||
|
M: unix default-serial-flags
|
||||||
|
flags{ IGNPAR ICRNL } >>iflag
|
||||||
|
flags{ } >>oflag
|
||||||
|
flags{ CS8 CLOCAL CREAD } >>cflag
|
||||||
|
flags{ ICANON } >>lflag ;
|
||||||
|
|
Loading…
Reference in New Issue