-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Tile support #25
base: main
Are you sure you want to change the base?
Add Tile support #25
Changes from all commits
c3ff527
30e90a5
a8fa1ce
240ec77
7808c8a
670c091
043f0be
65e73ab
9ba76a1
7d6e761
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use strict'; | ||
|
||
const LifxClient = require('../src/lifx').Client; | ||
|
||
const client = new LifxClient(); | ||
|
||
const LOOPTIME = 1000; | ||
const TILE_LABEL = process.argv.length > 2 ? process.argv[process.argv.length - 1] : '*'; | ||
|
||
/* | ||
* This example should show on all tiles of | ||
* your chain a random pattern. After all bits | ||
* of the tiles are set with the random pattern, | ||
* the example reads back all set values | ||
* from the tiles just to test the read back function. | ||
* I could not compare the set values out of the reason | ||
* that the setvalues are usally a bit modified. | ||
* | ||
* EASY: You should see an updating Pattern every two second | ||
*/ | ||
|
||
function getBits(light, idx, chain) { | ||
if (idx >= chain.totalCount) { | ||
console.log('All Bits get'); | ||
setTimeout(() => setBits(light, 0, chain), LOOPTIME); | ||
return; | ||
} | ||
console.log('getBits', idx); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this debugging info that needs to be removed? There are a few instances like these. Do we want to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it an test example programm, so why not 'log' that something is happens here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these are fine, they indicate at least what part of the code is currently executing. They could of course be more informative as I'm suspecting the API might be quite difficult to graps for many. |
||
light.getTileState64(idx, (err) => { | ||
if (err) { | ||
console.error('getTileState64:', err); | ||
return; | ||
} | ||
getBits(light, idx + 1, chain); | ||
}); | ||
} | ||
|
||
function setBits(light, idx, chain) { | ||
if (idx >= chain.totalCount) { | ||
setTimeout(() => getBits(light, 0, chain), LOOPTIME); | ||
return; | ||
} | ||
console.log('setBits', idx, chain.totalCount); | ||
const ofs = ~~(Math.random() * (65536 - (65536 / 64))); | ||
light.setTileState64(idx, | ||
Array(64).fill(undefined).map((_, idx) => ({ | ||
hue: (ofs + (idx * (65536 / 64))) & 0xffff, | ||
saturation: 50000, | ||
brightness: 16384, | ||
kelvin: 4096 | ||
})), {duration: 100}, () => setBits(light, idx + 1, chain)); | ||
} | ||
|
||
client.on('light-new', (light) => { | ||
console.log(TILE_LABEL, light.id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this |
||
if (!(TILE_LABEL === '*' || TILE_LABEL === light.id)) { | ||
return; | ||
} | ||
console.log('New light found.'); | ||
console.log('ID: ' + light.id); | ||
|
||
light.getDeviceChain(function(err, chain) { | ||
if (err) { | ||
console.log(err); | ||
} | ||
light.on(0, () => { | ||
setBits(light, 0, chain); | ||
}); | ||
}); | ||
}); | ||
|
||
// Give feedback when running | ||
client.on('listening', function() { | ||
const address = client.address(); | ||
console.log( | ||
'Started LIFX listening on ' + | ||
address.address + ':' + address.port + '\n' | ||
); | ||
}); | ||
|
||
client.init(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
const Packet = { | ||
size: 0 | ||
}; | ||
|
||
module.exports = Packet; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
|
||
const {validate} = require('../../lifx'); | ||
|
||
const Packet = { | ||
size: 6 | ||
}; | ||
|
||
/** | ||
* @typedef {Object} GetTileState64 | ||
* @property {Number} tileIndex an 8bit value | ||
* @property {Number} length an 8bit value | ||
* @property {Number} reserved an 8bit value | ||
* @property {Number} x an 8bit value | ||
* @property {Number} y an 8bit value | ||
* @property {Number} width an 8bit valu | ||
*/ | ||
|
||
/** | ||
* Converts the given packet specific object into a packet | ||
* @param {GetTileState64} obj object with configuration data | ||
* @return {Buffer} packet | ||
*/ | ||
Packet.toBuffer = function(obj) { | ||
const buf = Buffer.alloc(this.size); | ||
buf.fill(0); | ||
let offset = 0; | ||
|
||
['tileIndex', 'length', 'reserved', 'x', 'y', 'width'].forEach((field) => { | ||
validate.isUInt8(obj[field], `getTileState64:${field}`); | ||
}); | ||
buf.writeUInt8(obj.tileIndex, offset); | ||
offset += 1; | ||
buf.writeUInt8(obj.length, offset); | ||
offset += 1; | ||
buf.writeUInt8(obj.reserved, offset); | ||
offset += 1; | ||
buf.writeUInt8(obj.x, offset); | ||
offset += 1; | ||
buf.writeUInt8(obj.y, offset); | ||
offset += 1; | ||
buf.writeUInt8(obj.width, offset); | ||
offset += 1; | ||
|
||
return buf; | ||
}; | ||
|
||
/** | ||
* Converts the given packet specific object into a packet | ||
* @param {Buffer} buf object with configuration data | ||
* @return {GetTileState64} packet | ||
*/ | ||
Packet.toObject = function(buf) { | ||
const obj = {}; | ||
let offset = 0; | ||
|
||
obj.tileIndex = buf.readUInt8(offset); | ||
offset += 1; | ||
obj.length = buf.readUInt8(offset); | ||
offset += 1; | ||
obj.reserved = buf.readUInt8(offset); | ||
offset += 1; | ||
obj.x = buf.readUInt8(offset); | ||
offset += 1; | ||
obj.y = buf.readUInt8(offset); | ||
offset += 1; | ||
obj.width = buf.readUInt8(offset); | ||
offset += 1; | ||
|
||
return obj; | ||
}; | ||
|
||
module.exports = Packet; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
|
||
const {validate} = require('../../lifx'); | ||
|
||
const Packet = { | ||
size: 8 | ||
}; | ||
|
||
/** | ||
* @typedef {Object} HSBK | ||
* @property {Number} hsbk.hue - hue value | ||
* @property {Number} hsbk.saturation - saturation value | ||
* @property {Number} hsbk.brightness - brightness value | ||
* @property {Number} hsbk.kelvin - kelvin value | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} OffsetHSBK | ||
* @property {Number} offset - offset after the buffer | ||
* @property {HSBK} hsbk - HSBK value | ||
* | ||
*/ | ||
|
||
/** | ||
* Converts the given packet specific object into a packet | ||
* @param {Buffer} buf hsbk as buffer | ||
* @param {Number} offset offset to the buffer | ||
* @return {OffsetHSBK} packet | ||
*/ | ||
Packet.toObject = function(buf, offset) { | ||
const hsbk = {}; | ||
hsbk.hue = buf.readUInt16LE(offset); | ||
offset += 2; | ||
hsbk.saturation = buf.readUInt16LE(offset); | ||
offset += 2; | ||
hsbk.brightness = buf.readUInt16LE(offset); | ||
offset += 2; | ||
hsbk.kelvin = buf.readUInt16LE(offset); | ||
offset += 2; | ||
return {offset, hsbk}; | ||
}; | ||
|
||
/** | ||
* @typedef {Object} OffsetBuffer | ||
* @property {number} offset - offset after the buffer | ||
* @property {Buffer} buffer - buffer | ||
*/ | ||
/** | ||
* Converts the given packet specific object into a packet | ||
* @param {Buffer} buffer output buffer | ||
* @param {Number} offset offset in the output buffer | ||
* @param {HSBK} hsbk offset in the output buffer | ||
* @return {OffsetBuffer} packet | ||
*/ | ||
Packet.toBuffer = function(buffer, offset, hsbk) { | ||
validate.isUInt16(hsbk.hue); | ||
validate.isUInt16(hsbk.saturation); | ||
validate.isUInt16(hsbk.brightness); | ||
validate.isUInt16(hsbk.kelvin); | ||
buffer.writeUInt16LE(hsbk.hue, offset); | ||
offset += 2; | ||
buffer.writeUInt16LE(hsbk.saturation, offset); | ||
offset += 2; | ||
buffer.writeUInt16LE(hsbk.brightness, offset); | ||
offset += 2; | ||
buffer.writeUInt16LE(hsbk.kelvin, offset); | ||
offset += 2; | ||
return {offset, buffer}; | ||
}; | ||
|
||
module.exports = Packet; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want this to come from the command line execution or could it be done differently? Also, there's a library
yargs
which makes it a lot easier to pull in command line parameters. If we're doing this in Node.js anyway, why not pull it in from a config when creating the LIFX client?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree on this one. At least the script should fail if run without arguments and instruct on the correct usage. Example: