UNPKG

@betaflight/api

Version:

A high-level API to read data from betaflight flight controllers

237 lines 10.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeBatteryConfig = exports.readBatteryConfig = exports.readBatteryState = exports.writeCurrentMeterConfig = exports.readCurrentMeterConfigs = exports.writeLegacyCurrentMeterConfig = exports.readLegacyCurrentMeterConfig = exports.writeVoltageMeterConfig = exports.readVoltageMeterConfigs = exports.writeLegacyVoltageMeterConfig = exports.readLegacyVoltageMeterConfig = exports.readCurrentMeters = exports.readVoltageMeters = exports.BatteryVoltageMeterSources = exports.BatteryCurrentMeterSources = exports.MeterIndentiers = void 0; const msp_1 = require("@betaflight/msp"); const semver_1 = __importDefault(require("semver")); const codes_1 = __importDefault(require("../codes")); const utils_1 = require("../utils"); const types_1 = require("./types"); Object.defineProperty(exports, "MeterIndentiers", { enumerable: true, get: function () { return types_1.MeterIndentiers; } }); Object.defineProperty(exports, "BatteryCurrentMeterSources", { enumerable: true, get: function () { return types_1.BatteryCurrentMeterSources; } }); Object.defineProperty(exports, "BatteryVoltageMeterSources", { enumerable: true, get: function () { return types_1.BatteryVoltageMeterSources; } }); const readVoltageMeters = async (port) => { const data = await (0, msp_1.execute)(port, { code: codes_1.default.MSP_VOLTAGE_METERS }); return (0, utils_1.times)(() => ({ id: data.readU8(), voltage: data.readU8() / 10.0, }), data.byteLength / 2); }; exports.readVoltageMeters = readVoltageMeters; const readCurrentMeters = async (port) => { const data = await (0, msp_1.execute)(port, { code: codes_1.default.MSP_CURRENT_METERS }); return (0, utils_1.times)(() => ({ id: data.readU8(), mAhDrawn: data.readU16(), amperage: data.readU16() / 1000, // A }), data.byteLength / 5); }; exports.readCurrentMeters = readCurrentMeters; const readLegacyVoltageMeterConfig = async (port) => { const api = (0, msp_1.apiVersion)(port); if (semver_1.default.gte(api, "1.36.0")) { throw new Error("Not a legacy device"); } const data = await (0, msp_1.execute)(port, { code: codes_1.default.MSP_VOLTAGE_METER_CONFIG }); return { vbat: { scale: data.readU8(), minCellVoltage: data.readU8() / 10, maxCellVoltage: data.readU8() / 10, warningCellVoltage: data.readU8() / 10, // 10-50 }, meterType: semver_1.default.gte(api, "1.23.0") ? data.readU8() : 0, }; }; exports.readLegacyVoltageMeterConfig = readLegacyVoltageMeterConfig; const writeLegacyVoltageMeterConfig = async (port, config) => { const api = (0, msp_1.apiVersion)(port); if (semver_1.default.gte(api, "1.36.0")) { throw new Error("Not a legacy device"); } const buffer = new msp_1.WriteBuffer(); buffer .push8(config.vbat.scale) .push8(Math.round(config.vbat.minCellVoltage * 10)) .push8(Math.round(config.vbat.maxCellVoltage * 10)) .push8(Math.round(config.vbat.warningCellVoltage * 10)); if (semver_1.default.gte(api, "1.23.0")) { buffer.push8(config.meterType); } await (0, msp_1.execute)(port, { code: codes_1.default.MSP_SET_VOLTAGE_METER_CONFIG, data: buffer, }); }; exports.writeLegacyVoltageMeterConfig = writeLegacyVoltageMeterConfig; const readVoltageMeterConfigs = async (port) => { const api = (0, msp_1.apiVersion)(port); if (semver_1.default.lt(api, "1.36.0")) { throw new Error("Use readLegacyVoltageMeterConfig"); } const data = await (0, msp_1.execute)(port, { code: codes_1.default.MSP_VOLTAGE_METER_CONFIG }); return (0, utils_1.times)(() => { const subframeLength = data.readU8(); if (subframeLength !== 5) { // discard this config because it's the wrong length (0, utils_1.times)(() => data.readU8(), subframeLength); return undefined; } return { id: data.readU8(), sensorType: data.readU8(), vbat: { scale: data.readU8(), resDivVal: data.readU8(), resDivMultiplier: data.readU8(), }, }; }, data.readU8()).filter(Boolean); }; exports.readVoltageMeterConfigs = readVoltageMeterConfigs; const writeVoltageMeterConfig = async (port, id, config) => { const api = (0, msp_1.apiVersion)(port); if (semver_1.default.lt(api, "1.36.0")) { throw new Error("Use writeLegacyVoltageMeterConfig"); } const buffer = new msp_1.WriteBuffer(); buffer .push8(id) .push8(config.vbat.scale) .push8(config.vbat.resDivVal) .push8(config.vbat.resDivMultiplier); await (0, msp_1.execute)(port, { code: codes_1.default.MSP_SET_VOLTAGE_METER_CONFIG, data: buffer, }); }; exports.writeVoltageMeterConfig = writeVoltageMeterConfig; const readLegacyCurrentMeterConfig = async (port) => { const api = (0, msp_1.apiVersion)(port); if (semver_1.default.lt(api, "1.36.0")) { throw new Error("Use readCurrentMeterConfigs"); } const data = await (0, msp_1.execute)(port, { code: codes_1.default.MSP_CURRENT_METER_CONFIG }); return { scale: data.read16(), offset: data.read16(), meterType: data.readU8(), batteryCapacity: data.readU16(), }; }; exports.readLegacyCurrentMeterConfig = readLegacyCurrentMeterConfig; const writeLegacyCurrentMeterConfig = async (port, config) => { const api = (0, msp_1.apiVersion)(port); if (semver_1.default.lt(api, "1.36.0")) { throw new Error("Use readCurrentMeterConfigs"); } const buffer = new msp_1.WriteBuffer(); buffer .push16(config.scale) .push16(config.offset) .push8(config.meterType) .push16(config.batteryCapacity); await (0, msp_1.execute)(port, { code: codes_1.default.MSP_SET_CURRENT_METER_CONFIG, data: buffer, }); }; exports.writeLegacyCurrentMeterConfig = writeLegacyCurrentMeterConfig; const readCurrentMeterConfigs = async (port) => { const api = (0, msp_1.apiVersion)(port); if (semver_1.default.lt(api, "1.36.0")) { throw new Error("Use readLegacyCurrentMeterConfig"); } const data = await (0, msp_1.execute)(port, { code: codes_1.default.MSP_CURRENT_METER_CONFIG }); return (0, utils_1.times)(() => { const subframeLength = data.readU8(); if (subframeLength !== 6) { // discard this config because it's the wrong length (0, utils_1.times)(() => data.readU8(), subframeLength); return undefined; } return { id: data.readU8(), sensorType: data.readU8(), scale: data.read16(), offset: data.read16(), }; }, data.readU8()).filter(Boolean); }; exports.readCurrentMeterConfigs = readCurrentMeterConfigs; const writeCurrentMeterConfig = async (port, id, config) => { const api = (0, msp_1.apiVersion)(port); if (semver_1.default.lt(api, "1.36.0")) { throw new Error("Use writeLegacyCurrentMeterConfig"); } const buffer = new msp_1.WriteBuffer(); buffer.push8(id).push16(config.scale).push16(config.offset); await (0, msp_1.execute)(port, { code: codes_1.default.MSP_SET_CURRENT_METER_CONFIG, data: buffer, }); }; exports.writeCurrentMeterConfig = writeCurrentMeterConfig; const readBatteryState = async (port) => { const api = (0, msp_1.apiVersion)(port); const data = await (0, msp_1.execute)(port, { code: codes_1.default.MSP_BATTERY_STATE }); return { cellCount: data.readU8(), capacity: data.readU16(), // @ts-expect-error typescript is erroring here // but this shouldn't be a problem voltage: data.readU8() / 10, mAhDrawn: data.readU16(), amperage: data.readU16() / 100, ...(semver_1.default.gte(api, "1.41.0") ? { batteryState: data.readU8(), voltage: data.readU16() / 100, } : { batteryState: 0 }), }; }; exports.readBatteryState = readBatteryState; const readBatteryConfig = async (port) => { const api = (0, msp_1.apiVersion)(port); const data = await (0, msp_1.execute)(port, { code: codes_1.default.MSP_BATTERY_CONFIG }); const config = { vbat: { minCellVoltage: data.readU8() / 10, maxCellVoltage: data.readU8() / 10, warningCellVoltage: data.readU8() / 10, // 10-50 }, capacity: data.readU16(), voltageMeterSource: data.readU8(), currentMeterSource: data.readU8(), }; if (semver_1.default.gte(api, "1.41.0")) { config.vbat.minCellVoltage = data.readU16() / 100; config.vbat.maxCellVoltage = data.readU16() / 100; config.vbat.warningCellVoltage = data.readU16() / 100; } return config; }; exports.readBatteryConfig = readBatteryConfig; const writeBatteryConfig = async (port, config) => { const api = (0, msp_1.apiVersion)(port); const buffer = new msp_1.WriteBuffer(); buffer .push8(Math.round(config.vbat.minCellVoltage * 10)) .push8(Math.round(config.vbat.maxCellVoltage * 10)) .push8(Math.round(config.vbat.warningCellVoltage * 10)) .push16(config.capacity) .push8(config.voltageMeterSource) .push8(config.currentMeterSource); if (semver_1.default.gte(api, "1.41.0")) { buffer .push16(Math.round(config.vbat.minCellVoltage * 100)) .push16(Math.round(config.vbat.maxCellVoltage * 100)) .push16(Math.round(config.vbat.warningCellVoltage * 100)); } await (0, msp_1.execute)(port, { code: codes_1.default.MSP_SET_BATTERY_CONFIG, data: buffer }); }; exports.writeBatteryConfig = writeBatteryConfig; //# sourceMappingURL=index.js.map