🖨️ esc/pos implementation in gleam

feat: add usb printing

okk.moe 7c9e9151 7ce0fed0

verified
+34 -7
+1
gleam.toml
··· 9 [dependencies] 10 gleam_stdlib = ">= 0.44.0 and < 2.0.0" 11 mug = ">= 3.1.0 and < 4.0.0" 12 13 [dev-dependencies] 14 gleeunit = ">= 1.0.0 and < 2.0.0"
··· 9 [dependencies] 10 gleam_stdlib = ">= 0.44.0 and < 2.0.0" 11 mug = ">= 3.1.0 and < 4.0.0" 12 + simplifile = ">= 2.3.2 and < 3.0.0" 13 14 [dev-dependencies] 15 gleeunit = ">= 1.0.0 and < 2.0.0"
+3
manifest.toml
··· 2 # You typically do not need to edit this file 3 4 packages = [ 5 { name = "gleam_erlang", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "1124AD3AA21143E5AF0FC5CF3D9529F6DB8CA03E43A55711B60B6B7B3874375C" }, 6 { name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" }, 7 { name = "gleeunit", version = "1.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "FDC68A8C492B1E9B429249062CD9BAC9B5538C6FBF584817205D0998C42E1DAC" }, 8 { name = "mug", version = "3.1.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "mug", source = "hex", outer_checksum = "C01279D98E40371DA23461774B63F0E3581B8F1396049D881B0C7EB32799D93F" }, 9 ] 10 11 [requirements] 12 gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" } 13 gleeunit = { version = ">= 1.0.0 and < 2.0.0" } 14 mug = { version = ">= 3.1.0 and < 4.0.0" }
··· 2 # You typically do not need to edit this file 3 4 packages = [ 5 + { name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" }, 6 { name = "gleam_erlang", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "1124AD3AA21143E5AF0FC5CF3D9529F6DB8CA03E43A55711B60B6B7B3874375C" }, 7 { name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" }, 8 { name = "gleeunit", version = "1.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "FDC68A8C492B1E9B429249062CD9BAC9B5538C6FBF584817205D0998C42E1DAC" }, 9 { name = "mug", version = "3.1.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "mug", source = "hex", outer_checksum = "C01279D98E40371DA23461774B63F0E3581B8F1396049D881B0C7EB32799D93F" }, 10 + { name = "simplifile", version = "2.3.2", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "E049B4DACD4D206D87843BCF4C775A50AE0F50A52031A2FFB40C9ED07D6EC70A" }, 11 ] 12 13 [requirements] 14 gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" } 15 gleeunit = { version = ">= 1.0.0 and < 2.0.0" } 16 mug = { version = ">= 3.1.0 and < 4.0.0" } 17 + simplifile = { version = ">= 2.3.2 and < 3.0.0" }
+30 -7
src/escpos/printer.gleam
··· 1 import escpos/protocol 2 import gleam/result 3 import mug 4 5 @internal 6 pub type CommandBuffer { ··· 8 } 9 10 pub opaque type Printer { 11 - Printer(socket: mug.Socket) 12 } 13 14 pub opaque type PrinterError { 15 ConnectionFailed(mug.ConnectError) 16 DisconnectionFailed(mug.Error) 17 TransmissionError(mug.Error) 18 - PrintError(mug.Error) 19 } 20 21 pub fn connect(ip: String, port: Int) -> Result(Printer, PrinterError) { ··· 31 |> result.map_error(TransmissionError), 32 ) 33 34 - Ok(Printer(socket)) 35 } 36 37 /// Sends the CommandBuffer to the printer 38 pub fn print(cb: CommandBuffer, printer: Printer) -> Result(Nil, PrinterError) { 39 - mug.send(printer.socket, cb.data) 40 - |> result.map_error(PrintError) 41 } 42 43 pub fn disconnect(printer: Printer) -> Result(Nil, PrinterError) { 44 - mug.shutdown(printer.socket) 45 - |> result.map_error(DisconnectionFailed) 46 }
··· 1 import escpos/protocol 2 import gleam/result 3 import mug 4 + import simplifile 5 6 @internal 7 pub type CommandBuffer { ··· 9 } 10 11 pub opaque type Printer { 12 + NetworkPrinter(socket: mug.Socket) 13 + UsbPrinter(device: String) 14 } 15 16 pub opaque type PrinterError { 17 ConnectionFailed(mug.ConnectError) 18 DisconnectionFailed(mug.Error) 19 TransmissionError(mug.Error) 20 + NetworkPrintError(mug.Error) 21 + UsbPrintError(simplifile.FileError) 22 + UsbDeviceError(simplifile.FileError) 23 + UsbDeviceNotFound 24 + } 25 + 26 + pub fn device(path: String) -> Result(Printer, PrinterError) { 27 + case simplifile.is_file(path) { 28 + Ok(True) -> Ok(UsbPrinter(device: path)) 29 + Ok(False) -> Error(UsbDeviceNotFound) 30 + Error(err) -> Error(UsbDeviceError(err)) 31 + } 32 } 33 34 pub fn connect(ip: String, port: Int) -> Result(Printer, PrinterError) { ··· 44 |> result.map_error(TransmissionError), 45 ) 46 47 + Ok(NetworkPrinter(socket)) 48 } 49 50 /// Sends the CommandBuffer to the printer 51 pub fn print(cb: CommandBuffer, printer: Printer) -> Result(Nil, PrinterError) { 52 + case printer { 53 + NetworkPrinter(socket:) -> 54 + mug.send(socket, cb.data) 55 + |> result.map_error(NetworkPrintError) 56 + UsbPrinter(device:) -> 57 + simplifile.write_bits(device, cb.data) 58 + |> result.map_error(UsbPrintError) 59 + } 60 } 61 62 pub fn disconnect(printer: Printer) -> Result(Nil, PrinterError) { 63 + case printer { 64 + NetworkPrinter(socket:) -> 65 + mug.shutdown(socket) 66 + |> result.map_error(DisconnectionFailed) 67 + UsbPrinter(_) -> Ok(Nil) 68 + } 69 }