pxt-ev3/libs/infrared-sensor/ir.ts

336 lines
12 KiB
TypeScript
Raw Normal View History

const enum InfraredSensorMode {
None = 0,
2017-07-10 12:37:14 +02:00
Proximity = 0,
Seek = 1,
RemoteControl = 2,
}
const enum InfraredRemoteChannel {
//% block="channel 0"
2017-07-10 12:37:14 +02:00
Ch0 = 0, // top
//% block="channel 1"
2017-07-10 12:37:14 +02:00
Ch1 = 1,
//% block="channel 2"
2017-07-10 12:37:14 +02:00
Ch2 = 2,
//% block="channel 3"
2017-07-10 12:37:14 +02:00
Ch3 = 3,
}
const enum InfraredRemoteButton {
//% block="center beacon"
2017-07-10 12:37:14 +02:00
CenterBeacon = 0x01,
//% block="top left"
2017-07-10 12:37:14 +02:00
TopLeft = 0x02,
//% block="bottom left"
2017-07-10 12:37:14 +02:00
BottomLeft = 0x04,
//% block="top right"
2017-07-10 12:37:14 +02:00
TopRight = 0x08,
//% block="bottom right"
2017-07-10 12:37:14 +02:00
BottomRight = 0x10,
}
2017-10-31 05:39:50 +01:00
const enum InfraredSensorEvent {
//% block="object near"
ObjectNear = 3,
2017-10-31 05:39:50 +01:00
//% block="object detected"
ObjectDetected = 2
}
2017-10-28 18:13:02 +02:00
namespace sensors {
2017-07-10 12:37:14 +02:00
function mapButton(v: number) {
switch (v) {
case 1: return InfraredRemoteButton.TopLeft;
case 2: return InfraredRemoteButton.BottomLeft;
case 3: return InfraredRemoteButton.TopRight;
case 4: return InfraredRemoteButton.BottomRight;
case 5: return InfraredRemoteButton.TopLeft | InfraredRemoteButton.TopRight
case 6: return InfraredRemoteButton.TopLeft | InfraredRemoteButton.BottomRight
case 7: return InfraredRemoteButton.BottomLeft | InfraredRemoteButton.TopRight
case 8: return InfraredRemoteButton.BottomLeft | InfraredRemoteButton.BottomRight
case 9: return InfraredRemoteButton.CenterBeacon
case 10: return InfraredRemoteButton.BottomLeft | InfraredRemoteButton.TopLeft
case 11: return InfraredRemoteButton.TopRight | InfraredRemoteButton.BottomRight
default: return 0;
2017-07-10 12:37:14 +02:00
}
}
const __remoteButtons: RemoteInfraredBeaconButton[] = [];
function __irButton(id: InfraredRemoteButton): RemoteInfraredBeaconButton {
for (let i = 0; i < __remoteButtons.length; ++i) {
if (__remoteButtons[i].position == id)
return __remoteButtons[i];
2017-10-24 20:58:52 +02:00
}
const btn = new RemoteInfraredBeaconButton(id, new brick.Button());
__remoteButtons.push(btn);
return btn;
2017-10-24 20:58:52 +02:00
}
2017-10-27 06:10:37 +02:00
//% fixedInstances
export class RemoteInfraredBeaconButton extends control.Component {
position: InfraredRemoteButton;
private _button: brick.Button;
constructor(position: InfraredRemoteButton, button: brick.Button) {
2017-10-27 06:10:37 +02:00
super();
this.position = position;
this._button = button;
2017-10-27 06:10:37 +02:00
}
_update(curr: boolean) {
this._button._update(curr);
2017-10-27 06:10:37 +02:00
}
/**
* Check if a remote button is currently pressed or not.
* @param button the remote button to query the request
*/
//% help=sensors/beacon/is-pressed
//% block="**remote button** %button|is pressed"
2017-10-27 06:10:37 +02:00
//% blockId=remoteButtonIsPressed
//% parts="remote"
2017-10-28 18:13:02 +02:00
//% blockNamespace=sensors
2017-10-27 06:10:37 +02:00
//% weight=81 blockGap=8
//% group="Remote Infrared Beacon"
isPressed() {
return this._button.isPressed();
2017-10-27 06:10:37 +02:00
}
/**
* See if the remote button was pressed again since the last time you checked.
* @param button the remote button to query the request
*/
//% help=sensors/beacon/was-pressed
//% block="**remote button** %button|was pressed"
2017-10-27 06:10:37 +02:00
//% blockId=remotebuttonWasPressed
2018-04-03 13:10:35 +02:00
//% blockHidden=true
2017-10-27 06:10:37 +02:00
//% parts="remote"
2017-10-28 18:13:02 +02:00
//% blockNamespace=sensors
//% weight=80
2017-10-27 06:10:37 +02:00
//% group="Remote Infrared Beacon"
wasPressed() {
return this._button.wasPressed();
2017-10-27 06:10:37 +02:00
}
/**
* Do something when a remote button is pressed, bumped, or released
2017-10-27 06:10:37 +02:00
* @param button the button that needs to be clicked or used
* @param event the kind of button gesture that needs to be detected
* @param body code to run when the event is raised
*/
//% help=sensors/beacon/on-event
//% blockId=remotebuttonEvent block="on **remote button** %button|%event"
2017-10-27 06:10:37 +02:00
//% parts="remote"
2017-10-28 18:13:02 +02:00
//% blockNamespace=sensors
2017-10-27 06:10:37 +02:00
//% weight=99 blockGap=8
//% group="Remote Infrared Beacon"
onEvent(ev: ButtonEvent, body: () => void) {
this._button.onEvent(ev, body);
2017-10-27 06:10:37 +02:00
}
/**
* Pause until a remote button event happens
* @param ev the event to wait for
*/
//% help=sensors/beacon/pause-until
//% blockId=remoteButtonPauseUntil block="pause until **remote button** %button|%event"
//% parts="remote"
//% blockNamespace=sensors
//% weight=99 blockGap=8
//% group="Remote Infrared Beacon"
pauseUntil(ev: ButtonEvent) {
this._button.pauseUntil(ev);
}
2017-10-27 06:10:37 +02:00
}
2017-11-30 18:59:28 +01:00
2017-10-27 05:20:24 +02:00
//% fixedInstances
export class InfraredSensor extends internal.UartSensor {
private _channel: InfraredRemoteChannel;
private _proximityThreshold: sensors.ThresholdDetector;
2017-07-10 12:37:14 +02:00
2017-10-24 20:58:52 +02:00
constructor(port: number) {
super(port)
this._channel = InfraredRemoteChannel.Ch0
this._proximityThreshold = new sensors.ThresholdDetector(this._id, 0, 100, 10, 90);
this.setMode(InfraredSensorMode.Proximity);
2017-07-10 12:37:14 +02:00
}
_query() {
if (this.mode == InfraredSensorMode.RemoteControl)
return mapButton(this.getNumber(NumberFormat.UInt8LE, this._channel));
else if (this.mode == InfraredSensorMode.Proximity) {
2017-11-30 18:59:28 +01:00
return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
2017-10-27 05:51:13 +02:00
}
2017-07-10 12:37:14 +02:00
return 0
}
_info(): string {
if (this.mode == InfraredSensorMode.RemoteControl)
return "remote";
else if (this.mode == InfraredSensorMode.Proximity)
return `${this._query()}%`;
return "";
}
2017-07-10 12:37:14 +02:00
_update(prev: number, curr: number) {
if (this.mode == InfraredSensorMode.RemoteControl) {
for (let i = 0; i < __remoteButtons.length; ++i) {
const v = !!(curr & __remoteButtons[i].position);
__remoteButtons[i]._update(v)
2017-10-27 05:51:13 +02:00
}
} else if (this.mode == InfraredSensorMode.Proximity) {
this._proximityThreshold.setLevel(curr);
2017-07-10 12:37:14 +02:00
}
}
2017-07-10 13:47:00 +02:00
_deviceType() {
return DAL.DEVICE_TYPE_IR
2017-07-10 13:47:00 +02:00
}
setMode(m: InfraredSensorMode) {
2017-07-10 12:37:14 +02:00
this._setMode(m)
}
2017-10-27 05:51:13 +02:00
/**
* Register code to run when an object is getting near.
2017-10-27 05:51:13 +02:00
* @param handler the code to run when detected
*/
//% help=sensors/infrared/on-event
//% block="on **infrared** %this|%event"
2017-10-31 05:39:50 +01:00
//% blockId=infraredOn
2017-10-27 05:51:13 +02:00
//% parts="infraredsensor"
2017-10-28 18:13:02 +02:00
//% blockNamespace=sensors
2017-10-27 05:51:13 +02:00
//% weight=100 blockGap=8
//% group="Infrared Sensor"
//% this.fieldEditor="ports"
2017-11-30 18:53:43 +01:00
onEvent(event: InfraredSensorEvent, handler: () => void) {
this._setMode(InfraredSensorMode.Proximity)
2017-11-30 18:59:28 +01:00
control.onEvent(this._id, event, handler);
2017-10-27 05:51:13 +02:00
}
2017-10-27 05:57:18 +02:00
2017-10-31 05:39:50 +01:00
/**
* Wait until the infrared sensor detects something
2017-10-31 05:39:50 +01:00
*/
//% help=sensors/infrared/pause-until
//% block="pause until **infrared** %this| %event"
2017-10-31 17:42:53 +01:00
//% blockId=infraredwait
2017-10-31 05:39:50 +01:00
//% parts="infraredsensor"
//% blockNamespace=sensors
//% weight=99 blockGap=8
2017-11-16 21:41:47 +01:00
//% group="Infrared Sensor"
//% this.fieldEditor="ports"
pauseUntil(event: InfraredSensorEvent) {
this._setMode(InfraredSensorMode.Proximity)
2017-11-30 18:38:04 +01:00
control.waitForEvent(this._id, event);
2017-10-27 05:57:18 +02:00
}
2017-11-30 18:59:28 +01:00
2017-10-24 14:30:05 +02:00
/**
2017-10-27 05:20:24 +02:00
* Get the promixity measured by the infrared sensor, from ``0`` (close) to ``100`` (far)
2017-12-07 07:34:11 +01:00
* @param sensor the infrared sensor
2017-10-24 14:30:05 +02:00
*/
//% help=sensors/infrared/proximity
//% block="**infrared** %this|proximity"
2017-10-27 05:20:24 +02:00
//% blockId=infraredGetProximity
2017-10-24 14:30:05 +02:00
//% parts="infrared"
2017-10-28 18:13:02 +02:00
//% blockNamespace=sensors
//% weight=98 blockGap=8
//% group="Infrared Sensor"
//% this.fieldEditor="ports"
2017-11-30 18:53:43 +01:00
proximity(): number {
this.poke();
this._setMode(InfraredSensorMode.Proximity)
2017-07-10 12:37:14 +02:00
return this.getNumber(NumberFormat.UInt8LE, 0)
}
2017-10-24 14:30:05 +02:00
/**
* Set the remote channel to listen to
* @param channel the channel to listen
2017-10-24 14:30:05 +02:00
*/
2017-10-28 18:13:02 +02:00
//% blockNamespace=sensors
//% blockId=irSetRemoteChannel block="set **infrared** %this|remote channel to %channel"
//% weight=99
//% group="Remote Infrared Beacon"
//% this.fieldEditor="ports"
//% help=sensors/beacon/set-remote-channel
setRemoteChannel(channel: InfraredRemoteChannel) {
this.setMode(InfraredSensorMode.RemoteControl)
channel = Math.clamp(0, 3, channel | 0)
this._channel = channel;
2017-07-10 12:37:14 +02:00
}
/**
* Sets a threshold value
* @param condition the dark or bright light condition
* @param value the value threshold
*/
//% blockId=irSetThreshold block="set **infrared** %this|%condition|to %value"
//% group="Calibration" blockGap=8 weight=49
2018-01-10 20:45:08 +01:00
//% value.min=0 value.max=100
//% this.fieldEditor="ports"
setPromixityThreshold(condition: InfraredSensorEvent, value: number) {
if (condition == InfraredSensorEvent.ObjectNear)
this._proximityThreshold.setLowThreshold(value)
else
this._proximityThreshold.setHighThreshold(value);
}
2018-01-30 17:27:23 +01:00
/**
* Get a threshold value
2018-01-30 17:27:23 +01:00
* @param condition the proximity condition
*/
//% blockId=irGetThreshold block="**infrared** %this|%condition"
//% group="Calibration" weight=49
//% this.fieldEditor="ports"
proximityThreshold(condition: InfraredSensorEvent): number {
return this._proximityThreshold.threshold(<ThresholdState><number>condition);
}
// TODO
private getDirectionAndDistance() {
this.poke();
this._setMode(InfraredSensorMode.Seek)
return this.getNumber(NumberFormat.UInt16LE, this._channel * 2)
}
2017-07-10 12:37:14 +02:00
}
//% fixedInstance whenUsed block="1" jres=icons.port1
export const infrared1: InfraredSensor = new InfraredSensor(1)
2017-10-24 20:58:52 +02:00
//% fixedInstance whenUsed block="2" jres=icons.port2
export const infrared2: InfraredSensor = new InfraredSensor(2)
2017-10-24 20:58:52 +02:00
//% fixedInstance whenUsed block="3" jres=icons.port3
export const infrared3: InfraredSensor = new InfraredSensor(3)
2017-10-24 20:58:52 +02:00
//% fixedInstance whenUsed block="4" jres=icons.port4
export const infrared4: InfraredSensor = new InfraredSensor(4)
2017-07-10 15:26:19 +02:00
2017-10-27 06:10:37 +02:00
/**
* Remote beacon (center) button.
*/
//% whenUsed block="center" weight=95 fixedInstance
export const remoteButtonCenter = __irButton(InfraredRemoteButton.CenterBeacon)
2017-11-30 18:59:28 +01:00
2017-07-10 15:26:19 +02:00
/**
* Remote top-left button.
*/
//% whenUsed block="top left" weight=95 fixedInstance
export const remoteButtonTopLeft = __irButton(InfraredRemoteButton.TopLeft)
2017-07-10 15:26:19 +02:00
/**
* Remote top-right button.
*/
//% whenUsed block="top right" weight=95 fixedInstance
export const remoteButtonTopRight = __irButton(InfraredRemoteButton.TopRight)
2017-07-10 15:26:19 +02:00
/**
* Remote bottom-left button.
*/
//% whenUsed block="bottom left" weight=95 fixedInstance
export const remoteButtonBottomLeft = __irButton(InfraredRemoteButton.BottomLeft)
2017-07-10 15:26:19 +02:00
/**
* Remote bottom-right button.
*/
//% whenUsed block="bottom right" weight=95 fixedInstance
export const remoteButtonBottomRight = __irButton(InfraredRemoteButton.BottomRight)
2017-07-10 12:37:14 +02:00
}