-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
237 lines (237 loc) · 7.08 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
radio.onReceivedNumber(function (receivedNumber) {
if (testNumber == 2) {
if (receivedNumber != 0) {
justSwitched = 0
basic.showIcon(IconNames.Yes)
}
}
})
function CompassTest () {
if (!(calibrated)) {
basic.showString("Calibrate?", 60)
while (!(calibrated)) {
if (input.buttonIsPressed(Button.A)) {
input.calibrateCompass()
calibrated = 1
} else if (input.buttonIsPressed(Button.B)) {
calibrated = 1
}
}
}
degrees = input.compassHeading()
// Compass routine inspired from here:
// https://makecode.microbit.org/projects/compass#:~:text=%7B-,Step%207,-%7D
if (degrees < 45) {
basic.showString("N")
} else if (degrees < 135) {
basic.showString("E")
} else if (degrees < 225) {
basic.showString("S")
} else if (degrees < 315) {
basic.showString("W")
} else if (degrees <= 360) {
basic.showString("N")
}
}
input.onButtonPressed(Button.A, function () {
basic.pause(PAUSE_TIME)
if (input.buttonIsPressed(Button.B)) {
testNumber += -1
}
})
function AccelerometerTest () {
if (input.isGesture(Gesture.LogoDown)) {
basic.showArrow(ArrowNames.North)
} else if (input.isGesture(Gesture.LogoUp)) {
basic.showArrow(ArrowNames.South)
} else if (input.isGesture(Gesture.TiltLeft)) {
basic.showArrow(ArrowNames.West)
} else if (input.isGesture(Gesture.TiltRight)) {
basic.showArrow(ArrowNames.East)
} else if (input.isGesture(Gesture.Shake)) {
basic.showIcon(IconNames.Yes)
} else if (input.isGesture(Gesture.ThreeG)) {
basic.showIcon(IconNames.Yes)
}
}
function ButtonTest () {
while (input.buttonIsPressed(Button.AB)) {
basic.showArrow(ArrowNames.North)
}
while (input.buttonIsPressed(Button.A)) {
basic.showArrow(ArrowNames.West)
}
while (input.buttonIsPressed(Button.B)) {
basic.showArrow(ArrowNames.East)
}
}
function LightSensorTest () {
level = input.lightLevel()
radio.sendNumber(level)
basic.pause(PAUSE_TIME)
}
function EmptyAndShowTestNo () {
testNumber = TrueModulo(testNumber, 11)
if (!(justSwitched)) {
basic.pause(DEBOUNCE_TIME)
justSwitched = 1
}
basic.clearScreen()
led.plot(testNumber % 5, (testNumber/5)|0)
if (testNumber == 4 || testNumber == 6) {
if (justSwitched) {
basic.pause(DEBOUNCE_TIME)
justSwitched = 0
}
} else {
justSwitched = 1
}
}
function TrueModulo (dividend: number, divisor: number) {
return dividend < 0 ? Math.abs((dividend += divisor) % divisor) : dividend % divisor
}
function PinAnalogOutputTest2 () {
for (let index3 = 0; index3 <= 2; index3++) {
pins.analogWritePin(index3 + 100, 500)
}
basic.pause(DEBOUNCE_TIME)
for (let index4 = 0; index4 <= 2; index4++) {
pins.analogWritePin(index4 + 100, 0)
}
}
function PinResistiveTest () {
if (input.pinIsPressed(TouchPin.P0)) {
basic.showNumber(0)
} else if (input.pinIsPressed(TouchPin.P1)) {
basic.showNumber(1)
} else if (input.pinIsPressed(TouchPin.P2)) {
basic.showNumber(2)
}
}
input.onButtonPressed(Button.B, function () {
basic.pause(PAUSE_TIME)
if (input.buttonIsPressed(Button.A)) {
testNumber += 1
}
})
function RadioTest () {
if (input.buttonIsPressed(Button.A) || input.buttonIsPressed(Button.B)) {
// Debouncer copied from @joshkeys19 on the MakeCode Forum:
// https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/6
if (input.runningTime() > lastButtonPressTime + DEBOUNCE_TIME) {
radio.sendString("OK")
basic.pause(PAUSE_TIME)
radio.sendString("\"")
lastButtonPressTime = input.runningTime()
}
}
}
function ScreenTest () {
basic.pause(PAUSE_TIME)
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
basic.pause(PAUSE_TIME)
}
function PinAnalogInputTest () {
level = ((Math.max(pins.analogReadPin(AnalogPin.P0), Math.max(pins.analogReadPin(AnalogReadWritePin.P1), pins.analogReadPin(AnalogReadWritePin.P2)))) / (1023/25))|0
for (let index2 = 0; index2 <= level; index2++) {
led.plot(index2 % 5, (index2/5)|0)
}
}
function ThermometerTest () {
level = input.temperature() - 15
for (let index = 0; index <= level; index++) {
led.plot(index % 5, (index/5)|0)
}
}
let level = 0
let degrees = 0
let lastButtonPressTime = 0
let calibrated = 0
let justSwitched = 0
let testNumber = 0
let PAUSE_TIME = 0
let DEBOUNCE_TIME = 0
radio.setGroup(99)
DEBOUNCE_TIME = 500
PAUSE_TIME = 200
testNumber = 0
justSwitched = 1
calibrated = 0
lastButtonPressTime = 0
/**
* MIT License
*
* This file is part of the repository:
*
* https://github.com/Vegz78/microbit_test_testunit
*
* Please feel free to use, fork and develop, but I appreciate proper attribution
*
* to the developer, and - if possible - linking back to this original repository:
*
* https://github.com/Vegz78/microbit_test_testunit
*
* Copyright(c) 2024 Vegz78
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
*
* of this software and associated documentation files(the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
*
* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
*
* copies of the Software, and to permit persons to whom the Software is
*
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
*
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
*
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*
* SOFTWARE.
*/
basic.forever(function () {
EmptyAndShowTestNo()
if (!(testNumber)) {
ButtonTest()
} else if (testNumber == 1) {
ScreenTest()
} else if (testNumber == 2) {
RadioTest()
} else if (testNumber == 3) {
LightSensorTest()
} else if (testNumber == 4) {
CompassTest()
} else if (testNumber == 5) {
AccelerometerTest()
} else if (testNumber == 6) {
ThermometerTest()
} else if (testNumber == 7) {
PinResistiveTest()
} else if (testNumber == 8) {
music._playDefaultBackground(music.builtInPlayableMelody(Melodies.Funk), music.PlaybackMode.UntilDone)
} else if (testNumber == 9) {
PinAnalogInputTest()
} else if (testNumber == 10) {
PinAnalogOutputTest2()
}
})