forked from factor/factor
/
serial.factor
36 lines (29 loc) · 1.07 KB
/
serial.factor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.c-types assocs combinators destructors
kernel math math.bitwise math.parser sequences summary system
vocabs io ;
IN: io.serial
TUPLE: serial-port < disposable stream path baud
termios iflag oflag cflag lflag ;
ERROR: invalid-baud baud ;
M: invalid-baud summary ( invalid-baud -- string )
baud>> number>string
"Baud rate " " not supported" surround ;
HOOK: lookup-baud os ( m -- n )
HOOK: open-serial os ( serial -- serial' )
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 windows? ] [ "io.serial.windows" ] }
} cond require