From 9a8832fe90e3a3c50f4592c606e38c6b1544b3d7 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sat, 15 Mar 2025 12:01:50 -0500 Subject: [PATCH] add decoder for channel block --- src/decoders/index.ts | 2 ++ src/decoders/lighting/channel-block.ts | 20 ++++++++++++++++++++ src/models.ts | 6 ++++++ tests/decode.test.js | 10 ++++++++++ 4 files changed, 38 insertions(+) create mode 100644 src/decoders/lighting/channel-block.ts diff --git a/src/decoders/index.ts b/src/decoders/index.ts index e55d22a..ea9be20 100644 --- a/src/decoders/index.ts +++ b/src/decoders/index.ts @@ -1,3 +1,4 @@ +import ChannelBlock from './lighting/channel-block'; import CentroidAccelVelocity from './motion/centroid-accel-velocity'; import CentroidPosition from './motion/centroid-position'; import OrientationEuler from './motion/orientation-euler'; @@ -22,4 +23,5 @@ export const Decoders = { OrientationEuler, ZoneCollisionDetection, ZoneObject, + ChannelBlock, }; diff --git a/src/decoders/lighting/channel-block.ts b/src/decoders/lighting/channel-block.ts new file mode 100644 index 0000000..616d8c6 --- /dev/null +++ b/src/decoders/lighting/channel-block.ts @@ -0,0 +1,20 @@ +import { ChannelBlock } from '../../models'; + +export default (bytes: Uint8Array, isLittleEndian: boolean = false): ChannelBlock => { + if (bytes.length !== 5) { + throw new Error('Channel Block module must be 5 bytes'); + } + + const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); + + const offset = view.getUint16(0); + + const fade = view.getUint16(2, isLittleEndian); + + const value = view.getUint8(4); + return { + offset, + fade, + value, + }; +}; diff --git a/src/models.ts b/src/models.ts index 59b32e7..871cf42 100644 --- a/src/models.ts +++ b/src/models.ts @@ -114,3 +114,9 @@ export type ZoneObject = { nameLength: number; name: string; }; + +export type ChannelBlock = { + offset: number; + fade: number; + value: number; +}; diff --git a/tests/decode.test.js b/tests/decode.test.js index 380e59c..3d90dca 100644 --- a/tests/decode.test.js +++ b/tests/decode.test.js @@ -251,6 +251,16 @@ const goodTests = [ }, decoder: Decoders.RTTrPM, }, + { + description: 'Channel Block', + bytes: new Uint8Array([0x00, 0x08, 0x00, 0x06, 0x40]), + expected: { + offset: 0x08, + fade: 0x06, + value: 0x40, + }, + decoder: Decoders.ChannelBlock, + }, ]; describe('RTTrP Bytes Decoding', () => {