From 91489f1134568813fd5fac1e651d21cfd91e4ccd Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 25 Sep 2016 14:30:18 +0200 Subject: wip --- dev_darwin.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'dev_darwin.go') diff --git a/dev_darwin.go b/dev_darwin.go index d93f5c4..c49fd75 100644 --- a/dev_darwin.go +++ b/dev_darwin.go @@ -1,18 +1,15 @@ package robo import ( + "bufio" "errors" "io" "github.com/kylelemons/gousb/usb" ) -func Open() (io.ReadWriteCloser, error) { - dev, err := NewUSB() - if err != nil { - return Device{}, err - } - return Device{dev}, nil +func Open() (Device, error) { + return NewUSB() } type USB struct { @@ -96,3 +93,29 @@ func (d USB) Close() error { d.ctx.Close() return nil } + +// ReadString reads until End of Text +func (d USB) ReadString() (string, error) { + buf := bufio.NewReader(d.Reader) + resp, err := buf.ReadString(ETX) + if err != nil { + return "", err + } + return resp[:len(resp)-1], nil +} + +// WriteString terminates transfer with End of Text +func (d USB) WriteString(s string) error { + buf := bufio.NewWriter(d.Writer) + buf.WriteString(s) + buf.WriteByte(ETX) + return buf.Flush() +} + +// Command prefixes transfer with Escape +func (d USB) Command(b []byte) error { + buf := bufio.NewWriter(d.Writer) + buf.WriteByte(ESC) + buf.Write(b) + return buf.Flush() +} -- cgit v1.2.3