Skip to content

Commit

Permalink
- Fixes #54, implementation of xxx_inc values in the light state that…
Browse files Browse the repository at this point in the history
… was introduced in bridge version 1.7
  • Loading branch information
osirisoft-pmurray committed Jul 25, 2015
1 parent 12baeaa commit 9fb2a5c
Show file tree
Hide file tree
Showing 9 changed files with 550 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 1.1.0
- Added support for increment values in light state, issue #54

## 1.0.5
- Fixes issue #46 scene id was missing from body sent to the bridge for activation requests

Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,11 @@ which take values documented in the official Phillips Hue Lights API:
| `alert(value)` | Sets the alert state to value `none`, `select` or `lselect`. If no parameter is passed will default to `none`. |
| `effect(effectName)` | Sets the effect on the light(s) where `effectName` is either `none` or `colorloop`. |
| `transitiontime(milliseconds)` | Sets a transition time in milliseconds |
| `bri_inc(value)`| Increments/Decrements the brightness by the value specified. Accepts values -254 to 254. |
| `sat_inc(value)`| Increments/Decrements the saturation by the value specified. Accepts values -254 to 254. |
| `hue_inc(value)`| Increments/Decrements the hue by the value specified. Accepts values -65534 to 65534. |
| `ct_inc(value)` | Increments/Decrements the color temperature by the value specified. Accepts values -65534 to 65534. |
| `xy_inc(value)` | Increments/Decrements the xy co-ordinate by the value specified. Accepts values -0.5 to 0.5. |

There are also a number of convenience functions to provide extra functionality or a more natural language for building
up a desired Light State:
Expand All @@ -752,11 +757,19 @@ up a desired Light State:
| `turnOff()` |Turn the lights off |
| `off()` |Thurn the lights off |
| `brightness(percentage)` |Set the brightness from 0% to 100% (0% is not off)|
| `incrementBrightness(value)` |Alias for the `bri_inc()` function above |
| `colorTemperature(ct)` |Alias for the `ct()` function above|
| `colourTemperature(ct)` |Alias for the `ct()` function above|
| `colorTemp(ct)`| Alias for the `ct()` function above|
| `colourTemp(ct)` |Alias for the `ct()` function above|
| `incrementColorTemp(value)` |Alias for the `ct_inc()` function above |
| `incrementColorTemperature(value)` |Alias for the `ct_inc()` function above |
| `incrementColourTemp(value)` |Alias for the `ct_inc()` function above |
| `incrementColourTemperature(value)` |Alias for the `ct_inc()` function above |
| `saturation(percentage)`| Set the saturation as a percentage value between 0 and 100|
| `incrementSaturation(value)` |Alias for the `sat_inc()` function above |
| `incrementXY(value)` |Alias for the `xy_inc()` function above |
| `incrementHue(value)` |Alias for the `hue_inc()` function above |
| `shortAlert()` |Flashes the light(s) once|
| `alertShort()` |Flashes the light(s) once|
| `longAlert()` |Flashes the light(s) 10 times|
Expand Down
16 changes: 8 additions & 8 deletions hue-api/commands/lights-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
// Hue API documentation, than having it scatter piece meal through various other classes and functions.
//

var Trait = require("traits").Trait,
tApiMethod = require("./traits/tApiMethod"),
tDescription = require("./traits/tDescription"),
tBodyArguments = require("./traits/tBodyArguments"),
tLightStateBody = require("./traits/tLightStateBody"),
tPostProcessing = require("./traits/tPostProcessing"),
ApiError = require("../errors").ApiError,
utils = require("../utils")
var Trait = require("traits").Trait
, tApiMethod = require("./traits/tApiMethod")
, tDescription = require("./traits/tDescription")
, tBodyArguments = require("./traits/tBodyArguments")
, tLightStateBody = require("./traits/tLightStateBody")
, tPostProcessing = require("./traits/tPostProcessing")
, ApiError = require("../errors").ApiError
, utils = require("../utils")
;

var apiTraits = {}
Expand Down
40 changes: 40 additions & 0 deletions hue-api/commands/traits/tLightStateBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,46 @@ module.exports = function (withAlert, withScene) {
minValue: 0,
maxValue: 65535,
optional: true
},

{
name: "bri_inc",
type: "int8",
minValue: -254,
maxValue: 254,
optional: true
},

{
name: "sat_inc",
type: "int8",
minValue: -254,
maxValue: 254,
optional: true
},

{
name: "hue_inc",
type: "int16",
minValue: -65534,
maxValue: 65534,
optional: true
},

{
name: "ct_inc",
type: "int16",
minValue: -65534,
maxValue: 65534,
optional: true
},

{
name: "xy_inc",
type: "float",
minValue: -0.5,
maxValue: 0.5,
optional: true
}
];

Expand Down
120 changes: 119 additions & 1 deletion hue-api/lightstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,60 @@ State.prototype.transitiontime = function (value) {
return this;
};

/**
* Increments/Decrements the brightness value for the lights.
* @param value An amount to change the current brightness by, -254 to 254.
* @returns {State}
*/
State.prototype.bri_inc = function (value) {
this._addValues(_getBrightnessIncrementState(value));
return this;
};

/**
* Increments/Decrements the saturation value for the lights.
* @param value An amount to change the current saturation by, -254 to 254.
* @returns {State}
*/
State.prototype.sat_inc = function (value) {
this._addValues(_getSaturationIncrementState(value));
return this;
};

/**
* Increments/Decrements the Hue value for the lights.
* @param value An amount to change the current hue by, -65534 to 65534.
* @returns {State}
*/
State.prototype.hue_inc = function (value) {
this._addValues(_getHueIncrementState(value));
return this;
};

/**
* Increments/Decrements the color temperature value for the lights.
* @param value An amount to change the current color temperature by, -65534 to 65534.
* @returns {State}
*/
State.prototype.ct_inc = function (value) {
this._addValues(_getCtIncrementState(value));
return this;
};

/**
* Increments/Decrements the XY value for the lights.
* @param value An amount to change the current XY by, -0.5 to 0.5.
* @returns {State}
*/
State.prototype.xy_inc = function (value) {
this._addValues(_getXYIncrementState(value));
return this;
};

State.prototype.scene = function (value) {
this._addValues(_getSceneId(value));
return this;
}
};


///////////////////////////////////////////////////////////////////////
Expand All @@ -205,15 +255,28 @@ State.prototype.brightness = function (percentage) {
return this.bri(_convertBrightPercentToHueValue(percentage));
};

State.prototype.incrementBrightness = State.prototype.bri_inc;

State.prototype.colorTemperature = State.prototype.ct;
State.prototype.colourTemperature = State.prototype.ct;
State.prototype.colorTemp = State.prototype.ct;
State.prototype.colourTemp = State.prototype.ct;

State.prototype.incrementColorTemp = State.prototype.ct_inc;
State.prototype.incrementColorTemperature = State.prototype.ct_inc;
State.prototype.incrementColourTemp = State.prototype.ct_inc;
State.prototype.incrementColourTemperature = State.prototype.ct_inc;

State.prototype.incrementHue = State.prototype.hue_inc;

State.prototype.incrementXY = State.prototype.xy_inc;

State.prototype.saturation = function (percentage) {
return this.sat(_convertSaturationPercentToHueValue(percentage));
};

State.prototype.incrementSaturation = State.prototype.sat_inc;

State.prototype.shortAlert = function () {
return this.alert("select");
};
Expand Down Expand Up @@ -419,6 +482,36 @@ function _getTransitionState(value) {
};
}

function _getBrightnessIncrementState(value) {
return {
bri_inc: _getBrightnessIncrementValue(value)
}
}

function _getSaturationIncrementState(value) {
return {
sat_inc: _getSaturationIncrementValue(value)
}
}

function _getHueIncrementState(value) {
return {
hue_inc: _getHueIncrementValue(value)
}
}

function _getCtIncrementState(value) {
return {
ct_inc: _getCtIncrementValue(value)
}
}

function _getXYIncrementState(value) {
return {
xy_inc: _getXYIncrementValue(value)
}
}

function _getSceneId(value) {
var result = {};

Expand Down Expand Up @@ -454,6 +547,31 @@ function _getTransitionTimeValue(value) {
return valueForType(transition, _getRangeValue(transition, value));
}

function _getBrightnessIncrementValue(value) {
var bri_inc = stateDefinitions.bri_inc;
return valueForType(bri_inc, _getRangeValue(bri_inc, value));
}

function _getSaturationIncrementValue(value) {
var sat_inc = stateDefinitions.sat_inc;
return valueForType(sat_inc, _getRangeValue(sat_inc, value));
}

function _getHueIncrementValue(value) {
var hue_inc = stateDefinitions.hue_inc;
return valueForType(hue_inc, _getRangeValue(hue_inc, value));
}

function _getCtIncrementValue(value) {
var ct_inc = stateDefinitions.ct_inc;
return valueForType(ct_inc, _getRangeValue(ct_inc, value));
}

function _getXYIncrementValue(value) {
var xy_inc = stateDefinitions.xy_inc;
return valueForType(xy_inc, _getRangeValue(xy_inc, value));
}

function convertPercentToValue(definition, percent) {
var range = definition.range
, min = range.min
Expand Down
5 changes: 3 additions & 2 deletions hue-api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ module.exports.valueForType = function(type, value) {

if (type === "bool") {
result = Boolean(value);
} else if (type === "uint8" || type === "uint16") {
} else if (type === "uint8" || type === "uint16" || type === "int8" || type === "int16") {
result = Math.floor(value);
if (result < 0) {

if (/^uint.*/.test(type) && result < 0) {
result = 0;
}
} else if (type === "string") {
Expand Down
Loading

0 comments on commit 9fb2a5c

Please sign in to comment.