-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicroprobe.js
159 lines (146 loc) · 4.27 KB
/
microprobe.js
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
// ___ Config ___
// Gamma: the minimum acceptable proportion of requests that must
// receive correct responses.
let gamma = 0.9;
// Lambda: the minimum acceptable rate of correct Responses Per
// Second (RPS).
let lambda = 10;
// Length of the rolling evaluation window (milliseconds).
let windowLen = 5 * 1000
// The maximum Queries Per Second (QPS) expected during operation,
// used to size the internal buffer.
let maxSupportedRps = 100
// Minimum required length of the results buffer
// to support up to maxSupportedRps for the chosen eval window,
// adding a factor of 1.2 to be above the minimum.
let q_len = Math.max(25, Math.round(1.2 * maxSupportedRps * (windowLen / 1000)));
// ______________
class Response {
time: number;
val: boolean;
constructor(time: number, val: Buffer) {
this.time = time;
switch (val.toString()) {
case "1":
this.val = true
break;
case "0":
this.val = false
break;
default:
error("Invalid byte: ".concat(val.toString()))
}
}
isCorrect() {
return this.val;
}
}
class Queue {
bufferLen: number;
buffer: Response[];
// keep track of the number of correct responses in the window
correct: number = 0;
// the number of correct responses in the window to meet the
// response rate requirement
lambdaThreshold: number = Math.round(lambda * windowLen / 1000);
counter: number = 0;
// init as 1 because queue.shift() occurs before seekWindowCur()
windowCur: number = 0;
constructor(bufferLen: number) {
this.bufferLen = bufferLen;
let buf = [];
for (let index = 0; index < bufferLen; index++) {
buf.push(new Response(0, Buffer.fromUTF8("0")));
}
this.buffer = buf;
}
shift() {
this.buffer.shift();
this.windowCur = Math.max(0, this.windowCur - 1);
}
push(val: Buffer) {
let response = new Response(control.millis(), val)
this.buffer.push(response);
if (response.isCorrect()) {
this.correct += 1;
}
if (!this.bufferInitComplete()) {
this.counter += 1;
if (this.counter > this.bufferLen) {
this.buzz(700)
}
}
}
bufferInitComplete() {
return this.counter > this.bufferLen
}
seekWindowCur() {
let now = control.millis();
function windowBeginTime(that: Queue) {
return that.buffer[that.windowCur].time
}
while ((now - windowBeginTime(this)) > windowLen) {
// drop a Response from the window
if (this.buffer[this.windowCur].isCorrect()) {
this.correct -= 1;
}
this.windowCur += 1
}
return this.windowCur
}
lightUp() {
let valArray = this.buffer.map(
response => { return response.isCorrect() })
lightArray(valArray.slice(-25));
}
buzz(tone: number) {
music.play(music.tonePlayable(tone, 50), music.PlaybackMode.InBackground);
}
proportionIsHighEnough() {
let windowCur = this.seekWindowCur();
let windowNumElems = q_len - windowCur;
let gammaThreshold = gamma * windowNumElems;
return this.correct > gammaThreshold
}
rateIsHighEnough() {
return this.correct > this.lambdaThreshold
}
evaluateAndBuzz() {
if (!this.bufferInitComplete()) {
return;
}
let ok = (this.proportionIsHighEnough() || this.rateIsHighEnough());
if (!ok) {
this.buzz(260);
}
}
}
function lightArray(array: boolean[]) {
for (let i = 0; i <= 4; i++) {
for (let j = 0; j <= 4; j++) {
if (array[i + j * 5]) {
led.plot(i, j)
} else {
led.unplot(i, j)
}
}
}
}
function main() {
serial.setBaudRate(115200);
let queue = new Queue(q_len);
queue.buzz(500);
while (true) {
queue.shift();
queue.push(serial.readBuffer(1));
queue.lightUp();
queue.evaluateAndBuzz();
}
}
function error(msg: string) {
while (true) {
basic.showString(msg);
pause(10000);
}
}
main();