-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhrm.js
More file actions
72 lines (63 loc) · 1.42 KB
/
hrm.js
File metadata and controls
72 lines (63 loc) · 1.42 KB
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
/*
舊的程式碼:每次呼叫 next() 都會更新 bpm
let bpm=72;
let dir=1;
function next(){
bpm+=dir*(Math.random()*2);
if(bpm>85) dir=-1;
if(bpm<65) dir=1;
return Math.round(bpm);
}
*/
// --- 新的程式碼 ---
//// 全域變數,用於儲存心率值、方向和上次更新的時間戳
//var bpm = 72;
//var dir = 1;
//var lastUpdateTime = 0;
//
//// Date.now() 返回自 1970 年 1 月 1 日 00:00:00 UTC 以來的毫秒數
//function now_ms() {
// return Date.now();
//}
//
//// 核心函式,現在包含了計時邏輯
//function next() {
// const currentTime = now_ms();
//
// if (currentTime - lastUpdateTime > 15000) {
// bpm += dir * (Math.random() * 2);
// if (bpm > 85) dir = -1;
// if (bpm < 65) dir = 1;
// lastUpdateTime = currentTime;
//
// console.log('[hrm.js] BPM was updated to: ' + Math.round(bpm));
// }
//
// // 為了保持 evaluate("next()") 能工作,我們仍然回傳 bpm
// return Math.round(bpm);
//}
registerGatt("2A37", (function () {
let bpm = 72;
let dir = 1;
let last = 0;
function next() {
const now = Clock.millis();
if (last === 0) {
last = now;
return;
}
if (now - last >= 1000) {
bpm += dir * (Math.random() * 2);
if (bpm > 85) dir = -1;
if (bpm < 65) dir = 1;
last = now;
}
}
function getValue() {
return Math.round(bpm);
}
return {
next,
getValue
};
})());