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

Experiment: read sensor data with mode informations #60

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,18 @@ export enum BLECharacteristic {
WEDO2_NAME_ID = "00001524-1212-efde-1523-785feabcd123", // "1524"
LPF2_ALL = "00001624-1212-efde-1623-785feabcd123"
}


export enum ValueType {
Int8,
Int16,
Int32,
Float
}

export const VALUE_SIZE = {
[ValueType.Int8]: 1,
[ValueType.Int16]: 2,
[ValueType.Int32]: 4,
[ValueType.Float]: 4
};
21 changes: 20 additions & 1 deletion src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,29 @@ export class Hub extends EventEmitter {
if (mode !== undefined) {
newMode = mode;
}
this._ports[port].mode = newMode;
this._activatePortDevice(this._portLookup(port).value, this._portLookup(port).type, newMode, 0x00, () => {
return resolve();
});
});
}

public subscribeByModeName (port: string, mode?: string) {
const { modes } = this._portLookup(port);
const modeCode = modes.reduce(
(foundMode: number | null, definition, index) => {
return definition.name === mode ? index : foundMode;
},
null
);

if (!modeCode) {
throw new Error('Unknown device mode');
} else {
return this.subscribe(port, modeCode);
}
}

/**
* Unsubscribe to sensor notifications on a given port.
* @method Hub#unsubscribe
Expand All @@ -176,6 +193,7 @@ export class Hub extends EventEmitter {
public unsubscribe (port: string) {
return new Promise((resolve, reject) => {
const mode = this._getModeForDeviceType(this._portLookup(port).type);
this._ports[port].mode = null;
this._deactivatePortDevice(this._portLookup(port).value, this._portLookup(port).type, mode, 0x00, () => {
return resolve();
});
Expand Down Expand Up @@ -268,7 +286,7 @@ export class Hub extends EventEmitter {
if (port.connected) {
port.type = type;
if (this.autoSubscribe) {
this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00);
this.subscribe(port.id, this._getModeForDeviceType(type));
Comment on lines -271 to +289
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changed only to have the exact same result using manual subscribe or autosubscribe.

/**
* Emits when a motor or sensor is attached to the Hub.
* @event Hub#attach
Expand All @@ -279,6 +297,7 @@ export class Hub extends EventEmitter {
}
} else {
port.type = Consts.DeviceType.UNKNOWN;
port.modes = [];
debug(`Port ${port.id} disconnected`);
/**
* Emits when an attached motor or sensor is detached from the Hub.
Expand Down
Loading