This commit is contained in:
Matthew Frost 2023-11-18 14:51:29 +01:00
parent 1806a596ab
commit 4da54d788e
2 changed files with 136 additions and 0 deletions

41
server.go Normal file
View file

@ -0,0 +1,41 @@
package main
import (
"fmt"
"net/http"
"time"
)
func main() {
http.HandleFunc("/create_label", createLabelHandler)
fmt.Println("Server starting on port 8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
func createLabelHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
name := r.FormValue("name")
date := r.FormValue("date")
// Check if name is blank
if name == "" {
http.Error(w, "Name cannot be blank", http.StatusBadRequest)
return
}
// Set date to today if not defined
if date == "" {
date = time.Now().Format("2006-01-02")
}
// Here you can add the logic for creating a label using the POST data
fmt.Fprintf(w, "Label created! Name: %s, Date: %s", name, date)
}

95
trash.go Normal file
View file

@ -0,0 +1,95 @@
package main
import (
_ "image/jpeg"
_ "image/png" //I'm needed
"os"
"github.com/kenshaw/escpos"
)
const ESC = byte(27)
func pageWidth(widht int) []byte {
return []byte{escpos.GS, 87, byte(widht), 0}
}
func pageMargin(margin int) []byte {
return []byte{escpos.GS, 76, byte(margin), 0}
}
func pageMode() []byte {
return []byte{ESC, 'L'}
}
func pageDirection(direction uint8) []byte {
// https://www.epson-biz.com/modules/ref_escpos/index.php?content_id=55
return []byte{ESC, 'T', direction}
}
func pagePrintArea(x, y, width, height int) []byte {
return []byte{ESC, 'W',
byte(x & 255), byte(x >> 8 & 255),
byte(y & 255), byte(y >> 8 & 255),
byte(width & 255), byte(width >> 8 & 255),
byte(height & 255), byte(height >> 8 & 255)}
}
func offsetY(offset int) []byte {
// A positive number specifies movement downward, and a negative number specifies movement upward.
// The horizontal or vertical motion unit is used for the print direction set by ESC T.
// When the starting position is set to the upper left or lower right of the print area using ESC T, the vertical motion unit is used.
// When the starting position is set to the upper right or lower left of the print area using ESC T, the horizontal motion unit is used.
return []byte{escpos.GS, '\\', byte(offset & 255), byte(offset >> 8 & 255)}
}
func offsetX(offset int) []byte {
// A positive number specifies movement to the right, and a negative number specifies movement to the left.
// When Standard mode is selected, the horizontal motion unit is used.
// when Page mode is selected, the horizontal or vertical motion unit is used for the print direction set by ESC T.
// When the starting position is set to the upper left or lower right of the print area using ESC T, the horizontal motion unit is used.
// When the starting position is set to the upper right or lower left of the print area using ESC T, the vertical motion unit is used.
return []byte{ESC, '\\', byte(offset & 255), byte(offset >> 8 & 255)}
}
func invert(n int) []byte {
//When the LSB of n is 0, white/black reverse print mode is turned off.
//n = 0 255
return []byte{escpos.GS, byte('B'), byte(n)}
}
func disableSensors() []byte {
return []byte{ESC, 'c', '3', 0}
}
func printSpeed(m int) []byte {
// m = 0 9, 48 57 : slowest - fastest
// [Default] 0 = Setting value of GS ( E <Function 5> customize value (a = 6)
// m = 0
//Slower print speed seems to retain a bit more quality in dithered images. pure B/W images do not have a noticable difference
return []byte{escpos.GS, '(', 'K', 2, 0, 50, byte(m & 255), byte(m >> 8 & 255)}
}
func main() {
f, err := os.OpenFile("/dev/usb/lp1", os.O_WRONLY|os.O_CREATE, 0)
if err != nil {
panic(err)
}
defer f.Close()
ep := escpos.New(f)
ep.Init()
ep.WriteRaw(pageMode())
ep.WriteRaw(pageDirection(1))
ep.WriteRaw(pagePrintArea(0, 0, 1000, 850))
ep.WriteRaw(offsetY(30))
ep.SetFontSize(1,1)
ep.Write("Sheduled for trashing\n")
ep.WriteRaw(offsetY(35))
ep.SetFontSize(3,2)
ep.SetEmphasize(1)
ep.Write("Do Not Hack\n")
ep.SetEmphasize(0)
ep.WriteRaw(offsetY(15))
ep.SetFontSize(1,1)
ep.Write("Remove label to keep - 2023-10-14 \n")
ep.WriteRaw([]byte{12}) // FF: in Page mode, prints all the data in the print buffer collectively and switches from Page mode to Standard mode.
//ep.Formfeed()
ep.Cut()
ep.End()
}