All files / src/lifx/packets setWaveform.js

52.32% Statements 45/86
55.1% Branches 27/49
50% Functions 1/2
52.32% Lines 45/86

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156    1x   1x                 1x                                                                                                           1x 4x 4x 4x     4x   4x     4x     4x 4x   4x       4x     4x 4x   4x     4x 4x   4x     4x 4x   4x 1x   4x     4x 4x   4x     4x     4x 4x   4x     4x     4x 4x   4x     4x     4x 4x   4x     4x     4x 4x   4x     1x  
'use strict';
 
const {constants} = require('../../lifx');
 
const Packet = {
  size: 21
};
 
/**
 * Converts packet specific data from a buffer to an object
 * @param  {Buffer} buf Buffer containing only packet specific data no header
 * @return {Object}     Information contained in packet
 */
Packet.toObject = function(buf) {
  const obj = {};
  let offset = 0;
 
  if (buf.length !== this.size) {
    throw new Error('Invalid length given for setWaveform LIFX packet');
  }
 
  obj.stream = buf.readUInt8(offset);
  offset += 1;
 
  obj.isTransient = buf.readUInt8(offset);
  offset += 1;
 
  obj.color = {};
  obj.color.hue = buf.readUInt16LE(offset);
  offset += 2;
  obj.color.saturation = buf.readUInt16LE(offset);
  offset += 2;
  obj.color.brightness = buf.readUInt16LE(offset);
  offset += 2;
  obj.color.kelvin = buf.readUInt16LE(offset);
  offset += 2;
 
  obj.period = buf.readUInt32LE(offset);
  offset += 4;
 
  obj.cycles = buf.readFloatLE(offset);
  offset += 4;
 
  obj.skewRatio = buf.readUInt16LE(offset);
  offset += 2;
 
  obj.waveform = buf.readUInt8(offset);
  offset += 1;
 
  return obj;
};
 
/**
 * Converts the given packet specific object into a packet
 * @param  {Object}  obj object with configuration data
 * @param  {Boolean} obj.isTransient restore color used before effect
 * @param  {Object}  obj.color an objects with colors to set
 * @param  {Number}  obj.color.hue between 0 and 65535
 * @param  {Number}  obj.color.saturation between 0 and 65535
 * @param  {Number}  obj.color.brightness between 0 and 65535
 * @param  {Number}  [obj.color.kelvin=3500] between 2500 and 9000
 * @param  {Number}  obj.period length of one cycle in milliseconds
 * @param  {Number}  obj.cycles how often to repeat through effect
 * @param  {Number}  obj.skewRatio distribution between time on original and new color , positive is for more new color, negative for original color
 * @param  {Number}  obj.waveform between 0 and 4 for form of effect
 * @return {Buffer}  packet
 */
Packet.toBuffer = function(obj) {
  const buf = Buffer.alloc(this.size);
  buf.fill(0);
  let offset = 0;
 
  // obj.stream field has unknown function so leave it as 0
  offset += 1;
 
  Iif (obj.isTransient === undefined) {
    throw new TypeError('obj.isTransient value must be given for setWaveform LIFX packet');
  }
  Iif (typeof obj.isTransient !== 'boolean') {
    throw new TypeError('Invalid isTransient value given for setWaveform LIFX packet, must be boolean');
  }
  buf.writeUInt8(obj.isTransient, offset);
  offset += 1;
 
  Iif (typeof obj.color !== 'object') {
    throw new TypeError('Invalid object for color given for setWaveform LIFX packet');
  }
 
  Iif (typeof obj.color.hue !== 'number' && obj.color.hue < 0 || obj.color.hue > 65535) {
    throw new RangeError('Invalid color hue given for setWaveform LIFX packet, must be a number between 0 and 65535');
  }
  buf.writeUInt16LE(obj.color.hue, offset);
  offset += 2;
 
  Iif (typeof obj.color.saturation !== 'number' && obj.color.saturation < 0 || obj.color.saturation > 65535) {
    throw new RangeError('Invalid color saturation given for setWaveform LIFX packet, must be a number between 0 and 65535');
  }
  buf.writeUInt16LE(obj.color.saturation, offset);
  offset += 2;
 
  Iif (typeof obj.color.brightness !== 'number' && obj.color.brightness < 0 || obj.color.brightness > 65535) {
    throw new RangeError('Invalid color brightness given for setWaveform LIFX packet, must be a number between 0 and 65535');
  }
  buf.writeUInt16LE(obj.color.brightness, offset);
  offset += 2;
 
  if (obj.color.kelvin === undefined) {
    obj.color.kelvin = constants.HSBK_DEFAULT_KELVIN;
  }
  Iif (typeof obj.color.kelvin !== 'number' && obj.color.kelvin < 2500 || obj.color.kelvin > 9000) {
    throw new RangeError('Invalid color kelvin given for setWaveform LIFX packet, must be a number between 2500 and 9000');
  }
  buf.writeUInt16LE(obj.color.kelvin, offset);
  offset += 2;
 
  Iif (obj.period === undefined) {
    throw new TypeError('obj.period value must be given for setWaveform LIFX packet');
  }
  Iif (typeof obj.period !== 'number') {
    throw new TypeError('Invalid period type given for setWaveform LIFX packet, must be a number');
  }
  buf.writeUInt32LE(obj.period, offset);
  offset += 4;
 
  Iif (obj.cycles === undefined) {
    throw new TypeError('obj.cycles value must be given for setWaveform LIFX packet');
  }
  Iif (typeof obj.cycles !== 'number') {
    throw new TypeError('Invalid cycles type given for setWaveform LIFX packet, must be a number');
  }
  buf.writeFloatLE(obj.cycles, offset);
  offset += 4;
 
  Iif (obj.skewRatio === undefined) {
    throw new TypeError('obj.skewRatio value must be given for setWaveform LIFX packet');
  }
  Iif (typeof obj.skewRatio !== 'number') {
    throw new TypeError('Invalid skewRatio type given for setWaveform LIFX packet, must be a number');
  }
  buf.writeInt16LE(obj.skewRatio, offset);
  offset += 2;
 
  Iif (obj.waveform === undefined) {
    throw new TypeError('obj.waveform value must be given for setWaveform LIFX packet');
  }
  Iif (typeof obj.waveform !== 'number' && obj.waveform < 0 || obj.waveform > (constants.LIGHT_WAVEFORMS.length - 1)) {
    throw new RangeError('Invalid waveform value given for setWaveform LIFX packet, must be a number between 0 and ' + (constants.LIGHT_WAVEFORMS.length - 1));
  }
  buf.writeUInt8(obj.waveform, offset);
  offset += 1;
 
  return buf;
};
 
module.exports = Packet;