-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
183 lines (177 loc) · 9.75 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
/**
* Micro:Bit makeCode extension for neopixel.was2812b matrices
*
*
*/
//**********************************************//
// library for NeoPixel Displays/Matrices //
// //
//Written by Sjors Smit //
//June 2020 //
// //
//**********************************************//
//% weight=6 color=#00CC60 icon="\uf110"
//% groups=["Setup", "Tools", "PixelControl"]
namespace SmartMatrix {
/**
* A Matrix made of ws2812b LEDs
*/
export class Matrix {
strip: neopixel.Strip
Width: number
Height: number
//%blockId="Matrix_show" block="%matrix| show"
//%weight=90 group="Tools"
show(): void {
this.strip.show();
}
//%blockId="Matrix_Brighness" block="%matrix set brightness to %setpoint"
//%weight=80 group="Setup"
//%setpoint.defl=32
Brightness(setpoint: number): void {
this.strip.setBrightness(setpoint);
}
//%blockId="Matrix_clear" block="clear %matrix"
//%weight=80 group="Tools"
clear(): void {
this.strip.clear();
}
//%blockId="Matrix_setPixel" block="%matrix| set pixel at x %x| y %y| to colour %colour"
//%weight=80 group="PixelControl"
//%colour.shadow=neopixel_colors
setPixel(x: number, y: number, colour: number): void {
if (x < 0 || x > this.Width || y < 0 || y > this.Height) { return } //If the pixel does not fit on screen, do not draw it (to avoid aliasing)
if (!(x % 2)) { this.strip.setPixelColor(y + x * this.Height, colour); } //Because of the zig-zag formation of the panel all even rows (including 0) are drawn top to bottom
else { this.strip.setPixelColor(this.Height - y + x * this.Height, colour); } //While all odd rows are drawn bottom to top
}
/**
* scroll text on the matrix
*/
//%blockId="Matrix_scrollText" block="%matrix scroll text %text| with speed %speed| and colour %colour"
//%weight=75 group="PixelControl"
//%colour.shadow=neopixel_colors
//%speed.min=1 speed.max=1024 speed.defl=512
scrollText(text: string, speed: number, colour: number): void {
this.strip.clear();
for (let Xpos = this.Width; Xpos > -6 * text.length; Xpos--) {//for loop to scroll across the entire matrix
for (let letter = 0; letter < text.length; letter++) {//for loop to retrieve all the letters from te text
let bitmap = getLettermap(text.charAt(letter))
this.drawBitmap(bitmap, Xpos + (6 * letter), 0, 6, 8, colour)
}
this.strip.show();
basic.pause(2000 / speed);
this.strip.clear();
}
}
//%blockId="Matrix_drawBitmap" block="%matrix draw bitmap %bitmap at x %x y %y| with width %width height %height in colour %colour"
//%weight=70 group="PixelControl"
//% colour.shadow=neopixel.colors
drawBitmap(bitmap: number[], x: number, y: number, width: number, height: number, colour: number): void {
for (let bitmask = 0; bitmask < width; bitmask++) {
if (!((x + bitmask) % 2)) {//Zigzag pixel string: if the row that's being drawn to (Xpos+bitmask) is odd, then draw from bottom to top
for (let Ypos = height; Ypos >= 0; Ypos--) {
if (bitmap[Ypos] & (0x80 >> bitmask)) { //draw the pixel when there is a "1" in the bitmap
this.strip.setPixelColor(((x + bitmask) * this.Height) + Ypos + ((this.Height - 8) / 2), colour)
}
}
}
else {//else draw from top to bottom
for (let Ypos = 0; Ypos < this.Height; Ypos++) {
if (bitmap[7 - Ypos] & (0x80 >> bitmask)) {
this.strip.setPixelColor(((x + bitmask) * this.Height + Ypos + ((this.Height - 8) / 2)), colour)
}
}
}
}
}
//%blockId="Matrix_drawBitmap2" block="%matrix draw bitmap %bitmap at x %xoffset y %yoffset| with width %width height %height in colour %colour"
//%weight=70 group="PixelControl"
//% colour.shadow=neopixel.colors
drawBitmap2(bitmap: number[],xoffset: number, yoffset: number, width: number, height: number, colour: number, doMirror: boolean = false): void {
let mirrored = 0
if (doMirror) {
mirrored = 1
}
if (width % 2) { //To properly enable mirror, width has to be even, so if uneven width gets added to by 1
width++
}
//Setting end value of k to equal the width of the image to shift the bitmask to the correct position. for drawing the x-axis
for (let k = 0; k < width; k++) {
//Due to the zig-zag pattern of the matrix every odd value on the matrix has to be drawn from bottom to top, and the others top to bottom.
if (!((xoffset + k + mirrored) % 2)) {
//Value of j to select the values in the array to draw on the y-axis
for (let j = 0; j < height; j++) {
//only draw a pixel when there is a '1' in the bitmap, without drawing a "black" pixel when there is a '0', allowing layering of bitmaps.
if ((bitmap[j] & (0b1 << (width - k - 1))) && ((j + yoffset) < this.Height) && ((yoffset + j) >= 0)) {
//Draw the actual pixel at the position determined by the k, j , xoffset and yoffset values.
this.strip.setPixelColor((((((mirrored - 1) * (-k) + xoffset) + ((width - k - 1) * mirrored)) * this.Height) + j + yoffset), colour)
}
}
}
//Drawing the odd lines top to bottom.
else {
for (let j = 0; j < height; j++) {
if ((bitmap[j] & (0b1 << (width - k - 1))) && ((yoffset + j) < this.Height) && ((yoffset + j) >= 0)) {
this.strip.setPixelColor((((((mirrored - 1) * (-k) + xoffset) + ((width - k - 1) * mirrored)) * this.Height) + (this.Height - j - yoffset - 1)), colour)
}
}
}
}
}
}
/**
* Create a new matrix object
* @param pin the pin to which the matrix is connected
* @param matrixWidth the amount of leds horizontally
* @param matrixheight the amount of leds vertically
*/
//%blockId="Matrix_Create" block="Matrix at pin %pin|with a width of %matrixWidth|height of %matrixheight| and with %mode pixeltype"
//%weight=100 blockGap=8 group="Setup"
//%parts="SmartMatrix"
//%matrixWidth.defl=32 matrixheight.defl=8
//%blockSetVariable=matrix
export function create(pin: DigitalPin, matrixWidth: number, matrixHeight: number, mode: NeoPixelMode): Matrix {
let matrix = new Matrix;
matrix.strip = neopixel.create(pin, matrixHeight * matrixWidth, mode);
matrix.Width = matrixWidth;
matrix.Height = matrixHeight;
return matrix;
}
//Take in a string-character and return a bitmap to draw on the display
export function getLettermap(char: string): number[] {
let letterMap: number[] = [0, 0, 0, 0, 0, 0, 0, 0]
let offset = ((char.charCodeAt(0)) - 32); //Convert the ASCII-Character to it's code to generate the offset in the font-array
if (offset >= 0) {
for (let i = 0; i < 8; i++) {
//Every character has 8 arguments in the array, so multiply the offset by 8, and then take ne next 8 arguments as the value for the correct bitmap.
letterMap[i] = font8x3.getNumber(NumberFormat.UInt8BE, ((offset * 8) + i))
}
}
return letterMap;
}
}
const font8x3 = hex`
0000000000000000 1038381010001000 6C6C480000000000 00287C28287C2800
2038403008701000 64640810204C4C00 2050502054483400 3030200000000000
1020202020201000 2010101010102000 0028387C38280000 0010107C10100000
0000000000303020 0000007C00000000 0000000000303000 0004081020400000
38444C5464443800 1030101010103800 3844041820407C00 3844043804443800
081828487C080800 7C40407804443800 1820407844443800 7C04081020202000
3844443844443800 3844443C04083000 0000303000303000 0000303000303020
0810204020100800 00007C00007C0000 2010080408102000 3844041810001000
38445C545C403800 384444447C444400 7844447844447800 3844404040443800
7844444444447800 7C40407840407C00 7C40407840404000 3844405C44443C00
4444447C44444400 3810101010103800 0404040444443800 4448506050484400
4040404040407C00 446C544444444400 4464544C44444400 3844444444443800
7844447840404000 3844444454483400 7844447848444400 3844403804443800
7C10101010101000 4444444444443800 4444444444281000 4444545454542800
4444281028444400 4444442810101000 7808102040407800 3820202020203800
0040201008040000 3808080808083800 1028440000000000 00000000000000FC
3030100000000000 000038043C443C00 4040784444447800 0000384440443800
04043C4444443C00 0000384478403800 1820207820202000 00003C44443C0438
4040704848484800 1000101010101800 0800180808084830 4040485060504800
1010101010101800 0000685454444400 0000704848484800 0000384444443800
0000784444447840 00003C4444443C04 0000582420207000 0000384038043800
0020782020281000 0000484848582800 0000444444281000 00004444547C2800
0000484830484800 0000484848381060 0000780830407800 1820206020201800
1010100010101000 3008080C08083000 2850000000000000`;