-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypad.ts
35 lines (31 loc) · 1.38 KB
/
keypad.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
import {Cell, CellLoop, Stream, StreamSink, Unit} from "sodiumjs";
import {Keys} from "../types";
function wireKeypad(sKeypad: StreamSink<Keys>|Stream<Keys>, sClear: Stream<Unit>, cActive?: Cell<boolean>) {
if (cActive) {
return wireKeypad(sKeypad.gate(cActive), sClear);
}
const cValue = new CellLoop<number>();
const sKeyUpdate = sKeypad.snapshot(cValue, (key, value) => {
if (key === Keys.CLEAR) {
return 0;
} else {
const tmp = value * 10;
return tmp >= 1000
? null
: (key === Keys.ZERO ? tmp :
key === Keys.ONE ? tmp + 1 :
key === Keys.TWO ? tmp + 2 :
key === Keys.THREE ? tmp + 3 :
key === Keys.FOUR ? tmp + 4 :
key === Keys.FIVE ? tmp + 5 :
key === Keys.SIX ? tmp + 6 :
key === Keys.SEVEN ? tmp + 7 :
key === Keys.EIGHT ? tmp + 8 :
tmp + 9);
}
}).filterNotNull() as Stream<number>;
cValue.loop(sKeyUpdate.orElse(sClear.map(u => 0)).hold(0));
const sBeep = sKeyUpdate.map(() => Unit.UNIT);
return {sBeep, cValue};
}
export default wireKeypad;