-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolume-dial.js
53 lines (41 loc) · 1.18 KB
/
volume-dial.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
const Potentiometer = require('./potentiometer')
class VolumeDial {
constructor(sensorName, onChange) {
this.onChange = onChange;
this.potentiometer = new Potentiometer(
sensorName,
() => this.onPotentiometerChange()
);
this._potLevel = null;
}
onPotentiometerChange() {
// always run on initial change
if (this._potLevel === null) {
this._potLevel = this.currentPotLevel;
this.normalisedValue = this.currentPotLevel;
this.onChange(this);
}
// ignore noisy sensors jittering +/- 1
if (Math.abs(this._potLevel - this.currentPotLevel) <= 2) {
return;
}
this._potLevel = this.currentPotLevel;
this.normalisedValue = this.currentPotLevel;
this.onChange(this);
}
get sensorName() {
return this.potentiometer.sensorName
}
get currentPotLevel() {
return this.potentiometer.value
}
set normalisedValue(value) {
const normalised = Math.floor(value / 1023 * 100) / 100;
// correct the dial level to left: decrease -> right: increase
this._normalisedValue = 1 - normalised;
}
get normalisedValue() {
return this._normalisedValue;
}
}
module.exports = VolumeDial;