-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
145 lines (118 loc) · 3.51 KB
/
main.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {
Utils,
RequestCommandId,
ResponseCommandId,
NiimbotBluetoothClient,
ImageEncoder,
NiimbotSerialClient,
} from "@mmote/niimbluelib";
let client = null;
const bleConnectButton = document.querySelector("button.connect.ble");
const serialConnectButton = document.querySelector("button.connect.serial");
const disconnectButton = document.querySelector("button.disconnect");
const printButton = document.querySelector("button.print");
const logPane = document.querySelector(".logger");
const canvas = document.querySelector("canvas");
/** Draw canvas test content */
const repaint = () => {
const ctx = canvas.getContext("2d");
ctx.fillStyle = "white";
ctx.lineWidth = 3;
// fill background
ctx.fillRect(0, 0, canvas.width, canvas.height);
// draw diagonal line
ctx.beginPath();
ctx.moveTo(0, ctx.lineWidth / 2);
ctx.lineTo(canvas.width, canvas.height-ctx.lineWidth / 2);
ctx.stroke();
// draw border
ctx.strokeRect(0.5, 0.5, canvas.width - 1, canvas.height - 1);
};
/** Add text to log pane */
const logger = (text) => {
console.log(text);
logPane.innerText += text + "\n";
logPane.scrollTop = logPane.scrollHeight;
};
/** Instantiate client */
const newClient = (transport) => {
if (client) {
client.disconnect();
}
if (transport === "ble") {
client = new NiimbotBluetoothClient();
} else if (transport === "serial") {
client = new NiimbotSerialClient();
}
client.on("packetsent", (e) => {
logger(`>> ${Utils.bufToHex(e.packet.toBytes())} (${RequestCommandId[e.packet.command]})`);
});
client.on("packetreceived", (e) => {
logger(`<< ${Utils.bufToHex(e.packet.toBytes())} (${ResponseCommandId[e.packet.command]})`);
});
client.on("connect", () => {
logger("connected");
disconnectButton.disabled = false;
printButton.disabled = false;
bleConnectButton.disabled = true;
serialConnectButton.disabled = true;
});
client.on("disconnect", () => {
logger("disconnected");
disconnectButton.disabled = true;
printButton.disabled = true;
bleConnectButton.disabled = false;
serialConnectButton.disabled = false;
});
client.on("printprogress", (e) => {
logger(`Page ${e.page}/${e.pagesTotal}, Page print ${e.pagePrintProgress}%, Page feed ${e.pageFeedProgress}%`);
});
};
/** On "Disconnect" clicked */
disconnectButton.onclick = () => {
client.disconnect();
client = null;
}
/** On "Connect BLE" clicked */
bleConnectButton.onclick = async () => {
newClient("ble");
try {
await client.connect();
} catch (e) {
alert(e);
}
};
/** On "Connect Serial" clicked */
serialConnectButton.onclick = async () => {
newClient("serial");
try {
await client.connect();
} catch (e) {
alert(e);
}
};
/** On "Print" clicked */
printButton.onclick = async () => {
/** left or top */
const printDirection = "left";
const quantity = 1;
/** Convert image to black and white bits */
const encoded = ImageEncoder.encodeCanvas(canvas, printDirection);
/** todo: Auto-detection works only for a small set of printers so manual user selection is required */
const printTaskName = client.getPrintTaskType() ?? "D110";
const printTask = client.abstraction.newPrintTask(printTaskName, {
totalPages: quantity,
statusPollIntervalMs: 100,
statusTimeoutMs: 8_000,
});
try {
await printTask.printInit();
await printTask.printPage(encoded, quantity);
await printTask.waitForFinished();
} catch (e) {
alert(e);
} finally {
await client.abstraction.printEnd();
}
};
repaint();