mirror of
https://github.com/jwetzell/rttrp-js.git
synced 2026-08-18 13:33:59 +00:00
add decoder for centroid position submodule
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import CentroidPosition from './motion/centroid-position';
|
||||
import RTTrPHeader from './rttrp';
|
||||
|
||||
export const Decoders = {
|
||||
RTTrPHeader,
|
||||
CentroidPosition,
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
Reference in New Issue
Block a user