using threshold detector
This commit is contained in:
parent
34a5aeb7d2
commit
305a650125
@ -134,16 +134,16 @@ namespace sensors {
|
||||
this.button.onEvent(ev, body);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//% fixedInstances
|
||||
export class InfraredSensor extends internal.UartSensor {
|
||||
private channel: IrRemoteChannel;
|
||||
private proximityThreshold: number;
|
||||
private proximityThreshold: sensors.internal.ThresholdDetector;
|
||||
|
||||
constructor(port: number) {
|
||||
super(port)
|
||||
this.channel = IrRemoteChannel.Ch0
|
||||
this.proximityThreshold = 10;
|
||||
this.proximityThreshold = new sensors.internal.ThresholdDetector(this._id, 0, 100, 10, 90);
|
||||
irButton(0) // make sure buttons array is initalized
|
||||
|
||||
// and set the mode, as otherwise button events won't work
|
||||
@ -154,10 +154,7 @@ namespace sensors {
|
||||
if (this.mode == IrSensorMode.RemoteControl)
|
||||
return mapButton(this.getNumber(NumberFormat.UInt8LE, this.channel));
|
||||
else if (this.mode == IrSensorMode.Proximity) {
|
||||
const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
|
||||
return d < this.proximityThreshold ? InfraredSensorEvent.ObjectNear
|
||||
: d > this.proximityThreshold + 5 ? InfraredSensorEvent.ObjectDetected
|
||||
: 0;
|
||||
return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@ -168,9 +165,8 @@ namespace sensors {
|
||||
let v = !!(curr & (1 << i))
|
||||
buttons[i]._update(v)
|
||||
}
|
||||
} else {
|
||||
if (curr)
|
||||
control.raiseEvent(this._id, curr);
|
||||
} else if (this.mode == IrSensorMode.Proximity) {
|
||||
this.proximityThreshold.setLevel(curr);
|
||||
}
|
||||
}
|
||||
|
||||
@ -181,7 +177,7 @@ namespace sensors {
|
||||
setRemoteChannel(c: IrRemoteChannel) {
|
||||
c = Math.clamp(0, 3, c | 0)
|
||||
this.channel = c
|
||||
this._setMode(IrSensorMode.RemoteControl)
|
||||
this.setMode(IrSensorMode.RemoteControl)
|
||||
}
|
||||
|
||||
setMode(m: IrSensorMode) {
|
||||
@ -200,7 +196,7 @@ namespace sensors {
|
||||
//% weight=100 blockGap=8
|
||||
//% group="Infrared Sensor"
|
||||
onEvent(event: InfraredSensorEvent, handler: () => void) {
|
||||
control.onEvent(this._id, InfraredSensorEvent.ObjectNear, handler);
|
||||
control.onEvent(this._id, event, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -216,7 +212,7 @@ namespace sensors {
|
||||
waitUntil(event: InfraredSensorEvent) {
|
||||
control.waitForEvent(this._id, event);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the promixity measured by the infrared sensor, from ``0`` (close) to ``100`` (far)
|
||||
* @param ir the infrared sensor
|
||||
@ -274,7 +270,7 @@ namespace sensors {
|
||||
*/
|
||||
//% whenUsed block="center" weight=95 fixedInstance
|
||||
export const remoteButtonCenter = irButton(IrRemoteButton.CenterBeacon)
|
||||
|
||||
|
||||
/**
|
||||
* Remote top-left button.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user