rxpoweredup
Version:
A Typescript RxJS-based library for controlling LEGO Powered UP hubs & peripherals.
134 lines (133 loc) • 5.97 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { injectable } from 'tsyringe';
import { MessageType, PortModeInformationType } from '../../constants';
import { readBitAtPosition } from '../../helpers';
let PortModeInformationReplyParser = class PortModeInformationReplyParser {
messageType = MessageType.portModeInformation;
parseMessage(message) {
const baseMessageData = {
portId: message.payload[0],
mode: message.payload[1],
messageType: MessageType.portModeInformation,
};
const portModeInformationType = message.payload[2];
switch (portModeInformationType) {
case PortModeInformationType.name:
return this.parsePortModeInformationName(message.payload, baseMessageData);
case PortModeInformationType.rawRange:
return this.parsePortModeInformationRawRange(message.payload, baseMessageData);
case PortModeInformationType.pctRange:
return this.parsePortModeInformationPctRange(message.payload, baseMessageData);
case PortModeInformationType.siRange:
return this.parsePortModeInformationSiRange(message.payload, baseMessageData);
case PortModeInformationType.symbol:
return this.parsePortModeInformationSymbol(message.payload, baseMessageData);
case PortModeInformationType.mapping:
return this.parsePortModeInformationMapping(message.payload, baseMessageData);
case PortModeInformationType.motorBias:
return this.parsePortModeInformationMotorBias(message.payload, baseMessageData);
case PortModeInformationType.valueFormat:
return this.parsePortModeInformationValueFormat(message.payload, baseMessageData);
case PortModeInformationType.capabilityBits:
return this.parsePortModeInformationCapabilityBits(message.payload, baseMessageData);
default:
throw new Error(`Unknown port mode information type: ${portModeInformationType}`);
}
}
parsePortModeInformationName(payload, baseMessage) {
return {
...baseMessage,
modeInformationType: PortModeInformationType.name,
name: [...payload.slice(3)]
.filter((v) => v)
.map((value) => String.fromCharCode(value))
.join(''),
};
}
parsePortModeInformationRawRange(payload, baseMessage) {
return {
...baseMessage,
modeInformationType: PortModeInformationType.rawRange,
rawMin: payload[3],
rawMax: payload[4],
};
}
parsePortModeInformationPctRange(payload, baseMessage) {
return {
...baseMessage,
modeInformationType: PortModeInformationType.pctRange,
pctMin: payload[3],
pctMax: payload[4],
};
}
parsePortModeInformationSiRange(payload, baseMessage) {
return {
...baseMessage,
modeInformationType: PortModeInformationType.siRange,
siMin: payload[3],
siMax: payload[4],
};
}
parsePortModeInformationSymbol(payload, baseMessage) {
return {
...baseMessage,
modeInformationType: PortModeInformationType.symbol,
symbol: [...payload.slice(3)]
.filter((v) => v)
.map((value) => String.fromCharCode(value))
.join(''),
};
}
parsePortModeInformationMapping(payload, baseMessage) {
const inputSideValue = payload[3];
const outputSideValue = payload[4];
return {
...baseMessage,
modeInformationType: PortModeInformationType.mapping,
inputSide: {
supportsNull: readBitAtPosition(inputSideValue, 7),
supportsFunctionalMapping: readBitAtPosition(inputSideValue, 6),
abs: readBitAtPosition(inputSideValue, 4),
rel: readBitAtPosition(inputSideValue, 3),
dis: readBitAtPosition(inputSideValue, 2),
},
outputSide: {
supportsNull: readBitAtPosition(outputSideValue, 7),
supportsFunctionalMapping: readBitAtPosition(outputSideValue, 6),
abs: readBitAtPosition(outputSideValue, 4),
rel: readBitAtPosition(outputSideValue, 3),
dis: readBitAtPosition(outputSideValue, 2),
},
};
}
parsePortModeInformationMotorBias(payload, baseMessage) {
return {
...baseMessage,
modeInformationType: PortModeInformationType.motorBias,
motorBias: payload[3],
};
}
parsePortModeInformationCapabilityBits(payload, baseMessage) {
return {
...baseMessage,
modeInformationType: PortModeInformationType.capabilityBits,
capabilityBitsBE: [...payload.slice(3)],
};
}
parsePortModeInformationValueFormat(payload, baseMessage) {
return {
...baseMessage,
modeInformationType: PortModeInformationType.valueFormat,
valueFormat: [...payload.slice(3)],
};
}
};
PortModeInformationReplyParser = __decorate([
injectable()
], PortModeInformationReplyParser);
export { PortModeInformationReplyParser };