-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
187 lines (187 loc) · 5.11 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
enum Time {
Hour,
Minute,
Second
}
enum TimeFormat {
hhmm,
hhmmss
}
/**
* Clock functions
*/
//% weight=100 color=#00b370 icon="\uf017" block="Clock"
namespace clock {
let toffset = 0
let tcounter = Math.floor(input.runningTime() / 1000)
let corrector = 3.25
let ampm = false
/**
* Returns the Hours, Minutes, or Seconds of the time
* needs 1 enum Time parameter
*/
//% block
export function getTime(t: Time): number {
while (Math.floor(input.runningTime() / 1000) - tcounter >= 900) {
tcounter += 900
toffset += corrector
}
let time = Math.floor(input.runningTime() / 1000) + toffset
while (time >= 24 * 3600) {
toffset -= 24 * 3600
time -= 24 * 3600
}
switch (t) {
case Time.Hour:
if (ampm) {
if (Math.floor(time / 3600) > 12) {
return Math.floor(time / 3600) - 12
} else if (Math.floor(time / 3600) == 0) {
return 12
} else {
return Math.floor(time / 3600)
}
} else {
return Math.floor(time / 3600)
}
break
case Time.Minute:
return Math.floor((time - Math.floor(time / 3600) * 3600) / 60)
break
case Time.Second:
return (Math.floor(time - Math.floor(time / 3600) * 3600
- Math.floor((time - Math.floor(time / 3600) * 3600) / 60) * 60))
break
}
}
/**
* Returns the time as a string in the format "00:00"
*/
//% block
export function timeString(f: TimeFormat): string {
let str = ""
let H = getTime(Time.Hour)
let M = getTime(Time.Minute)
if (f == TimeFormat.hhmmss) {
let S = getTime(Time.Second)
if (S < 10) {
str = ":0" + S
} else {
str = ":" + S
}
}
if (M < 10) {
str = ":0" + M + str
} else {
str = ":" + M + str
}
if (ampm && H > 12) {
H -= 12
} else if (ampm && H == 0) {
H = 12
}
if (H < 10) {
str = "0" + H + str
} else {
str = "" + H + str
}
return str
}
/**
* Sets the time to a specific value
* takes 2 or 3 arguments, returns false if invalid time is trying to be set
*/
//% block
export function setTime(h: number, m: number, s: number = 0): boolean {
if (!(h >= 0 && h < 24 && m >= 0 && m < 60 && s >= 0 && s < 60))
return false
//subtract 1 day from CPU clock, then add the actual time back
toffset = - Math.floor(input.runningTime() / 1000) + (h * 3600 + m * 60 + s)
return true
}
/**
* Enables or disables the 12 hour clock
*/
//% block
export function enableAmPm(value: boolean): void {
ampm = value
}
/**
* Returns true if the 12 hour clock is enabled,
* otherwise false
*/
//% block
export function getAmPm(): boolean {
return ampm
}
/**
* Sets the time correction value.
* If the clock is too slow, set it to a higher value than 3.25
* If the clock is too fast, set it to a lower value than 3.25
*/
//% block
export function setTimeCorrector(v: number): void {
corrector = v
}
}
/**
* Countdown
*/
//% weight=100 color=#00b3b3 icon="\uf252" block="Countdown"
//% advanced=true
namespace countdown {
let cdstate = false
let end: number
/**
* Starts the countdown of X seconds
*/
//% block
export function start(secs: number): void {
end = input.runningTime() + secs * 1000
cdstate = true
}
/**
* Stops and resets the countdown
*/
//% block
export function stop(): void {
cdstate = false
}
/**
* Returns true if cd is initiated, otherwise false
*/
//% block
export function state(): boolean {
return cdstate
}
/**
* Returns the remaining time in Secs
* e.g. 01:12 -> 72 Secs
*/
//% block
export function remainingTime(): number {
if (cdstate && Math.floor((end - input.runningTime()) / 1000) > 0) {
return Math.floor((end - input.runningTime()) / 1000)
} else return 0
}
/**
* Only returns the Mins of the remaining time
* e.g. 01:12 -> 1 Min
*/
//% block
export function remainingMinute(): number {
if (cdstate && remainingTime() > 0) {
return Math.floor(Math.floor((end - input.runningTime()) / 1000) / 60)
} else return 0
}
/**
* Only returns the Secs of the remaining time
* e.g. 01:12 -> 12 Secs
*/
//% block
export function remainingSecond(): number {
if (cdstate && remainingTime() > 0) {
return Math.floor((end - input.runningTime()) / 1000) - remainingMinute() * 60
} else return 0
}
}