aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-09-24 01:03:41 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-09-24 01:03:41 +0200
commite138e277348c53f99b1f022a9828819245ee7a53 (patch)
treeea150db30b10fc423009a363bae44259b4c8768f
parent16415e31ff390fe106017e3e7fc5c692674bdcbf (diff)
wip - broken
-rw-r--r--cmd/version/main.go3
-rw-r--r--device.go5
-rw-r--r--device_darwin.go7
-rw-r--r--robo.go9
-rw-r--r--usb_darwin.go54
5 files changed, 44 insertions, 34 deletions
diff --git a/cmd/version/main.go b/cmd/version/main.go
index 2170fdd..b68f02e 100644
--- a/cmd/version/main.go
+++ b/cmd/version/main.go
@@ -9,6 +9,5 @@ import (
func main() {
dev := robo.NewDevice()
defer dev.Close()
- handle := dev.Handle()
- fmt.Println(robo.Version(handle))
+ fmt.Println(robo.Version(dev))
}
diff --git a/device.go b/device.go
index 46e7ea6..001b8de 100644
--- a/device.go
+++ b/device.go
@@ -1,8 +1,7 @@
package robo
-import "bufio"
+import "io"
type Device interface {
- Close()
- Handle() *bufio.ReadWriter
+ io.ReadWriteCloser
}
diff --git a/device_darwin.go b/device_darwin.go
index 036eb8d..3eb6ae1 100644
--- a/device_darwin.go
+++ b/device_darwin.go
@@ -1,8 +1,11 @@
package robo
-import "log"
+import (
+ "io"
+ "log"
+)
-func NewDevice() Device {
+func NewDevice() io.ReadWriteCloser {
dev, err := NewUSB()
if err != nil {
log.Fatal(err)
diff --git a/robo.go b/robo.go
index 2181bc5..d0cf9da 100644
--- a/robo.go
+++ b/robo.go
@@ -3,6 +3,7 @@ package robo
import (
"bufio"
"fmt"
+ "io"
)
const (
@@ -164,7 +165,13 @@ func str(c *bufio.ReadWriter, cmd string) string {
return recv(c.Reader)
}
-func Version(c *bufio.ReadWriter) string { return str(c, "FG") }
+func Version(c io.Writer) string {
+ io.WriteString(c, "FG")
+ //return str(c, "FG")
+ return ""
+}
+
+//func Version(c *bufio.ReadWriter) string { return str(c, "FG") }
func StatusWord(c *bufio.ReadWriter) string { return str(c, "@") }
type Orientation int
diff --git a/usb_darwin.go b/usb_darwin.go
index 1a54bdd..e0b1503 100644
--- a/usb_darwin.go
+++ b/usb_darwin.go
@@ -1,9 +1,8 @@
package robo
import (
- "bufio"
"errors"
- "log"
+ "io"
"github.com/kylelemons/gousb/usb"
)
@@ -11,6 +10,8 @@ import (
type USB struct {
ctx *usb.Context
dev *usb.Device
+ w io.Writer
+ r io.Reader
}
var (
@@ -21,7 +22,6 @@ var (
silhouette_sd = usb.ID(0x111d)
silhouette_cameo = usb.ID(0x1121)
silhouette_portrait = usb.ID(0x1123)
- debug = 3
)
func init() {
@@ -43,11 +43,11 @@ func match(desc *usb.Descriptor) bool {
}
func NewUSB() (USB, error) {
- ctx := usb.NewContext()
- ctx.Debug(debug)
- devs, err := ctx.ListDevices(match)
+ u := USB{}
+ u.ctx = usb.NewContext()
+ devs, err := u.ctx.ListDevices(match)
if err != nil {
- log.Fatal(err)
+ return USB{}, err
}
if len(devs) != 1 {
for _, dev := range devs {
@@ -55,42 +55,44 @@ func NewUSB() (USB, error) {
}
return USB{}, errors.New("Cannot find Craft ROBO")
}
- return USB{ctx, devs[0]}, nil
-}
+ u.dev = devs[0]
-func (d USB) Close() {
- d.dev.Close()
- d.ctx.Close()
-}
-
-func (d USB) Handle() *bufio.ReadWriter {
- var (
- r *bufio.Reader
- w *bufio.Writer
- )
-
- for _, c := range d.dev.Configs {
+ for _, c := range u.dev.Configs {
for _, i := range c.Interfaces {
for _, s := range i.Setups {
for _, ep := range s.Endpoints {
- e, err := d.dev.OpenEndpoint(
+ e, err := u.dev.OpenEndpoint(
c.Config,
i.Number,
s.Number,
ep.Address)
if err != nil {
- log.Fatal(err)
+ return USB{}, err
}
switch ep.Direction() {
case usb.ENDPOINT_DIR_OUT:
- w = bufio.NewWriter(e)
+ u.w = e
case usb.ENDPOINT_DIR_IN:
- r = bufio.NewReader(e)
+ u.r = e
}
}
}
}
}
- return bufio.NewReadWriter(r, w)
+ return u, nil
+}
+
+func (d USB) Close() error {
+ d.dev.Close()
+ d.ctx.Close()
+ return nil
+}
+
+func (d USB) Read(b []byte) (int, error) {
+ return d.r.Read(b)
+}
+
+func (d USB) Write(b []byte) (int, error) {
+ return d.w.Write(b)
}