From 0729d3b3174de5f0802276172dc46383de1447f2 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 6 May 2015 15:37:57 +0200 Subject: Runtime OS detection --- cutter.go | 16 ++++++++-------- main.go | 35 +++++++++++++++++++++++------------ misc.go | 6 +++--- usb.go | 11 ++++++++--- usblp.go | 2 +- 5 files changed, 43 insertions(+), 27 deletions(-) diff --git a/cutter.go b/cutter.go index be1a40c..43a1e4d 100644 --- a/cutter.go +++ b/cutter.go @@ -57,7 +57,7 @@ type Triple struct { } var ( - A4 = Point{5440, 4000} // Portrait + A4 = Point{272 * MM, 200 * MM} // Portrait Origin = Point{0, 0} ) @@ -73,7 +73,7 @@ type Cutter struct { *bufio.ReadWriter } -func NewCutter(io *bufio.ReadWriter, o Orientation) Cutter { +func NewCutter(io *bufio.ReadWriter, o Orientation, rmlen float64) Cutter { c := Cutter{io} c.Initialize() if !c.Ready() { @@ -96,7 +96,9 @@ func NewCutter(io *bufio.ReadWriter, o Orientation) Cutter { fmt.Println("Calibration", c.GetCalibration()) fmt.Println("FA", c.UnknownFA()) - + if rmlen > 0 { + c.RegMarkLen(rmlen) + } c.Orientation(o) return Cutter{io} @@ -275,7 +277,7 @@ func (c Cutter) parseTriple() (t Triple) { return } -func (c Cutter) RegMarkLen(n int) { +func (c Cutter) RegMarkLen(n float64) { c.Send("TB51,", n) } @@ -373,16 +375,14 @@ func (c Cutter) TrackEnhancing(state OnOff) { c.Send("FY", state) } -func (c Cutter) SearchMarks(p Point, l int) bool { - c.RegMarkLen(l) +func (c Cutter) SearchMarks(p Point) bool { c.Send("TB99") c.Send("TB55,1") c.Send("TB123,", p) return c.parseDigit() == 0 } -func (c Cutter) ManualSearchMarks(p Point, l int) bool { - c.RegMarkLen(l) +func (c Cutter) ManualSearchMarks(p Point) bool { c.Send("TB99") c.Send("TB55,1") c.Send("TB23,", p) diff --git a/main.go b/main.go index 20a83c0..0c3c012 100644 --- a/main.go +++ b/main.go @@ -3,17 +3,28 @@ package main import ( "fmt" "log" + "runtime" ) func main() { - //dev, err := NewDevice() - dev, err := NewLPDevice("/dev/usb/lp0") + var ( + dev Devicer + err error + ) + + if runtime.GOOS == "linux" { + dev, err = NewLPDevice("/dev/usb/lp0") + } else { + dev, err = NewDevice() + } + if err != nil { log.Fatal(err) } + defer dev.Close() - c := NewCutter(dev.Handle(), Portrait) + c := NewCutter(dev.Handle(), Portrait, 0) defer c.Home() //defer c.LineType(Solid) @@ -26,18 +37,18 @@ func main() { //c.WriteUpperRight(Point{4000,5440}) //fmt.Println(c.UnknownFQ5()) //c.Bezier(1, Point{0,0},Point{0,1000},Point{0,0},Point{1000,0}) - //c.DrawCircles() - fmt.Println("Gin", c.Gin()) - fmt.Println("Call Gin", c.CallGin()) + //fmt.Println("Gin", c.Gin()) + //fmt.Println("Call Gin", c.CallGin()) - fmt.Println("Offset", c.ReadOffset()) - fmt.Println("Upper Right", c.ReadUpperRight()) - fmt.Println("Lower Left", c.ReadLowerLeft()) - //fmt.Println(c.StatusWord()) - - if c.SearchMarks(Point{19 * CM, 18 * CM}, 2*CM) { + if c.SearchMarks(Point{19 * CM, 18 * CM}) { fmt.Println("Reg Marks ok") + + fmt.Println("Offset", c.ReadOffset()) + fmt.Println("Upper Right", c.ReadUpperRight()) + fmt.Println("Lower Left", c.ReadLowerLeft()) + + c.DrawCircles() } } diff --git a/misc.go b/misc.go index 5934211..3366770 100644 --- a/misc.go +++ b/misc.go @@ -21,8 +21,8 @@ func (c Cutter) DrawLines() { } func (c Cutter) DrawCircles() { - base := Point{3000, 2000} - for i := 1; i < 10; i++ { + base := Point{1000, 1000} + for i := 1; i <= 3; i++ { c.Circle(base, Polar{100 * float64(i), 0}, Polar{100 * float64(i), 3600}) @@ -39,7 +39,7 @@ func (c Cutter) DrawPic() { } func (c Cutter) MustMarks(p Point) { - if !c.SearchMarks(p, 400) { + if !c.SearchMarks(p) { log.Fatal("marks not found") } } diff --git a/usb.go b/usb.go index 8d89274..18b2b5c 100644 --- a/usb.go +++ b/usb.go @@ -8,6 +8,11 @@ import ( "github.com/kylelemons/gousb/usb" ) +type Devicer interface { + Close() + Handle() *bufio.ReadWriter +} + type Device struct { ctx *usb.Context dev *usb.Device @@ -30,7 +35,7 @@ func init() { usb.DefaultWriteTimeout *= 60 } -func CC100(desc *usb.Descriptor) bool { +func cc100(desc *usb.Descriptor) bool { if desc.Vendor == graphtec { switch desc.Product { case cc200_20, cc300_20, @@ -43,10 +48,10 @@ func CC100(desc *usb.Descriptor) bool { return false } -func NewDevice() (Device, error) { +func NewDevice() (Devicer, error) { ctx := usb.NewContext() ctx.Debug(debug) - devs, err := ctx.ListDevices(CC100) + devs, err := ctx.ListDevices(cc100) if err != nil { log.Fatal(err) } diff --git a/usblp.go b/usblp.go index 6a8bf55..da0dfd0 100644 --- a/usblp.go +++ b/usblp.go @@ -9,7 +9,7 @@ type LPDevice struct { *os.File } -func NewLPDevice(path string) (LPDevice, error) { +func NewLPDevice(path string) (Devicer, error) { f, err := os.OpenFile(path, os.O_RDWR, 0666) return LPDevice{f}, err } -- cgit v1.2.3