-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint.js
121 lines (100 loc) · 2.66 KB
/
print.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
var SP = require('serialport');
var port = new SP('/dev/usb/lp0', {autoOpen:false, baudRate: 57600})
var qr = require('qr-image');
function getPort(cb) {
if (port.isOpen()) {
cb(port);
} else {
port.open(function(err){
if(err) return console.error(err);
cb(port);
});
}
}
function closePort(cb) {
port.drain(function(){
setTimeout(function(){
port.close(cb);
}, 500);
});
}
var printQueue = [
printMatrixJob(qr.matrix("Hei Larsen")),
printMatrixJob(qr.matrix("Hei Orhan")),
printMatrixJob(qr.matrix("Hei Sylliaas")),
];
var printing = false;
function processPrintQueue() {
if(!printing) {
if(!printQueue.length){
setTimeout(processPrintQueue, 50);
return;
}
printing = true;
getPort(function(port) {
job = printQueue.shift();
job(port, function(){
printing = false;
processPrintQueue();
})
});
}
}
processPrintQueue();
function printMatrixJob(matrix) {
return function(port, cb) {
printMatrix(port, matrix, cb);
}
}
function printMatrix(port, matrix, cb) {
width = matrix.length * 8 * 2 // * Math.floor(48/matrix.length) //384;
height = matrix.length * 8 * 2 // * Math.floor(48/matrix.length) //384;
height -= 1; // ?!?!?!? If height is not decreased, it does not print the whole buffer
m = 48;
xL = Math.floor(width / 8) % 256;
xH = Math.floor(Math.floor(width / 8) / 256);
yL = height % 256;
yH = Math.floor(height / 256);
console.log(m, xL, xH, yL, yH);
var pixels = [];
console.log('generating bitmap');
for(var y = 0; y < height; y++) {
for(var x = 0; x < width; x+=8) {
var byte = 0;
for(var k=0; k < 8; k++) {
y_index = Math.floor(y * matrix.length / height);
x_index = Math.floor((x+k) * matrix.length / width);
value = matrix[y_index][x_index];
byte |= value << (7-k);
}
pixels.push(byte);
}
}
console.log('generated');
console.log((xL+xH*256)*(yL+yH*256), pixels.length);
var buffer = Buffer.concat([
// new Buffer([0x1b, 0x40]), // ESC @
new Buffer([0x1d, 0x76, 0x30, m, xL, xH, yL, yH]),
new Buffer(pixels),
new Buffer([0x1b, 0x64, 2])
]);
writeAndDrain(port, buffer, function(){
console.log('wrote data');
w(0);
setTimeout(cb, 0);
});
}
function writeAndDrain(port, data, cb){
port.write(data, function(){
port.drain(cb);
});
}
function w(i){return;
console.log(i);
writeAndDrain(port, i+"", function(){
setTimeout(function(){w(i+1)}, 100);
})
}
port.on('close', function(){console.log('closed');});
port.on('data', function(d){console.log('data', d);});
port.on('error', function(e){console.log('err', e);});