simplified IR API
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
const enum PromixityEvent {
|
||||
const enum UltrasonicSensorEvent {
|
||||
//% block="object near"
|
||||
ObjectNear = 1,
|
||||
//% block="object detected"
|
||||
@ -10,10 +10,12 @@ namespace sensors {
|
||||
//% fixedInstances
|
||||
export class UltraSonicSensor extends internal.UartSensor {
|
||||
private promixityThreshold: number;
|
||||
private movementThreshold: number;
|
||||
|
||||
constructor(port: number) {
|
||||
super(port)
|
||||
this.promixityThreshold = 10;
|
||||
this.movementThreshold = 1;
|
||||
}
|
||||
|
||||
_deviceType() {
|
||||
@ -21,36 +23,49 @@ namespace sensors {
|
||||
}
|
||||
|
||||
_query(): number {
|
||||
const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
|
||||
return d < this.promixityThreshold ? PromixityEvent.ObjectNear
|
||||
: d > this.promixityThreshold + 5 ? PromixityEvent.ObjectDetected
|
||||
: 0;
|
||||
return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
|
||||
}
|
||||
|
||||
_update(prev: number, curr: number) {
|
||||
if (curr)
|
||||
control.raiseEvent(this._id, curr);
|
||||
// is there an object near?
|
||||
if (prev >= this.promixityThreshold && curr < this.promixityThreshold)
|
||||
control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectNear); // TODO proper HI-LO sensor
|
||||
|
||||
// did something change?
|
||||
if (Math.abs(prev - curr) > this.movementThreshold)
|
||||
control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectDetected); // TODO debouncing
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers code to run when the given color is close
|
||||
* @param handler the code to run when detected
|
||||
*/
|
||||
//% help=input/ultrasonic/on-object-near
|
||||
//% block="on %sensor|object near"
|
||||
//% blockId=ultrasonicOnObjectClose
|
||||
//% help=input/ultrasonic/on
|
||||
//% block="on %sensor|%event"
|
||||
//% blockId=ultrasonicOn
|
||||
//% parts="infraredsensor"
|
||||
//% blockNamespace=sensors
|
||||
//% weight=100 blockGap=8
|
||||
//% group="Ultrasonic Sensor"
|
||||
onObjectNear(handler: () => void) {
|
||||
control.onEvent(this._id, PromixityEvent.ObjectNear, handler);
|
||||
if (this.distance() == PromixityEvent.ObjectNear)
|
||||
on(event: UltrasonicSensorEvent, handler: () => void) {
|
||||
control.onEvent(this._id, event, handler);
|
||||
if (event == UltrasonicSensorEvent.ObjectNear
|
||||
&& this.distance() < this.promixityThreshold)
|
||||
control.runInBackground(handler);
|
||||
}
|
||||
|
||||
setObjectNearThreshold(distance: number) {
|
||||
this.promixityThreshold = Math.max(1, Math.min(250, distance));
|
||||
/**
|
||||
* Waits for the event to occur
|
||||
*/
|
||||
//% help=input/ultrasonic/wait
|
||||
//% block="wait %sensor|for %event"
|
||||
//% blockId=ultrasonicWait
|
||||
//% parts="infraredsensor"
|
||||
//% blockNamespace=sensors
|
||||
//% weight=99 blockGap=8
|
||||
//% group="Ultrasonic Sensor"
|
||||
wait(event: UltrasonicSensorEvent) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,7 +80,7 @@ namespace sensors {
|
||||
//% weight=65 blockGap=8
|
||||
//% group="Ultrasonic Sensor"
|
||||
distance() {
|
||||
// it supposedly also has an inch mode, but we stick to mm
|
||||
// it supposedly also has an inch mode, but we stick to cm
|
||||
this._setMode(0)
|
||||
return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
|
||||
}
|
||||
|
Reference in New Issue
Block a user