aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-05-15 17:41:19 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-05-15 17:41:19 +0200
commit9a65aafab104564fb64e5b97f9628ef809a10379 (patch)
tree0eed6c1db0ba6aa0f781a3883ed5566d908ae3a7
parente2640ff058e077bf9de9a80394e649bbd819586c (diff)
Refactor Devicer
-rw-r--r--lp.go10
-rw-r--r--main.go4
-rw-r--r--usb.go12
3 files changed, 13 insertions, 13 deletions
diff --git a/lp.go b/lp.go
index da0dfd0..2a36f07 100644
--- a/lp.go
+++ b/lp.go
@@ -5,20 +5,20 @@ import (
"os"
)
-type LPDevice struct {
+type LP struct {
*os.File
}
-func NewLPDevice(path string) (Devicer, error) {
+func NewLP(path string) (Devicer, error) {
f, err := os.OpenFile(path, os.O_RDWR, 0666)
- return LPDevice{f}, err
+ return LP{f}, err
}
-func (d LPDevice) Close() {
+func (d LP) Close() {
d.File.Close()
}
-func (d LPDevice) Handle() *bufio.ReadWriter {
+func (d LP) Handle() *bufio.ReadWriter {
r := bufio.NewReader(d.File)
w := bufio.NewWriter(d.File)
return bufio.NewReadWriter(r, w)
diff --git a/main.go b/main.go
index a4769c7..16aa8f7 100644
--- a/main.go
+++ b/main.go
@@ -16,9 +16,9 @@ func main() {
flag.Parse()
if runtime.GOOS == "linux" {
- dev, err = NewLPDevice("/dev/usb/lp0")
+ dev, err = NewLP("/dev/usb/lp0")
} else {
- dev, err = NewDevice()
+ dev, err = NewUSB()
}
if err != nil {
diff --git a/usb.go b/usb.go
index 50351e9..660ca29 100644
--- a/usb.go
+++ b/usb.go
@@ -13,7 +13,7 @@ type Devicer interface {
Handle() *bufio.ReadWriter
}
-type Device struct {
+type USB struct {
ctx *usb.Context
dev *usb.Device
}
@@ -48,7 +48,7 @@ func cc100(desc *usb.Descriptor) bool {
return false
}
-func NewDevice() (Devicer, error) {
+func NewUSB() (USB, error) {
ctx := usb.NewContext()
ctx.Debug(debug)
devs, err := ctx.ListDevices(cc100)
@@ -59,17 +59,17 @@ func NewDevice() (Devicer, error) {
for _, dev := range devs {
dev.Close()
}
- return Device{}, errors.New("Cannot find " + craftRobo)
+ return USB{}, errors.New("Cannot find " + craftRobo)
}
- return Device{ctx, devs[0]}, nil
+ return USB{ctx, devs[0]}, nil
}
-func (d Device) Close() {
+func (d USB) Close() {
d.dev.Close()
d.ctx.Close()
}
-func (d Device) Handle() *bufio.ReadWriter {
+func (d USB) Handle() *bufio.ReadWriter {
var (
r *bufio.Reader
w *bufio.Writer