2017-10-27 05:38:17 +02:00
|
|
|
enum UltrasonicEvent {
|
|
|
|
ObjectClose = 1,
|
|
|
|
ObjectDetected = 2
|
|
|
|
}
|
|
|
|
|
2017-07-10 16:07:23 +02:00
|
|
|
namespace input {
|
|
|
|
|
2017-10-25 00:33:28 +02:00
|
|
|
//% fixedInstances
|
2017-07-10 16:07:23 +02:00
|
|
|
export class UltraSonicSensor extends internal.UartSensor {
|
2017-10-27 05:38:17 +02:00
|
|
|
private threshold: number;
|
|
|
|
|
2017-10-24 20:58:52 +02:00
|
|
|
constructor(port: number) {
|
|
|
|
super(port)
|
2017-10-27 05:38:17 +02:00
|
|
|
this.threshold = 10;
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_deviceType() {
|
2017-07-11 16:18:59 +02:00
|
|
|
return DAL.DEVICE_TYPE_ULTRASONIC
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 05:38:17 +02:00
|
|
|
_query(): number {
|
|
|
|
const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
|
|
|
|
return d < this.threshold ? UltrasonicEvent.ObjectClose
|
|
|
|
: d > this.threshold + 5 ? UltrasonicEvent.ObjectDetected
|
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_update(prev: number, curr: number) {
|
|
|
|
if (curr)
|
|
|
|
control.raiseEvent(this._id, curr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers code to run when the given color is close
|
|
|
|
* @param distance the distance in centimeters when an object is close, eg: 10
|
|
|
|
* @param handler the code to run when detected
|
|
|
|
*/
|
|
|
|
//% help=input/ultrasonic/on-object
|
|
|
|
//% block="on %sensor|object within %distance|cm"
|
|
|
|
//% blockId=ultrasonicOnObjectClose
|
|
|
|
//% parts="ultrasonicsensor"
|
|
|
|
//% blockNamespace=input
|
|
|
|
//% weight=100 blockGap=8
|
|
|
|
//% group="Ultrasonic Sensor"
|
|
|
|
onObject(distance: number, handler: () => void) {
|
|
|
|
this.threshold = Math.max(1, Math.min(95, distance));
|
|
|
|
control.onEvent(this._id, UltrasonicEvent.ObjectClose, handler);
|
|
|
|
this._setMode(0)
|
|
|
|
if (this._query() == UltrasonicEvent.ObjectClose)
|
|
|
|
control.runInBackground(handler);
|
|
|
|
}
|
|
|
|
|
2017-10-25 00:33:28 +02:00
|
|
|
/**
|
|
|
|
* Gets the distance from the sonar in millimeters
|
2017-10-25 00:35:42 +02:00
|
|
|
* @param sensor the ultrasonic sensor port
|
2017-10-25 00:33:28 +02:00
|
|
|
*/
|
|
|
|
//% help=input/ultrasonic/distance
|
2017-10-25 00:35:42 +02:00
|
|
|
//% block="%sensor|distance"
|
2017-10-25 00:33:28 +02:00
|
|
|
//% blockId=sonarGetDistance
|
|
|
|
//% parts="ultrasonic"
|
|
|
|
//% blockNamespace=input
|
|
|
|
//% weight=65 blockGap=8
|
2017-10-25 01:52:13 +02:00
|
|
|
//% group="Ultrasonic Sensor"
|
2017-10-25 00:33:28 +02:00
|
|
|
distance() {
|
2017-07-10 16:07:23 +02:00
|
|
|
// it supposedly also has an inch mode, but we stick to mm
|
|
|
|
this._setMode(0)
|
2017-10-27 05:38:17 +02:00
|
|
|
return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 01:52:13 +02:00
|
|
|
//% fixedInstance whenUsed block="ultrasonic sensor 4"
|
|
|
|
export const ultrasonic4: UltraSonicSensor = new UltraSonicSensor(4)
|
|
|
|
|
2017-10-25 00:35:42 +02:00
|
|
|
//% fixedInstance whenUsed block="ultrasonic sensor 1"
|
2017-10-24 20:58:52 +02:00
|
|
|
export const ultrasonic1: UltraSonicSensor = new UltraSonicSensor(1)
|
|
|
|
|
2017-10-25 01:52:13 +02:00
|
|
|
//% fixedInstance whenUsed block="ultrasonic sensor 2"
|
2017-10-24 20:58:52 +02:00
|
|
|
export const ultrasonic2: UltraSonicSensor = new UltraSonicSensor(2)
|
|
|
|
|
2017-10-25 01:52:13 +02:00
|
|
|
//% fixedInstance whenUsed block="ultrasonic sensor 3"
|
2017-10-24 20:58:52 +02:00
|
|
|
export const ultrasonic3: UltraSonicSensor = new UltraSonicSensor(3)
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|