add decoder for centroid position submodule

This commit is contained in:
2025-03-15 08:37:44 -05:00
parent 2f3d8486a0
commit 3c5cd86ae2
4 changed files with 59 additions and 0 deletions
+2
View File
@@ -1,5 +1,7 @@
import CentroidPosition from './motion/centroid-position';
import RTTrPHeader from './rttrp';
export const Decoders = {
RTTrPHeader,
CentroidPosition,
};
+31
View File
@@ -0,0 +1,31 @@
import { CentroidPosition } from '../../models';
export default (bytes: Uint8Array, isLittleEndian: boolean = false): CentroidPosition => {
if (bytes.length !== 29) {
throw new Error('Centroid Position module must be 29 bytes');
}
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const type = view.getUint8(0);
if (type !== 0x02) {
throw new Error('Centroid Position module must have a type of 0x02');
}
const size = view.getUint16(1, isLittleEndian);
if (size !== 29) {
throw new Error('Centroid Position module must be 29 bytes');
}
const latency = view.getUint16(3, isLittleEndian);
const x = view.getFloat64(5, isLittleEndian);
const y = view.getFloat64(13, isLittleEndian);
const z = view.getFloat64(21, isLittleEndian);
return {
type,
size,
latency,
x,
y,
z,
};
};
+10
View File
@@ -8,4 +8,14 @@ export type RTTrPHeader = {
context: number;
subModuleCount: number;
};
export type CentroidPosition = {
type: number;
size: number;
latency: number;
x: number;
y: number;
z: number;
};
};
+16
View File
@@ -19,6 +19,22 @@ const goodTests = [
},
decoder: Decoders.RTTrPHeader,
},
{
description: 'Centroid Position',
bytes: new Uint8Array([
0x02, 0x00, 0x1d, 0x00, 0x10, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]),
expected: {
type: 0x02,
size: 0x1d,
latency: 16,
x: 1.0,
y: 2.0,
z: 3.0,
},
decoder: Decoders.CentroidPosition,
},
];
describe('RTTrP Bytes Decoding', () => {