using threshold detector

This commit is contained in:
Peli de Halleux 2017-11-30 09:59:28 -08:00
parent 34a5aeb7d2
commit 305a650125

View File

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