-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKeyDecoding.js
More file actions
600 lines (524 loc) · 26.2 KB
/
KeyDecoding.js
File metadata and controls
600 lines (524 loc) · 26.2 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
const display = require("display");
const dialog = require("dialog");
const storage = require("storage");
const keyboard = require("keyboard");
const displayWidth = display.width();
const displayHeight = display.height();
const bgColor = BRUCE_BGCOLOR;
const priColor = BRUCE_PRICOLOR;
const secColor = BRUCE_SECCOLOR;
const version = "1.14.1";
var logo = new Uint8Array([0x00,0x00,0x00,0x00,0xf0,0x03,0x00,0xfc,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf8,0x07,0x00,0xfe,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x0f,0x00,0xcf,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1c,0x0e,0x00,0x87,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1c,0x0c,0x00,0x83,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1c,0x0e,0x00,0x87,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x0e,0x00,0xc7,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x00,0xf8,0x07,0x00,0xfe,0x01,0xe0,0x01,0x00,0x00,0x00,0x00,0xfc,0x01,0xf0,0x03,0x00,0xfc,0x00,0xf8,0x03,0x00,0x00,0x00,0x00,0xfe,0x03,0xe0,0x07,0x00,0x7e,0x00,0xfc,0x07,0x00,0x00,0x00,0x00,0x87,0x03,0x00,0x0e,0x00,0x07,0x00,0x1c,0x0e,0x00,0x00,0x00,0x00,0x07,0x03,0x00,0x1c,0x80,0x03,0x00,0x0c,0x0e,0x00,0x00,0x00,0x00,0x07,0x03,0x00,0x38,0xc0,0x01,0x00,0x0c,0x0e,0x00,0x00,0x00,0x00,0x87,0x03,0x00,0x70,0xe0,0x00,0x00,0x1c,0x0e,0x00,0x00,0x00,0x00,0xfe,0x03,0x00,0x70,0xe0,0x00,0x00,0xfc,0x07,0x00,0x00,0x00,0x00,0xfc,0x03,0x00,0x70,0xe0,0x00,0x00,0xfc,0x03,0x00,0x00,0x00,0x00,0xf8,0x07,0x00,0x70,0xe0,0x00,0x00,0xfe,0x01,0x00,0x00,0x00,0x00,0x00,0x0e,0x00,0x70,0xe0,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x0c,0x00,0x70,0xe0,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x70,0xe0,0x00,0x80,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x70,0xe0,0x00,0xc0,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x70,0xe0,0x00,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x00,0x70,0xe0,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xc0,0x01,0x70,0xe0,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x80,0x03,0x70,0xe0,0x00,0x1c,0x00,0xc0,0x07,0x00,0x00,0x7f,0x00,0x00,0x07,0x70,0xe0,0x00,0x0e,0x00,0xe0,0x0f,0x00,0x00,0xe7,0x00,0x00,0x06,0x70,0xe0,0x00,0x06,0x00,0x70,0x0e,0x00,0x80,0xc3,0x01,0x00,0x0c,0x70,0xe0,0x00,0x03,0x00,0x38,0x1c,0x00,0x80,0xc3,0xff,0x0f,0x1c,0x70,0xe0,0x80,0x03,0xff,0x3f,0x1c,0x00,0x80,0xc3,0xff,0x1f,0x38,0x70,0xe0,0xc0,0x81,0xff,0x3f,0x1c,0x00,0x80,0xc3,0x01,0x38,0x30,0x70,0xe0,0xc0,0xc0,0x01,0x38,0x1c,0x00,0x00,0xff,0x00,0x70,0x60,0x70,0xe0,0x60,0xe0,0x00,0xf0,0x0f,0x00,0x00,0x7e,0x00,0xe0,0xe0,0x70,0xe0,0x70,0x70,0x00,0xe0,0x07,0x00,0x00,0x3c,0x00,0xc0,0xc1,0x70,0xe0,0x30,0x38,0x00,0xc0,0x03,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc3,0x70,0xe0,0x30,0x1c,0x00,0x00,0x00,0x00,0xf8,0x03,0x00,0x80,0xc3,0xfc,0xff,0x33,0x1c,0x00,0x00,0xfc,0x01,0xfc,0x03,0x00,0x80,0xc3,0xff,0xff,0x3f,0x1c,0x00,0x00,0xfc,0x03,0x1c,0x07,0x00,0x80,0xc3,0x1f,0x80,0x3f,0x1c,0x00,0x00,0x8e,0x03,0x0e,0x07,0x00,0x80,0xf3,0x03,0x00,0xfc,0x1c,0x00,0x00,0x0e,0x07,0x0e,0x0f,0x00,0x80,0x7b,0x00,0x00,0xe0,0x1d,0x00,0x00,0x0f,0x07,0x0e,0x07,0x00,0x80,0x3f,0x00,0x00,0xc0,0x1f,0x00,0x00,0x0e,0x07,0x1c,0x07,0x00,0x80,0x1f,0xc0,0x3f,0x80,0x1f,0x00,0x00,0x8e,0x03,0xfc,0x03,0x00,0x80,0x07,0xf0,0xff,0x00,0x1e,0x00,0x00,0xfc,0x03,0xf8,0x01,0x00,0xc0,0x03,0xf8,0xff,0x01,0x3c,0x00,0x00,0xf8,0x01,0xf0,0x00,0x00,0xc0,0x01,0xfc,0xff,0x03,0x38,0x00,0x00,0xf0,0x00,0x60,0x00,0x00,0xe0,0x00,0xfe,0xff,0x07,0x70,0x00,0x00,0x60,0x00,0x60,0x00,0x00,0x70,0x00,0xfe,0xff,0x07,0xe0,0x00,0x00,0x60,0x00,0x60,0x00,0x00,0x70,0x00,0x7f,0xe0,0x0f,0xe0,0x00,0x00,0x60,0x00,0x60,0x00,0x00,0x38,0x00,0x3f,0xc0,0x0f,0xc0,0x01,0x00,0x60,0x00,0x60,0x00,0x00,0x3c,0x00,0x3f,0xc0,0x0f,0xc0,0x03,0x00,0x60,0x00,0x60,0x00,0x00,0x1c,0x00,0x3f,0xc0,0x0f,0x80,0x03,0x00,0x60,0x00,0x60,0x00,0x00,0x1c,0x80,0x1f,0x80,0x1f,0x80,0x03,0x00,0x60,0x00,0x60,0x00,0x00,0x0c,0x80,0x1f,0x80,0x1f,0x00,0x03,0x00,0x60,0x00,0x60,0x00,0x00,0x0e,0x80,0x1f,0x80,0x1f,0x00,0x07,0x00,0x60,0x00,0x60,0x00,0x00,0x0e,0x80,0x1f,0x80,0x1f,0x00,0x07,0x00,0x60,0x00,0x60,0x00,0x00,0x0e,0xf0,0xff,0xff,0xff,0x00,0x07,0x00,0x60,0x00,0x60,0x00,0x00,0x0f,0xf8,0xff,0xff,0xff,0x01,0x0f,0x00,0x60,0x00,0xe0,0xff,0xff,0x07,0xf8,0xff,0xff,0xff,0x01,0xfe,0xff,0x7f,0x00,0xe0,0xff,0xff,0x07,0xf8,0xff,0xff,0xff,0x01,0xfe,0xff,0x7f,0x00,0x00,0x00,0x00,0x07,0xf8,0xff,0xff,0xff,0x01,0x0e,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xf8,0xff,0xff,0xff,0x01,0x0e,0x00,0x00,0x00,0xc0,0xff,0xff,0x07,0xc0,0xff,0xf9,0x3f,0x00,0xfe,0xff,0x3f,0x00,0xe0,0xff,0xff,0x07,0xf8,0xff,0xf0,0xff,0x01,0xfe,0xff,0x7f,0x00,0xe0,0xff,0xff,0x0f,0xf8,0xff,0xf0,0xff,0x01,0xff,0xff,0x7f,0x00,0x60,0x00,0x00,0x0e,0xf8,0xff,0xf0,0xff,0x01,0x07,0x00,0x60,0x00,0x60,0x00,0x00,0x0e,0xf8,0xff,0xf0,0xff,0x01,0x07,0x00,0x60,0x00,0x60,0x00,0x00,0x0e,0xc0,0xff,0xf9,0x3f,0x00,0x07,0x00,0x60,0x00,0x60,0x00,0x00,0x0c,0xf8,0xff,0xf0,0xff,0x01,0x03,0x00,0x60,0x00,0x60,0x00,0x00,0x1c,0xf8,0xff,0xf0,0xff,0x81,0x03,0x00,0x60,0x00,0x60,0x00,0x00,0x1c,0xf8,0xff,0xf0,0xff,0x81,0x03,0x00,0x60,0x00,0x60,0x00,0x00,0x3c,0xf0,0xff,0xf0,0xff,0xc0,0x03,0x00,0x60,0x00,0xf8,0x01,0x00,0x38,0xf8,0xff,0xff,0xff,0xc1,0x01,0x00,0xf8,0x01,0xfc,0x03,0x00,0x70,0xf8,0xff,0xff,0xff,0xe1,0x00,0x00,0xfc,0x03,0x9c,0x07,0x00,0x70,0xf8,0xff,0xff,0xff,0xe1,0x00,0x00,0x9e,0x03,0x0e,0x07,0x00,0xe0,0xf8,0xff,0xff,0xff,0x71,0x00,0x00,0x0e,0x07,0x0e,0x07,0x00,0xc0,0xf9,0xff,0xff,0xff,0x39,0x00,0x00,0x0e,0x07,0x0e,0x07,0x00,0xc0,0xf3,0xff,0xff,0xff,0x3c,0x00,0x00,0x0e,0x07,0x0e,0x07,0x00,0x80,0x07,0x00,0x00,0x00,0x1e,0x00,0x00,0x0e,0x07,0xfc,0x03,0x00,0x00,0x1f,0x00,0x00,0x80,0x0f,0x00,0x00,0xfc,0x03,0xf8,0x01,0x00,0x00,0x3e,0x00,0x00,0xc0,0x07,0x00,0x00,0xf8,0x01,0xf0,0x00,0x00,0x00,0x78,0x00,0x00,0xe0,0x01,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0xf0,0x03,0x00,0xfc,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xc0,0x1f,0x80,0x3f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfc,0xff,0x03,0x00,0x00,0x00,0x00,0x00]).buffer;
function renderBitmap(x, y, bitmap, width, height, color, bgColor) {
var data = new Uint8Array(bitmap);
var byteWidth = Math.floor((width + 7) / 8);
for (var j = 0; j < height; j++) {
for (var i = 0; i < width; i++) {
var byteIndex = j * byteWidth + Math.floor(i / 8);
var bitIndex = i % 8;
var pixel = (data[byteIndex] >> bitIndex) & 1;
if (pixel) {
display.drawPixel(x + i, y + j, color);
} else if (bgColor !== undefined) {
display.drawPixel(x + i, y + j, bgColor);
}
}
}
}
display.fill(bgColor);
display.setTextColor(priColor);
display.setTextSize(1);
display.drawString("SasPes", 10, 10);
display.drawString(version, displayWidth - 10 - 6 * 5, 10);
display.setTextSize(3);
display.drawString("Key Decoding", (displayWidth - 18 * 12) / 2, displayHeight - 40);
// display.drawXBitmap() is not working in Bruce Release 1.14
// display.drawXBitmap((displayWidth - 100) / 2, 20, logo, 100, 100, priColor, bgColor);
renderBitmap((displayWidth - 100) / 2, 20, logo, 100, 100, priColor, bgColor);
delay(500);
/*
KeyExample: {
displayName: "Key Example", // display name shown in menu
isDiskDetainer: false, // whether the key is a disk detainer type (default false)
bladeHeight: 45, // blade height for disk detainer keys (default 45)
outlines: ["5 pins", "6 pins"], // number of pins
pinSpacing: 31, // distance between pins (default 31)
maxKeyCut: 9, // number of cuts (default 9)
flatSpotWidth: 5, // width of flat spot of the cut (default 5)
cutDepthOffset: 5, // depth offset of each cut (default 5)
zeroCutOffset: 0, // depth offset of zero cut (default 0)
edgeOffsetX: 0, // x offset of the bottom-right diagonal (default 0)
edgeOffsetY: 0, // y offset of the bottom line (default 0)
pinsStartAtZero: false, // whether pin numbers start at 0 or 1 (default false)
pinNumbersOffset: 0 // x offset for pin numbers with underline (default 0)
}
*/
var keys = {
Titan: {
outlines: ["5 pins"],
pinSpacing: 30,
maxKeyCut: 9,
cutDepthOffset: 4,
zeroCutOffset: 2,
edgeOffsetX: 5,
edgeOffsetY: 1
},
Kwikset: {
outlines: ["5 pins"],
pinSpacing: 30,
maxKeyCut: 7,
flatSpotWidth: 10,
edgeOffsetX: 15
},
Master: {
outlines: ["4 pins", "5 pins", "6 pins"],
pinSpacing: 24,
maxKeyCut: 8,
flatSpotWidth: 8,
cutDepthOffset: 3,
edgeOffsetX: -5,
edgeOffsetY: -10,
pinsStartAtZero: true,
pinNumbersOffset: -4
},
American: {
outlines: ["5 pins", "6 pins"],
pinSpacing: 24,
maxKeyCut: 8,
flatSpotWidth: 8,
cutDepthOffset: 3,
edgeOffsetX: -5,
edgeOffsetY: -10,
pinNumbersOffset: -4
},
Best: {
outlines: ["7 pins"],
pinSpacing: 29,
maxKeyCut: 10,
flatSpotWidth: 6,
cutDepthOffset: 3,
edgeOffsetX: -5,
pinsStartAtZero: true
},
ASSA: {
outlines: ["5 pins", "6 pins", "7 pins"],
pinSpacing: 30,
maxKeyCut: 9,
flatSpotWidth: 2,
cutDepthOffset: 4,
zeroCutOffset: 1,
edgeOffsetX: 7
},
Schlage: {
outlines: ["5 pins/SC1", "6 pins/SC4"],
pinSpacing: 30,
maxKeyCut: 10,
cutDepthOffset: 3,
pinsStartAtZero: true,
flatSpotWidth: 10,
edgeOffsetY: 1
},
Yale: {
outlines: ["5 pins"],
pinSpacing: 31,
maxKeyCut: 10,
cutDepthOffset: 3,
edgeOffsetX: 17,
edgeOffsetY: -5,
pinsStartAtZero: true,
},
YaleSmall: {
displayName: "Yale Small",
outlines: ["4 pins", "5 pins"],
pinSpacing: 24,
maxKeyCut: 6,
flatSpotWidth: 6,
cutDepth: 5,
edgeOffsetX: 6,
edgeOffsetY: -9,
pinNumbersOffset: -4,
pinsStartAtZero: true
},
AbloyClassic: {
displayName: "Abloy Classic",
isDiskDetainer: true,
outlines: ["7 disks", "9 disks", "11 disks"],
pinSpacing: 16,
maxKeyCut: 6
},
AbloyHighProfile: {
displayName: "Abloy High Profile",
isDiskDetainer: true,
outlines: ["7 disks", "9 disks", "11 disks"],
pinSpacing: 16,
maxKeyCut: 6,
bladeHeight: 57
},
CorbinRusswin:{
displayName: "Corbin Russwin",
outlines: ["5 pins/RU45", "6 pins/CX6A"],
pinSpacing: 30,
maxKeyCut: 6,
cutDepthOffset: 5,
pinsStartAtZero: false,
flatSpotWidth: 10,
edgeOffsetX: -3,
edgeOffsetY: 1
},
MedecoBiaxial: {
displayName: "Medeco Biaxial",
outlines: ["6 pins"],
pinSpacing: 33,
maxKeyCut: 6,
flatSpotWidth: 2,
cutDepthOffset: 6,
zeroCutOffset: 2,
edgeOffsetX: 9,
edgeOffsetY: -7
},
};
function Key(type, outline, show) {
this.type = type;
this.outline = outline;
this.show = show;
this.pins = [];
// Initialize pins
if (typeof outline === "string" && typeof show === "string") {
var pinCount = parseInt(outline.substring(0, 2), 10);
if (!isNaN(pinCount)) {
if (show === "decode") {
for (var i = 0; i < pinCount; i++) {
this.pins.push(0);
}
} else {
for (var i = 0; i < pinCount; i++) {
var maxKeyCut = (keys[this.type] && keys[this.type].maxKeyCut) || 9;
this.pins.push(Math.floor(Math.random() * maxKeyCut - 1) + 1);
}
// Ensure last disk shows as 1 for disk detainer (internal index 0)
if (keys[this.type] && keys[this.type].isDiskDetainer && pinCount > 0) {
this.pins[pinCount - 1] = 0;
}
}
}
}
this.updatePins = function () {
var pinCount = parseInt(outline.substring(0, 2), 10);
this.pins = [];
for (var i = 0; i < pinCount; i++) {
var maxKeyCut = (keys[this.type] && keys[this.type].maxKeyCut) || 9;
this.pins.push(Math.floor(Math.random() * maxKeyCut - 1) + 1);
}
// Ensure last disk shows as 1 for disk detainer (internal index 0)
if (keys[this.type] && keys[this.type].isDiskDetainer && pinCount > 0) {
this.pins[pinCount - 1] = 0;
}
};
this.draw = function () {
var numberOfActions = 2; // Save, Load
if (selectedPinIndex >= this.pins.length + numberOfActions) {
selectedPinIndex = 0;
}
display.fill(bgColor);
display.drawRoundRect(1, 1, displayWidth - 2, displayHeight - 2, 4, priColor);
display.setTextSize(2);
var displayName = keys[this.type] && keys[this.type].displayName || this.type;
if (displayName.length > 12) {
display.drawString(displayName, 10, 10);
display.drawString(this.outline, 10, 28);
} else {
display.drawString(displayName + " - " + this.outline, 10, 10);
}
if (this.show === "decode") {
display.drawRoundRect(displayWidth - 65, 3, 60, 8 + numberOfActions * 24, 4, priColor);
display.drawString("Save", displayWidth - 58, 12);
display.drawString("Load", displayWidth - 58, 36);
var selectedAction = selectedPinIndex - this.pins.length;
if (selectedPinIndex >= this.pins.length) {
display.drawRect(displayWidth - 60, 28 + selectedAction * 24, 50, 2, secColor);
display.setTextSize(1);
if (selectedAction === 0) {
display.drawString("Next: Save key", displayWidth - 90, displayHeight - 11);
} else if (selectedAction === 1) {
display.drawString("Next: Load key", displayWidth - 90, displayHeight - 11);
}
display.setTextSize(2);
}
}
var pinSpacing = keys[this.type] ? keys[this.type].pinSpacing : 31;
drawPinsWithUnderline(this.pins, selectedPinIndex, this.show, pinSpacing, this.type);
};
this.save = function () {
var data = {
type: this.type,
outline: this.outline,
pins: this.pins
};
var fileName = "/keys/key_" + this.type + "_" + this.pins.join('') + "_" + Date.now() + ".json";
const success = storage.write(fileName, JSON.stringify(data));
if (success) {
dialog.success(" Key saved successfully! " + fileName, true);
}
display.setTextColor(priColor);
selectedPinIndex = 0;
};
this.load = function (keyData) {
if (keyData) {
var data = JSON.parse(keyData);
this.type = data.type;
this.outline = data.outline;
this.show = "decode";
this.pins = data.pins;
}
display.fill(bgColor);
};
}
function generateDipShapes(pinSpacing, maxKeyCut, flatSpotWidth, cutDepthOffset, zeroCutOffset) {
var dipShapes = {};
for (var cut = 0; cut < maxKeyCut; cut++) {
var shape = [];
var centerIndex = Math.floor(pinSpacing / 2);
var cutDepth = cut * cutDepthOffset;
var flatHalfWidth = Math.floor(flatSpotWidth / 2);
for (var i = 0; i < pinSpacing; i++) {
var distanceFromCenter = Math.abs(i - centerIndex);
if (cut === 0) {
if (distanceFromCenter <= flatSpotWidth) {
shape[i] = zeroCutOffset;
} else {
shape[i] = 0;
}
} else {
if (distanceFromCenter <= flatHalfWidth) {
shape[i] = cutDepth;
} else {
var slopeDistance = distanceFromCenter - flatHalfWidth;
var remainingDistance = centerIndex - flatHalfWidth;
var depth = cutDepth - Math.floor(slopeDistance * (cutDepth - 1) / remainingDistance);
shape[i] = Math.max(1, depth);
}
}
}
dipShapes[cut] = shape;
}
return dipShapes;
}
function drawKeyShape(x, y, width, height, color, pinCount, pins, keyType) {
var keyConfig = keys[keyType] || {};
var pinSpacing = keyConfig.pinSpacing || 31;
var maxKeyCut = keyConfig.maxKeyCut || 9;
var flatSpotWidth = keyConfig.flatSpotWidth || 5;
var cutDepthOffset = keyConfig.cutDepthOffset || 5;
var zeroCutOffset = keyConfig.zeroCutOffset || 0;
var dipShapes = generateDipShapes(pinSpacing, maxKeyCut, flatSpotWidth, cutDepthOffset, zeroCutOffset);
var edgeOffsetX = keyConfig.edgeOffsetX || 0;
var edgeOffsetY = keyConfig.edgeOffsetY || 0;
for (var px = Math.round(x); px <= Math.round(x + width + pinSpacing / 2); px++) {
var py = y;
for (var i = 0; i < pinCount; i++) {
var pinValue = pins && pins[i];
var dipShape = dipShapes[pinValue];
if (dipShape) {
var dipWidth = dipShape.length;
var pinCenter = Math.round(x + (i + 1) * pinSpacing);
var dipStart = pinCenter - Math.floor(dipWidth / 2);
var dipEnd = pinCenter + Math.floor(dipWidth / 2);
if (px >= dipStart && px < dipEnd) {
var dipIdx = px - dipStart;
py = y + dipShape[dipIdx];
break;
}
}
}
display.drawPixel(px, py, color);
}
var edgeX = x + width + pinSpacing / 2 + edgeOffsetX;
var edgeY = y + height + edgeOffsetY;
var diagLength = 30;
var diagBottomX = edgeX + diagLength;
var diagBottomY = edgeY - diagLength;
// Draw diagonals
display.drawLine(edgeX, edgeY, diagBottomX, diagBottomY, color); // bottom-right diagonal
// Draw straight bottom edge
display.drawLine(x, edgeY, edgeX, edgeY, color);
}
function drawPinsWithUnderline(pins, selectedPinIndex, showMode, pinSpacing, keyType) {
if (keys[keyType] && keys[keyType].isDiskDetainer) {
drawDisksWithUnderline(pins, selectedPinIndex, showMode, pinSpacing, keyType);
return;
}
var startY = 55;
var underlineY = startY + 15;
var totalWidth = pinSpacing * pins.length;
var startX = (displayWidth - totalWidth) / 2;
var numberSize = 12;
var keyConfig = keys[keyType] || {};
var pinsStartCount = keyConfig.pinsStartAtZero === true;
var pinNumbersOffset = keyConfig.pinNumbersOffset || 0;
// Draw pins numbers
for (var i = 0; i < pins.length; i++) {
var pinNumberX = startX + numberSize + i * pinSpacing + pinNumbersOffset;
var displayNumber = pinsStartCount ? pins[i] : (pins[i] + 1);
display.drawString(displayNumber.toString(), pinNumberX, startY);
if (showMode !== "random" && typeof selectedPinIndex !== "undefined" && i === selectedPinIndex) {
display.drawRect(pinNumberX - 1, underlineY, 12, 2, secColor);
}
}
// Draw the key shape under the pins
var keyX = startX - pinSpacing / 2;
var keyY = startY + pinSpacing;
drawKeyShape(keyX, keyY, totalWidth, 66, priColor, pins.length, pins, keyType);
}
function drawDiskKeyShape(x, y, width, height, color, diskCount, disks, keyType) {
var keyConfig = keys[keyType] || {};
var pinSpacing = keyConfig.pinSpacing || 32;
var bladeHeight = keyConfig.bladeHeight || 45;
var bladeY = y + (height - bladeHeight) / 2;
var bladeBottom = bladeY + bladeHeight;
var diskCutDepths = [0, 2, 4, 8, 14, 21];
var entryX = x;
var entryY = bladeY;
var keyStartOffset = 20;
var keyStartX = entryX + keyStartOffset;
var currX = keyStartX;
var prevCutDepth = diskCutDepths[disks[0]] || 0;
// Connect left side: vertical from bottom to first cut
display.drawLine(currX, bladeBottom, currX, bladeBottom - prevCutDepth, color);
// Draw the first horizontal segment
display.drawLine(currX, bladeBottom - prevCutDepth, currX + pinSpacing, bladeBottom - prevCutDepth, color);
for (var i = 1; i < diskCount; i++) {
var cutIdx = disks[i] || 0;
var cutDepth = diskCutDepths[cutIdx] || 0;
var nextX = currX + pinSpacing;
// Draw vertical line connecting previous cut to current cut
display.drawLine(nextX, bladeBottom - prevCutDepth, nextX, bladeBottom - cutDepth, color);
// Draw horizontal line for current cut
display.drawLine(nextX, bladeBottom - cutDepth, nextX + pinSpacing, bladeBottom - cutDepth, color);
currX = nextX;
prevCutDepth = cutDepth;
}
// Draw end vertical (right side)
display.drawLine(currX + pinSpacing + 2, bladeBottom - prevCutDepth, currX + pinSpacing + 2, bladeY, color);
// Draw bottom edge (from entry to start of key)
display.drawLine(entryX, bladeBottom, entryX + keyStartOffset, bladeBottom, color);
// Draw top edge (from entry to start of key)
display.drawLine(entryX, entryY, currX + pinSpacing, bladeY, color);
// Diagonal from top-left to key start (top edge)
display.drawLine(30, bladeY - 10, entryX, bladeY, color);
// Diagonal from bottom-left to key start (bottom edge)
display.drawLine(30, bladeBottom + 10, entryX, bladeBottom, color);
}
function drawDisksWithUnderline(disks, selectedDiskIndex, showMode, pinSpacing, keyType) {
var startY = 55;
var underlineY = startY + 15;
var totalWidth = pinSpacing * disks.length;
var startX = (displayWidth - totalWidth) / 2;
var numberSize = 12;
for (var i = 0; i < disks.length; i++) {
var diskNumberX = startX + numberSize + i * pinSpacing;
display.drawString((disks[i] + 1).toString(), diskNumberX, startY);
if (showMode !== "random" && typeof selectedDiskIndex !== "undefined" && i === selectedDiskIndex) {
display.drawRect(diskNumberX - 1, underlineY, 12, 2, secColor);
}
}
// Draw the disk detainer key shape under the disks
var keyX = startX - pinSpacing / 2;
var keyY = startY + pinSpacing;
drawDiskKeyShape(keyX, keyY, totalWidth, 66, priColor, disks.length, disks, keyType);
}
var key = null;
var selectedPinIndex = 0;
function chooseAndCreateKey() {
selectedPinIndex = 0;
var keyTypeChoices = {};
var brandNames = Object.keys(keys).sort();
for (var i = 0; i < brandNames.length; i++) {
var brand = brandNames[i];
var displayName = keys[brand].displayName || brand;
keyTypeChoices[displayName] = brand;
}
keyTypeChoices.Load = "Load";
keyTypeChoices.Exit = "Exit";
var type = dialog.choice(keyTypeChoices);
if (!type) type = "Exit";
var outline, show;
if (type !== "Exit") {
if (type === "Load") {
key = new Key(type, "", "decode");
var filePath = dialog.pickFile("/keys", {withFileTypes: true});
if (!filePath) {
return chooseAndCreateKey();
}
var fileContent = storage.read(filePath);
if (!fileContent) {
return chooseAndCreateKey();
}
key.load(fileContent);
} else {
var outlines = keys[String(type)].outlines || [];
var outlineChoices = {};
for (var j = 0; j < outlines.length; j++) {
var o = outlines[j];
outlineChoices[o] = o;
}
outlineChoices.Cancel = "Cancel";
outline = dialog.choice(outlineChoices);
if (!outline || outline === "Cancel") {
return chooseAndCreateKey();
}
show = dialog.choice({
Decode: "decode",
Random: "random",
Cancel: "Cancel"
});
if (!show || show === "Cancel") {
return chooseAndCreateKey();
}
}
}
if (type !== "Load") {
key = new Key(type, outline, show);
}
if (type !== "Exit") {
key.draw();
}
}
if (!key) {
chooseAndCreateKey();
}
while (true) {
if (key.type === "Exit") {
break;
}
if (keyboard.getSelPress()) {
if (key.show === "random") {
chooseAndCreateKey();
} else {
selectedPinIndex++;
key.draw();
}
}
if (keyboard.getNextPress()) {
if (key.show === "random") {
key.updatePins();
} else if (key.show === "decode" && selectedPinIndex !== null && selectedPinIndex < key.pins.length) {
var maxKeyCut = (keys[key.type] && keys[key.type].maxKeyCut) || 9;
key.pins[selectedPinIndex] = Math.min(maxKeyCut - 1, key.pins[selectedPinIndex] + 1);
} else if (selectedPinIndex === key.pins.length) { // Save action
key.save();
} else if (selectedPinIndex === key.pins.length + 1) { // Load action
key.load(storage.read(dialog.pickFile("/keys", {withFileTypes: true})))
}
key.draw();
}
if (keyboard.getPrevPress()) {
if (key.show === "decode" && selectedPinIndex !== null && selectedPinIndex < key.pins.length) {
key.pins[selectedPinIndex] = Math.max(0, key.pins[selectedPinIndex] - 1);
key.draw();
}
}
if (keyboard.getEscPress()) {
chooseAndCreateKey();
}
delay(10);
}