-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
209 lines (171 loc) · 4.51 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
const BUTTON_LONG_CLICK = 4;
const MAX_ALLOWED_MINUTES = 25;
const MAX_STEP = 5
const MIN_STEP = 1
const EVENT_TIMER_SOURCE = 90000;
enum TimerState {
stopped,
started,
paused
}
let _enableSound = true;
let _elapsed = 0
let _listY : number[] = []
let _listX : number[] = []
let _timerState: TimerState = TimerState.stopped;
let _minuteStep = 5
let _minutes = 10
let _remainingMinutes = _minutes
initList()
reset()
control.onEvent(EventBusSource.MICROBIT_ID_BUTTON_AB, BUTTON_LONG_CLICK, () => {
_enableSound = !_enableSound
basic.showNumber(+_enableSound);
playMelody(Melodies.JumpUp);
blinkLeds(3)
renderScreen()
})
control.onEvent(EventBusSource.MICROBIT_ID_BUTTON_B, BUTTON_LONG_CLICK, stopTimer)
input.onButtonPressed(Button.AB, endTimer)
input.onButtonPressed(Button.B, toggleTimer)
control.onEvent(EventBusSource.MICROBIT_ID_BUTTON_A, BUTTON_LONG_CLICK, () => {
let plotOnY = 2;
_minuteStep = _minuteStep == MAX_STEP ? MIN_STEP : MAX_STEP;
basic.clearScreen()
if (_minuteStep == MAX_STEP) {
for (let x = 0; x <= _minuteStep - 1; x++) {
led.plot(x, plotOnY)
}
} else {
led.plot(plotOnY, plotOnY)
}
blinkLeds(3)
renderScreen();
})
input.onButtonPressed(Button.A, () => {
if (_timerState == TimerState.stopped) {
if (_minutes >= MAX_ALLOWED_MINUTES) {
_minutes = _minuteStep;
} else {
// add remainer if step is 5 and modulus is not 0
_minutes += _minutes % 5 > 0 && _minuteStep == MAX_STEP ? 5 - (_minutes % 5) : _minuteStep;
}
_remainingMinutes = _minutes
renderScreen()
}
})
function initList() {
for (let y = 0; y < MAX_STEP; y++) {
for (let x = 0; x < MAX_STEP; x++) {
_listX.push(x)
_listY.push(y)
}
}
}
function reset() {
basic.clearScreen()
setDefaultValues()
renderScreen()
}
function stopTimer() {
if (_timerState != TimerState.stopped) {
_timerState = TimerState.stopped
basic.clearScreen()
basic.showLeds(`
. . . . .
. # # # .
. # . # .
. # # # .
. . . . .
`)
blinkLeds()
reset()
}
}
function blinkLeds(times: number = 3, msout: number = 300, msin: number = 500) {
for (let index = 0; index <= times; index++) {
led.fadeOut(msout)
led.fadeIn(msin)
}
}
function pauseTimer() {
// control.raiseEvent(EVENT_TIMER_SOURCE, TimerState.paused) ;
_timerState = TimerState.paused
playMelody(Melodies.Funk)
}
function startTimer() {
renderScreen()
_timerState = TimerState.started
playMelody(Melodies.PowerUp)
}
function playMelody(melody: Melodies) {
if (_enableSound) {
music.startMelody(music.builtInMelody(melody), MelodyOptions.OnceInBackground)
}
}
function playSound(frequency: number) {
if (_enableSound) {
music.playTone(frequency, music.beat(BeatFraction.Sixteenth))
}
}
function toggleTimer() {
led.fadeIn()
if (_timerState != TimerState.started) {
startTimer()
} else {
pauseTimer()
}
}
function endTimer() {
_timerState = TimerState.stopped
playMelody(Melodies.Wawawawaa)
basic.showIcon(IconNames.No)
blinkLeds(5)
reset()
}
function setDefaultValues() {
_timerState = TimerState.stopped
_remainingMinutes = _minutes
_elapsed = 0
}
function renderScreen() {
basic.clearScreen()
let x = 0
let y = 0
let minIdx = _remainingMinutes - 1
for (let index = 0; index < _remainingMinutes; index++) {
x = _listX[index]
y = _listY[index]
led.plot(x, y)
}
}
loops.everyInterval(1000, () => {
let x: number;
let y: number;
if (_timerState == TimerState.started) {
_elapsed = _elapsed + 1
x = _listX[_remainingMinutes - 1]
y = _listY[_remainingMinutes - 1]
led.toggle(x, y)
if (_elapsed == 60) {
_elapsed = 0
_remainingMinutes = _remainingMinutes - 1
renderScreen()
playSound(131);
}
if (_remainingMinutes <= 0) {
endTimer()
}
}
if (_timerState == TimerState.paused) {
led.fadeOut(100)
basic.showLeds(`
. . . . .
. # . # .
. # . # .
. # . # .
. . . . .
`)
led.fadeIn(300)
}
})