@@ -9,7 +9,12 @@ import {
9
9
} from "./conversions" ;
10
10
import { FileSystem } from "./fs" ;
11
11
import { WebAssemblyOperations } from "./listener" ;
12
- import { EnumSensor , RangeSensor , Sensor } from "./sensors" ;
12
+ import {
13
+ EnumSensor ,
14
+ RangeSensor ,
15
+ RangeSensorWithThresholds ,
16
+ Sensor ,
17
+ } from "./sensors" ;
13
18
import { clamp } from "./util" ;
14
19
15
20
const stoppedOpactity = "0.5" ;
@@ -75,7 +80,8 @@ export class BoardUI {
75
80
this . temperature = new RangeSensor ( "temperature" , - 5 , 50 , 21 , "°C" ) ;
76
81
this . accelerometer = new AccelerometerUI ( onSensorChange ) ;
77
82
this . microphone = new MicrophoneUI (
78
- this . svg . querySelector ( "#LitMicrophone" ) !
83
+ this . svg . querySelector ( "#LitMicrophone" ) ! ,
84
+ onSensorChange
79
85
) ;
80
86
81
87
this . sensors = [
@@ -434,22 +440,20 @@ export class AccelerometerUI {
434
440
}
435
441
436
442
export class MicrophoneUI {
437
- public soundLevel : RangeSensor = new RangeSensor (
443
+ public soundLevel : RangeSensorWithThresholds = new RangeSensorWithThresholds (
438
444
"soundLevel" ,
439
445
0 ,
440
446
255 ,
441
447
0 ,
442
- undefined
448
+ undefined ,
449
+ 75 ,
450
+ 150
443
451
) ;
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 ;
448
452
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
+ ) { }
453
457
454
458
microphoneOn ( ) {
455
459
this . element . style . display = "unset" ;
@@ -460,18 +464,26 @@ export class MicrophoneUI {
460
464
}
461
465
462
466
setThreshold ( threshold : "low" | "high" , value : number ) {
467
+ const proposed = value > 255 ? 255 : value < 0 ? 0 : value ;
463
468
if ( threshold === "low" ) {
464
- this . lowThreshold = value ;
469
+ this . soundLevel . lowThreshold = proposed ;
465
470
} else {
466
- this . highThreshold = value ;
471
+ this . soundLevel . highThreshold = proposed ;
467
472
}
473
+ this . onSensorChange ( ) ;
468
474
}
469
475
470
476
initialize ( soundLevelCallback : ( v : number ) => void ) {
471
477
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
+ ) {
473
482
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
+ ) {
475
487
soundLevelCallback ( convertSoundEventStringToNumber ( "high" ) ) ;
476
488
}
477
489
} ;
0 commit comments