-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
89 lines (60 loc) · 1.69 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
// ############################################
const startY = 0
const numLeds = 8
const ledOverflow = true
const flashColor = NeoPixelColors.Violet
const stripColor = NeoPixelColors.Yellow
const flashDelay = 190 // delay in ms
const buttonDelay = 250 // delay in ms
const buttonsSwapped = false
const controlsSwapped = false
// ############################################
let reversed = false
const updateScreen = () => {
basic.showString(reversed ? "O" : ".")
}
updateScreen()
input.onButtonPressed(Button.AB, () => {
reversed = !reversed
updateScreen()
})
let y = startY
let button1 = Button.A
let button2 = Button.B
if (buttonsSwapped) {
button1 = Button.B
button2 = Button.A
}
let increment = 1
if (controlsSwapped) {
increment = -1
}
const mod = (a: number, b: number) => ledOverflow ? ((a % b) + b) % b : Math.max(Math.min(a, numLeds - 1), 0)
const strip = neopixel.create(DigitalPin.P0, numLeds, NeoPixelMode.RGB)
for (let i = 0; i < 3; i++) {
strip.showColor(neopixel.colors(flashColor))
basic.pause(flashDelay)
strip.clear()
strip.show()
basic.pause(flashDelay)
}
loops.everyInterval(buttonDelay, () => {
if (input.buttonIsPressed(button1)) {
y = mod(y + increment, numLeds)
}
if (input.buttonIsPressed(button2)) {
y = mod(y - increment, numLeds)
}
strip.clear()
if (reversed) {
for (let i = 0; i < numLeds; i++) {
if (!(i === y)) {
strip.setPixelColor(i, stripColor)
}
}
} else {
strip.setPixelColor(y, stripColor)
}
strip.show()
})
if (input.buttonIsPressed(Button.AB)) { } // for initializing A + B button in makecode Simulator