From ac2879d312f3cca787db0b14f27edf9359130cc3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 19 Apr 2015 11:12:37 +0200 Subject: Initial import --- cutter.go | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++ doc/CC100m-UM-152.pdf | Bin 0 -> 1319064 bytes doc/GP-GL.pdf | Bin 0 -> 39654 bytes doc/HP-GL.pdf | Bin 0 -> 39963 bytes doc/TestCutIntel.plt | 1 + doc/gp-gl.txt | 93 ++++++++++++++++++++++++++ main.go | 43 ++++++++++++ pen.go | 25 +++++++ testpattern.go | 27 ++++++++ usb.go | 76 +++++++++++++++++++++ 10 files changed, 445 insertions(+) create mode 100644 cutter.go create mode 100644 doc/CC100m-UM-152.pdf create mode 100644 doc/GP-GL.pdf create mode 100644 doc/HP-GL.pdf create mode 100644 doc/TestCutIntel.plt create mode 100644 doc/gp-gl.txt create mode 100644 main.go create mode 100644 pen.go create mode 100644 testpattern.go create mode 100644 usb.go diff --git a/cutter.go b/cutter.go new file mode 100644 index 0000000..6ca03a3 --- /dev/null +++ b/cutter.go @@ -0,0 +1,180 @@ +package main + +import ( + "bufio" + "fmt" + "log" +) + +type Point struct { + X, Y int +} + +/* + A4 Cutting area + + 5mm y 5mm + +-----------------+ + | | 5mm + | +-------------+ | + | | | | + . . . . x + . . . . + . . . . + | | | | + | +-------------+ | + | | + | | 20mm + +-----------------+ + + Default size: 20000, 4000 + 1000 pt == 50 mm + + A4: 210x297 mm => 4200x5940 + Usable: 4000x5440 pt +*/ + +var ( + A4 = Point{5440, 4000} // Portrait + Origin = Point{0, 0} +) + +func (p Point) String() string { + return fmt.Sprintf("%v,%v", p.X, p.Y) +} + +type Cutter struct { + *bufio.ReadWriter +} + +func NewCutter(io *bufio.ReadWriter) Cutter { + return Cutter{io} +} + +func (c Cutter) EOT() { + defer c.Flush() + c.WriteByte(0x03) +} + +func (c Cutter) Home() { + defer c.EOT() + c.WriteString("H") +} + +func (c Cutter) Draw(p Point) { + defer c.EOT() + fmt.Fprint(c, "D", p) +} + +func (c Cutter) Move(p Point) { + defer c.EOT() + fmt.Fprint(c, "M", p) +} + +type LineStyle int + +const ( + Solid LineStyle = iota + Dots + ShortDash + Dash + LongDash + DashDot + DashLongDot + DashDoubleDot + DashLongDoubleDot +) + +func (c Cutter) LineType(n LineStyle) { + defer c.EOT() + fmt.Fprint(c, "L", n) +} + +func (c Cutter) Factor(p, q, r int) { + defer c.EOT() + fmt.Fprintf(c, "&%v,%v,%v", p, q, r) +} + +func (c Cutter) Offset(p Point) { + defer c.EOT() + fmt.Fprint(c, "^", p) +} + +func (c Cutter) WriteLowerLeft(p Point) { + defer c.EOT() + fmt.Fprint(c, "\\", p) +} + +func (c Cutter) WriteUpperRight(p Point) { + defer c.EOT() + fmt.Fprint(c, "Z", p) +} + +func (c Cutter) Version() { + c.WriteString("FG") + c.Flush() + ans, err := c.ReadString(0x03) + if err != nil { + log.Println(err) + } + log.Println(ans) +} + +func (c Cutter) ReadUpperRight() { + c.WriteString("U") + c.Flush() + ans, err := c.ReadString(0x03) + if err != nil { + log.Println(err) + } + log.Println(ans) +} + +func (c Cutter) Speed(n int) { + defer c.EOT() + fmt.Fprint(c, "!", n) +} + +func (c Cutter) Force(n int) { + defer c.EOT() + fmt.Fprint(c, "FX", n) +} + +func (c Cutter) Initialize() { + c.WriteString("\x1b\x04") // Initialize ??? + c.Flush() +} + +func (c Cutter) Status() { + c.WriteString("\x1b\x05") // Status ??? + c.Flush() + ans, err := c.ReadString(0x03) + if err != nil { + log.Println(err) + } + switch ans[:1] { + case "0": + log.Println("Ready") + case "1": + log.Println("Moving") + default: + log.Println("Unknown", ans) + } +} + +func (c Cutter) Bezier(a int, p0, p1, p2, p3 Point) { + defer c.EOT() + fmt.Fprintf(c, "BZ%v,%v,%v,%v,%v", a, p0, p1, p2, p3) +} + +type Page int + +const ( + Portrait Page = iota + Landscape +) + +func (c Cutter) Orientation(l Page) { + defer c.EOT() + fmt.Fprint(c, "FN", l) +} diff --git a/doc/CC100m-UM-152.pdf b/doc/CC100m-UM-152.pdf new file mode 100644 index 0000000..17717bf Binary files /dev/null and b/doc/CC100m-UM-152.pdf differ diff --git a/doc/GP-GL.pdf b/doc/GP-GL.pdf new file mode 100644 index 0000000..d28fdea Binary files /dev/null and b/doc/GP-GL.pdf differ diff --git a/doc/HP-GL.pdf b/doc/HP-GL.pdf new file mode 100644 index 0000000..c4b9a99 Binary files /dev/null and b/doc/HP-GL.pdf differ diff --git a/doc/TestCutIntel.plt b/doc/TestCutIntel.plt new file mode 100644 index 0000000..48a4435 --- /dev/null +++ b/doc/TestCutIntel.plt @@ -0,0 +1 @@ +FN0,&100,100,100,^0,0,\0,0,M510,637,BZ1,510,637,439,637,383,580,383,510,BZ1,383,510,383,439,439,383,510,383,BZ1,510,383,580,383,637,439,637,510,BZ1,637,510,637,580,580,637,510,637,M764,764,D256,764,D256,256,D764,256,D764,764,M2,510,D1018,510,M510,1018,D510,2,M0,0, \ No newline at end of file diff --git a/doc/gp-gl.txt b/doc/gp-gl.txt new file mode 100644 index 0000000..a8e64ab --- /dev/null +++ b/doc/gp-gl.txt @@ -0,0 +1,93 @@ +Plots line + +D Draw Dx1,y1,x2,y2...xn,yn[t] +E Relative Draw E∆x1,∆x1,∆x2,∆x2...∆xn,∆yn[t] +M Move Mx,y, +O Relative Move O∆x,∆y, +MP Move Polar MPr,θ[t] +DP Drar Polar DPr1,θ1,r2,θ2...rn,θn[t] +EP Relative Draw Polar EP∆r,θ[t] +OP Relative Move Polar EP∆r,∆θ[t] +RP Radius Plot EPθ,l1,l2 + +Character and symbol + +P Print Pc1,c2...cn[t] +K Kana (greek) Kc1,c2...cn[t] +N Mark Nn, +SP Select Point Mark SPc[t] +( User Pattern (n1,n2...nn[t] Not Supported +(P User Program Pattern (P[p,]∆x1,∆y1,[p,]∆x2,∆y2...[p,]∆xn,∆yn + +Circle and curve + +WP 3-Point Circle WPx1,y1,x2,y2,x3,y3[,d][t] +W Circle Wx0,y0,r1,r2,θ1,θ2[,d][t] +] Relative Circle ]r1,r2,θ1,θ2[,d][t] +Y Curve Ya,x1,y1,x2,y2...xn,yn[t] +_ Relative Curve _a,∆x1,∆y1,∆x2,∆y2...∆xn,∆yn[t] +) Ellipse )a,x0,y0,r1,r2,θ1,θ2,θ3 +BZ Bezier Curve BZa,x1,y1,x2,y2,x3,y3,x4,y4[,d][t] + +Line specification + +L Line Type Lp, +B Line Scale Bl, + +Character and symbol specification + +$ Font $n,(m,) +S Alpha Scale Sn,(m,) +Q Alpha Space Ql(k,) +R Alpha Rotate Rθ, +I Alpha Italic Ip, +LP Label Position LPn[t] +A Alpha Reset A +RC Replot Character RCc,x1,y1,[P,]x2,y2,[P1]...xn,yn[t] + +Control + +H Home H +^ Offset ^x,y, +^P Offset Polar ^Px,y[,θ[f][t]] +J New Pen Jn,(m) n=1..8 +! Speed !l[,n][t] l=1..10,101..140 n=1..8 +* Pen Acceleration&Force *a,f[,n][t] a=1..3 f=1..31 n=1..8 +FC Cutter Offset FCp,q[,n][t] +FD Blade Rotation Control FDθ[t] +\ Write Lower Left \x,y, +Z Write Upper Right Zx,y, +/ Rotate /x,y,θ, +> Clipping >x1,y1...xn,yn[t] + +& Factor &p,q,r, +SO Set Origin SOn +T Buzzer Tn +F Chart Feed Fl[t] + +Output coordinates + +G Gin G +C Call Gin C +? Read Offset ? +[ Read Lower Left [ +U Read Upper Right U + +Interface control + +V Read Status Word1 V +@ Read Status Word2 @ +# Read Status Word3 # += Term =t1,t2 +" Error Mask "m, +: Clear : +; Interface Clear ; +BS Buffer Size BSs1,s2,s3,s4 Not Supported + +Graph + +X Axis Xp,q,r[,t1,t2][t] +% Hatching %n,x,y,d,θ[t] n=1..3 + %n,r1,r2,θ1,θ2,d[t] n=11..13 + %n,d,θ,x1,y1...xn,yn[t] n=21..23 + diff --git a/main.go b/main.go new file mode 100644 index 0000000..09b8153 --- /dev/null +++ b/main.go @@ -0,0 +1,43 @@ +package main + +func main() { + dev := NewDevice() + defer dev.Close() + + cu := NewCutter(dev.Handle()) + p := pens["pen"] + + //cu.Version() + cu.Orientation(Portrait) + cu.WriteUpperRight(A4) + cu.Speed(p.Speed) + cu.Force(p.Force) + + /* + cu.TestPattern() + cu.Draw(Point{4000,0}) + cu.Draw(Point{4000,4000}) + cu.Draw(Point{0,4000}) + cu.Draw(Point{0,0}) + */ + + /* + for i := 0; i < 5; i++ { + cu.Move(Point{1000*i,0}) + cu.Draw(Point{1000*i,4000}) + + cu.Move(Point{0, 1000*i}) + cu.Draw(Point{4000, 1000*i}) + } + */ + + defer cu.Home() + defer cu.LineType(Solid) + + for i := 0; i < 9; i++ { + cu.LineType(LineStyle(i)) + cu.Move(Point{100 * i, 0}) + cu.Draw(Point{100 * i, 1000}) + } + +} diff --git a/pen.go b/pen.go new file mode 100644 index 0000000..6bcc02b --- /dev/null +++ b/pen.go @@ -0,0 +1,25 @@ +package main + +type Pen struct { + Speed int + Force int + Cap string +} + +var pens = map[string]Pen{ + "pen": Pen{ + Speed: 10, + Force: 10, + Cap: "pen", + }, + "thin": Pen{ + Speed: 10, + Force: 2, + Cap: "blue", + }, + "thick": Pen{ + Speed: 10, + Force: 27, + Cap: "yellow", + }, +} diff --git a/testpattern.go b/testpattern.go new file mode 100644 index 0000000..85d71c8 --- /dev/null +++ b/testpattern.go @@ -0,0 +1,27 @@ +package main + +func (c Cutter) TestPattern() { + c.Factor(100, 100, 100) + c.Offset(Origin) + c.WriteLowerLeft(Origin) + + c.Move(Point{510, 637}) + c.Bezier(1, Point{510, 637}, Point{439, 637}, + Point{383, 580}, Point{383, 510}) + c.Bezier(1, Point{383, 510}, Point{383, 439}, + Point{439, 383}, Point{510, 383}) + c.Bezier(1, Point{510, 383}, Point{580, 383}, + Point{637, 439}, Point{637, 510}) + c.Bezier(1, Point{637, 510}, Point{637, 580}, + Point{580, 637}, Point{510, 637}) + c.Move(Point{764, 764}) + c.Draw(Point{256, 764}) + c.Draw(Point{256, 256}) + c.Draw(Point{764, 256}) + c.Draw(Point{764, 764}) + c.Move(Point{2, 510}) + c.Draw(Point{1018, 510}) + c.Move(Point{510, 1018}) + c.Draw(Point{510, 2}) + c.Move(Origin) +} diff --git a/usb.go b/usb.go new file mode 100644 index 0000000..4770f92 --- /dev/null +++ b/usb.go @@ -0,0 +1,76 @@ +package main + +import ( + "bufio" + "log" + + "github.com/kylelemons/gousb/usb" +) + +type Device struct { + ctx *usb.Context + dev *usb.Device +} + +var ( + vendor = usb.ID(0x0b4d) + product = usb.ID(0x110a) + debug = 3 +) + +func CC100(desc *usb.Descriptor) bool { + return desc.Vendor == vendor && desc.Product == product +} + +func NewDevice() (d Device) { + 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() + } + log.Fatal("wrong number of devices") + } + return Device{ctx: ctx, dev: devs[0]} +} + +func (d Device) Close() { + d.dev.Close() + d.ctx.Close() +} + +func (d Device) 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) +} -- cgit v1.2.3