2017-07-10 12:37:14 +02:00
|
|
|
const enum IrSensorMode {
|
|
|
|
None = -1,
|
|
|
|
Proximity = 0,
|
|
|
|
Seek = 1,
|
|
|
|
RemoteControl = 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
const enum IrRemoteChannel {
|
|
|
|
Ch0 = 0, // top
|
|
|
|
Ch1 = 1,
|
|
|
|
Ch2 = 2,
|
|
|
|
Ch3 = 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
const enum IrRemoteButton {
|
|
|
|
None = 0x00,
|
|
|
|
CenterBeacon = 0x01,
|
|
|
|
TopLeft = 0x02,
|
|
|
|
BottomLeft = 0x04,
|
|
|
|
TopRight = 0x08,
|
|
|
|
BottomRight = 0x10,
|
|
|
|
}
|
|
|
|
|
2017-10-31 05:39:50 +01:00
|
|
|
const enum InfraredSensorEvent {
|
|
|
|
//% block="object near"
|
|
|
|
ObjectNear = 1,
|
|
|
|
//% 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 0: return IrRemoteButton.None
|
|
|
|
case 1: return IrRemoteButton.TopLeft
|
|
|
|
case 2: return IrRemoteButton.BottomLeft
|
|
|
|
case 3: return IrRemoteButton.TopRight
|
|
|
|
case 4: return IrRemoteButton.TopRight | IrRemoteButton.BottomRight
|
|
|
|
case 5: return IrRemoteButton.TopLeft | IrRemoteButton.TopRight
|
|
|
|
case 6: return IrRemoteButton.TopLeft | IrRemoteButton.BottomRight
|
|
|
|
case 7: return IrRemoteButton.BottomLeft | IrRemoteButton.TopRight
|
|
|
|
case 8: return IrRemoteButton.BottomLeft | IrRemoteButton.BottomRight
|
|
|
|
case 9: return IrRemoteButton.CenterBeacon
|
|
|
|
case 10: return IrRemoteButton.BottomLeft | IrRemoteButton.TopLeft
|
|
|
|
case 11: return IrRemoteButton.TopRight | IrRemoteButton.BottomRight
|
|
|
|
default: return IrRemoteButton.None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 06:10:37 +02:00
|
|
|
let buttons: RemoteInfraredBeaconButton[]
|
2017-10-24 20:58:52 +02:00
|
|
|
|
2017-10-27 05:20:24 +02:00
|
|
|
function create(ir: InfraredSensor) {
|
2017-10-24 20:58:52 +02:00
|
|
|
// it's created by referencing it
|
|
|
|
}
|
|
|
|
|
2017-10-27 06:10:37 +02:00
|
|
|
export function irButton(id: IrRemoteButton): RemoteInfraredBeaconButton {
|
2017-10-24 20:58:52 +02:00
|
|
|
if (buttons == null) {
|
|
|
|
buttons = []
|
|
|
|
for (let i = 0; i < 5; ++i) {
|
2017-10-28 18:13:02 +02:00
|
|
|
buttons.push(new RemoteInfraredBeaconButton(new brick.Button()))
|
2017-10-24 20:58:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure sensors are up
|
2017-10-27 05:51:13 +02:00
|
|
|
create(infraredSensor1)
|
|
|
|
create(infraredSensor2)
|
|
|
|
create(infraredSensor3)
|
|
|
|
create(infraredSensor4)
|
2017-10-24 20:58:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let num = -1
|
|
|
|
while (id) {
|
|
|
|
id >>= 1;
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
num = Math.clamp(0, buttons.length - 1, num)
|
|
|
|
return buttons[num]
|
|
|
|
}
|
2017-10-27 06:10:37 +02:00
|
|
|
|
|
|
|
//% fixedInstances
|
|
|
|
export class RemoteInfraredBeaconButton extends control.Component {
|
2017-10-28 18:13:02 +02:00
|
|
|
private button: brick.Button;
|
|
|
|
constructor(button: brick.Button) {
|
2017-10-27 06:10:37 +02:00
|
|
|
super();
|
|
|
|
this.button = button;
|
|
|
|
}
|
|
|
|
|
|
|
|
_update(curr: boolean) {
|
|
|
|
this.button._update(curr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a remote button is currently pressed or not.
|
|
|
|
* @param button the remote button to query the request
|
|
|
|
*/
|
|
|
|
//% help=input/remote-infrared-beacon/is-pressed
|
2017-12-19 20:37:33 +01:00
|
|
|
//% block="%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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See if the remote button was pressed again since the last time you checked.
|
|
|
|
* @param button the remote button to query the request
|
|
|
|
*/
|
|
|
|
//% help=input/remote-infrared-beacon/was-pressed
|
2017-12-19 20:37:33 +01:00
|
|
|
//% block="%button|was pressed"
|
2017-10-27 06:10:37 +02:00
|
|
|
//% blockId=remotebuttonWasPressed
|
|
|
|
//% parts="remote"
|
2017-10-28 18:13:02 +02:00
|
|
|
//% blockNamespace=sensors
|
2018-01-03 23:00:08 +01:00
|
|
|
//% weight=80
|
2017-10-27 06:10:37 +02:00
|
|
|
//% group="Remote Infrared Beacon"
|
|
|
|
wasPressed() {
|
|
|
|
return this.button.wasPressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do something when a button or sensor is clicked, up or down
|
|
|
|
* @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=input/remote-infrared-beacon/on-event
|
2017-12-19 20:37:33 +01:00
|
|
|
//% blockId=remotebuttonEvent block="on %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-11-30 18:59:28 +01:00
|
|
|
|
2017-10-27 05:20:24 +02:00
|
|
|
//% fixedInstances
|
|
|
|
export class InfraredSensor extends internal.UartSensor {
|
2017-10-27 05:51:13 +02:00
|
|
|
private channel: IrRemoteChannel;
|
2017-11-30 18:59:28 +01:00
|
|
|
private proximityThreshold: sensors.internal.ThresholdDetector;
|
2017-07-10 12:37:14 +02:00
|
|
|
|
2017-10-24 20:58:52 +02:00
|
|
|
constructor(port: number) {
|
|
|
|
super(port)
|
2017-07-10 12:37:14 +02:00
|
|
|
this.channel = IrRemoteChannel.Ch0
|
2017-11-30 18:59:28 +01:00
|
|
|
this.proximityThreshold = new sensors.internal.ThresholdDetector(this._id, 0, 100, 10, 90);
|
2017-10-24 20:58:52 +02:00
|
|
|
irButton(0) // make sure buttons array is initalized
|
2017-07-10 12:37:14 +02:00
|
|
|
|
2017-10-24 20:58:52 +02:00
|
|
|
// and set the mode, as otherwise button events won't work
|
2017-10-27 05:51:13 +02:00
|
|
|
this.mode = IrSensorMode.RemoteControl;
|
2017-07-10 12:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_query() {
|
|
|
|
if (this.mode == IrSensorMode.RemoteControl)
|
2017-10-27 05:51:13 +02:00
|
|
|
return mapButton(this.getNumber(NumberFormat.UInt8LE, this.channel));
|
|
|
|
else if (this.mode == IrSensorMode.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
|
|
|
|
}
|
|
|
|
|
|
|
|
_update(prev: number, curr: number) {
|
2017-10-27 05:51:13 +02:00
|
|
|
if (this.mode == IrSensorMode.RemoteControl) {
|
|
|
|
for (let i = 0; i < buttons.length; ++i) {
|
|
|
|
let v = !!(curr & (1 << i))
|
2017-10-27 06:10:37 +02:00
|
|
|
buttons[i]._update(v)
|
2017-10-27 05:51:13 +02:00
|
|
|
}
|
2017-11-30 18:59:28 +01:00
|
|
|
} else if (this.mode == IrSensorMode.Proximity) {
|
|
|
|
this.proximityThreshold.setLevel(curr);
|
2017-07-10 12:37:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 13:47:00 +02:00
|
|
|
_deviceType() {
|
2017-07-11 16:18:59 +02:00
|
|
|
return DAL.DEVICE_TYPE_IR
|
2017-07-10 13:47:00 +02:00
|
|
|
}
|
|
|
|
|
2017-07-10 12:37:14 +02:00
|
|
|
setRemoteChannel(c: IrRemoteChannel) {
|
|
|
|
c = Math.clamp(0, 3, c | 0)
|
|
|
|
this.channel = c
|
2017-11-30 18:59:28 +01:00
|
|
|
this.setMode(IrSensorMode.RemoteControl)
|
2017-07-10 12:37:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 20:18:47 +02:00
|
|
|
setMode(m: IrSensorMode) {
|
2017-07-10 12:37:14 +02:00
|
|
|
this._setMode(m)
|
|
|
|
}
|
|
|
|
|
2017-10-27 05:51:13 +02:00
|
|
|
/**
|
2017-10-27 05:57:18 +02:00
|
|
|
* Registers 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
|
|
|
|
*/
|
2017-10-31 05:39:50 +01:00
|
|
|
//% help=input/infrared/on
|
2017-12-19 20:37:33 +01:00
|
|
|
//% block="on %sensor|%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"
|
2017-11-30 18:53:43 +01:00
|
|
|
onEvent(event: InfraredSensorEvent, handler: () => void) {
|
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
|
|
|
/**
|
|
|
|
* Waits for the event to occur
|
|
|
|
*/
|
|
|
|
//% help=input/ultrasonic/wait
|
2017-12-19 20:37:33 +01:00
|
|
|
//% block="pause until %sensor| %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"
|
2017-12-12 19:46:56 +01:00
|
|
|
pauseUntil(event: InfraredSensorEvent) {
|
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
|
|
|
*/
|
2017-10-27 05:20:24 +02:00
|
|
|
//% help=input/infrared/proximity
|
2017-12-19 20:37:33 +01:00
|
|
|
//% block="%sensor|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
|
2017-10-25 00:33:28 +02:00
|
|
|
//% weight=65 blockGap=8
|
2017-10-25 01:52:13 +02:00
|
|
|
//% group="Infrared Sensor"
|
2017-11-30 18:53:43 +01:00
|
|
|
proximity(): number {
|
2017-10-27 05:51:13 +02:00
|
|
|
this._setMode(IrSensorMode.Proximity)
|
2017-07-10 12:37:14 +02:00
|
|
|
return this.getNumber(NumberFormat.UInt8LE, 0)
|
|
|
|
}
|
|
|
|
|
2017-10-24 14:30:05 +02:00
|
|
|
/**
|
|
|
|
* Get the remote commandreceived the infrared sensor.
|
2017-12-07 07:34:11 +01:00
|
|
|
* @param sensor the infrared sensor
|
2017-10-24 14:30:05 +02:00
|
|
|
*/
|
|
|
|
//% help=input/infrared/remote-command
|
2017-12-19 20:37:33 +01:00
|
|
|
//% block="%sensor|remote command"
|
2017-10-24 14:30:05 +02:00
|
|
|
//% blockId=infraredGetRemoteCommand
|
|
|
|
//% parts="infrared"
|
2017-10-28 18:13:02 +02:00
|
|
|
//% blockNamespace=sensors
|
2018-01-03 23:00:08 +01:00
|
|
|
//% weight=65
|
2017-10-25 01:52:13 +02:00
|
|
|
//% group="Infrared Sensor"
|
2017-11-30 18:53:43 +01:00
|
|
|
remoteCommand(): number {
|
2017-10-27 05:51:13 +02:00
|
|
|
this._setMode(IrSensorMode.RemoteControl)
|
2017-07-10 12:37:14 +02:00
|
|
|
return this.getNumber(NumberFormat.UInt8LE, this.channel)
|
|
|
|
}
|
|
|
|
|
2017-10-24 14:30:05 +02:00
|
|
|
// TODO
|
2017-07-10 12:37:14 +02:00
|
|
|
getDirectionAndDistance() {
|
2017-10-27 05:51:13 +02:00
|
|
|
this._setMode(IrSensorMode.Seek)
|
2017-07-10 12:37:14 +02:00
|
|
|
return this.getNumber(NumberFormat.UInt16LE, this.channel * 2)
|
|
|
|
}
|
2018-01-09 21:46:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a threshold value
|
|
|
|
* @param condition the dark or bright light condition
|
|
|
|
* @param value the value threshold
|
|
|
|
*/
|
|
|
|
//% blockId=irSetThreshold block="set %condition|to %value"
|
|
|
|
//% group="Threshold" blockGap=8
|
|
|
|
setThreshold(condition: InfraredSensorEvent, value: number) {
|
|
|
|
if (condition == InfraredSensorEvent.ObjectNear)
|
|
|
|
this.proximityThreshold.setLowThreshold(value)
|
|
|
|
else
|
|
|
|
this.proximityThreshold.setHighThreshold(value);
|
|
|
|
}
|
2017-07-10 12:37:14 +02:00
|
|
|
}
|
|
|
|
|
2017-12-19 20:37:33 +01:00
|
|
|
//% fixedInstance whenUsed block="infrared 1" jres=icons.port1
|
2017-10-27 05:51:13 +02:00
|
|
|
export const infraredSensor1: InfraredSensor = new InfraredSensor(1)
|
2017-10-24 20:58:52 +02:00
|
|
|
|
2017-12-19 20:37:33 +01:00
|
|
|
//% fixedInstance whenUsed block="infrared 2" jres=icons.port2
|
2017-10-27 05:51:13 +02:00
|
|
|
export const infraredSensor2: InfraredSensor = new InfraredSensor(2)
|
2017-10-24 20:58:52 +02:00
|
|
|
|
2017-12-19 20:37:33 +01:00
|
|
|
//% fixedInstance whenUsed block="infrared 3" jres=icons.port3
|
2017-10-27 05:51:13 +02:00
|
|
|
export const infraredSensor3: InfraredSensor = new InfraredSensor(3)
|
2017-10-24 20:58:52 +02:00
|
|
|
|
2017-12-19 20:37:33 +01:00
|
|
|
//% fixedInstance whenUsed block="infrared 4" jres=icons.port4
|
2017-10-27 05:51:13 +02:00
|
|
|
export const infraredSensor4: InfraredSensor = new InfraredSensor(4)
|
2017-07-10 15:26:19 +02:00
|
|
|
|
2017-10-27 06:10:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remote beacon (center) button.
|
|
|
|
*/
|
2017-11-16 22:03:15 +01:00
|
|
|
//% whenUsed block="center" weight=95 fixedInstance
|
2017-10-27 06:10:37 +02:00
|
|
|
export const remoteButtonCenter = irButton(IrRemoteButton.CenterBeacon)
|
2017-11-30 18:59:28 +01:00
|
|
|
|
2017-07-10 15:26:19 +02:00
|
|
|
/**
|
|
|
|
* Remote top-left button.
|
|
|
|
*/
|
2017-11-16 22:03:15 +01:00
|
|
|
//% whenUsed block="top-left" weight=95 fixedInstance
|
2017-10-27 06:10:37 +02:00
|
|
|
export const remoteButtonTopLeft = irButton(IrRemoteButton.TopLeft)
|
2017-07-10 15:26:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remote top-right button.
|
|
|
|
*/
|
2017-11-16 22:03:15 +01:00
|
|
|
//% whenUsed block="top-right" weight=95 fixedInstance
|
2017-10-27 06:10:37 +02:00
|
|
|
export const remoteButtonTopRight = irButton(IrRemoteButton.TopRight)
|
2017-07-10 15:26:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remote bottom-left button.
|
|
|
|
*/
|
2017-11-16 22:03:15 +01:00
|
|
|
//% whenUsed block="bottom-left" weight=95 fixedInstance
|
2017-10-27 06:10:37 +02:00
|
|
|
export const remoteButtonBottomLeft = irButton(IrRemoteButton.BottomLeft)
|
2017-07-10 15:26:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remote bottom-right button.
|
|
|
|
*/
|
2017-11-16 22:03:15 +01:00
|
|
|
//% whenUsed block="bottom-right" weight=95 fixedInstance
|
2017-10-27 06:10:37 +02:00
|
|
|
export const remoteButtonBottomRight = irButton(IrRemoteButton.BottomRight)
|
2017-07-10 12:37:14 +02:00
|
|
|
}
|