A partial implementation of the LEGO wireless protocol using JavaScript.
The documentation of the LEGO wireless protocol can be found here.
Using yarn
:
yarn add lego-wireless-protocol
Using npm
:
npm install lego-wireless-protocol --save
import {parseIncomingMessage} from 'lego-wireless-protocol';
const incomingMessage = parseIncomingMessage(
Uint8Array.from([9, 0, 4, 16, 2, 39, 0, 0, 1]).buffer
);
assert.deepEqual(incomingMessage, {
messageType: 'HubAttachedIo',
port: {portType: 'External', portId: 16},
eventType: 'HubAttachedVirtualIo',
ioType: 'InternalMotorWithTacho',
portA: {portType: 'External', portId: 0},
portB: {portType: 'External', portId: 1}
});
import {serializeOutgoingMessage} from 'lego-wireless-protocol';
const data = serializeOutgoingMessage({
messageType: 'PortInformationRequest',
portId: 0,
portInformationRequestType: 'ModeInfo'
});
assert.deepEqual([...new Uint8Array(data)], [5, 0, 33, 0, 1]);
function parseIncomingMessage(data: ArrayBuffer): IncomingMessage;
function serializeOutgoingMessage(
outgoingMessage: OutgoingMessage
): ArrayBuffer;
type IncomingMessage =
| HubAttachedIoIncomingMessage
| ErrorIncomingMessage
| PortInformationIncomingMessage
| PortModeInformationIncomingMessage
| PortValueIncomingMessage
| PortInputFormatIncomingMessage
| UnknownIncomingMessage;
Show Type
type HubAttachedIoIncomingMessage = {
readonly messageType: 'HubAttachedIo';
} & (
| {
readonly port: Port;
readonly eventType: 'HubAttachedIo';
readonly ioType: IoType;
}
| {
readonly port: Port;
readonly eventType: 'HubAttachedVirtualIo';
readonly ioType: IoType;
readonly portA: Port;
readonly portB: Port;
}
| {
readonly port: Port;
readonly eventType: 'HubDetachedIo';
}
);
interface Port {
readonly portType: 'External' | 'Internal' | 'Reserved';
readonly portId: number;
}
type IoType =
| 'Motor'
| 'SystemTrainMotor'
| 'Button'
| 'LedLight'
| 'Voltage'
| 'Current'
| 'PiezoTone'
| 'RgbLight'
| 'ExternalTiltSensor'
| 'MotionSensor'
| 'VisionSensor'
| 'ExternalMotorWithTacho'
| 'InternalMotorWithTacho'
| 'InternalTilt'
| 'Unknown';
Show Type
interface ErrorIncomingMessage {
readonly messageType: 'Error';
readonly commandTypeId: number;
readonly errorCode: ErrorCode;
}
type ErrorCode =
| 'Ack'
| 'Mack'
| 'BufferOverflow'
| 'Timeout'
| 'CommandNotRecognized'
| 'InvalidUse'
| 'Overcurrent'
| 'InternalError'
| 'Unknown';
Show Type
type PortInformationIncomingMessage = {
readonly messageType: 'PortInformation';
} & (
| {
readonly portId: number;
readonly portInformationType: 'ModeInfo';
readonly inputModeIds: number[];
readonly outputModeIds: number[];
}
| {
readonly portId: number;
readonly portInformationType: 'PossibleModeCombinations';
}
);
Show Type
type PortModeInformationIncomingMessage = {
readonly messageType: 'PortModeInformation';
readonly portId: number;
readonly modeId: number;
} & (
| {
readonly portModeInformationType: 'Name';
readonly name: string;
}
| {
readonly portModeInformationType: 'Raw';
readonly rawRange: [number, number];
}
| {
readonly portModeInformationType: 'Pct';
readonly pctRange: [number, number];
}
| {
readonly portModeInformationType: 'Si';
readonly siRange: [number, number];
}
| {
readonly portModeInformationType: 'Symbol';
readonly symbol: string;
}
| {
readonly portModeInformationType: 'ValueFormat';
readonly valueFormat: ValueFormat;
}
| {
readonly portModeInformationType: 'Unknown';
}
);
interface ValueFormat {
readonly datasetType: DatasetType;
readonly numberOfDatasets: number;
}
type DatasetType = 'Int8' | 'Int16' | 'Int32' | 'Float' | 'Unknown';
Show Type
interface PortValueIncomingMessage {
readonly messageType: 'PortValue';
readonly portId: number;
readonly valueData: ArrayBuffer;
}
Show Type
interface PortInputFormatIncomingMessage {
readonly messageType: 'PortInputFormat';
readonly portId: number;
readonly modeId: number;
readonly deltaInterval: number;
readonly notificationsEnabled: boolean;
}
Show Type
interface UnknownIncomingMessage {
readonly messageType: 'Unknown';
}
type OutgoingMessage =
| PortInformationRequestOutgoingMessage
| PortModeInformationRequestOutgoingMessage
| PortInputFormatSetupOutgoingMessage
| PortOutputCommandOutgoingMessage;
Show Type
interface PortInformationRequestOutgoingMessage {
readonly messageType: 'PortInformationRequest';
readonly portId: number;
readonly portInformationRequestType: 'PortValue' | 'ModeInfo';
}
Show Type
interface PortModeInformationRequestOutgoingMessage {
readonly messageType: 'PortModeInformationRequest';
readonly portId: number;
readonly modeId: number;
readonly portModeInformationRequestType: PortModeInformationRequestType;
}
type PortModeInformationRequestType =
| 'Name'
| 'Raw'
| 'Pct'
| 'Si'
| 'Symbol'
| 'ValueFormat';
Show Type
interface PortInputFormatSetupOutgoingMessage {
readonly messageType: 'PortInputFormatSetup';
readonly portId: number;
readonly modeId: number;
readonly deltaInterval: number;
readonly notificationsEnabled: boolean;
}
Show Type
interface PortOutputCommandOutgoingMessage {
readonly messageType: 'PortOutputCommand';
readonly portId: number;
readonly portOutputSubCommandData: ArrayBuffer;
}
yarn release patch
yarn release minor
yarn release major
After a new release has been created by pushing the tag, it must be published via the GitHub UI. This triggers the final publication to npm.
Copyright (c) 2020, Clemens Akens. Released under the terms of the MIT License.