-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
348 lines (348 loc) · 11.5 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
enum Animation {
Welcome,
DeviceName,
Lines,
FillHorizontal,
FillVertical,
BarHorizontal,
BarVertical
}
/**
* Some utilities
*/
//% weight=100 color=#ff9900 icon="\uf12e" block="Utilities"
//% advanced=true
namespace utilities {
export class rgbLed {
red: AnalogPin
green: AnalogPin
blue: AnalogPin
constructor(r: AnalogPin, g: AnalogPin, b: AnalogPin) {
this.red = r
this.green = g
this.blue = b
}
SetClolor(r: number, g: number, b: number) {
if (r >= 0 && r < 256) pins.analogWritePin(this.red, r)
if (r >= 0 && r < 256) pins.analogWritePin(this.green, g)
if (r >= 0 && r < 256) pins.analogWritePin(this.blue, b)
}
}
/**
* Draws a little boot animation
*/
//% block
export function bootAnimation(anim: Animation): void {
led.enable(true)
if (anim == Animation.Welcome) {
basic.showString("W")
basic.showString("e")
basic.showString("l")
basic.showString("c")
basic.showString("o")
basic.showString("m")
basic.showString("e")
basic.showString("!")
} else if (anim == Animation.DeviceName) {
let str = control.deviceName()
for (let i = 0; i < str.length; i++) {
basic.showString(str[i])
}
} else if (anim == Animation.Lines) {
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
led.plotBrightness(j, i, 128)
basic.pause(75)
led.plot(j, i)
basic.pause(50)
}
}
} else if (anim == Animation.FillHorizontal) {
for (let i = 0; i < 5; i++) {
for (let k = 1; k < 256; k++) {
for (let j = 0; j < 5; j++) {
led.plotBrightness(j, 4 - i, k)
}
basic.pause(10 - i * 2)
}
basic.pause(1 / Math.pow(2, i) * 250)
}
} else if (anim == Animation.FillVertical) {
for (let i = 0; i < 5; i++) {
for (let k = 1; k < 256; k++) {
for (let j = 0; j < 5; j++) {
led.plotBrightness(i, j, k)
}
basic.pause(10 - i * 2)
}
basic.pause(1 / Math.pow(2, i) * 250)
}
} else if (anim == Animation.BarHorizontal) {
for (let i = 0; i < 5; i++) {
for (let j = 1; j < 256; j++) {
led.plotBrightness(i, 3, j)
led.plotBrightness(i, 4, j)
basic.pause(10 - i * 2)
}
}
} else if (anim == Animation.BarVertical) {
for (let i = 0; i < 5; i++) {
for (let j = 1; j < 256; j++) {
led.plotBrightness(0, i, j)
led.plotBrightness(1, i, j)
basic.pause(10 - i * 2)
}
}
}
basic.pause(750)
basic.clearScreen()
}
/**
* Returns true if the parameter string only contains numbers
*/
//% block
export function isNumber(s: string): boolean {
const n = "0123456789"
for (let i = 0; i < s.length; i++) {
let b = false
for (let j = 0; j < n.length; j++) {
if (s[i] == n[j]) b = true
}
if (!b) return false
}
return true
}
/**
* Represents the temperature on a 0-14 scale
* (useful for smaller bars or indicators)
*/
//% block
export function temperatureScale() {
let temp = input.temperature() - 1
let num = 0
if (temp <= -1)
num = 0
else if (temp <= 5)
num = 1
else if (temp <= 9)
num = 2
else if (temp <= 12)
num = 3
else if (temp <= 15)
num = 4
else if (temp <= 18)
num = 5
else if (temp <= 20)
num = 6
else if (temp <= 22)
num = 7
else if (temp <= 24)
num = 8
else if (temp <= 27)
num = 9
else if (temp <= 30)
num = 10
else if (temp <= 35)
num = 11
else if (temp <= 40)
num = 12
else if (temp <= 45)
num = 13
else
num = 14
return num
}
/**
* Returns the direction that the Micro:bit's faced
* e.g. North, North-East, ...
*/
//% block
export function compassDirection(): string {
let heading = input.compassHeading()
let str = ""
if (heading >= 337.5 || heading < 22.5)
str = "North"
else if (heading >= 22.5 && heading < 67.5)
str = "North-East"
else if (heading >= 67.5 && heading < 112.5)
str = "East"
else if (heading >= 112.5 && heading < 157.5)
str = "South-East"
else if (heading >= 157.5 && heading < 202.5)
str = "South"
else if (heading >= 202.5 && heading < 247.5)
str = "South-West"
else if (heading >= 247.5 && heading < 292.5)
str = "West"
else if (heading >= 292.5 && heading < 337.5)
str = "North-West"
return str
}
/**
* Draws a compass on the micro:bit screen
* (has 12 animation stages)
*/
//% block
export function drawCompass(): void {
if (input.compassHeading() >= 352.5 || input.compassHeading() < 7.5) {
basic.clearScreen()
led.plot(2, 0)
led.plot(2, 1)
led.plot(2, 2)
} else if (input.compassHeading() >= 7.5 && input.compassHeading() < 22.5) {
basic.clearScreen()
led.plot(1, 0)
led.plot(2, 1)
led.plot(2, 2)
} else if (input.compassHeading() >= 22.5 && input.compassHeading() < 37.5) {
basic.clearScreen()
led.plot(1, 0)
led.plot(1, 1)
led.plot(2, 2)
} else if (input.compassHeading() >= 37.5 && input.compassHeading() < 52.5) {
basic.clearScreen()
led.plot(0, 0)
led.plot(1, 1)
led.plot(2, 2)
} else if (input.compassHeading() >= 52.5 && input.compassHeading() < 67.5) {
basic.clearScreen()
led.plot(0, 1)
led.plot(1, 1)
led.plot(2, 2)
} else if (input.compassHeading() >= 67.5 && input.compassHeading() < 82.5) {
basic.clearScreen()
led.plot(0, 1)
led.plot(1, 2)
led.plot(2, 2)
} else if (input.compassHeading() >= 82.5 && input.compassHeading() < 97.5) {
basic.clearScreen()
led.plot(0, 2)
led.plot(1, 2)
led.plot(2, 2)
} else if (input.compassHeading() >= 97.5 && input.compassHeading() < 112.5) {
basic.clearScreen()
led.plot(0, 3)
led.plot(1, 2)
led.plot(2, 2)
} else if (input.compassHeading() >= 112.5 && input.compassHeading() < 127.5) {
basic.clearScreen()
led.plot(0, 3)
led.plot(1, 3)
led.plot(2, 2)
} else if (input.compassHeading() >= 127.5 && input.compassHeading() < 142.5) {
basic.clearScreen()
led.plot(0, 4)
led.plot(1, 3)
led.plot(2, 2)
} else if (input.compassHeading() >= 142.5 && input.compassHeading() < 157.5) {
basic.clearScreen()
led.plot(1, 4)
led.plot(1, 3)
led.plot(2, 2)
} else if (input.compassHeading() >= 157.5 && input.compassHeading() < 172.5) {
basic.clearScreen()
led.plot(1, 4)
led.plot(2, 3)
led.plot(2, 2)
} else if (input.compassHeading() >= 172.5 && input.compassHeading() < 187.5) {
basic.clearScreen()
led.plot(2, 4)
led.plot(2, 3)
led.plot(2, 2)
} else if (input.compassHeading() >= 187.5 && input.compassHeading() < 202.5) {
basic.clearScreen()
led.plot(3, 4)
led.plot(2, 3)
led.plot(2, 2)
} else if (input.compassHeading() >= 202.5 && input.compassHeading() < 217.5) {
basic.clearScreen()
led.plot(3, 4)
led.plot(3, 3)
led.plot(2, 2)
} else if (input.compassHeading() >= 217.5 && input.compassHeading() < 232.5) {
basic.clearScreen()
led.plot(4, 4)
led.plot(3, 3)
led.plot(2, 2)
} else if (input.compassHeading() >= 232.5 && input.compassHeading() < 247.5) {
basic.clearScreen()
led.plot(4, 3)
led.plot(3, 3)
led.plot(2, 2)
} else if (input.compassHeading() >= 247.5 && input.compassHeading() < 262.5) {
basic.clearScreen()
led.plot(4, 3)
led.plot(3, 2)
led.plot(2, 2)
} else if (input.compassHeading() >= 262.5 && input.compassHeading() < 277.5) {
basic.clearScreen()
led.plot(4, 2)
led.plot(3, 2)
led.plot(2, 2)
} else if (input.compassHeading() >= 277.5 && input.compassHeading() < 292.5) {
basic.clearScreen()
led.plot(4, 1)
led.plot(3, 2)
led.plot(2, 2)
} else if (input.compassHeading() >= 292.5 && input.compassHeading() < 307.5) {
basic.clearScreen()
led.plot(4, 1)
led.plot(3, 1)
led.plot(2, 2)
} else if (input.compassHeading() >= 307.5 && input.compassHeading() < 322.5) {
basic.clearScreen()
led.plot(4, 0)
led.plot(3, 1)
led.plot(2, 2)
} else if (input.compassHeading() >= 322.5 && input.compassHeading() < 337.5) {
basic.clearScreen()
led.plot(3, 0)
led.plot(3, 1)
led.plot(2, 2)
} else if (input.compassHeading() >= 337.5 && input.compassHeading() < 352.5) {
basic.clearScreen()
led.plot(3, 0)
led.plot(2, 1)
led.plot(2, 2)
}
}
}
namespace Math {
/**
* Returns the minimum value of a number array
*/
//% block
export function arrayMin(l: number[]): number {
let m = l[0]
for (let i = 1; i < l.length; i++) {
if (l[i] < m) {
m = l[i]
}
}
return m
}
/**
* Returns the maximum value of a number array
*/
//% block
export function arrayMax(l: number[]): number {
let m = l[0]
for (let i = 1; i < l.length; i++) {
if (l[i] > m) {
m = l[i]
}
}
return m
}
/**
* Returns the average value of a number array
*/
//% block
export function arrayAvg(l: number[]): number {
let s = 0
for (let i = 0; i < l.length; i++) {
s += l[i]
}
return s / l.length
}
}