PF extended channel mode

This commit is contained in:
Nathan Kellenicki 2020-03-24 19:09:48 -07:00
parent 32b7ba3d45
commit 0ec0692aad

View File

@ -10,6 +10,8 @@ import * as Consts from "../consts";
*/
export class ColorDistanceSensor extends Device {
private _pfToggleBit = 0;
constructor (hub: IDeviceInterface, portId: number) {
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
* @method ColorDistanceSensor#sendPFIRMessage
* @method ColorDistanceSensor#setPFPower
* @param {number} channel Channel number, between 1-4
* @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.
* @returns {Promise} Resolved upon successful issuance of the command.
*/
public setPFPower (channel: number, output: string, power: 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) + (output === "RED" ? 4 : 5);
message[0] = ((channel - 1) << 4) + (address << 3) + (output === "RED" ? 4 : 5);
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
*
* 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} 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.
* @returns {Promise} Resolved upon successful issuance of the command.
*/
public startPFMotors (channel: number, powerA: number, powerB: number) {
let address = 0;
if (channel > 4) {
channel -= 4;
address = 1;
}
const message = Buffer.alloc(2);
// 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;
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.
*/
public sendPFIRMessage (message: Buffer) {
const payload = Buffer.alloc(2);
payload[0] = (message[0] << 4) + (message[1] >> 4);
payload[1] = message[0] >> 4;
this.subscribe(Mode.PF_IR);
this.writeDirect(0x07, payload);
if (this.isWeDo2SmartHub) {
throw new Error("Power Functions IR is not available on the WeDo 2.0 Smart Hub");
} else {
const payload = Buffer.alloc(2);
payload[0] = (message[0] << 4) + (message[1] >> 4);
payload[1] = message[0] >> 4;
this.subscribe(Mode.PF_IR);
return this.writeDirect(0x07, payload);
}
}