Merge pull request #80 from nathankellenicki/feature/power-functions-ir

[feat] Power Functions IR
This commit is contained in:
Nathan Kellenicki 2020-04-04 17:16:17 -07:00 committed by GitHub
commit 6343c97175
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 704 additions and 604 deletions

1170
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -16,21 +16,21 @@
"author": "Nathan Kellenicki <nathan@kellenicki.com>", "author": "Nathan Kellenicki <nathan@kellenicki.com>",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"compare-versions": "^3.5.1", "compare-versions": "^3.6.0",
"debug": "^4.1.1", "debug": "^4.1.1",
"@abandonware/noble": "1.9.2-5" "@abandonware/noble": "1.9.2-8"
}, },
"devDependencies": { "devDependencies": {
"@types/debug": "4.1.5", "@types/debug": "4.1.5",
"@types/node": "^13.7.0", "@types/node": "^13.11.0",
"@types/web-bluetooth": "0.0.5", "@types/web-bluetooth": "0.0.5",
"ink-docstrap": "^1.3.2", "ink-docstrap": "^1.3.2",
"jsdoc": "^3.6.3", "jsdoc": "^3.6.3",
"jsdoc-to-markdown": "^5.0.3", "jsdoc-to-markdown": "^5.0.3",
"ts-loader": "^6.2.1", "ts-loader": "^6.2.2",
"tslint": "^6.0.0", "tslint": "^6.1.1",
"typescript": "^3.7.5", "typescript": "^3.8.3",
"webpack": "^4.41.5", "webpack": "^4.42.1",
"webpack-cli": "^3.3.10" "webpack-cli": "^3.3.11"
} }
} }

View File

@ -79,11 +79,128 @@ 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 "Extended toggle address command"
message[0] = ((channel - 1) << 4) + (address << 3);
message[1] = 6 << 4;
return this.sendPFIRMessage(message);
}
/**
* Set the power of a Power Functions motor via IR
* @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: Output, 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) + (address << 3) + (output === "RED" ? 4 : 5);
message[1] = this._pfPowerToPWM(power) << 4;
return this.sendPFIRMessage(message);
}
/**
* 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#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, powerBlue: number, powerRed: number) {
let address = 0;
if (channel > 4) {
channel -= 4;
address = 1;
}
const message = Buffer.alloc(2);
// Send "Combo PWM mode"
message[0] = (((channel - 1) + 4 + (address << 3)) << 4) + this._pfPowerToPWM(powerBlue);
message[1] = this._pfPowerToPWM(powerRed) << 4;
return this.sendPFIRMessage(message);
}
/**
* Send a raw Power Functions IR command
* @method ColorDistanceSensor#sendPFIRMessage
* @param {Buffer} message 2 byte payload making up a Power Functions protocol command. NOTE: Only specify nibbles 1-3, nibble 4 should be zeroed.
* @returns {Promise} Resolved upon successful issuance of the command.
*/
public sendPFIRMessage (message: Buffer) {
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);
}
}
/**
* Set the color of the LED on the sensor via a color value.
* @method ColorDistanceSensor#setColor
* @param {Color} color
* @returns {Promise} Resolved upon successful issuance of the command.
*/
public setColor (color: number | boolean) {
return new Promise((resolve, reject) => {
if (color === false) {
color = 0;
}
if (this.isWeDo2SmartHub) {
throw new Error("Setting LED color is not available on the WeDo 2.0 Smart Hub");
} else {
this.subscribe(Mode.LED);
this.writeDirect(0x05, Buffer.from([color]));
}
return resolve();
});
}
private _pfPowerToPWM (power: number) {
return power & 15;
}
} }
export enum Mode { export enum Mode {
COLOR = 0x00, COLOR = 0x00,
DISTANCE = 0x01, DISTANCE = 0x01,
LED = 0x05,
PF_IR = 0x07,
COLOR_AND_DISTANCE = 0x08 COLOR_AND_DISTANCE = 0x08
} }
@ -92,3 +209,8 @@ export const ModeMap: {[event: string]: number} = {
"distance": Mode.DISTANCE, "distance": Mode.DISTANCE,
"colorAndDistance": Mode.COLOR_AND_DISTANCE "colorAndDistance": Mode.COLOR_AND_DISTANCE
}; };
export enum Output {
RED = "RED",
BLUE = "BLUE"
}