refactoring IR
This commit is contained in:
parent
6b44352839
commit
282134f5dc
@ -32,15 +32,18 @@
|
|||||||
"input.ColorSensor.reflectedLight": "Get current reflected light value from the color sensor.",
|
"input.ColorSensor.reflectedLight": "Get current reflected light value from the color sensor.",
|
||||||
"input.GyroSensor.angle": "Get the current angle from the gyroscope.",
|
"input.GyroSensor.angle": "Get the current angle from the gyroscope.",
|
||||||
"input.GyroSensor.rate": "Get the current rotation rate from the gyroscope.",
|
"input.GyroSensor.rate": "Get the current rotation rate from the gyroscope.",
|
||||||
|
"input.InfraredSensor.onObjectNear": "Registers code to run when an object is getting near",
|
||||||
|
"input.InfraredSensor.onObjectNear|param|distance": "the proximity, eg: 10",
|
||||||
|
"input.InfraredSensor.onObjectNear|param|handler": "the code to run when detected",
|
||||||
"input.InfraredSensor.proximity": "Get the promixity measured by the infrared sensor, from ``0`` (close) to ``100`` (far)",
|
"input.InfraredSensor.proximity": "Get the promixity measured by the infrared sensor, from ``0`` (close) to ``100`` (far)",
|
||||||
"input.InfraredSensor.remoteCommand": "Get the remote commandreceived the infrared sensor.",
|
"input.InfraredSensor.remoteCommand": "Get the remote commandreceived the infrared sensor.",
|
||||||
"input.TouchSensor.isTouched": "Check if touch sensor is touched.",
|
"input.TouchSensor.isTouched": "Check if touch sensor is touched.",
|
||||||
"input.TouchSensor.onEvent": "Do something when a touch sensor is touched...",
|
"input.TouchSensor.onEvent": "Do something when a touch sensor is touched...",
|
||||||
"input.TouchSensor.onEvent|param|body": "code to run when the event is raised",
|
"input.TouchSensor.onEvent|param|body": "code to run when the event is raised",
|
||||||
"input.UltraSonicSensor.distance": "Gets the distance from the sonar in millimeters",
|
"input.UltraSonicSensor.distance": "Gets the distance from the sonar in millimeters",
|
||||||
"input.UltraSonicSensor.onObject": "Registers code to run when the given color is close",
|
"input.UltraSonicSensor.onObjectNear": "Registers code to run when the given color is close",
|
||||||
"input.UltraSonicSensor.onObject|param|distance": "the distance in centimeters when an object is close, eg: 10",
|
"input.UltraSonicSensor.onObjectNear|param|distance": "the distance in centimeters when an object is close, eg: 10",
|
||||||
"input.UltraSonicSensor.onObject|param|handler": "the code to run when detected",
|
"input.UltraSonicSensor.onObjectNear|param|handler": "the code to run when detected",
|
||||||
"input.buttonDown": "Down button on the EV3 Brick.",
|
"input.buttonDown": "Down button on the EV3 Brick.",
|
||||||
"input.buttonEnter": "Enter button on the EV3 Brick.",
|
"input.buttonEnter": "Enter button on the EV3 Brick.",
|
||||||
"input.buttonLeft": "Left button on the EV3 Brick.",
|
"input.buttonLeft": "Left button on the EV3 Brick.",
|
||||||
|
@ -25,6 +25,8 @@
|
|||||||
"Output.B|block": "B",
|
"Output.B|block": "B",
|
||||||
"Output.C|block": "C",
|
"Output.C|block": "C",
|
||||||
"Output.D|block": "D",
|
"Output.D|block": "D",
|
||||||
|
"PromixityEvent.ObjectDetected|block": "object detected",
|
||||||
|
"PromixityEvent.ObjectNear|block": "object near",
|
||||||
"TouchSensorEvent.Bumped|block": "bumped",
|
"TouchSensorEvent.Bumped|block": "bumped",
|
||||||
"TouchSensorEvent.Pressed|block": "pressed",
|
"TouchSensorEvent.Pressed|block": "pressed",
|
||||||
"TouchSensorEvent.Released|block": "released",
|
"TouchSensorEvent.Released|block": "released",
|
||||||
@ -39,12 +41,13 @@
|
|||||||
"input.ColorSensor.reflectedLight|block": "%color| reflected light",
|
"input.ColorSensor.reflectedLight|block": "%color| reflected light",
|
||||||
"input.GyroSensor.angle|block": "%sensor|angle",
|
"input.GyroSensor.angle|block": "%sensor|angle",
|
||||||
"input.GyroSensor.rate|block": "%sensor|rotation rate",
|
"input.GyroSensor.rate|block": "%sensor|rotation rate",
|
||||||
|
"input.InfraredSensor.onObjectNear|block": "on %sensor|object within %distance",
|
||||||
"input.InfraredSensor.proximity|block": "%infrared|proximity",
|
"input.InfraredSensor.proximity|block": "%infrared|proximity",
|
||||||
"input.InfraredSensor.remoteCommand|block": "%infrared|remote command",
|
"input.InfraredSensor.remoteCommand|block": "%infrared|remote command",
|
||||||
"input.TouchSensor.isTouched|block": "%sensor|is touched",
|
"input.TouchSensor.isTouched|block": "%sensor|is touched",
|
||||||
"input.TouchSensor.onEvent|block": "on %sensor|%event",
|
"input.TouchSensor.onEvent|block": "on %sensor|%event",
|
||||||
"input.UltraSonicSensor.distance|block": "%sensor|distance",
|
"input.UltraSonicSensor.distance|block": "%sensor|distance",
|
||||||
"input.UltraSonicSensor.onObject|block": "on %sensor|object within %distance|cm",
|
"input.UltraSonicSensor.onObjectNear|block": "on %sensor|object within %distance|cm",
|
||||||
"input.buttonDown|block": "brick button down",
|
"input.buttonDown|block": "brick button down",
|
||||||
"input.buttonEnter|block": "brick button enter",
|
"input.buttonEnter|block": "brick button enter",
|
||||||
"input.buttonLeft|block": "brick button left",
|
"input.buttonLeft|block": "brick button left",
|
||||||
@ -58,6 +61,10 @@
|
|||||||
"input.gyro2|block": "gyro sensor 2",
|
"input.gyro2|block": "gyro sensor 2",
|
||||||
"input.gyro3|block": "gyro sensor 3",
|
"input.gyro3|block": "gyro sensor 3",
|
||||||
"input.gyro4|block": "gyro sensor 4",
|
"input.gyro4|block": "gyro sensor 4",
|
||||||
|
"input.infraredSensor1|block": "infrared sensor 1",
|
||||||
|
"input.infraredSensor2|block": "infrared sensor 2",
|
||||||
|
"input.infraredSensor3|block": "infrared sensor 3",
|
||||||
|
"input.infraredSensor4|block": "infrared sensor 4",
|
||||||
"input.remoteBottomLeft|block": "remote bottom-left",
|
"input.remoteBottomLeft|block": "remote bottom-left",
|
||||||
"input.remoteBottomRight|block": "remote bottom-right",
|
"input.remoteBottomRight|block": "remote bottom-right",
|
||||||
"input.remoteCenter|block": "remote center",
|
"input.remoteCenter|block": "remote center",
|
||||||
|
@ -54,10 +54,10 @@ namespace input {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// make sure sensors are up
|
// make sure sensors are up
|
||||||
create(infrared1)
|
create(infraredSensor1)
|
||||||
create(infrared2)
|
create(infraredSensor2)
|
||||||
create(infrared3)
|
create(infraredSensor3)
|
||||||
create(infrared4)
|
create(infraredSensor4)
|
||||||
}
|
}
|
||||||
|
|
||||||
let num = -1
|
let num = -1
|
||||||
@ -68,30 +68,43 @@ namespace input {
|
|||||||
num = Math.clamp(0, buttons.length - 1, num)
|
num = Math.clamp(0, buttons.length - 1, num)
|
||||||
return buttons[num]
|
return buttons[num]
|
||||||
}
|
}
|
||||||
|
|
||||||
//% fixedInstances
|
//% fixedInstances
|
||||||
export class InfraredSensor extends internal.UartSensor {
|
export class InfraredSensor extends internal.UartSensor {
|
||||||
private channel: IrRemoteChannel
|
private channel: IrRemoteChannel;
|
||||||
|
private proximityThreshold: number;
|
||||||
|
|
||||||
constructor(port: number) {
|
constructor(port: number) {
|
||||||
super(port)
|
super(port)
|
||||||
this.channel = IrRemoteChannel.Ch0
|
this.channel = IrRemoteChannel.Ch0
|
||||||
|
this.proximityThreshold = 10;
|
||||||
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
|
||||||
this.mode = IrSensorMode.RemoteControl
|
this.mode = IrSensorMode.RemoteControl;
|
||||||
}
|
}
|
||||||
|
|
||||||
_query() {
|
_query() {
|
||||||
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) {
|
||||||
|
const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
|
||||||
|
return d < this.proximityThreshold ? PromixityEvent.ObjectNear
|
||||||
|
: d > this.proximityThreshold + 5 ? PromixityEvent.ObjectDetected
|
||||||
|
: 0;
|
||||||
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
_update(prev: number, curr: number) {
|
_update(prev: number, curr: number) {
|
||||||
for (let i = 0; i < buttons.length; ++i) {
|
if (this.mode == IrSensorMode.RemoteControl) {
|
||||||
let v = !!(curr & (1 << i))
|
for (let i = 0; i < buttons.length; ++i) {
|
||||||
buttons[i].update(v)
|
let v = !!(curr & (1 << i))
|
||||||
|
buttons[i].update(v)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (curr)
|
||||||
|
control.raiseEvent(this._id, curr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,13 +115,33 @@ namespace input {
|
|||||||
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) {
|
||||||
this._setMode(m)
|
this._setMode(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers code to run when an object is getting near
|
||||||
|
* @param distance the proximity, eg: 10
|
||||||
|
* @param handler the code to run when detected
|
||||||
|
*/
|
||||||
|
//% help=input/infrared/on-object-near
|
||||||
|
//% block="on %sensor|object within %distance"
|
||||||
|
//% blockId=infraredOnObjectNear
|
||||||
|
//% parts="infraredsensor"
|
||||||
|
//% blockNamespace=input
|
||||||
|
//% weight=100 blockGap=8
|
||||||
|
//% distance.min=1 distance.max=95
|
||||||
|
//% group="Infrared Sensor"
|
||||||
|
onObjectNear(distance: number, handler: () => void) {
|
||||||
|
this.proximityThreshold = Math.max(1, Math.min(95, distance));
|
||||||
|
control.onEvent(this._id, PromixityEvent.ObjectNear, handler);
|
||||||
|
if (this.proximity() == PromixityEvent.ObjectNear)
|
||||||
|
control.runInBackground(handler);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
@ -121,7 +154,7 @@ namespace input {
|
|||||||
//% weight=65 blockGap=8
|
//% weight=65 blockGap=8
|
||||||
//% group="Infrared Sensor"
|
//% group="Infrared Sensor"
|
||||||
proximity() {
|
proximity() {
|
||||||
this.setMode(IrSensorMode.Proximity)
|
this._setMode(IrSensorMode.Proximity)
|
||||||
return this.getNumber(NumberFormat.UInt8LE, 0)
|
return this.getNumber(NumberFormat.UInt8LE, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,28 +170,28 @@ namespace input {
|
|||||||
//% weight=65 blockGap=8
|
//% weight=65 blockGap=8
|
||||||
//% group="Infrared Sensor"
|
//% group="Infrared Sensor"
|
||||||
remoteCommand() {
|
remoteCommand() {
|
||||||
this.setMode(IrSensorMode.RemoteControl)
|
this._setMode(IrSensorMode.RemoteControl)
|
||||||
return this.getNumber(NumberFormat.UInt8LE, this.channel)
|
return this.getNumber(NumberFormat.UInt8LE, this.channel)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
getDirectionAndDistance() {
|
getDirectionAndDistance() {
|
||||||
this.setMode(IrSensorMode.Seek)
|
this._setMode(IrSensorMode.Seek)
|
||||||
return this.getNumber(NumberFormat.UInt16LE, this.channel * 2)
|
return this.getNumber(NumberFormat.UInt16LE, this.channel * 2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//% fixedInstance whenUsed
|
//% fixedInstance whenUsed block="infrared sensor 1"
|
||||||
export const infrared1: InfraredSensor = new InfraredSensor(1)
|
export const infraredSensor1: InfraredSensor = new InfraredSensor(1)
|
||||||
|
|
||||||
//% fixedInstance whenUsed
|
//% fixedInstance whenUsed block="infrared sensor 2"
|
||||||
export const infrared2: InfraredSensor = new InfraredSensor(2)
|
export const infraredSensor2: InfraredSensor = new InfraredSensor(2)
|
||||||
|
|
||||||
//% fixedInstance whenUsed
|
//% fixedInstance whenUsed block="infrared sensor 3"
|
||||||
export const infrared3: InfraredSensor = new InfraredSensor(3)
|
export const infraredSensor3: InfraredSensor = new InfraredSensor(3)
|
||||||
|
|
||||||
//% fixedInstance whenUsed
|
//% fixedInstance whenUsed block="infrared sensor 4"
|
||||||
export const infrared4: InfraredSensor = new InfraredSensor(4)
|
export const infraredSensor4: InfraredSensor = new InfraredSensor(4)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remote top-left button.
|
* Remote top-left button.
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
//% color="#B4009E" weight=98 icon="\uf192"
|
//% color="#B4009E" weight=98 icon="\uf192"
|
||||||
//% groups='["Touch Sensor", "Color Sensor", "Ultrasonic Sensor", "Infrared Sensor", "Remote", "Brick", "Gyro Sensor"]'
|
//% groups='["Brick", "Touch Sensor", "Color Sensor", "Ultrasonic Sensor", "Infrared Sensor", "Remote", "Gyro Sensor"]'
|
||||||
namespace input {
|
namespace input {
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
enum UltrasonicEvent {
|
const enum PromixityEvent {
|
||||||
ObjectClose = 1,
|
//% block="object near"
|
||||||
|
ObjectNear = 1,
|
||||||
|
//% block="object detected"
|
||||||
ObjectDetected = 2
|
ObjectDetected = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -7,11 +9,11 @@ namespace input {
|
|||||||
|
|
||||||
//% fixedInstances
|
//% fixedInstances
|
||||||
export class UltraSonicSensor extends internal.UartSensor {
|
export class UltraSonicSensor extends internal.UartSensor {
|
||||||
private threshold: number;
|
private promixityThreshold: number;
|
||||||
|
|
||||||
constructor(port: number) {
|
constructor(port: number) {
|
||||||
super(port)
|
super(port)
|
||||||
this.threshold = 10;
|
this.promixityThreshold = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
_deviceType() {
|
_deviceType() {
|
||||||
@ -20,8 +22,8 @@ namespace input {
|
|||||||
|
|
||||||
_query(): number {
|
_query(): number {
|
||||||
const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
|
const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
|
||||||
return d < this.threshold ? UltrasonicEvent.ObjectClose
|
return d < this.promixityThreshold ? PromixityEvent.ObjectNear
|
||||||
: d > this.threshold + 5 ? UltrasonicEvent.ObjectDetected
|
: d > this.promixityThreshold + 5 ? PromixityEvent.ObjectDetected
|
||||||
: 0;
|
: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,18 +37,18 @@ namespace input {
|
|||||||
* @param distance the distance in centimeters when an object is close, eg: 10
|
* @param distance the distance in centimeters when an object is close, eg: 10
|
||||||
* @param handler the code to run when detected
|
* @param handler the code to run when detected
|
||||||
*/
|
*/
|
||||||
//% help=input/ultrasonic/on-object
|
//% help=input/ultrasonic/on-object-near
|
||||||
//% block="on %sensor|object within %distance|cm"
|
//% block="on %sensor|object within %distance|cm"
|
||||||
//% blockId=ultrasonicOnObjectClose
|
//% blockId=ultrasonicOnObjectClose
|
||||||
//% parts="ultrasonicsensor"
|
//% parts="infraredsensor"
|
||||||
//% blockNamespace=input
|
//% blockNamespace=input
|
||||||
//% weight=100 blockGap=8
|
//% weight=100 blockGap=8
|
||||||
//% group="Ultrasonic Sensor"
|
//% group="Ultrasonic Sensor"
|
||||||
onObject(distance: number, handler: () => void) {
|
//% distance.min=1 distance.max=250
|
||||||
this.threshold = Math.max(1, Math.min(95, distance));
|
onObjectNear(distance: number, handler: () => void) {
|
||||||
control.onEvent(this._id, UltrasonicEvent.ObjectClose, handler);
|
this.promixityThreshold = Math.max(1, Math.min(95, distance));
|
||||||
this._setMode(0)
|
control.onEvent(this._id, PromixityEvent.ObjectNear, handler);
|
||||||
if (this._query() == UltrasonicEvent.ObjectClose)
|
if (this.distance() == PromixityEvent.ObjectNear)
|
||||||
control.runInBackground(handler);
|
control.runInBackground(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user