Skip to content

Commit 33d32d4

Browse files
Receipt Printing Added Lol, still need to have tests mostly for the text conversion
1 parent 7797aeb commit 33d32d4

File tree

5 files changed

+122
-5
lines changed

5 files changed

+122
-5
lines changed

Debug/Error_Handling.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ func HandleKnownError(id int, condition bool, w fyne.Window) bool {
2929
return false
3030
}
3131

32-
func ShowError(state string, err error, w fyne.Window) {
32+
func ShowError(state string, err error, w fyne.Window) bool {
3333
if err != nil {
3434
dialog.ShowInformation("Oops", "Error while"+state+"\nError: "+err.Error(), w)
35+
return true
3536
}
37+
return false
3638
}

Main.go

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"BronzeHermes/Database"
55
"BronzeHermes/Debug"
66
"BronzeHermes/Graph"
7+
"BronzeHermes/Printer"
78
"BronzeHermes/UI"
89
unknown "BronzeHermes/Unknown"
910
"fmt"
@@ -20,6 +21,8 @@ import (
2021
"fyne.io/fyne/v2/widget"
2122
)
2223

24+
var printer Printer.Printer
25+
2326
func main() {
2427
a := app.NewWithID("PINKSTONE")
2528
go Graph.StartServer()
@@ -42,13 +45,16 @@ func CreateWindow(a fyne.App) {
4245
Database.CleanUpDeadItems()
4346
Database.SaveData()
4447
Database.SaveBackUp()
48+
if printer.Active {
49+
printer.Close()
50+
}
4551
},
4652
)
4753

4854
Debug.ShowError("Loading Data", Database.LoadData(), w)
4955

5056
w.SetContent(container.NewVBox(container.NewAppTabs(
51-
container.NewTabItem("Main", makeMainMenu(a)),
57+
container.NewTabItem("Main", makeMainMenu()),
5258
container.NewTabItem("Shop", makeShoppingMenu()),
5359
container.NewTabItem("Inventory", Database.MakeInfoMenu(w)),
5460
container.NewTabItem("Report", makeReportMenu()),
@@ -66,7 +72,7 @@ func CreateWindow(a fyne.App) {
6672
w.ShowAndRun()
6773
}
6874

69-
func makeMainMenu(a fyne.App) fyne.CanvasObject {
75+
func makeMainMenu() fyne.CanvasObject {
7076
var SignInStartUp dialog.Dialog
7177
var CreateUser dialog.Dialog
7278
usrData := binding.NewIntList()
@@ -128,6 +134,7 @@ func makeMainMenu(a fyne.App) fyne.CanvasObject {
128134
widget.NewButton("Save Backup Data", func() {
129135
go Debug.ShowError("Backing up Data", Database.SaveBackUp(), w)
130136
}),
137+
131138
widget.NewButton("Load Backup Data", func() {
132139
dialog.ShowConfirm("Are you Sure?", "Are you sure you want to load the backup data?", func(b bool) {
133140
if !b {
@@ -137,6 +144,64 @@ func makeMainMenu(a fyne.App) fyne.CanvasObject {
137144
dialog.ShowInformation("Loaded", "Back Up Loaded", w)
138145
}, w)
139146
}),
147+
148+
widget.NewButton("Connect Printer", func() {
149+
devices, idxes, err := Printer.FindUSBDevices()
150+
Debug.ShowError("Looking for USB Devices", err, w)
151+
152+
devListData := binding.NewIntList()
153+
devListData.Set(idxes)
154+
devList := widget.NewListWithData(devListData,
155+
func() fyne.CanvasObject {
156+
return container.NewBorder(nil, nil, nil, nil, widget.NewLabel(""))
157+
},
158+
func(di binding.DataItem, co fyne.CanvasObject) {
159+
idx, _ := di.(binding.Int).Get()
160+
co.(*fyne.Container).Objects[0].(*widget.Label).SetText(devices[idx].Product)
161+
})
162+
163+
devList.OnSelected = func(id widget.ListItemID) {
164+
dialog.ShowCustomConfirm(devices[id].Product, "Select", "Close",
165+
widget.NewLabel(fmt.Sprintf("ID: %d\nProduct: %s\nVender ID: %d\nManufacturer: %s\nSerial: %s\nPath: %s",
166+
devices[id].ProductID, devices[id].Product, devices[id].VendorID, devices[id].Manufacturer, devices[id].Serial, devices[id].Path)), func(b bool) {
167+
168+
if !b {
169+
return
170+
}
171+
172+
p, e := Printer.Init(devices[id])
173+
174+
if Debug.ShowError("Selecting "+devices[id].Product, e, w) {
175+
return
176+
}
177+
178+
if printer.Active {
179+
printer.Close()
180+
}
181+
182+
printer = p
183+
}, w)
184+
devList.Unselect(id)
185+
}
186+
187+
dialog.ShowCustom("Devices", "Close", container.NewVBox(devList), w)
188+
}),
189+
190+
widget.NewButton("Test Printing", func() {
191+
if printer.Active {
192+
entry := widget.NewEntry()
193+
dialog.ShowForm("", "Send", "Close", []*widget.FormItem{
194+
widget.NewFormItem("Message", entry),
195+
}, func(b bool) {
196+
if !b {
197+
return
198+
}
199+
printer.Print(Printer.ConvertForPrinter(entry.Text))
200+
}, w)
201+
} else {
202+
dialog.ShowInformation("", "No Printer Connected", w)
203+
}
204+
}),
140205
)
141206
}
142207

@@ -206,8 +271,8 @@ func makeShoppingMenu() fyne.CanvasObject {
206271
widget.NewLabelWithStyle("PINKSTONE TRADING", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
207272
widget.NewLabelWithStyle(receipt, fyne.TextAlignCenter, fyne.TextStyle{}),
208273
), func(printing bool) {
209-
if printing {
210-
// TODO: Send print msg to Printer & the receipt,
274+
if printing && printer.Active {
275+
printer.Print(Printer.ConvertForPrinter(receipt))
211276
}
212277
}, w)
213278
}, w)

Printer/printer.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Printer
2+
3+
import (
4+
"strings"
5+
6+
"github.com/karalabe/usb"
7+
)
8+
9+
type Printer struct {
10+
device usb.Device
11+
Active bool
12+
}
13+
14+
func FindUSBDevices() (devs []usb.DeviceInfo, out []int, err error) {
15+
devs, err = usb.Enumerate(0, 0)
16+
for i := range devs {
17+
out = append(out, i)
18+
}
19+
return
20+
}
21+
22+
func Init(dev usb.DeviceInfo) (printer Printer, err error) {
23+
printer.device, err = dev.Open()
24+
printer.Active = true
25+
return
26+
}
27+
28+
func (p Printer) Close() {
29+
p.device.Close()
30+
}
31+
32+
func (p Printer) Print(out string) {
33+
p.device.Write([]byte(out))
34+
}
35+
36+
func ConvertForPrinter(in string) string {
37+
in = string([]byte{0x1B, '@'}) + in // Adding intialisation to the beginnning
38+
39+
// check for byte(183) and convert the line into Underline
40+
// in = strings.replaceAll(in, string([]byte{183}), string([]byte{0x1B, }))
41+
42+
in = strings.ReplaceAll(in, "\n", string([]byte{0x1B, 'd', 1})) // replacing /n with line feed
43+
44+
in = string([]byte{0x1B, 'V', 66}) + in // add cut command
45+
46+
return in
47+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.18
55
require (
66
fyne.io/fyne/v2 v2.1.4
77
github.com/go-echarts/go-echarts/v2 v2.2.4
8+
github.com/karalabe/usb v0.0.2
89
)
910

1011
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff h1:W71vTCKoxtdXgnm1E
2828
github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff/go.mod h1:wfqRWLHRBsRgkp5dmbG56SA0DmVtwrF5N3oPdI8t+Aw=
2929
github.com/jackmordaunt/icns v0.0.0-20181231085925-4f16af745526/go.mod h1:UQkeMHVoNcyXYq9otUupF7/h/2tmHlhrS2zw7ZVvUqc=
3030
github.com/josephspurrier/goversioninfo v0.0.0-20200309025242-14b0ab84c6ca/go.mod h1:eJTEwMjXb7kZ633hO3Ln9mBUCOjX2+FlTljvpl9SYdE=
31+
github.com/karalabe/usb v0.0.2 h1:M6QQBNxF+CQ8OFvxrT90BA0qBOXymndZnk5q235mFc4=
32+
github.com/karalabe/usb v0.0.2/go.mod h1:Od972xHfMJowv7NGVDiWVxk2zxnWgjLlJzE+F4F7AGU=
3133
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
3234
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
3335
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=

0 commit comments

Comments
 (0)