-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
585 lines (571 loc) · 14.3 KB
/
main.ts
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
//% color=#27b0ba icon="\uf26c"
namespace ssd1306 {
let font: Buffer;
const SSD1306_SETCONTRAST = 0x81
const SSD1306_SETCOLUMNADRESS = 0x21
const SSD1306_SETPAGEADRESS = 0x22
const SSD1306_DISPLAYALLON_RESUME = 0xA4
const SSD1306_DISPLAYALLON = 0xA5
const SSD1306_NORMALDISPLAY = 0xA6
const SSD1306_INVERTDISPLAY = 0xA7
const SSD1306_DISPLAYOFF = 0xAE
const SSD1306_DISPLAYON = 0xAF
const SSD1306_SETDISPLAYOFFSET = 0xD3
const SSD1306_SETCOMPINS = 0xDA
const SSD1306_SETVCOMDETECT = 0xDB
const SSD1306_SETDISPLAYCLOCKDIV = 0xD5
const SSD1306_SETPRECHARGE = 0xD9
const SSD1306_SETMULTIPLEX = 0xA8
const SSD1306_SETLOWCOLUMN = 0x00
const SSD1306_SETHIGHCOLUMN = 0x10
const SSD1306_SETSTARTLINE = 0x40
const SSD1306_MEMORYMODE = 0x20
const SSD1306_COMSCANINC = 0xC0
const SSD1306_COMSCANDEC = 0xC8
const SSD1306_SEGREMAP = 0xA0
const SSD1306_CHARGEPUMP = 0x8D
const xOffset = 0
const yOffset = 0
let chipAddress: number = 0x3D;
let charX = 0
let charY = 0
let displayWidth = 128
let displayHeight = 64 / 8
let screenSize = 0
let loadStarted: boolean;
let loadPercent: number;
function command(cmd: number, first: number = null, second: number = null) {
let buf;
if(second != null)
{
buf = pins.createBuffer(6)
buf[2] = 0x80;
buf[3] = first;
buf[4] = 0x80;
buf[5] = second;
}
else if(first != null)
{
buf = pins.createBuffer(4)
buf[2] = 0x80;
buf[3] = first;
}
else
{
buf = pins.createBuffer(2)
}
buf[0] = 0x80
buf[1] = cmd
pins.i2cWriteBuffer(chipAddress, buf, false)
}
//% block="clear OLED display"
//% weight=3
export function clear() {
loadStarted = false
loadPercent = 0
command(SSD1306_SETCOLUMNADRESS,0x00,displayWidth -1)
command(SSD1306_SETPAGEADRESS,0x00,displayHeight -1)
let data = pins.createBuffer(screenSize+1);
data[0] = 0x40; // Data Mode
for (let i = 1; i < screenSize+1; i++) {
data[i] = 0x00
}
pins.i2cWriteBuffer(chipAddress, data, false)
charX = xOffset;
charY = yOffset;
}
function drawLoadingFrame() {
command(SSD1306_SETCOLUMNADRESS,0x00,displayWidth -1)
command(SSD1306_SETPAGEADRESS,0x00,displayHeight -1)
let col = 0
let page = 0
let data = pins.createBuffer(17);
data[0] = 0x40; // Data Mode
let i = 1
for (let page = 0; page < displayHeight; page++) {
for (let col = 0; col < displayWidth; col++) {
if (page === 3 && col > 12 && col < displayWidth - 12) {
data[i] = 0x60
} else if (page === 5 && col > 12 && col < displayWidth - 12) {
data[i] = 0x06
} else if (page === 4 && (col === 12 || col === 13 || col === displayWidth - 12 || col === displayWidth - 13)) {
data[i] = 0xFF
} else {
data[i] = 0x00
}
if (i === 16) {
pins.i2cWriteBuffer(chipAddress, data, false)
i = 1
} else {
i++
}
}
}
charX = 30
charY = 2
writeString("Loading:")
}
function drawLoadingBar(percent: number) {
charX = 78
charY = 2
let num = Math.floor(percent)
writeNum(num)
writeString("%")
let width = displayWidth - 14 - 13
let lastStart = width * (loadPercent / displayWidth)
command(SSD1306_SETCOLUMNADRESS,14+lastStart,displayWidth -13)
command(SSD1306_SETPAGEADRESS,4,5)
let data = pins.createBuffer(2);
data[0] = 0x40; // Data Mode
data[1] = 0x7E
for (let i = lastStart; i < width * (Math.floor(percent) / 100); i++) {
pins.i2cWriteBuffer(chipAddress, data, false)
}
loadPercent = num
}
//% block="draw loading bar at $percent percent"
//% percent.min=0 percent.max=100
//% weight=2
export function drawLoading(percent: number) {
if (loadStarted) {
drawLoadingBar(percent)
} else {
drawLoadingFrame()
drawLoadingBar(percent)
loadStarted = true
}
}
//% block="show (without newline) string $str"
//% weight=6
export function writeString(str: string) {
for (let i = 0; i < str.length(); i++) {
if (charX > displayWidth - 6) {
newLine()
}
drawChar(charX, charY, str.charAt(i))
charX += 6
}
}
//% block="show (without newline) number $n"
//% weight=5
export function writeNum(n: number) {
let numString = n.toString()
writeString(numString)
}
//% block="show string $str"
//% weight=8
export function writeStringNewLine(str: string) {
writeString(str)
newLine()
}
//% block="show number $n"
//% weight=7
export function writeNumNewLine(n: number) {
writeNum(n)
newLine()
}
//% block="insert newline"
//% weight=4
export function newLine() {
charY++
charX = xOffset
}
function drawChar(x: number, y: number, c: string) {
command(SSD1306_SETCOLUMNADRESS,x,x+5)
command(SSD1306_SETPAGEADRESS,y,y+1)
let line = pins.createBuffer(2)
line[0] = 0x40
for (let i = 0; i < 6; i++) {
if (i === 5) {
line[1] = 0x00
} else {
let charIndex = c.charCodeAt(0)
let charNumber = font.getNumber(NumberFormat.UInt8BE, 5 * charIndex + i)
line[1] = charNumber
}
pins.i2cWriteBuffer(chipAddress, line, false)
}
}
function drawShape(pixels: Array<Array<number>>) {
let x1 = displayWidth
let y1 = displayHeight * 8
let x2 = 0
let y2 = 0
for (let i = 0; i < pixels.length; i++) {
if (pixels[i][0] < x1) {
x1 = pixels[i][0]
}
if (pixels[i][0] > x2) {
x2 = pixels[i][0]
}
if (pixels[i][1] < y1) {
y1 = pixels[i][1]
}
if (pixels[i][1] > y2) {
y2 = pixels[i][1]
}
}
let page1 = Math.floor(y1 / 8)
let page2 = Math.floor(y2 / 8)
let line = pins.createBuffer(2)
line[0] = 0x40
for (let x = x1; x <= x2; x++) {
for (let page = page1; page <= page2; page++) {
line[1] = 0x00
for (let i = 0; i < pixels.length; i++) {
if (pixels[i][0] === x) {
if (Math.floor(pixels[i][1] / 8) === page) {
line[1] |= Math.pow(2, (pixels[i][1] % 8))
}
}
}
if (line[1] !== 0x00) {
command(SSD1306_SETCOLUMNADRESS,x,x+1)
command(SSD1306_SETPAGEADRESS,page,page+1)
//line[1] |= pins.i2cReadBuffer(chipAdress, 2)[1]
pins.i2cWriteBuffer(chipAddress, line, false)
}
}
}
}
//% block="draw line from:|x: $x0 y: $y0 to| x: $x1 y: $y1"
//% x0.defl=0
//% y0.defl=0
//% x1.defl=20
//% y1.defl=20
//% weight=1
export function drawLine(x0: number, y0: number, x1: number, y1: number) {
let pixels: Array<Array<number>> = []
let kx: number, ky: number, c: number, i: number, xx: number, yy: number, dx: number, dy: number;
let targetX = x1
let targetY = y1
x1 -= x0; kx = 0; if (x1 > 0) kx = +1; if (x1 < 0) { kx = -1; x1 = -x1; } x1++;
y1 -= y0; ky = 0; if (y1 > 0) ky = +1; if (y1 < 0) { ky = -1; y1 = -y1; } y1++;
if (x1 >= y1) {
c = x1
for (i = 0; i < x1; i++ , x0 += kx) {
pixels.push([x0, y0])
c -= y1; if (c <= 0) { if (i != x1 - 1) pixels.push([x0 + kx, y0]); c += x1; y0 += ky; if (i != x1 - 1) pixels.push([x0, y0]); }
if (pixels.length > 20) {
drawShape(pixels)
pixels = []
drawLine(x0, y0, targetX, targetY)
return
}
}
} else {
c = y1
for (i = 0; i < y1; i++ , y0 += ky) {
pixels.push([x0, y0])
c -= x1; if (c <= 0) { if (i != y1 - 1) pixels.push([x0, y0 + ky]); c += y1; x0 += kx; if (i != y1 - 1) pixels.push([x0, y0]); }
if (pixels.length > 20) {
drawShape(pixels)
pixels = []
drawLine(x0, y0, targetX, targetY)
return
}
}
}
drawShape(pixels)
}
//% block="draw rectangle from:|x: $x0 y: $y0 to| x: $x1 y: $y1"
//% x0.defl=0
//% y0.defl=0
//% x1.defl=20
//% y1.defl=20
//% weight=0
export function drawRectangle(x0: number, y0: number, x1: number, y1: number) {
drawLine(x0, y0, x1, y0)
drawLine(x0, y1, x1, y1)
drawLine(x0, y0, x0, y1)
drawLine(x1, y0, x1, y1)
}
//% block="initialize OLED at i2caddress $chipAddr with width $width height $height and reset $reset"
//% width.defl=128
//% height.defl=64
//% weight=9
export function init(width: number, height: number, reset: DigitalPin = null, chipAddr: number = 0x3D) {
chipAddress = chipAddr
if(reset != null)
{
pins.digitalWritePin(reset, 1);
control.waitMicros(1000);
pins.digitalWritePin(reset, 0);
control.waitMicros(10000);
pins.digitalWritePin(reset, 1);
}
command(SSD1306_DISPLAYOFF);
command(SSD1306_CHARGEPUMP,0x14);
command(SSD1306_MEMORYMODE,0x00);
command(SSD1306_COMSCANDEC);
command(SSD1306_SEGREMAP|0x01);
command(SSD1306_SETPRECHARGE,0xF1);
command(SSD1306_SETVCOMDETECT,0x30);
command(SSD1306_DISPLAYALLON_RESUME);
command(SSD1306_DISPLAYON);
displayWidth = width
displayHeight = height / 8
screenSize = displayWidth * displayHeight
charX = xOffset
charY = yOffset
font = hex`
0000000000
3E5B4F5B3E
3E6B4F6B3E
1C3E7C3E1C
183C7E3C18
1C577D571C
1C5E7F5E1C
00183C1800
FFE7C3E7FF
0018241800
FFE7DBE7FF
30483A060E
2629792926
407F050507
407F05253F
5A3CE73C5A
7F3E1C1C08
081C1C3E7F
14227F2214
5F5F005F5F
06097F017F
006689956A
6060606060
94A2FFA294
08047E0408
10207E2010
08082A1C08
081C2A0808
1E10101010
0C1E0C1E0C
30383E3830
060E3E0E06
0000000000
00005F0000
0007000700
147F147F14
242A7F2A12
2313086462
3649562050
0008070300
001C224100
0041221C00
2A1C7F1C2A
08083E0808
0080703000
0808080808
0000606000
2010080402
3E5149453E
00427F4000
7249494946
2141494D33
1814127F10
2745454539
3C4A494931
4121110907
3649494936
464949291E
0000140000
0040340000
0008142241
1414141414
0041221408
0201590906
3E415D594E
7C1211127C
7F49494936
3E41414122
7F4141413E
7F49494941
7F09090901
3E41415173
7F0808087F
00417F4100
2040413F01
7F08142241
7F40404040
7F021C027F
7F0408107F
3E4141413E
7F09090906
3E4151215E
7F09192946
2649494932
03017F0103
3F4040403F
1F2040201F
3F4038403F
6314081463
0304780403
6159494D43
007F414141
0204081020
004141417F
0402010204
4040404040
0003070800
2054547840
7F28444438
3844444428
384444287F
3854545418
00087E0902
18A4A49C78
7F08040478
00447D4000
2040403D00
7F10284400
00417F4000
7C04780478
7C08040478
3844444438
FC18242418
18242418FC
7C08040408
4854545424
04043F4424
3C4040207C
1C2040201C
3C4030403C
4428102844
4C9090907C
4464544C44
0008364100
0000770000
0041360800
0201020402
3C2623263C
1EA1A16112
3A4040207A
3854545559
2155557941
2154547841
2155547840
2054557940
0C1E527212
3955555559
3954545459
3955545458
0000457C41
0002457D42
0001457C40
F0292429F0
F0282528F0
7C54554500
2054547C54
7C0A097F49
3249494932
3248484832
324A484830
3A4141217A
3A42402078
009DA0A07D
3944444439
3D4040403D
3C24FF2424
487E494366
2B2FFC2F2B
FF0929F620
C0887E0903
2054547941
0000447D41
3048484A32
384040227A
007A0A0A72
7D0D19317D
2629292F28
2629292926
30484D4020
3808080808
0808080838
2F10C8ACBA
2F102834FA
00007B0000
08142A1422
22142A1408
AA005500AA
AA55AA55AA
000000FF00
101010FF00
141414FF00
1010FF00FF
1010F010F0
141414FC00
1414F700FF
0000FF00FF
1414F404FC
141417101F
10101F101F
1414141F00
101010F000
0000001F10
1010101F10
101010F010
000000FF10
1010101010
101010FF10
000000FF14
0000FF00FF
00001F1017
0000FC04F4
1414171017
1414F404F4
0000FF00F7
1414141414
1414F700F7
1414141714
10101F101F
141414F414
1010F010F0
00001F101F
0000001F14
000000FC14
0000F010F0
1010FF10FF
141414FF14
1010101F00
000000F010
FFFFFFFFFF
F0F0F0F0F0
FFFFFF0000
000000FFFF
0F0F0F0F0F
3844443844
7C2A2A3E14
7E02020606
027E027E02
6355494163
3844443C04
407E201E20
06027E0202
99A5E7A599
1C2A492A1C
4C7201724C
304A4D4D30
3048784830
BC625A463D
3E49494900
7E0101017E
2A2A2A2A2A
44445F4444
40514A4440
40444A5140
0000FF0103
E080FF0000
08086B6B08
3612362436
060F090F06
0000181800
0000101000
3040FF0101
001F01011E
00191D1712
003C3C3C3C
0000000000`
loadStarted = false
loadPercent = 0
clear()
}
}