From 1c8536e27ade954c0dce05930505d4f984185798 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sat, 15 Mar 2025 12:56:12 -0500 Subject: [PATCH 1/6] add boilerplate for lighting encode tests --- tests/lighting-encode.test.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/lighting-encode.test.js diff --git a/tests/lighting-encode.test.js b/tests/lighting-encode.test.js new file mode 100644 index 0000000..846a8ca --- /dev/null +++ b/tests/lighting-encode.test.js @@ -0,0 +1,26 @@ +const { deepEqual, throws } = require('assert'); +const { describe, it } = require('node:test'); +const { Encoders } = require('../'); + +const goodTests = []; + +describe('RTTrPL Message Encoding', () => { + goodTests.forEach((messageTest) => { + it(messageTest.description, () => { + const actual = messageTest.bytes(); + deepEqual(actual, messageTest.expected); + }); + }); +}); + +//TODO(jwetzell): add tests that handle errors +const badTests = []; + +describe('RTTrPL Message Encoding Throws', () => { + badTests.forEach((messageTest) => { + it(messageTest.description, () => { + messageTest.bytes(); + throws(() => {}, messageTest.throwsMessage); + }); + }); +}); From f8f3f060dc30802620b4d01ef61f35e2ddb17cdd Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sat, 15 Mar 2025 13:08:37 -0500 Subject: [PATCH 2/6] add encoder for channel-block --- src/encoders/index.ts | 2 ++ src/encoders/lighting/channel-block.ts | 13 +++++++++++++ tests/lighting-encode.test.js | 10 +++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/encoders/lighting/channel-block.ts diff --git a/src/encoders/index.ts b/src/encoders/index.ts index 77121f3..21bf15e 100644 --- a/src/encoders/index.ts +++ b/src/encoders/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 Encoders = { TrackedPointAccelVelocity, ZoneCollisionDetection, ZoneObject, + ChannelBlock, }; diff --git a/src/encoders/lighting/channel-block.ts b/src/encoders/lighting/channel-block.ts new file mode 100644 index 0000000..703768a --- /dev/null +++ b/src/encoders/lighting/channel-block.ts @@ -0,0 +1,13 @@ +export default (offset: number, fade: number, value: number, isLittleEndian: boolean = false): Uint8Array => { + const bytes = new Uint8Array(5); + const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); + + let dataOffset = 0; + view.setUint16(dataOffset, offset, isLittleEndian); + dataOffset += 2; + view.setUint16(dataOffset, fade, isLittleEndian); + dataOffset += 2; + view.setUint8(dataOffset, value); + + return bytes; +}; diff --git a/tests/lighting-encode.test.js b/tests/lighting-encode.test.js index 846a8ca..cc16572 100644 --- a/tests/lighting-encode.test.js +++ b/tests/lighting-encode.test.js @@ -2,7 +2,15 @@ const { deepEqual, throws } = require('assert'); const { describe, it } = require('node:test'); const { Encoders } = require('../'); -const goodTests = []; +const goodTests = [ + { + description: 'Channel Block', + expected: new Uint8Array([0x00, 0x08, 0x00, 0x06, 0x40]), + bytes: () => { + return Encoders.ChannelBlock(8, 6, 64); + }, + }, +]; describe('RTTrPL Message Encoding', () => { goodTests.forEach((messageTest) => { From 09719a87911dd5c045b73eb493d638c9732c1762 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sat, 15 Mar 2025 13:09:44 -0500 Subject: [PATCH 3/6] add encoder for spot module --- src/encoders/index.ts | 2 ++ src/encoders/lighting/spot.ts | 32 ++++++++++++++++++++++++++++++++ tests/lighting-encode.test.js | 7 +++++++ 3 files changed, 41 insertions(+) create mode 100644 src/encoders/lighting/spot.ts diff --git a/src/encoders/index.ts b/src/encoders/index.ts index 21bf15e..5d83213 100644 --- a/src/encoders/index.ts +++ b/src/encoders/index.ts @@ -1,4 +1,5 @@ import ChannelBlock from './lighting/channel-block'; +import Spot from './lighting/spot'; import CentroidAccelVelocity from './motion/centroid-accel-velocity'; import CentroidPosition from './motion/centroid-position'; import OrientationEuler from './motion/orientation-euler'; @@ -23,5 +24,6 @@ export const Encoders = { TrackedPointAccelVelocity, ZoneCollisionDetection, ZoneObject, + Spot, ChannelBlock, }; diff --git a/src/encoders/lighting/spot.ts b/src/encoders/lighting/spot.ts new file mode 100644 index 0000000..4ecf7b4 --- /dev/null +++ b/src/encoders/lighting/spot.ts @@ -0,0 +1,32 @@ +export default ( + id: number, + offset: number, + channelBlockModules: Uint8Array[], + isLittleEndian: boolean = false +): Uint8Array => { + let totalSpotModuleSize = 1 + 2 + 2 + 2 + 2; + + channelBlockModules.forEach((channelBlockModule) => { + totalSpotModuleSize += channelBlockModule.byteLength; + }); + + const bytes = new Uint8Array(totalSpotModuleSize); + const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); + + let dataOffset = 0; + view.setUint8(dataOffset, 0x0a); + dataOffset += 1; + view.setUint16(dataOffset, bytes.byteLength, isLittleEndian); + dataOffset += 2; + view.setUint16(dataOffset, id, isLittleEndian); + dataOffset += 2; + view.setUint16(dataOffset, offset, isLittleEndian); + dataOffset += 2; + view.setUint16(dataOffset, channelBlockModules.length, isLittleEndian); + dataOffset += 2; + channelBlockModules.forEach((channelBlockModule) => { + bytes.set(channelBlockModule, dataOffset); + dataOffset += channelBlockModule.length; + }); + return bytes; +}; diff --git a/tests/lighting-encode.test.js b/tests/lighting-encode.test.js index cc16572..2160059 100644 --- a/tests/lighting-encode.test.js +++ b/tests/lighting-encode.test.js @@ -3,6 +3,13 @@ const { describe, it } = require('node:test'); const { Encoders } = require('../'); const goodTests = [ + { + description: 'Spot + Single Channel Block', + expected: new Uint8Array([0x0a, 0x00, 0x0e, 0x12, 0x34, 0x00, 0x01, 0x00, 0x01, 0x00, 0x08, 0x00, 0x06, 0x40]), + bytes: () => { + return Encoders.Spot(0x1234, 1, [Encoders.ChannelBlock(8, 6, 64)]); + }, + }, { description: 'Channel Block', expected: new Uint8Array([0x00, 0x08, 0x00, 0x06, 0x40]), From a184e0899b9939cb751cc90bd7a54a0eb74bba18 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sat, 15 Mar 2025 13:09:54 -0500 Subject: [PATCH 4/6] add encoder for universe module --- src/encoders/index.ts | 2 ++ src/encoders/lighting/universe.ts | 25 +++++++++++++++++++++++++ tests/lighting-encode.test.js | 10 ++++++++++ 3 files changed, 37 insertions(+) create mode 100644 src/encoders/lighting/universe.ts diff --git a/src/encoders/index.ts b/src/encoders/index.ts index 5d83213..afc9326 100644 --- a/src/encoders/index.ts +++ b/src/encoders/index.ts @@ -1,5 +1,6 @@ import ChannelBlock from './lighting/channel-block'; import Spot from './lighting/spot'; +import Universe from './lighting/universe'; import CentroidAccelVelocity from './motion/centroid-accel-velocity'; import CentroidPosition from './motion/centroid-position'; import OrientationEuler from './motion/orientation-euler'; @@ -24,6 +25,7 @@ export const Encoders = { TrackedPointAccelVelocity, ZoneCollisionDetection, ZoneObject, + Universe, Spot, ChannelBlock, }; diff --git a/src/encoders/lighting/universe.ts b/src/encoders/lighting/universe.ts new file mode 100644 index 0000000..1954287 --- /dev/null +++ b/src/encoders/lighting/universe.ts @@ -0,0 +1,25 @@ +export default (id: number, spotModules: Uint8Array[], isLittleEndian: boolean = false): Uint8Array => { + let totalSpotModuleSize = 1 + 2 + 2 + 2; + + spotModules.forEach((spotModule) => { + totalSpotModuleSize += spotModule.byteLength; + }); + + const bytes = new Uint8Array(totalSpotModuleSize); + const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); + + let dataOffset = 0; + view.setUint8(dataOffset, 0x09); + dataOffset += 1; + view.setUint16(dataOffset, bytes.byteLength, isLittleEndian); + dataOffset += 2; + view.setUint16(dataOffset, id, isLittleEndian); + dataOffset += 2; + view.setUint16(dataOffset, spotModules.length, isLittleEndian); + dataOffset += 2; + spotModules.forEach((spotModule) => { + bytes.set(spotModule, dataOffset); + dataOffset += spotModule.length; + }); + return bytes; +}; diff --git a/tests/lighting-encode.test.js b/tests/lighting-encode.test.js index 2160059..abda897 100644 --- a/tests/lighting-encode.test.js +++ b/tests/lighting-encode.test.js @@ -3,6 +3,16 @@ const { describe, it } = require('node:test'); const { Encoders } = require('../'); const goodTests = [ + { + description: 'Universe + Spot + Single Channel Block', + expected: new Uint8Array([ + 0x09, 0x00, 0x15, 0x12, 0x34, 0x00, 0x01, 0x0a, 0x00, 0x0e, 0x12, 0x34, 0x00, 0x01, 0x00, 0x01, 0x00, 0x08, 0x00, + 0x06, 0x40, + ]), + bytes: () => { + return Encoders.Universe(0x1234, [Encoders.Spot(0x1234, 1, [Encoders.ChannelBlock(8, 6, 64)])]); + }, + }, { description: 'Spot + Single Channel Block', expected: new Uint8Array([0x0a, 0x00, 0x0e, 0x12, 0x34, 0x00, 0x01, 0x00, 0x01, 0x00, 0x08, 0x00, 0x06, 0x40]), From bd427c2de39a81822516546d5831fdb9c606dbe7 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sat, 15 Mar 2025 13:20:15 -0500 Subject: [PATCH 5/6] add encoder for lighting output module --- src/encoders/index.ts | 2 ++ src/encoders/lighting/lighting-output.ts | 36 ++++++++++++++++++++++++ tests/lighting-encode.test.js | 12 ++++++++ 3 files changed, 50 insertions(+) create mode 100644 src/encoders/lighting/lighting-output.ts diff --git a/src/encoders/index.ts b/src/encoders/index.ts index afc9326..0b7a353 100644 --- a/src/encoders/index.ts +++ b/src/encoders/index.ts @@ -1,4 +1,5 @@ import ChannelBlock from './lighting/channel-block'; +import LightingOutput from './lighting/lighting-output'; import Spot from './lighting/spot'; import Universe from './lighting/universe'; import CentroidAccelVelocity from './motion/centroid-accel-velocity'; @@ -25,6 +26,7 @@ export const Encoders = { TrackedPointAccelVelocity, ZoneCollisionDetection, ZoneObject, + LightingOutput, Universe, Spot, ChannelBlock, diff --git a/src/encoders/lighting/lighting-output.ts b/src/encoders/lighting/lighting-output.ts new file mode 100644 index 0000000..07f620a --- /dev/null +++ b/src/encoders/lighting/lighting-output.ts @@ -0,0 +1,36 @@ +export default ( + sequence: number, + action: number, + holdTime: number, + universeModules: Uint8Array[], + isLittleEndian: boolean = false +): Uint8Array => { + let totalLightingOutputModuleSize = 1 + 2 + 4 + 1 + 4 + 2; + + universeModules.forEach((universeModule) => { + totalLightingOutputModuleSize += universeModule.byteLength; + }); + + const bytes = new Uint8Array(totalLightingOutputModuleSize); + const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); + + let dataOffset = 0; + view.setUint8(dataOffset, 0x07); + dataOffset += 1; + view.setUint16(dataOffset, bytes.byteLength, isLittleEndian); + dataOffset += 2; + view.setUint32(dataOffset, sequence, isLittleEndian); + dataOffset += 4; + view.setUint8(dataOffset, action); + dataOffset += 1; + view.setUint32(dataOffset, holdTime, isLittleEndian); + dataOffset += 4; + view.setUint16(dataOffset, universeModules.length, isLittleEndian); + dataOffset += 2; + + universeModules.forEach((universeModule) => { + bytes.set(universeModule, dataOffset); + dataOffset += universeModule.length; + }); + return bytes; +}; diff --git a/tests/lighting-encode.test.js b/tests/lighting-encode.test.js index abda897..d1f8058 100644 --- a/tests/lighting-encode.test.js +++ b/tests/lighting-encode.test.js @@ -3,6 +3,18 @@ const { describe, it } = require('node:test'); const { Encoders } = require('../'); const goodTests = [ + { + description: 'Lighting Output + Universe + Spot + Single Channel Block', + expected: new Uint8Array([ + 0x07, 0x00, 0x23, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x64, 0x00, 0x01, 0x09, 0x00, 0x15, 0x12, 0x34, + 0x00, 0x01, 0x0a, 0x00, 0x0e, 0x12, 0x34, 0x00, 0x01, 0x00, 0x01, 0x00, 0x08, 0x00, 0x06, 0x40, + ]), + bytes: () => { + return Encoders.LightingOutput(1, 1, 100, [ + Encoders.Universe(0x1234, [Encoders.Spot(0x1234, 1, [Encoders.ChannelBlock(8, 6, 64)])]), + ]); + }, + }, { description: 'Universe + Spot + Single Channel Block', expected: new Uint8Array([ From 96e4e12ca3a08d15b1a6d1620d71afc0d5e67df9 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sat, 15 Mar 2025 13:20:41 -0500 Subject: [PATCH 6/6] add encoder for RTTrPL packet --- src/encoders/index.ts | 2 ++ src/encoders/lighting/rttrpl.ts | 27 +++++++++++++++++++++++++++ tests/lighting-encode.test.js | 15 +++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/encoders/lighting/rttrpl.ts diff --git a/src/encoders/index.ts b/src/encoders/index.ts index 0b7a353..f4d8555 100644 --- a/src/encoders/index.ts +++ b/src/encoders/index.ts @@ -1,5 +1,6 @@ import ChannelBlock from './lighting/channel-block'; import LightingOutput from './lighting/lighting-output'; +import RTTrPL from './lighting/rttrpl'; import Spot from './lighting/spot'; import Universe from './lighting/universe'; import CentroidAccelVelocity from './motion/centroid-accel-velocity'; @@ -17,6 +18,7 @@ import RTTrPHeader from './rttrp-header'; export const Encoders = { RTTrPHeader, RTTrPM, + RTTrPL, Trackable, CentroidPosition, OrientationQuaternion, diff --git a/src/encoders/lighting/rttrpl.ts b/src/encoders/lighting/rttrpl.ts new file mode 100644 index 0000000..4b4579d --- /dev/null +++ b/src/encoders/lighting/rttrpl.ts @@ -0,0 +1,27 @@ +import RTTrPHeader from '../rttrp-header'; + +export default ( + packetId: number, + context: number, + lightingOutputs: Uint8Array[], + isLittleEndian: boolean = false +): Uint8Array => { + let totalPacketSize = 18; //header size + + lightingOutputs.forEach((lightingOutput) => { + totalPacketSize += lightingOutput.length; + }); + const bytes = new Uint8Array(totalPacketSize); + + let dataOffset = 0; + + const headerBytes = RTTrPHeader(0x4434, 0x02, packetId, 0x00, context, lightingOutputs, isLittleEndian); + + bytes.set(headerBytes, dataOffset); + dataOffset += headerBytes.length; + lightingOutputs.forEach((lightingOutput) => { + bytes.set(lightingOutput, dataOffset); + dataOffset += lightingOutput.length; + }); + return bytes; +}; diff --git a/tests/lighting-encode.test.js b/tests/lighting-encode.test.js index d1f8058..0a924af 100644 --- a/tests/lighting-encode.test.js +++ b/tests/lighting-encode.test.js @@ -3,6 +3,21 @@ const { describe, it } = require('node:test'); const { Encoders } = require('../'); const goodTests = [ + { + description: 'RTTrPL + Lighting Output + Universe + Spot + Single Channel Block', + expected: new Uint8Array([ + 65, 84, 0x44, 0x34, 0, 2, 0, 0, 0, 1, 0, 0, 53, 18, 52, 86, 120, 1, 0x07, 0x00, 0x23, 0x00, 0x00, 0x00, 0x01, + 0x01, 0x00, 0x00, 0x00, 0x64, 0x00, 0x01, 0x09, 0x00, 0x15, 0x12, 0x34, 0x00, 0x01, 0x0a, 0x00, 0x0e, 0x12, 0x34, + 0x00, 0x01, 0x00, 0x01, 0x00, 0x08, 0x00, 0x06, 0x40, + ]), + bytes: () => { + return Encoders.RTTrPL(1, 0x12345678, [ + Encoders.LightingOutput(1, 1, 100, [ + Encoders.Universe(0x1234, [Encoders.Spot(0x1234, 1, [Encoders.ChannelBlock(8, 6, 64)])]), + ]), + ]); + }, + }, { description: 'Lighting Output + Universe + Spot + Single Channel Block', expected: new Uint8Array([