Repository navigation
Expand file tree
/
Copy pathprinter.html
More file actions
69 lines (60 loc) · 1.54 KB
/
Copy pathprinter.html
File metadata and controls
69 lines (60 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<html>
<body>
<textarea id="printContent"></textarea>
<input type="submit" onclick="connectAndPrint()" value="Print"/>
<P>Type text into box and click on submit button.
<script>
// https://github.com/samccone/thermal_print/blob/master/driver.ts
var device;
String.prototype.toBytes = function() {
var arr = []
for (var i=0; i < this.length; i++) {
arr.push(this[i].charCodeAt(0))
}
return arr
}
function setup(device) {
return device.open()
.then(() => device.selectConfiguration(1))
.then(() => device.claimInterface(0))
}
function print() {
var string = document.getElementById("printContent").value + "\n";
var encoder = new TextEncoder();
//var data = encoder.encode(string);
var data = 'XXL hello world'.toBytes().concat([ 0x1b, 0x21, 0x10 , 0x0a ]); // 0x0a = LF
var buffer = new Uint8Array(data) //.buffer
console.log(data)
console.log(buffer)
var i;
for (i = 0; i < 10; i++) {
console.log("Try"+i)
device.transferOut(i, buffer)
.catch(error => { console.log(error); })
}
}
function connectAndPrint() {
if (device == null) {
navigator.usb.requestDevice({ filters: [{ vendorId: "0x0456" }] })
.then(selectedDevice => {
device = selectedDevice;
console.log(device);
return setup(device);
})
.then(() => print())
.catch(error => { console.log(error); })
}
else
print();
}
navigator.usb.getDevices()
.then(devices => {
if (devices.length > 0) {
device = devices[0];
return setup(device);
}
})
.catch(error => { console.log(error); });
</script>
</body>
</html>