Added support for passing brightness in % to the ReST API

This commit is contained in:
Mark van Renswoude 2017-03-27 22:57:41 +02:00
parent e70018ec9d
commit 0a41a25163

View File

@ -125,12 +125,22 @@ function readModeData(mode, reader)
function lsb(value) { return value & 0xFF; } function lsb(value) { return value & 0xFF; }
function msb(value) { return (value >> 8) & 0xFF; } function msb(value) { return (value >> 8) & 0xFF; }
function getBrightness(value)
{
if (typeof(value) == 'string' && value.substr(-1) === '%')
return (Number(value.substr(0, value.length - 1)) * 4096 / 100);
return Number(value) || 0;
}
function writeModeData(mode, data) function writeModeData(mode, data)
{ {
switch (mode) switch (mode)
{ {
case protocol.Mode.Static: case protocol.Mode.Static:
if (typeof(data.brightness) == 'undefined') data.brightness = 0; var brightness = getBrightness(data.brightness);
return new Buffer([protocol.Command.SetMode, mode, lsb(data.brightness), msb(data.brightness)]); return new Buffer([protocol.Command.SetMode, mode, lsb(data.brightness), msb(data.brightness)]);
@ -143,27 +153,27 @@ function writeModeData(mode, data)
buffer.writeInt8(mode, 1); buffer.writeInt8(mode, 1);
for (var index = 0; index < valueCount; index++) for (var index = 0; index < valueCount; index++)
buffer.writeInt16LE(brightness[index], 2 + (index * 2)); buffer.writeInt16LE(getBrightness(brightness[index]), 2 + (index * 2));
return buffer; return buffer;
case protocol.Mode.Alternate: case protocol.Mode.Alternate:
if (typeof(data.brightness) == 'undefined') data.brightness = 0; var brightness = getBrightness(data.brightness);
if (typeof(data.interval) == 'undefined') data.interval = 500; if (typeof(data.interval) == 'undefined') data.interval = 500;
return new Buffer([protocol.Command.SetMode, mode, return new Buffer([protocol.Command.SetMode, mode,
lsb(data.interval), msb(data.interval), lsb(data.interval), msb(data.interval),
lsb(data.brightness), msb(data.brightness)]); lsb(brightness), msb(brightness)]);
case protocol.Mode.Slide: case protocol.Mode.Slide:
if (typeof(data.brightness) == 'undefined') data.brightness = 0; var brightness = getBrightness(data.brightness);
if (typeof(data.interval) == 'undefined') data.interval = 500; if (typeof(data.interval) == 'undefined') data.interval = 500;
if (typeof(data.direction) == 'undefined') data.direction = 0; if (typeof(data.direction) == 'undefined') data.direction = 0;
if (typeof(data.fadeOutTime) == 'undefined') data.fadeOutTime = 0; if (typeof(data.fadeOutTime) == 'undefined') data.fadeOutTime = 0;
return new Buffer([protocol.Command.SetMode, mode, return new Buffer([protocol.Command.SetMode, mode,
lsb(data.interval), msb(data.interval), lsb(data.interval), msb(data.interval),
lsb(data.brightness), msb(data.brightness), lsb(brightness), msb(brightness),
data.direction, data.direction,
lsb(data.fadeOutTime), msb(data.fadeOutTime)]); lsb(data.fadeOutTime), msb(data.fadeOutTime)]);
} }