-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
296 lines (247 loc) · 8.77 KB
/
index.html
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Luck Jingle Printer</title>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.container {
display: flex;
flex-direction: column;
gap: 20px;
}
canvas {
border: 1px solid #ccc;
max-width: 100%;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
#status {
margin-top: 10px;
padding: 10px;
border-radius: 4px;
}
.success {
background: #e6ffe6;
}
.error {
background: #ffe6e6;
}
#debug {
font-family: monospace;
white-space: pre-wrap;
background: #f5f5f5;
padding: 10px;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>Luck Jingle Printer</h1>
<div>
<input type="file" id="imageInput" accept="image/*">
<button id="connectBtn">Connect Printer</button>
<button id="printBtn" disabled>Print Image</button>
</div>
<canvas id="preview"></canvas>
<div id="status"></div>
<div id="debug"></div>
</div>
<script>
const PRINTER_SERVICE = "0000ff00-0000-1000-8000-00805f9b34fb";
const WRITE_CHARACTERISTIC = "0000ff02-0000-1000-8000-00805f9b34fb";
const NOTIFY_CHARACTERISTIC = "0000ff01-0000-1000-8000-00805f9b34fb";
const WIDTH = 384;
let device, characteristic;
let imageData = '';
// UI Elements
const canvas = document.getElementById('preview');
const ctx = canvas.getContext('2d');
const imageInput = document.getElementById('imageInput');
const connectBtn = document.getElementById('connectBtn');
const printBtn = document.getElementById('printBtn');
const status = document.getElementById('status');
const debug = document.getElementById('debug');
function showStatus(message, isError = false) {
status.textContent = message;
status.className = isError ? 'error' : 'success';
}
function log(message) {
debug.textContent += message + '\n';
console.log(message);
}
async function connectPrinter() {
try {
debug.textContent = ''; // Clear debug log
log('Requesting Bluetooth Device...');
device = await navigator.bluetooth.requestDevice({
filters: [
{ namePrefix: 'LuckP_D1' },
{ namePrefix: 'DP_D1' },
],
optionalServices: [PRINTER_SERVICE]
});
log('Device selected. Connecting to GATT Server...');
const server = await device.gatt.connect();
log('Connected. Getting Services...');
const services = await server.getPrimaryServices();
log(`Found ${services.length} services:`);
for (const service of services) {
log(`> Service: ${service.uuid}`);
const characteristics = await service.getCharacteristics();
log(`>> Found ${characteristics.length} characteristics:`);
for (const char of characteristics) {
log(`>> Characteristic: ${char.uuid}`);
// Find our target characteristic
if (char.uuid.toLowerCase() === WRITE_CHARACTERISTIC.toLowerCase()) {
log('>> Found write characteristic!');
characteristic = char;
}
}
}
if (characteristic) {
printBtn.disabled = false;
showStatus('Printer connected successfully!');
log('Connection complete!');
} else {
throw new Error('Required characteristic not found');
}
} catch (error) {
showStatus('Connection failed: ' + error.message, true);
log('Error: ' + error.message);
}
}
function applyDither(imageData, width, height) {
const data = imageData.data;
const brightness = 0.35;
const contrast = Math.pow(1.45, 2);
// Apply brightness and contrast
for (let i = 0; i < data.length; i += 4) {
for (let j = 0; j < 3; j++) {
let value = data[i + j];
value += (brightness - 0.5) * 256;
value = (value - 128) * contrast + 128;
data[i + j] = Math.min(Math.max(value, 0), 255);
}
}
// Floyd-Steinberg dithering
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const idx = (y * width + x) * 4;
const oldPixel = (data[idx] + data[idx + 1] + data[idx + 2]) / 3;
const newPixel = oldPixel < 128 ? 0 : 255;
const error = oldPixel - newPixel;
data[idx] = data[idx + 1] = data[idx + 2] = newPixel;
if (x < width - 1) {
distributeError(data, idx + 4, error * 7 / 16);
}
if (y < height - 1) {
if (x > 0) distributeError(data, idx + width * 4 - 4, error * 3 / 16);
distributeError(data, idx + width * 4, error * 5 / 16);
if (x < width - 1) distributeError(data, idx + width * 4 + 4, error * 1 / 16);
}
}
}
return imageData;
}
function distributeError(data, idx, error) {
for (let i = 0; i < 3; i++) {
data[idx + i] = Math.min(Math.max(data[idx + i] + error, 0), 255);
}
}
async function processImage(file) {
return new Promise((resolve) => {
const img = new Image();
img.onload = () => {
const height = Math.floor(img.height * WIDTH / img.width);
canvas.width = WIDTH;
canvas.height = height;
// Draw and resize image
ctx.drawImage(img, 0, 0, WIDTH, height);
// Get image data and apply dithering
let imageData = ctx.getImageData(0, 0, WIDTH, height);
imageData = applyDither(imageData, WIDTH, height);
ctx.putImageData(imageData, 0, 0);
// Convert to binary string
let binStr = '1' + '0'.repeat(318); // Start bits
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const brightness = data[i] + data[i + 1] + data[i + 2];
binStr += brightness > 600 ? '0' : '1';
}
resolve(binStr);
};
img.src = URL.createObjectURL(file);
});
}
async function sendToPrinter(hexData) {
try {
log('Starting print process...');
// Enable printer
log('Enabling printer...');
await characteristic.writeValue(new Uint8Array([0x10, 0xFF, 0x40]));
await characteristic.writeValue(new Uint8Array([0x10, 0xFF, 0xF1, 0x03]));
// Set density (high)
log('Setting density...');
const densityCommand = new Uint8Array([0x10, 0xFF, 0x10, 0x00, 0x02, 0x00]);
await characteristic.writeValue(densityCommand);
// Calculate length and send image data
const hexLen = Math.floor(hexData.length / 96) + 3;
const frontHex = (hexLen & 0xFF).toString(16).padStart(2, '0');
const endHex = ((hexLen >> 8) & 0xFF).toString(16).padStart(2, '0');
// Send data in chunks
log('Sending image data...');
const startCommand = `1D7630003000${frontHex}${endHex}`;
await characteristic.writeValue(hexStringToBytes(startCommand + hexData.slice(0, 224)));
for (let i = 224; i < hexData.length; i += 256) {
const chunk = hexData.slice(i, i + 256).padEnd(256, '0');
await characteristic.writeValue(hexStringToBytes(chunk));
await new Promise(resolve => setTimeout(resolve, 35));
log(`Sent chunk ${Math.floor(i / hexData.length * 100)}%`);
}
// End command
log('Sending end command...');
await characteristic.writeValue(hexStringToBytes('1B4A64'));
await characteristic.writeValue(new Uint8Array([0x10, 0xFF, 0xF1, 0x45]));
log('Print job completed!');
showStatus('Print job sent successfully!');
} catch (error) {
showStatus('Print failed: ' + error.message, true);
log('Error: ' + error.message);
}
}
function hexStringToBytes(hex) {
const bytes = new Uint8Array(Math.ceil(hex.length / 2));
for (let i = 0; i < hex.length; i += 2) {
bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
}
return bytes;
}
// Event Listeners
connectBtn.addEventListener('click', connectPrinter);
imageInput.addEventListener('change', async (e) => {
if (e.target.files[0]) {
const binStr = await processImage(e.target.files[0]);
imageData = (BigInt('0b' + binStr).toString(16));
showStatus('Image processed and ready to print!');
}
});
printBtn.addEventListener('click', async () => {
if (!imageData) {
showStatus('Please select an image first', true);
return;
}
await sendToPrinter(imageData);
});
</script>
</body>
</html>