Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional modes to TechnicColorSensor #175

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 101 additions & 9 deletions src/devices/techniccolorsensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ export class TechnicColorSensor extends Device {

public receive (message: Buffer) {
const mode = this._mode;
let hue;
let saturation;
let value;
let intensity;

switch (mode) {
case Mode.COLOR:
if (message.length !== 5) {
// if mode of device has not changed to this._mode yet
break;
}
if (message[4] <= 10) {
const color = parseColor(message[4]);

Expand All @@ -33,9 +41,12 @@ export class TechnicColorSensor extends Device {
}
break;

case Mode.REFLECTIVITY:
case Mode.REFLECT:
if (message.length !== 5) {
// if mode of device has not changed to this._mode yet
break;
}
const reflect = message[4];

/**
* Emits when the light reflectivity changes.
* @event TechnicColorSensor#reflect
Expand All @@ -45,9 +56,12 @@ export class TechnicColorSensor extends Device {
this.notify("reflect", { reflect });
break;

case Mode.AMBIENT_LIGHT:
case Mode.AMBIENT:
if (message.length !== 5) {
// if mode of device has not changed to this._mode yet
break;
}
const ambient = message[4];

/**
* Emits when the ambient light changes.
* @event TechnicColorSensor#ambient
Expand All @@ -56,6 +70,76 @@ export class TechnicColorSensor extends Device {
*/
this.notify("ambient", { ambient });
break;

case Mode.RGB_I:
if (this.isWeDo2SmartHub) {
break;
}
if (message.length !== 12) {
// if mode of device has not changed to this._mode yet
break;
}
const red = message.readUInt16LE(4);
const green = message.readUInt16LE(6);
const blue = message.readUInt16LE(8);
intensity = message.readUInt16LE(10);
/**
* Emits when a color sensor is activated. Measured with light on.
* @event TechnicColorSensor#rgbIntensity
* @type {object}
* @param {number} red
* @param {number} green
* @param {number} blue
* @param {number} intensity
*/
this.notify("rgbIntensity", { red, green, blue, intensity });
break;

case Mode.HSV:
if (this.isWeDo2SmartHub) {
break;
}
if (message.length !== 10) {
// if mode of device has not changed to this._mode yet
break;
}
hue = message.readUInt16LE(4);
saturation = message.readUInt16LE(6);
value = message.readUInt16LE(8);
/**
* Emits when a color sensor is activated. Measured with light on.
* @event TechnicColorSensor#hsvIntensity
* @type {object}
* @param {number} hue
* @param {number} saturation
* @param {number} value
*/
this.notify("hsvIntensity", { hue, saturation, value });
break;

case Mode.SHSV:
if (this.isWeDo2SmartHub) {
break;
}
if (message.length !== 12) {
// if mode of device has not changed to this._mode yet
break;
}
hue = message.readUInt16LE(4);
saturation = message.readUInt16LE(6);
value = message.readUInt16LE(8);
intensity = message.readUInt16LE(10);
/**
* Emits when a color sensor is activated. Measured with light off.
* @event TechnicColorSensor#hsvAmbient
* @type {object}
* @param {number} hue
* @param {number} saturation
* @param {number} value
* @param {number} intensity
*/
this.notify("hsvAmbient", { hue, saturation, value, intensity });
break;
}
}

Expand All @@ -68,19 +152,27 @@ export class TechnicColorSensor extends Device {
* @returns {Promise} Resolved upon successful issuance of the command.
*/
public setBrightness (firstSegment: number, secondSegment: number, thirdSegment: number) {
this.writeDirect(0x03, Buffer.from([firstSegment, secondSegment, thirdSegment]));
this.subscribe(Mode.LIGHT); // other modes use different light setting
return this.writeDirect(Mode.LIGHT, Buffer.from([firstSegment, secondSegment, thirdSegment]));
}

}

export enum Mode {
COLOR = 0x00,
REFLECTIVITY = 0x01,
AMBIENT_LIGHT = 0x02
REFLECT = 0x01,
AMBIENT = 0x02,
LIGHT = 0x03,
RGB_I = 0x05,
HSV = 0x06,
SHSV = 0x07
}

export const ModeMap: {[event: string]: number} = {
"color": Mode.COLOR,
"reflect": Mode.REFLECTIVITY,
"ambient": Mode.AMBIENT_LIGHT
"reflect": Mode.REFLECT,
"ambient": Mode.AMBIENT,
"rgbIntensity": Mode.RGB_I,
"hsvIntensity": Mode.HSV,
"hsvAmbient": Mode.SHSV
};