tangled
alpha
login
or
join now
okk.moe
/
escpos
2
fork
atom
🖨️ esc/pos implementation in gleam
2
fork
atom
overview
issues
pulls
pipelines
feat: add usb printing
okk.moe
1 month ago
7c9e9151
7ce0fed0
verified
This commit was signed with the committer's
known signature
.
okk.moe
SSH Key Fingerprint:
SHA256:cVofqXFhnCrIkqo1ixFFvbU8h8MTjljdVwg5pBQ7wxY=
+34
-7
3 changed files
expand all
collapse all
unified
split
gleam.toml
manifest.toml
src
escpos
printer.gleam
+1
gleam.toml
···
9
9
[dependencies]
10
10
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
11
11
mug = ">= 3.1.0 and < 4.0.0"
12
12
+
simplifile = ">= 2.3.2 and < 3.0.0"
12
13
13
14
[dev-dependencies]
14
15
gleeunit = ">= 1.0.0 and < 2.0.0"
+3
manifest.toml
···
2
2
# You typically do not need to edit this file
3
3
4
4
packages = [
5
5
+
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
5
6
{ name = "gleam_erlang", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "1124AD3AA21143E5AF0FC5CF3D9529F6DB8CA03E43A55711B60B6B7B3874375C" },
6
7
{ name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" },
7
8
{ name = "gleeunit", version = "1.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "FDC68A8C492B1E9B429249062CD9BAC9B5538C6FBF584817205D0998C42E1DAC" },
8
9
{ name = "mug", version = "3.1.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "mug", source = "hex", outer_checksum = "C01279D98E40371DA23461774B63F0E3581B8F1396049D881B0C7EB32799D93F" },
10
10
+
{ name = "simplifile", version = "2.3.2", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "E049B4DACD4D206D87843BCF4C775A50AE0F50A52031A2FFB40C9ED07D6EC70A" },
9
11
]
10
12
11
13
[requirements]
12
14
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
13
15
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
14
16
mug = { version = ">= 3.1.0 and < 4.0.0" }
17
17
+
simplifile = { version = ">= 2.3.2 and < 3.0.0" }
+30
-7
src/escpos/printer.gleam
···
1
1
import escpos/protocol
2
2
import gleam/result
3
3
import mug
4
4
+
import simplifile
4
5
5
6
@internal
6
7
pub type CommandBuffer {
···
8
9
}
9
10
10
11
pub opaque type Printer {
11
11
-
Printer(socket: mug.Socket)
12
12
+
NetworkPrinter(socket: mug.Socket)
13
13
+
UsbPrinter(device: String)
12
14
}
13
15
14
16
pub opaque type PrinterError {
15
17
ConnectionFailed(mug.ConnectError)
16
18
DisconnectionFailed(mug.Error)
17
19
TransmissionError(mug.Error)
18
18
-
PrintError(mug.Error)
20
20
+
NetworkPrintError(mug.Error)
21
21
+
UsbPrintError(simplifile.FileError)
22
22
+
UsbDeviceError(simplifile.FileError)
23
23
+
UsbDeviceNotFound
24
24
+
}
25
25
+
26
26
+
pub fn device(path: String) -> Result(Printer, PrinterError) {
27
27
+
case simplifile.is_file(path) {
28
28
+
Ok(True) -> Ok(UsbPrinter(device: path))
29
29
+
Ok(False) -> Error(UsbDeviceNotFound)
30
30
+
Error(err) -> Error(UsbDeviceError(err))
31
31
+
}
19
32
}
20
33
21
34
pub fn connect(ip: String, port: Int) -> Result(Printer, PrinterError) {
···
31
44
|> result.map_error(TransmissionError),
32
45
)
33
46
34
34
-
Ok(Printer(socket))
47
47
+
Ok(NetworkPrinter(socket))
35
48
}
36
49
37
50
/// Sends the CommandBuffer to the printer
38
51
pub fn print(cb: CommandBuffer, printer: Printer) -> Result(Nil, PrinterError) {
39
39
-
mug.send(printer.socket, cb.data)
40
40
-
|> result.map_error(PrintError)
52
52
+
case printer {
53
53
+
NetworkPrinter(socket:) ->
54
54
+
mug.send(socket, cb.data)
55
55
+
|> result.map_error(NetworkPrintError)
56
56
+
UsbPrinter(device:) ->
57
57
+
simplifile.write_bits(device, cb.data)
58
58
+
|> result.map_error(UsbPrintError)
59
59
+
}
41
60
}
42
61
43
62
pub fn disconnect(printer: Printer) -> Result(Nil, PrinterError) {
44
44
-
mug.shutdown(printer.socket)
45
45
-
|> result.map_error(DisconnectionFailed)
63
63
+
case printer {
64
64
+
NetworkPrinter(socket:) ->
65
65
+
mug.shutdown(socket)
66
66
+
|> result.map_error(DisconnectionFailed)
67
67
+
UsbPrinter(_) -> Ok(Nil)
68
68
+
}
46
69
}