aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go13
-rw-r--r--usb.go7
-rw-r--r--usblp.go25
3 files changed, 39 insertions, 6 deletions
diff --git a/main.go b/main.go
index 05160e2..ecdeaf9 100644
--- a/main.go
+++ b/main.go
@@ -1,9 +1,16 @@
package main
-import "fmt"
+import (
+ "fmt"
+ "log"
+)
func main() {
- dev := NewDevice()
+ //dev, err := NewDevice()
+ dev, err := NewLPDevice("/dev/usb/lp0")
+ if err != nil {
+ log.Fatal(err)
+ }
defer dev.Close()
c := NewCutter(dev.Handle(), Portrait)
@@ -27,6 +34,6 @@ func main() {
fmt.Println("Offset", c.ReadOffset())
fmt.Println("Upper Right", c.ReadUpperRight())
fmt.Println("Lower Left", c.ReadLowerLeft())
- fmt.Println(c.StatusWord())
+ //fmt.Println(c.StatusWord())
}
diff --git a/usb.go b/usb.go
index a935810..8d89274 100644
--- a/usb.go
+++ b/usb.go
@@ -2,6 +2,7 @@ package main
import (
"bufio"
+ "errors"
"log"
"github.com/kylelemons/gousb/usb"
@@ -42,7 +43,7 @@ func CC100(desc *usb.Descriptor) bool {
return false
}
-func NewDevice() (d Device) {
+func NewDevice() (Device, error) {
ctx := usb.NewContext()
ctx.Debug(debug)
devs, err := ctx.ListDevices(CC100)
@@ -53,9 +54,9 @@ func NewDevice() (d Device) {
for _, dev := range devs {
dev.Close()
}
- log.Fatal("Cannot find ", craftRobo)
+ return Device{}, errors.New("Cannot find " + craftRobo)
}
- return Device{ctx, devs[0]}
+ return Device{ctx, devs[0]}, nil
}
func (d Device) Close() {
diff --git a/usblp.go b/usblp.go
new file mode 100644
index 0000000..b6cf987
--- /dev/null
+++ b/usblp.go
@@ -0,0 +1,25 @@
+package main
+
+import (
+ "bufio"
+ "os"
+)
+
+type LPDevice struct {
+ *os.File
+}
+
+func NewLPDevice(path string) (LPDevice, error) {
+ f, err := os.OpenFile(path, os.O_RDWR, 0666)
+ return LPDevice{f}, err
+}
+
+func (d LPDevice) Close() {
+ d.Close()
+}
+
+func (d LPDevice) Handle() *bufio.ReadWriter {
+ r := bufio.NewReader(d.File)
+ w := bufio.NewWriter(d.File)
+ return bufio.NewReadWriter(r, w)
+}