@betaflight/api
Version:
A high-level API to read data from betaflight flight controllers
315 lines • 8.39 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.huffmanDecodeBuffer = void 0;
const HUFFMAN_EOF = -1;
const tree = {
"2": { 0b11: 0 },
"3": { 0b101: 1 },
"4": { 0b1001: 2 },
"5": { 0b10001: 3, 0b10000: 4, 0b01111: 80 },
"6": {
0b011101: 5,
0b011100: 6,
0b011011: 7,
0b011010: 8,
0b011001: 16,
},
"7": {
0b0110001: 9,
0b0110000: 10,
0b0101111: 11,
0b0101110: 12,
0b0101101: 13,
0b0101100: 14,
0b0101011: 15,
0b0101010: 17,
0b0101001: 18,
},
"8": {
0b01010001: 19,
0b01010000: 20,
0b01001111: 21,
0b01001110: 22,
0b01001101: 23,
0b01001100: 24,
0b01001011: 25,
0b01001010: 26,
0b01001001: 27,
0b01001000: 28,
0b01000111: 29,
0b01000110: 30,
0b01000101: 31,
0b01000100: 32,
0b01000011: 33,
0b01000010: 34,
0b01000001: 35,
0b01000000: 36,
0b00111111: 48,
0b00111110: 64,
0b00111101: 240,
},
"9": {
0b001111001: 37,
0b001111000: 38,
0b001110111: 39,
0b001110110: 40,
0b001110101: 41,
0b001110100: 42,
0b001110011: 43,
0b001110010: 44,
0b001110001: 45,
0b001110000: 46,
0b001101111: 47,
0b001101110: 49,
0b001101101: 50,
0b001101100: 51,
0b001101011: 52,
0b001101010: 53,
0b001101001: 54,
0b001101000: 55,
0b001100111: 56,
0b001100110: 57,
0b001100101: 58,
0b001100100: 59,
0b001100011: 60,
0b001100010: 61,
0b001100001: 62,
0b001100000: 63,
0b001011111: 65,
0b001011110: 66,
0b001011101: 67,
0b001011100: 68,
0b001011011: 69,
0b001011010: 70,
0b001011001: 71,
0b001011000: 72,
0b001010111: 73,
0b001010110: 76,
0b001010101: 79,
0b001010100: 81,
0b001010011: 128,
0b001010010: 224,
0b001010001: 241,
0b001010000: 255,
},
"10": {
0b0010011111: 74,
0b0010011110: 75,
0b0010011101: 77,
0b0010011100: 78,
0b0010011011: 82,
0b0010011010: 83,
0b0010011001: 84,
0b0010011000: 85,
0b0010010111: 86,
0b0010010110: 87,
0b0010010101: 88,
0b0010010100: 89,
0b0010010011: 90,
0b0010010010: 91,
0b0010010001: 92,
0b0010010000: 93,
0b0010001111: 94,
0b0010001110: 95,
0b0010001101: 96,
0b0010001100: 97,
0b0010001011: 98,
0b0010001010: 99,
0b0010001001: 100,
0b0010001000: 101,
0b0010000111: 102,
0b0010000110: 103,
0b0010000101: 104,
0b0010000100: 105,
0b0010000011: 106,
0b0010000010: 107,
0b0010000001: 108,
0b0010000000: 109,
0b0001111111: 110,
0b0001111110: 111,
0b0001111101: 112,
0b0001111100: 113,
0b0001111011: 114,
0b0001111010: 115,
0b0001111001: 116,
0b0001111000: 117,
0b0001110111: 118,
0b0001110110: 119,
0b0001110101: 120,
0b0001110100: 121,
0b0001110011: 122,
0b0001110010: 123,
0b0001110001: 124,
0b0001110000: 125,
0b0001101111: 126,
0b0001101110: 127,
0b0001101101: 129,
0b0001101100: 130,
0b0001101011: 131,
0b0001101010: 132,
0b0001101001: 133,
0b0001101000: 134,
0b0001100111: 135,
0b0001100110: 136,
0b0001100101: 137,
0b0001100100: 138,
0b0001100011: 139,
0b0001100010: 140,
0b0001100001: 141,
0b0001100000: 142,
0b0001011111: 143,
0b0001011110: 144,
0b0001011101: 145,
0b0001011100: 146,
0b0001011011: 147,
0b0001011010: 148,
0b0001011001: 149,
0b0001011000: 150,
0b0001010111: 151,
0b0001010110: 152,
0b0001010101: 153,
0b0001010100: 154,
0b0001010011: 155,
0b0001010010: 156,
0b0001010001: 157,
0b0001010000: 158,
0b0001001111: 159,
0b0001001110: 160,
0b0001001101: 161,
0b0001001100: 162,
0b0001001011: 163,
0b0001001010: 164,
0b0001001001: 165,
0b0001001000: 166,
0b0001000111: 167,
0b0001000110: 168,
0b0001000101: 169,
0b0001000100: 170,
0b0001000011: 171,
0b0001000010: 172,
0b0001000001: 173,
0b0001000000: 174,
0b0000111111: 175,
0b0000111110: 176,
0b0000111101: 177,
0b0000111100: 178,
0b0000111011: 179,
0b0000111010: 180,
0b0000111001: 181,
0b0000111000: 182,
0b0000110111: 183,
0b0000110110: 184,
0b0000110101: 185,
0b0000110100: 186,
0b0000110011: 187,
0b0000110010: 188,
0b0000110001: 189,
0b0000110000: 190,
0b0000101111: 191,
0b0000101110: 192,
0b0000101101: 193,
0b0000101100: 194,
0b0000101011: 195,
0b0000101010: 196,
0b0000101001: 197,
0b0000101000: 198,
0b0000100111: 199,
0b0000100110: 200,
0b0000100101: 201,
0b0000100100: 202,
0b0000100011: 203,
0b0000100010: 204,
0b0000100001: 205,
0b0000100000: 206,
0b0000011111: 207,
0b0000011110: 208,
0b0000011101: 209,
0b0000011100: 210,
0b0000011011: 211,
0b0000011010: 212,
0b0000011001: 214,
0b0000011000: 215,
0b0000010111: 216,
0b0000010110: 217,
0b0000010101: 218,
0b0000010100: 219,
0b0000010011: 220,
0b0000010010: 222,
0b0000010001: 223,
0b0000010000: 225,
0b0000001111: 226,
0b0000001110: 228,
0b0000001101: 239,
},
"11": {
0b00000011001: 213,
0b00000011000: 221,
0b00000010111: 227,
0b00000010110: 229,
0b00000010101: 230,
0b00000010100: 231,
0b00000010011: 232,
0b00000010010: 233,
0b00000010001: 234,
0b00000010000: 235,
0b00000001111: 236,
0b00000001110: 237,
0b00000001101: 238,
0b00000001100: 242,
0b00000001011: 243,
0b00000001010: 244,
0b00000001001: 245,
0b00000001000: 246,
0b00000000111: 247,
0b00000000110: 248,
0b00000000101: 250,
0b00000000100: 251,
0b00000000011: 252,
0b00000000010: 253,
0b00000000001: 254,
},
"12": { 0b000000000001: 249, 0b000000000000: HUFFMAN_EOF },
};
// eslint-disable-next-line import/prefer-default-export
const huffmanDecodeBuffer = (inBuf, expectedSize) => {
var _a;
let code = 0;
let depth = 0;
let bitOffset = 0;
let currentByte = 0;
const outBuf = [];
while (bitOffset / 8 < inBuf.byteLength) {
if (outBuf.length === expectedSize) {
// we've exhausted the input stream, discard any odd bits on the end
break;
}
if (bitOffset % 8 === 0) {
// move ono the next byte
currentByte = inBuf.readInt8(bitOffset / 8);
}
// get the next bit from the input buffer
code <<= 1;
depth += 1;
// if the next bit is one, set to one 000 -> 001
if ((128 >> bitOffset % 8) & currentByte) {
code |= 0x01;
}
const decodedValue = (_a = tree[depth]) === null || _a === void 0 ? void 0 : _a[code];
if (decodedValue !== undefined) {
if (decodedValue === HUFFMAN_EOF) {
break;
}
else {
// output the value
outBuf.push(decodedValue);
}
// reset the code to continue decompressing the input buffer
code = 0;
depth = 0;
}
bitOffset += 1;
}
return Buffer.from(outBuf);
};
exports.huffmanDecodeBuffer = huffmanDecodeBuffer;
//# sourceMappingURL=huffman.js.map