--- /dev/null
+! Copyright (C) 2008 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: alien.c-types alien.syntax assocs classes.struct
+io.serial kernel system ;
+IN: io.serial.linux.ffi
+
+CONSTANT: NCCS 32
+
+TYPEDEF: uchar cc_t
+TYPEDEF: uint speed_t
+TYPEDEF: uint tcflag_t
+
+STRUCT: termios
+ { iflag tcflag_t }
+ { oflag tcflag_t }
+ { cflag tcflag_t }
+ { lflag tcflag_t }
+ { line cc_t }
+ { cc { cc_t NCCS } }
+ { ispeed speed_t }
+ { ospeed speed_t } ;
+
+FUNCTION: speed_t cfgetispeed ( termios* t ) ;
+FUNCTION: speed_t cfgetospeed ( termios* t ) ;
+FUNCTION: int cfsetispeed ( termios* t, speed_t s ) ;
+FUNCTION: int cfsetospeed ( termios* t, speed_t s ) ;
+FUNCTION: int tcgetattr ( int i1, termios* t ) ;
+FUNCTION: int tcsetattr ( int i1, int i2, termios* t ) ;
+FUNCTION: int tcdrain ( int i1 ) ;
+FUNCTION: int tcflow ( int i1, int i2 ) ;
+FUNCTION: int tcflush ( int i1, int i2 ) ;
+FUNCTION: int tcsendbreak ( int i1, int i2 ) ;
+FUNCTION: void cfmakeraw ( termios* t ) ;
+FUNCTION: int cfsetspeed ( termios* t, speed_t s ) ;
+
+CONSTANT: TCSANOW 0
+CONSTANT: TCSADRAIN 1
+CONSTANT: TCSAFLUSH 2
+
+CONSTANT: TCIFLUSH 0
+CONSTANT: TCOFLUSH 1
+CONSTANT: TCIOFLUSH 2
+
+CONSTANT: TCOOFF 0
+CONSTANT: TCOON 1
+CONSTANT: TCIOFF 2
+CONSTANT: TCION 3
+
+! iflag
+CONSTANT: IGNBRK OCT: 0000001
+CONSTANT: BRKINT OCT: 0000002
+CONSTANT: IGNPAR OCT: 0000004
+CONSTANT: PARMRK OCT: 0000010
+CONSTANT: INPCK OCT: 0000020
+CONSTANT: ISTRIP OCT: 0000040
+CONSTANT: INLCR OCT: 0000100
+CONSTANT: IGNCR OCT: 0000200
+CONSTANT: ICRNL OCT: 0000400
+CONSTANT: IUCLC OCT: 0001000
+CONSTANT: IXON OCT: 0002000
+CONSTANT: IXANY OCT: 0004000
+CONSTANT: IXOFF OCT: 0010000
+CONSTANT: IMAXBEL OCT: 0020000
+CONSTANT: IUTF8 OCT: 0040000
+
+! oflag
+CONSTANT: OPOST OCT: 0000001
+CONSTANT: OLCUC OCT: 0000002
+CONSTANT: ONLCR OCT: 0000004
+CONSTANT: OCRNL OCT: 0000010
+CONSTANT: ONOCR OCT: 0000020
+CONSTANT: ONLRET OCT: 0000040
+CONSTANT: OFILL OCT: 0000100
+CONSTANT: OFDEL OCT: 0000200
+CONSTANT: NLDLY OCT: 0000400
+CONSTANT: NL0 OCT: 0000000
+CONSTANT: NL1 OCT: 0000400
+CONSTANT: CRDLY OCT: 0003000
+CONSTANT: CR0 OCT: 0000000
+CONSTANT: CR1 OCT: 0001000
+CONSTANT: CR2 OCT: 0002000
+CONSTANT: CR3 OCT: 0003000
+CONSTANT: TABDLY OCT: 0014000
+CONSTANT: TAB0 OCT: 0000000
+CONSTANT: TAB1 OCT: 0004000
+CONSTANT: TAB2 OCT: 0010000
+CONSTANT: TAB3 OCT: 0014000
+CONSTANT: BSDLY OCT: 0020000
+CONSTANT: BS0 OCT: 0000000
+CONSTANT: BS1 OCT: 0020000
+CONSTANT: FFDLY OCT: 0100000
+CONSTANT: FF0 OCT: 0000000
+CONSTANT: FF1 OCT: 0100000
+
+! cflags
+CONSTANT: CSIZE OCT: 0000060
+CONSTANT: CS5 OCT: 0000000
+CONSTANT: CS6 OCT: 0000020
+CONSTANT: CS7 OCT: 0000040
+CONSTANT: CS8 OCT: 0000060
+CONSTANT: CSTOPB OCT: 0000100
+CONSTANT: CREAD OCT: 0000200
+CONSTANT: PARENB OCT: 0000400
+CONSTANT: PARODD OCT: 0001000
+CONSTANT: HUPCL OCT: 0002000
+CONSTANT: CLOCAL OCT: 0004000
+CONSTANT: CIBAUD OCT: 002003600000
+CONSTANT: CRTSCTS OCT: 020000000000
+
+! lflags
+CONSTANT: ISIG OCT: 0000001
+CONSTANT: ICANON OCT: 0000002
+CONSTANT: XCASE OCT: 0000004
+CONSTANT: ECHO OCT: 0000010
+CONSTANT: ECHOE OCT: 0000020
+CONSTANT: ECHOK OCT: 0000040
+CONSTANT: ECHONL OCT: 0000100
+CONSTANT: NOFLSH OCT: 0000200
+CONSTANT: TOSTOP OCT: 0000400
+CONSTANT: ECHOCTL OCT: 0001000
+CONSTANT: ECHOPRT OCT: 0002000
+CONSTANT: ECHOKE OCT: 0004000
+CONSTANT: FLUSHO OCT: 0010000
+CONSTANT: PENDIN OCT: 0040000
+CONSTANT: IEXTEN OCT: 0100000
+
+M: linux lookup-baud ( n -- n )
+ H{
+ { 0 OCT: 0000000 }
+ { 50 OCT: 0000001 }
+ { 75 OCT: 0000002 }
+ { 110 OCT: 0000003 }
+ { 134 OCT: 0000004 }
+ { 150 OCT: 0000005 }
+ { 200 OCT: 0000006 }
+ { 300 OCT: 0000007 }
+ { 600 OCT: 0000010 }
+ { 1200 OCT: 0000011 }
+ { 1800 OCT: 0000012 }
+ { 2400 OCT: 0000013 }
+ { 4800 OCT: 0000014 }
+ { 9600 OCT: 0000015 }
+ { 19200 OCT: 0000016 }
+ { 38400 OCT: 0000017 }
+ { 57600 OCT: 0010001 }
+ { 115200 OCT: 0010002 }
+ { 230400 OCT: 0010003 }
+ { 460800 OCT: 0010004 }
+ { 500000 OCT: 0010005 }
+ { 576000 OCT: 0010006 }
+ { 921600 OCT: 0010007 }
+ { 1000000 OCT: 0010010 }
+ { 1152000 OCT: 0010011 }
+ { 1500000 OCT: 0010012 }
+ { 2000000 OCT: 0010013 }
+ { 2500000 OCT: 0010014 }
+ { 3000000 OCT: 0010015 }
+ { 3500000 OCT: 0010016 }
+ { 4000000 OCT: 0010017 }
+ } ?at [ invalid-baud ] unless ;
+
--- /dev/null
+! Copyright (C) 2008 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors classes.struct combinators io.backend.unix
+io.ports io.serial io.streams.duplex kernel literals math
+system unix unix.ffi ;
+IN: io.serial.linux
+
+: fd>duplex-stream ( fd -- duplex-stream )
+ <fd> init-fd
+ [ <input-port> ] [ <output-port> ] bi <duplex-stream> ;
+
+: open-rw ( path -- fd ) O_RDWR file-mode open-file ;
+
+: <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
+
+: open-unix-serial-port ( serial-port -- )
+ [
+ path>> flags{ O_RDWR O_NOCTTY O_NDELAY } file-mode open-file
+ fd>duplex-stream
+ ] keep stream<< ;
+
+: serial-fd ( serial -- fd )
+ stream>> in>> handle>> fd>> ;
+
+: set-termios ( serial -- )
+ [
+ serial-fd
+ termios <struct> [ tcgetattr io-error ] keep
+ ] keep termios<< ;
+
+: configure-termios ( serial -- )
+ dup termios>>
+ {
+ [ [ iflag>> ] dip over [ iflag<< ] [ 2drop ] if ]
+ [ [ oflag>> ] dip over [ oflag<< ] [ 2drop ] if ]
+ [
+ [
+ [ cflag>> 0 or ] [ baud>> lookup-baud ] bi bitor
+ ] dip cflag<<
+ ]
+ [ [ lflag>> ] dip over [ lflag<< ] [ 2drop ] if ]
+ } 2cleave ;
+
+: tciflush ( serial -- )
+ serial-fd TCIFLUSH tcflush io-error ;
+
+: apply-termios ( serial -- )
+ [ serial-fd TCSANOW ]
+ [ 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 ;
--- /dev/null
+linux
+windows
[ open-serial ] dip with-duplex-stream ; inline
{
- { [ os unix? ] [ "io.serial.unix" ] }
+ { [ os linux? ] [ "io.serial.linux" ] }
{ [ os windows? ] [ "io.serial.windows" ] }
} cond require
+++ /dev/null
-! Copyright (C) 2008 Doug Coleman.
-! See http://factorcode.org/license.txt for BSD license.
-USING: assocs alien.syntax kernel io.serial system unix ;
-IN: io.serial.unix
-
-CONSTANT: TCSANOW 0
-CONSTANT: TCSADRAIN 1
-CONSTANT: TCSAFLUSH 2
-
-CONSTANT: TCIFLUSH 0
-CONSTANT: TCOFLUSH 1
-CONSTANT: TCIOFLUSH 2
-
-CONSTANT: TCOOFF 0
-CONSTANT: TCOON 1
-CONSTANT: TCIOFF 2
-CONSTANT: TCION 3
-
-! iflag
-CONSTANT: IGNBRK OCT: 0000001
-CONSTANT: BRKINT OCT: 0000002
-CONSTANT: IGNPAR OCT: 0000004
-CONSTANT: PARMRK OCT: 0000010
-CONSTANT: INPCK OCT: 0000020
-CONSTANT: ISTRIP OCT: 0000040
-CONSTANT: INLCR OCT: 0000100
-CONSTANT: IGNCR OCT: 0000200
-CONSTANT: ICRNL OCT: 0000400
-CONSTANT: IUCLC OCT: 0001000
-CONSTANT: IXON OCT: 0002000
-CONSTANT: IXANY OCT: 0004000
-CONSTANT: IXOFF OCT: 0010000
-CONSTANT: IMAXBEL OCT: 0020000
-CONSTANT: IUTF8 OCT: 0040000
-
-! oflag
-CONSTANT: OPOST OCT: 0000001
-CONSTANT: OLCUC OCT: 0000002
-CONSTANT: ONLCR OCT: 0000004
-CONSTANT: OCRNL OCT: 0000010
-CONSTANT: ONOCR OCT: 0000020
-CONSTANT: ONLRET OCT: 0000040
-CONSTANT: OFILL OCT: 0000100
-CONSTANT: OFDEL OCT: 0000200
-CONSTANT: NLDLY OCT: 0000400
-CONSTANT: NL0 OCT: 0000000
-CONSTANT: NL1 OCT: 0000400
-CONSTANT: CRDLY OCT: 0003000
-CONSTANT: CR0 OCT: 0000000
-CONSTANT: CR1 OCT: 0001000
-CONSTANT: CR2 OCT: 0002000
-CONSTANT: CR3 OCT: 0003000
-CONSTANT: TABDLY OCT: 0014000
-CONSTANT: TAB0 OCT: 0000000
-CONSTANT: TAB1 OCT: 0004000
-CONSTANT: TAB2 OCT: 0010000
-CONSTANT: TAB3 OCT: 0014000
-CONSTANT: BSDLY OCT: 0020000
-CONSTANT: BS0 OCT: 0000000
-CONSTANT: BS1 OCT: 0020000
-CONSTANT: FFDLY OCT: 0100000
-CONSTANT: FF0 OCT: 0000000
-CONSTANT: FF1 OCT: 0100000
-
-! cflags
-CONSTANT: CSIZE OCT: 0000060
-CONSTANT: CS5 OCT: 0000000
-CONSTANT: CS6 OCT: 0000020
-CONSTANT: CS7 OCT: 0000040
-CONSTANT: CS8 OCT: 0000060
-CONSTANT: CSTOPB OCT: 0000100
-CONSTANT: CREAD OCT: 0000200
-CONSTANT: PARENB OCT: 0000400
-CONSTANT: PARODD OCT: 0001000
-CONSTANT: HUPCL OCT: 0002000
-CONSTANT: CLOCAL OCT: 0004000
-CONSTANT: CIBAUD OCT: 002003600000
-CONSTANT: CRTSCTS OCT: 020000000000
-
-! lflags
-CONSTANT: ISIG OCT: 0000001
-CONSTANT: ICANON OCT: 0000002
-CONSTANT: XCASE OCT: 0000004
-CONSTANT: ECHO OCT: 0000010
-CONSTANT: ECHOE OCT: 0000020
-CONSTANT: ECHOK OCT: 0000040
-CONSTANT: ECHONL OCT: 0000100
-CONSTANT: NOFLSH OCT: 0000200
-CONSTANT: TOSTOP OCT: 0000400
-CONSTANT: ECHOCTL OCT: 0001000
-CONSTANT: ECHOPRT OCT: 0002000
-CONSTANT: ECHOKE OCT: 0004000
-CONSTANT: FLUSHO OCT: 0010000
-CONSTANT: PENDIN OCT: 0040000
-CONSTANT: IEXTEN OCT: 0100000
-
-M: linux lookup-baud ( n -- n )
- H{
- { 0 OCT: 0000000 }
- { 50 OCT: 0000001 }
- { 75 OCT: 0000002 }
- { 110 OCT: 0000003 }
- { 134 OCT: 0000004 }
- { 150 OCT: 0000005 }
- { 200 OCT: 0000006 }
- { 300 OCT: 0000007 }
- { 600 OCT: 0000010 }
- { 1200 OCT: 0000011 }
- { 1800 OCT: 0000012 }
- { 2400 OCT: 0000013 }
- { 4800 OCT: 0000014 }
- { 9600 OCT: 0000015 }
- { 19200 OCT: 0000016 }
- { 38400 OCT: 0000017 }
- { 57600 OCT: 0010001 }
- { 115200 OCT: 0010002 }
- { 230400 OCT: 0010003 }
- { 460800 OCT: 0010004 }
- { 500000 OCT: 0010005 }
- { 576000 OCT: 0010006 }
- { 921600 OCT: 0010007 }
- { 1000000 OCT: 0010010 }
- { 1152000 OCT: 0010011 }
- { 1500000 OCT: 0010012 }
- { 2000000 OCT: 0010013 }
- { 2500000 OCT: 0010014 }
- { 3000000 OCT: 0010015 }
- { 3500000 OCT: 0010016 }
- { 4000000 OCT: 0010017 }
- } ?at [ invalid-baud ] unless ;
+++ /dev/null
-! Copyright (C) 2008 Doug Coleman.
-! See http://factorcode.org/license.txt for BSD license.
-USING: alien.c-types alien.syntax classes.struct kernel system
-unix ;
-IN: io.serial.unix.termios
-
-CONSTANT: NCCS 32
-
-TYPEDEF: uchar cc_t
-TYPEDEF: uint speed_t
-TYPEDEF: uint tcflag_t
-
-STRUCT: termios
- { iflag tcflag_t }
- { oflag tcflag_t }
- { cflag tcflag_t }
- { lflag tcflag_t }
- { line cc_t }
- { cc { cc_t NCCS } }
- { ispeed speed_t }
- { ospeed speed_t } ;
+++ /dev/null
-! Copyright (C) 2008 Doug Coleman.
-! See http://factorcode.org/license.txt for BSD license.
-USING: alien.c-types alien.syntax classes.struct kernel
-sequences system ;
-IN: io.serial.unix.termios
-
-CONSTANT: NCCS 20
-
-TYPEDEF: uint tcflag_t
-TYPEDEF: uchar cc_t
-TYPEDEF: uint speed_t
-
-STRUCT: termios
- { iflag tcflag_t }
- { oflag tcflag_t }
- { cflag tcflag_t }
- { lflag tcflag_t }
- { cc { cc_t NCCS } }
- { ispeed speed_t }
- { ospeed speed_t } ;
+++ /dev/null
-! Copyright (C) 2008 Doug Coleman.
-! See http://factorcode.org/license.txt for BSD license.
-USING: combinators system vocabs ;
-IN: io.serial.unix.termios
-
-{
- { [ os linux? ] [ "io.serial.unix.termios.linux" ] }
- { [ os macosx? ] [ "io.serial.unix.termios.macosx" ] }
-} cond require
+++ /dev/null
-! Copyright (C) 2008 Doug Coleman.
-! See http://factorcode.org/license.txt for BSD license.
-USING: accessors io io.serial io.serial.unix kernel literals
-math.bitwise tools.test ;
-IN: io.serial.unix
-
-: <serial-port-test> ( -- serial-port )
- "/dev/ttyS0" 19200 <serial-port> ;
-
-: with-serial-port-test ( quot -- )
- [ <serial-port-test> ] dip with-serial-port ; inline
-
-! [ ] [
- ! [ "hello" over stream-write stream-flush ] with-serial-port-test
-! ] unit-test
+++ /dev/null
-! Copyright (C) 2008 Doug Coleman.
-! See http://factorcode.org/license.txt for BSD license.
-USING: accessors alien.c-types alien.syntax alien.data
-classes.struct combinators io.ports io.streams.duplex
-system kernel math math.bitwise vocabs io.serial
-io.serial.unix.termios io.backend.unix unix unix.ffi
-literals ;
-IN: io.serial.unix
-
-<< {
- { [ os linux? ] [ "io.serial.unix.linux" ] }
-} cond require >>
-
-FUNCTION: speed_t cfgetispeed ( termios* t ) ;
-FUNCTION: speed_t cfgetospeed ( termios* t ) ;
-FUNCTION: int cfsetispeed ( termios* t, speed_t s ) ;
-FUNCTION: int cfsetospeed ( termios* t, speed_t s ) ;
-FUNCTION: int tcgetattr ( int i1, termios* t ) ;
-FUNCTION: int tcsetattr ( int i1, int i2, termios* t ) ;
-FUNCTION: int tcdrain ( int i1 ) ;
-FUNCTION: int tcflow ( int i1, int i2 ) ;
-FUNCTION: int tcflush ( int i1, int i2 ) ;
-FUNCTION: int tcsendbreak ( int i1, int i2 ) ;
-FUNCTION: void cfmakeraw ( termios* t ) ;
-FUNCTION: int cfsetspeed ( termios* t, speed_t s ) ;
-
-: fd>duplex-stream ( fd -- duplex-stream )
- <fd> init-fd
- [ <input-port> ] [ <output-port> ] bi <duplex-stream> ;
-
-: open-rw ( path -- fd ) O_RDWR file-mode open-file ;
-
-: <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
-
-: open-unix-serial-port ( serial-port -- )
- [
- path>> flags{ O_RDWR O_NOCTTY O_NDELAY } file-mode open-file
- fd>duplex-stream
- ] keep stream<< ;
-
-: serial-fd ( serial -- fd )
- stream>> in>> handle>> fd>> ;
-
-: set-termios ( serial -- )
- [
- serial-fd
- termios <struct> [ tcgetattr io-error ] keep
- ] keep termios<< ;
-
-: configure-termios ( serial -- )
- dup termios>>
- {
- [ [ iflag>> ] dip over [ iflag<< ] [ 2drop ] if ]
- [ [ oflag>> ] dip over [ oflag<< ] [ 2drop ] if ]
- [
- [
- [ cflag>> 0 or ] [ baud>> lookup-baud ] bi bitor
- ] dip cflag<<
- ]
- [ [ lflag>> ] dip over [ lflag<< ] [ 2drop ] if ]
- } 2cleave ;
-
-: tciflush ( serial -- )
- serial-fd TCIFLUSH tcflush io-error ;
-
-: apply-termios ( serial -- )
- [ serial-fd TCSANOW ]
- [ 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 ;