aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-09-25 14:30:18 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-09-25 14:30:18 +0200
commit91489f1134568813fd5fac1e651d21cfd91e4ccd (patch)
tree23ed18304426d6f64a0e781b0b2315e56e4ecc9d
parentdf0f1c3f0ca7264c44012ac657d1f7168f864965 (diff)
wip
-rw-r--r--cmd/version/main.go11
-rw-r--r--dev.go12
-rw-r--r--dev_darwin.go35
-rw-r--r--dev_linux.go6
-rw-r--r--dev_openbsd.go6
-rw-r--r--plot.go34
-rw-r--r--robo.go12
7 files changed, 87 insertions, 29 deletions
diff --git a/cmd/version/main.go b/cmd/version/main.go
index 0a758ce..53bfa16 100644
--- a/cmd/version/main.go
+++ b/cmd/version/main.go
@@ -8,10 +8,15 @@ import (
)
func main() {
- dev, err := robo.Open()
+ r, err := robo.NewRobo()
if err != nil {
log.Fatal(err)
}
- defer dev.Close()
- fmt.Println(robo.Version(dev))
+ defer r.Close()
+ r.Init()
+ if !r.Ready() {
+ log.Fatal("not ready")
+ }
+ ver := r.Version()
+ fmt.Println("Version:", ver)
}
diff --git a/dev.go b/dev.go
index eca5919..d640e5f 100644
--- a/dev.go
+++ b/dev.go
@@ -7,8 +7,18 @@ import (
"syscall"
)
-type Device struct {
+const (
+ NUL = byte(0x00) // Null
+ ETX = byte(0x03) // End of Text
+ ESC = byte(0x1B) // Escape
+ FS = byte(0x1C) // File Separator
+)
+
+type Device interface {
io.ReadWriteCloser
+ ReadString() (string, error)
+ WriteString(string) error
+ Command([]byte) error
}
// Open is implemented in according GOOS files
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()
+}
diff --git a/dev_linux.go b/dev_linux.go
index dd8a5a3..66ab472 100644
--- a/dev_linux.go
+++ b/dev_linux.go
@@ -1,9 +1,5 @@
package robo
func Open() (Device, error) {
- dev, err := NewLP("/dev/usb/lp0")
- if err != nil {
- return Device{}, nil
- }
- return Device{dev}
+ return NewLP("/dev/usb/lp0")
}
diff --git a/dev_openbsd.go b/dev_openbsd.go
index edba157..46eaa94 100644
--- a/dev_openbsd.go
+++ b/dev_openbsd.go
@@ -2,9 +2,5 @@ package robo
func Open() (Device, error) {
// note: ulpt* doesn't support read(), thus this dev is broken atm.
- dev, err := NewLP("/dev/ulpt0")
- if err != nil {
- return Device{}, nil
- }
- return Device{dev}
+ return NewLP("/dev/ulpt0")
}
diff --git a/plot.go b/plot.go
index 7993885..d939b7f 100644
--- a/plot.go
+++ b/plot.go
@@ -2,9 +2,26 @@ package robo
import (
"fmt"
+ "log"
"strings"
)
+type Robo struct {
+ dev Device
+}
+
+func NewRobo() (Robo, error) {
+ dev, err := Open()
+ if err != nil {
+ return Robo{}, err
+ }
+ return Robo{dev}, nil
+}
+
+func (r Robo) Close() error {
+ return r.dev.Close()
+}
+
type Plotter interface {
Plot() []byte
}
@@ -39,3 +56,20 @@ func CuttingArea(p Point) string { return "FU" + p.String() }
//func Calibration(p Point) string { return "TB72" + p.String() }
func Curve(a int, p ...Point) string { return fmt.Sprintf("Y%d,%v", a, Path(p)) }
+
+func (r Robo) Version() string {
+ r.dev.WriteString("FG")
+ resp, _ := r.dev.ReadString()
+ return resp
+}
+
+func (r Robo) Init() {
+ r.dev.Command([]byte{4})
+}
+
+func (r Robo) Ready() bool {
+ r.dev.Command([]byte{5})
+ resp, _ := r.dev.ReadString()
+ log.Printf("ready %q", resp)
+ return resp == "0"
+}
diff --git a/robo.go b/robo.go
index 8e63ad3..8623083 100644
--- a/robo.go
+++ b/robo.go
@@ -3,14 +3,6 @@ package robo
import (
"bufio"
"fmt"
- "io"
-)
-
-const (
- NUL = 0x00 // Null
- ETX = 0x03 // End of Text
- ESC = 0x1B // Escape
- FS = 0x1C // File Separator
)
func etx(c *bufio.Writer) {
@@ -154,11 +146,13 @@ func str(c *bufio.ReadWriter, cmd string) string {
return recv(c.Reader)
}
+/*
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, "@") }
@@ -193,7 +187,7 @@ func Initialize(c *bufio.ReadWriter, mid int, o Orientation) {
}
GoHome(c.Writer)
- fmt.Println("Craft ROBO Ver.", Version(c))
+ //fmt.Println("Craft ROBO Ver.", Version(c))
if pen, ok := MediaID[mid]; ok {
pen.Apply(c.Writer)