Repository navigation
Expand file tree
/
Copy pathapi.ts
More file actions
208 lines (195 loc) · 7.48 KB
/
Copy pathapi.ts
File metadata and controls
208 lines (195 loc) · 7.48 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
//%block="2of5 font"
//%icon="\uf02a"
//%color="#0749a6"
//%weight=5
namespace font2of5 {
const pin2of5: Buffer = hex`181412110C0A09060503`;
// [0b11000, 0b10100, 0b10010, 0b10001, 0b01100, 0b01010, 0b01001, 0b00110, 0b00101, 0b00011]
function find2of5number (t: number): number {
let l = 0, r = 9;
while (l <= r) {
const m = l + ((r - l) >>> 1);
const icmp = t - pin2of5[m];
if (icmp > 0) r = m - 1;
else if (icmp < 0) l = m + 1;
else return m;
}
return -1;
}
let nb10: Buffer = null;
function makeb10buf (n: number) {
if (!nb10) nb10 = pins.createBuffer(6);
for (let i = 0; i < 6; i++) {
if (!n) break;
nb10[i] = (n % 10);
n = Math.idiv(n, 10);
}
}
function pointhere(x: number, y: number, transpose: boolean) {
if (transpose) {
if (led.pointBrightness(y, x) > 0 || led.point(y, x)) return true;
return false;
}
if (led.pointBrightness(x, y) > 0 || led.point(x, y)) return true;
return false;
}
function drawhere(place: boolean, x: number, y: number, transpose: boolean, brigth: number) {
if (place) {
brigth = brigth & 0xff;
if (transpose) {
led.plotBrightness(y, x, brigth);
return;
}
led.plotBrightness(x, y, brigth);
return;
}
if (transpose) {
led.unplot(y, x);
return;
}
led.unplot(x, y);
}
let frtn: uint8 = 0;
function findRendered(i: number, transpose: boolean, inv: boolean) {
frtn = 0;
for (let j = 4; j >= 0; j--) {
frtn = frtn << 1;
if (
pointhere(i, j, transpose) ||
(inv && !pointhere(i, j, transpose))
) frtn = frtn | 1;
}
return find2of5number(frtn);
}
/**
* write to number in 2of5 code
* but two param are not existing by two value
* @param to write 1st 2of5 code
* @param to write 2nd 2of5 code
*/
//%blockid=font2of5_write2of5input
//%block="write 2of5 code ( $a and $b )"
//%a.min=0 a.max=4 a.defl=1
//%b.min=0 b.max=4 b.defl=3
//%group="write code"
//%weight=1
export function write(a: number, b: number) {
a = Math.clamp(0, 4, a);
b = Math.clamp(0, 4, b);
if (a === b) return -1;
let nb = (1 << a) + (1 << b);
return find2of5number(nb);
}
/**
* write number to show number in 2of5 code to current screen
* like 1d barcode
* @param number input
* @param make 2of5 guard with reference from 6th number digit
* @param render in transpose mode
* @param brightness render level
*/
//%blockid=font2of5_print2of5number
//%block="show 2of5 number $n with guard? $guard|| transpose $transpose and brigthness $brigth"
//%n.defl=84210
//%brigth.min=0 brigth.max=255 brigth.defl=255
//%group="show screen"
//%weight=8
export function showNumber(n: number, guard: boolean, transpose?: boolean, brigth?: number) {
n = n | 0;
const neg = (n < 0);
if (neg) n = -n;
if (guard) n = n % 1000000;
else n = n % 100000;
makeb10buf(n); let gn = 0xA;
if (guard) gn = nb10[5];
for (let x = 0, gnt = pin2of5[gn], nt = 0; x < 5; x++, gnt = gnt >>> 1) {
nt = nb10[4 - x];
if (gn < 0xA) {
showSingleNumber(nt, x, !!(gnt & 1) !== neg, transpose, brigth);
continue;
}
showSingleNumber(nt, x, neg, transpose, brigth);
}
}
/**
* show number in one digit to render to current screen
* @param single digit number
* @param col positon index
* @param 2of5 invert flag
* @param render in transpose mode
* @param brightness render level
*/
//%blockid=font2of5_showin1digit
//%block="show 2of5 in 1digit $n at $col inverse $inv|| transpose $transpose brigthness $brigth"
//%n.min=0 n.max=9 n.defl=8
//%brigth.min=0 brigth.max=255 brigth.defl=255
//%col.min=0 col.max=4 col.defl=2
//%group="show screen"
//%weight=4
export function showSingleNumber(n: number, col: number, inv: boolean, transpose?: boolean, brigth?: number) {
n = n | 0;
n = n % 10;
col = Math.clamp(0, 4, col);
if (n === findRendered(col, transpose, inv)) return;
for (let row = 0, nt = pin2of5[n], ntb = 0; row < 5; row++, nt = nt >>> 1) {
ntb = nt & 1;
if (inv) {
drawhere(!!!ntb, col, row, transpose, brigth);
continue;
}
drawhere(!!ntb, col, row, transpose, brigth);
}
}
export const enum alignment {
//%block="Left"
left = -1,
//%block="Center"
center = 0,
//%block="Right"
right = 1,
}
let numbufinfo: Buffer = null;
let numl1: Buffer = null;
let numl2: Buffer = null;
/**
* render two number values in 2of5 code to current screen
* @param 1st number value
* @param 2nd number value
* @param render alignment as offset
* @param render in transpose mode
* @param brightness render level
*/
//%blockId=font2of5_showdualnumber
//%block="show ( $a and $b ) at $align|| transpose $transpose brigthness $brigth"
//%a.min=0 a.max=99 a.defl=26
//%b.min=0 b.max=99 b.defl=48
//%brigth.min=0 brigth.max=255 brigth.defl=255
// %align.min=-1 align.max=1 align.defl=0
//%group="show screen"
//%weight=2
export function showDualNumber(a: number, b: number, align: alignment, transpose?: boolean, brigth?: number) {
align = Math.clamp(-1, 1, align);
a = a | 0; b = b | 0;
const ang = (a < 0); if (ang) a = -a;
const bng = (b < 0); if (bng) b = -b;
if (!numl1) numl1 = pins.createBuffer(3);
numl1[1] = Math.idiv(a, 10) % 10, numl1[2] = a % 10;
if (!numl2) numl2 = pins.createBuffer(3);
numl2[1] = Math.idiv(b, 10) % 10, numl2[2] = b % 10;
if (!numbufinfo) numbufinfo = pins.createBuffer(12);
switch (align) {
case -1:
numl1[0] = (Math.idiv(a, 100) % 10);
numbufinfo[0x0] = numl1[0], numbufinfo[0x4] = +(ang), numbufinfo[0x8] = 0; numbufinfo[0x1] = numl1[1], numbufinfo[0x5] = +(ang), numbufinfo[0x9] = 1; numbufinfo[0x2] = numl1[2], numbufinfo[0x6] = +(ang), numbufinfo[0xa] = 2; numbufinfo[0x3] = numl2[2], numbufinfo[0x7] = +(bng), numbufinfo[0xb] = 4;
break;
case 0: default:
numbufinfo[0x0] = numl1[1], numbufinfo[0x4] = +(ang), numbufinfo[0x8] = 0; numbufinfo[0x1] = numl1[2], numbufinfo[0x5] = +(ang), numbufinfo[0x9] = 1; numbufinfo[0x2] = numl2[1], numbufinfo[0x6] = +(bng), numbufinfo[0xa] = 3; numbufinfo[0x3] = numl2[2], numbufinfo[0x7] = +(bng), numbufinfo[0xb] = 4;
break;
case 1:
numl2[0] = (Math.idiv(b, 100) % 10);
numbufinfo[0x0] = numl1[2], numbufinfo[0x4] = +(ang), numbufinfo[0x8] = 0; numbufinfo[0x1] = numl2[0], numbufinfo[0x5] = +(bng), numbufinfo[0x9] = 2; numbufinfo[0x2] = numl2[1], numbufinfo[0x6] = +(bng), numbufinfo[0xa] = 3; numbufinfo[0x3] = numl2[2], numbufinfo[0x7] = +(bng), numbufinfo[0xb] = 4;
break;
}
for (let i = 0; i < 4; i++) showSingleNumber(numbufinfo[i], numbufinfo[i + 8], !!numbufinfo[i + 4], transpose, brigth);
}
}