aboutsummaryrefslogtreecommitdiff
path: root/usb_darwin.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-05-23 17:51:25 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-05-23 17:51:25 +0200
commit0cd8982a904a53ad280657f9047edf9ed06743b0 (patch)
tree7f48587507851241692729c06c572ef69fa24a7d /usb_darwin.go
parentd5d6560c566fc27c947698503960115827fbe834 (diff)
OS-dependent build
Diffstat (limited to 'usb_darwin.go')
-rw-r--r--usb_darwin.go97
1 files changed, 97 insertions, 0 deletions
diff --git a/usb_darwin.go b/usb_darwin.go
new file mode 100644
index 0000000..96612b6
--- /dev/null
+++ b/usb_darwin.go
@@ -0,0 +1,97 @@
+package robo
+
+import (
+ "bufio"
+ "errors"
+ "log"
+
+ "github.com/kylelemons/gousb/usb"
+)
+
+type USB struct {
+ ctx *usb.Context
+ dev *usb.Device
+}
+
+var (
+ craftRobo = "Craft ROBO"
+ graphtec = usb.ID(0x0b4d)
+ cc200_20 = usb.ID(0x110a)
+ cc300_20 = usb.ID(0x111a)
+ silhouette_sd_1 = usb.ID(0x111c)
+ silhouette_sd_2 = usb.ID(0x111d)
+ silhouette_portrait = usb.ID(0x1123)
+ debug = 3
+)
+
+func init() {
+ // bump timeouts
+ usb.DefaultReadTimeout *= 60
+ usb.DefaultWriteTimeout *= 300
+}
+
+func cc100(desc *usb.Descriptor) bool {
+ if desc.Vendor == graphtec {
+ switch desc.Product {
+ case cc200_20, cc300_20,
+ silhouette_sd_1,
+ silhouette_sd_2,
+ silhouette_portrait:
+ return true
+ }
+ }
+ return false
+}
+
+func NewUSB() (USB, error) {
+ ctx := usb.NewContext()
+ ctx.Debug(debug)
+ devs, err := ctx.ListDevices(cc100)
+ if err != nil {
+ log.Fatal(err)
+ }
+ if len(devs) != 1 {
+ for _, dev := range devs {
+ dev.Close()
+ }
+ return USB{}, errors.New("Cannot find " + craftRobo)
+ }
+ return USB{ctx, devs[0]}, nil
+}
+
+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 _, i := range c.Interfaces {
+ for _, s := range i.Setups {
+ for _, ep := range s.Endpoints {
+ e, err := d.dev.OpenEndpoint(
+ c.Config,
+ i.Number,
+ s.Number,
+ ep.Address)
+ if err != nil {
+ log.Fatal(err)
+ }
+ switch ep.Direction() {
+ case usb.ENDPOINT_DIR_OUT:
+ w = bufio.NewWriter(e)
+ case usb.ENDPOINT_DIR_IN:
+ r = bufio.NewReader(e)
+ }
+ }
+ }
+ }
+ }
+
+ return bufio.NewReadWriter(r, w)
+}