Skip to content

Commit 1e0fe19

Browse files
Add range sensor with thresholds. (#32)
-Add range sensor with thresholds. -Clamp microphone thresholds.
1 parent 2513f20 commit 1e0fe19

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

src/board/sensors.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,35 @@ export class RangeSensor extends Sensor {
6666
}
6767
}
6868

69+
export class RangeSensorWithThresholds extends RangeSensor {
70+
constructor(
71+
id: string,
72+
public min: number,
73+
public max: number,
74+
public initial: number,
75+
public unit: string | undefined,
76+
public lowThreshold: number,
77+
public highThreshold: number
78+
) {
79+
super(id, min, max, initial, unit);
80+
this.lowThreshold = lowThreshold;
81+
this.highThreshold = highThreshold;
82+
}
83+
84+
toSerializable() {
85+
return {
86+
type: "range",
87+
id: this.id,
88+
min: this.min,
89+
max: this.max,
90+
value: this.value,
91+
unit: this.unit,
92+
lowThreshold: this.lowThreshold,
93+
highThreshold: this.highThreshold,
94+
};
95+
}
96+
}
97+
6998
export class EnumSensor extends Sensor {
7099
public value: string;
71100
public onchange: (v: string) => void = () => {};

src/board/ui.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import {
99
} from "./conversions";
1010
import { FileSystem } from "./fs";
1111
import { WebAssemblyOperations } from "./listener";
12-
import { EnumSensor, RangeSensor, Sensor } from "./sensors";
12+
import {
13+
EnumSensor,
14+
RangeSensor,
15+
RangeSensorWithThresholds,
16+
Sensor,
17+
} from "./sensors";
1318
import { clamp } from "./util";
1419

1520
const stoppedOpactity = "0.5";
@@ -75,7 +80,8 @@ export class BoardUI {
7580
this.temperature = new RangeSensor("temperature", -5, 50, 21, "°C");
7681
this.accelerometer = new AccelerometerUI(onSensorChange);
7782
this.microphone = new MicrophoneUI(
78-
this.svg.querySelector("#LitMicrophone")!
83+
this.svg.querySelector("#LitMicrophone")!,
84+
onSensorChange
7985
);
8086

8187
this.sensors = [
@@ -434,22 +440,20 @@ export class AccelerometerUI {
434440
}
435441

436442
export class MicrophoneUI {
437-
public soundLevel: RangeSensor = new RangeSensor(
443+
public soundLevel: RangeSensorWithThresholds = new RangeSensorWithThresholds(
438444
"soundLevel",
439445
0,
440446
255,
441447
0,
442-
undefined
448+
undefined,
449+
75,
450+
150
443451
);
444-
// In future we might try to expose these so they can be drawn as
445-
// marks on the sensor.
446-
private lowThreshold: number;
447-
private highThreshold: number;
448452

449-
constructor(private element: SVGElement) {
450-
this.lowThreshold = 75;
451-
this.highThreshold = 150;
452-
}
453+
constructor(
454+
private element: SVGElement,
455+
private onSensorChange: () => void
456+
) {}
453457

454458
microphoneOn() {
455459
this.element.style.display = "unset";
@@ -460,18 +464,26 @@ export class MicrophoneUI {
460464
}
461465

462466
setThreshold(threshold: "low" | "high", value: number) {
467+
const proposed = value > 255 ? 255 : value < 0 ? 0 : value;
463468
if (threshold === "low") {
464-
this.lowThreshold = value;
469+
this.soundLevel.lowThreshold = proposed;
465470
} else {
466-
this.highThreshold = value;
471+
this.soundLevel.highThreshold = proposed;
467472
}
473+
this.onSensorChange();
468474
}
469475

470476
initialize(soundLevelCallback: (v: number) => void) {
471477
this.soundLevel.onchange = (prev: number, curr: number) => {
472-
if (prev > this.lowThreshold && curr <= this.lowThreshold) {
478+
if (
479+
prev > this.soundLevel.lowThreshold &&
480+
curr <= this.soundLevel.lowThreshold
481+
) {
473482
soundLevelCallback(convertSoundEventStringToNumber("low"));
474-
} else if (prev < this.highThreshold && curr >= this.highThreshold) {
483+
} else if (
484+
prev < this.soundLevel.highThreshold &&
485+
curr >= this.soundLevel.highThreshold
486+
) {
475487
soundLevelCallback(convertSoundEventStringToNumber("high"));
476488
}
477489
};

0 commit comments

Comments
 (0)