PF extended channel mode
This commit is contained in:
parent
32b7ba3d45
commit
0ec0692aad
@ -10,6 +10,8 @@ import * as Consts from "../consts";
|
|||||||
*/
|
*/
|
||||||
export class ColorDistanceSensor extends Device {
|
export class ColorDistanceSensor extends Device {
|
||||||
|
|
||||||
|
private _pfToggleBit = 0;
|
||||||
|
|
||||||
constructor (hub: IDeviceInterface, portId: number) {
|
constructor (hub: IDeviceInterface, portId: number) {
|
||||||
super(hub, portId, ModeMap, Consts.DeviceType.COLOR_DISTANCE_SENSOR);
|
super(hub, portId, ModeMap, Consts.DeviceType.COLOR_DISTANCE_SENSOR);
|
||||||
}
|
}
|
||||||
@ -80,20 +82,47 @@ export class ColorDistanceSensor extends Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Switches the IR receiver into extended channel mode. After setting this, use channels 5-8 instead of 1-4 for this receiver.
|
||||||
|
*
|
||||||
|
* NOTE: Calling this with channel 5-8 with switch off extended channel mode for this receiver.
|
||||||
|
* @method ColorDistanceSensor#setPFExtendedChannel
|
||||||
|
* @param {number} channel Channel number, between 1-8
|
||||||
|
* @returns {Promise} Resolved upon successful issuance of the command.
|
||||||
|
*/
|
||||||
|
public setPFExtendedChannel (channel: number) {
|
||||||
|
let address = 0;
|
||||||
|
if (channel >= 4) {
|
||||||
|
channel -= 4;
|
||||||
|
address = 1;
|
||||||
|
}
|
||||||
|
const message = Buffer.alloc(2);
|
||||||
|
// Send "Single output mode"
|
||||||
|
message[0] = ((channel - 1) << 4) + (address << 3);
|
||||||
|
message[1] = 6;
|
||||||
|
return this.sendPFIRMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the power of a Power Functions motor via IR
|
* Set the power of a Power Functions motor via IR
|
||||||
* @method ColorDistanceSensor#sendPFIRMessage
|
* @method ColorDistanceSensor#setPFPower
|
||||||
* @param {number} channel Channel number, between 1-4
|
* @param {number} channel Channel number, between 1-4
|
||||||
* @param {string} output Outport port, "RED" (A) or "BLUE" (B)
|
* @param {string} output Outport port, "RED" (A) or "BLUE" (B)
|
||||||
* @param {number} power -7 (full reverse) to 7 (full forward). 0 is stop. 8 is brake.
|
* @param {number} power -7 (full reverse) to 7 (full forward). 0 is stop. 8 is brake.
|
||||||
* @returns {Promise} Resolved upon successful issuance of the command.
|
* @returns {Promise} Resolved upon successful issuance of the command.
|
||||||
*/
|
*/
|
||||||
public setPFPower (channel: number, output: string, power: number) {
|
public setPFPower (channel: number, output: string, power: number) {
|
||||||
|
let address = 0;
|
||||||
|
if (channel > 4) {
|
||||||
|
channel -= 4;
|
||||||
|
address = 1;
|
||||||
|
}
|
||||||
const message = Buffer.alloc(2);
|
const message = Buffer.alloc(2);
|
||||||
// Send "Single output mode"
|
// Send "Single output mode"
|
||||||
message[0] = ((channel - 1) << 4) + (output === "RED" ? 4 : 5);
|
message[0] = ((channel - 1) << 4) + (address << 3) + (output === "RED" ? 4 : 5);
|
||||||
message[1] = this._pfPowerToPWM(power) << 4;
|
message[1] = this._pfPowerToPWM(power) << 4;
|
||||||
this.sendPFIRMessage(message);
|
return this.sendPFIRMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -101,18 +130,23 @@ export class ColorDistanceSensor extends Device {
|
|||||||
* Start Power Functions motors running via IR
|
* Start Power Functions motors running via IR
|
||||||
*
|
*
|
||||||
* NOTE: This command is designed for bang-bang style operation. To keep the motors running, the sensor needs to be within range of the IR receiver constantly.
|
* NOTE: This command is designed for bang-bang style operation. To keep the motors running, the sensor needs to be within range of the IR receiver constantly.
|
||||||
* @method ColorDistanceSensor#sendPFIRMessage
|
* @method ColorDistanceSensor#startPFMotors
|
||||||
* @param {Buffer} channel Channel number, between 1-4
|
* @param {Buffer} channel Channel number, between 1-4
|
||||||
* @param {Buffer} powerA -7 (full reverse) to 7 (full forward). 0 is stop. 8 is brake.
|
* @param {Buffer} powerA -7 (full reverse) to 7 (full forward). 0 is stop. 8 is brake.
|
||||||
* @param {Buffer} powerB -7 (full reverse) to 7 (full forward). 0 is stop. 8 is brake.
|
* @param {Buffer} powerB -7 (full reverse) to 7 (full forward). 0 is stop. 8 is brake.
|
||||||
* @returns {Promise} Resolved upon successful issuance of the command.
|
* @returns {Promise} Resolved upon successful issuance of the command.
|
||||||
*/
|
*/
|
||||||
public startPFMotors (channel: number, powerA: number, powerB: number) {
|
public startPFMotors (channel: number, powerA: number, powerB: number) {
|
||||||
|
let address = 0;
|
||||||
|
if (channel > 4) {
|
||||||
|
channel -= 4;
|
||||||
|
address = 1;
|
||||||
|
}
|
||||||
const message = Buffer.alloc(2);
|
const message = Buffer.alloc(2);
|
||||||
// Send "Combo PWD mode"
|
// Send "Combo PWD mode"
|
||||||
message[0] = (((channel - 1) + 4) << 4) + this._pfPowerToPWM(powerA);
|
message[0] = (((channel - 1) + 4 + (address << 3)) << 4) + this._pfPowerToPWM(powerA);
|
||||||
message[1] += this._pfPowerToPWM(powerB) << 4;
|
message[1] += this._pfPowerToPWM(powerB) << 4;
|
||||||
this.sendPFIRMessage(message);
|
return this.sendPFIRMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -123,11 +157,15 @@ export class ColorDistanceSensor extends Device {
|
|||||||
* @returns {Promise} Resolved upon successful issuance of the command.
|
* @returns {Promise} Resolved upon successful issuance of the command.
|
||||||
*/
|
*/
|
||||||
public sendPFIRMessage (message: Buffer) {
|
public sendPFIRMessage (message: Buffer) {
|
||||||
const payload = Buffer.alloc(2);
|
if (this.isWeDo2SmartHub) {
|
||||||
payload[0] = (message[0] << 4) + (message[1] >> 4);
|
throw new Error("Power Functions IR is not available on the WeDo 2.0 Smart Hub");
|
||||||
payload[1] = message[0] >> 4;
|
} else {
|
||||||
this.subscribe(Mode.PF_IR);
|
const payload = Buffer.alloc(2);
|
||||||
this.writeDirect(0x07, payload);
|
payload[0] = (message[0] << 4) + (message[1] >> 4);
|
||||||
|
payload[1] = message[0] >> 4;
|
||||||
|
this.subscribe(Mode.PF_IR);
|
||||||
|
return this.writeDirect(0x07, payload);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user