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 1 commit
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,30 @@ 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 = "") {
aileo marked this conversation as resolved.
Show resolved Hide resolved
aileo marked this conversation as resolved.
Show resolved Hide resolved
const { value, type, modes } = this._portLookup(port);

return new Promise((resolve, reject) => {
this._activatePortDevice(
value,
type,
modes.reduce(
(newMode, definition, index) => definition.name === mode ? index : newMode,
this._getModeForDeviceType(type)
aileo marked this conversation as resolved.
Show resolved Hide resolved
),
0x00,
() => resolve()
);
});
}

/**
* Unsubscribe to sensor notifications on a given port.
* @method Hub#unsubscribe
Expand All @@ -176,6 +194,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 +287,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 Down
Loading