All files / lanes_initialize.js

100.00% Branches 6/6
100.00% Lines 66/66
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x4
x4
x1
x5
x5
x4
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x5
x5
x1
x6
x5
x1
x1
x1
x1
x1
x1
x1
x1
x1
x6
x1
x9
x9
x6































































/**
 * Encoding/decoding functions for lanes-initialize packet
 *
 * More detail of packet protocol, see `designs/packet-protocols.md`
 *
 * @module
 */


/**
 * Packet ID of lanes-initialize
 */
const PACKET_ID_LANES_INITIALIZE = 0x11;

/**
 * DMX channels count
 */
export const DMX_CHANNEL_COUNT = 512;


/**
 * Create lanes-initialize packet
 *
 * @param {Uint8Array} values Values array to contain
 * @returns {ArrayBuffer} Encoded packet
 *
 * @throws {RangeError} When `values` length is not 512
 */
export function encode_lanes_initialize_packet(values) {
  if(values.length !== DMX_CHANNEL_COUNT)
    throw new RangeError(`values length must be 512, but got ${values.length}`);

  const packet = Uint8Array.of(PACKET_ID_LANES_INITIALIZE, ...values).buffer;
  return packet;
}


/**
 * Unpack lanes-initialize packet
 *
 * @param {ArrayBuffer} packet Packet to decode
 * @returns {Uint8Array} Decoded packet data - is values array
 *
 * @throws {Error} When non lanes-initialize packet array passed
 */
export function decode_lanes_initialize_packet(packet) {
  if(!is_lanes_initialize_packet(packet))
    throw new Error(`It is not a lanes-initialize packet`);

  return new Uint8Array(packet.slice(1));
}


/**
 * Verify the packet is lanes-initialize packet
 *
 * @param {ArrayBuffer} packet Packet to verify
 * @returns {boolean} `packet` is lanes-initialize packet: true, otherwise: false
 */
export function is_lanes_initialize_packet(packet) {
  if(packet.byteLength !== DMX_CHANNEL_COUNT + 1) return false;

  const packet_id = new DataView(packet).getUint8(0);
  return packet_id === PACKET_ID_LANES_INITIALIZE;
}