First Commit

This commit is contained in:
Caleb Braaten 2024-04-03 18:04:39 -07:00
commit 4ced372eff
4 changed files with 2152 additions and 0 deletions

23
README.md Normal file
View File

@ -0,0 +1,23 @@
# HUE LightShow
## Motivation
For the gender reveal of my second kid, I wanted to do something unique and fun. It was the perfect excuse to justify the purchase of a Philips Hue Lightstrip to the Mrs.
I wrapped the light strip around our Christmas tree and wrote some Javascipt to alternate between blue and pink. There were a couple iterations
## Technical Details
`lib.js` contains some helper functions (and authentication to the Hue Bridge) that leverage the Philips Hue REST API. These functions are used in `main.js` to control the light strip.
The first iteration of the light show was a simple alternating sequence that loaded all the color transitions into the event loop at the start with the setTimeout progressively having a longer delay to create the sequence.
The linear nature was a bit boring, so I pulled in a [bezier.js library](https://github.com/Pomax/bezierjs/blob/master/src/utils.js) (Thank you [Pomax](https://github.com/Pomax)) to create a more dynamic pacing of the sequence.
Following the same setTimeout strategy, I created a list of values from the bezier curve to use as the root of the setTimeout delay making the sequence speed up and then slow down to a stop. Once the sequence was complete, the final color would start to glow in and out.
## Video
Here is the final product. Please be aware, the video contains rapid flashes near the beginning. If you are sensitive to flashing lights and still want to watch it, consider skipping to the middle of the video where the sequence slows down considerably.
[![HUE Light Show](https://img.youtube.com/vi/1Q2J9zv3Q1Y/0.jpg)](https://photos.app.goo.gl/Qst3tRiBQrFXAEuZ6)

1973
bezier.js Normal file

File diff suppressed because it is too large Load Diff

91
lib.js Normal file
View File

@ -0,0 +1,91 @@
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const baseURL = 'https://192.168.254.49/clip/v2/resource/'
const resource = 'light/c84fd63f-aa24-4203-9d92-dfa15e65bd54'
const headers = {
'Content-Type': 'application/json',
'hue-application-key': 'NpvdpFNOk3ROMGeWi2R598Jz31zyZ8Eiv5HC3S2o'
};
function setBlue(delay){
const body = `{
"color": {
"xy": {
"x":0.16,
"y":0.08
}
},
"dynamics": {
"duration": 0
}
}`
console.log("Send Blue @", delay)
sendRequest(body)
}
function setPink(delay){
const body = `{
"color": {
"xy": {
"x":0.45,
"y":0.20
}
},
"dynamics": {
"duration": 0
}
}`
console.log("Send Pink @", delay)
sendRequest(body)
}
function sendRequest(body){
try {
fetch(`${baseURL}${resource}`, {
method: 'PUT',
headers,
body
})
.then(
(res) => {
// console.log("Status:", res.status)
return res.json()
}
)
.then(
// (res) => console.log("Body:", res)
)
} catch (error) {
console.log(error)
}
}
function glow(delay){
const body = `{
"dimming": {
"brightness":100.0
},
"dynamics": {
"duration": 1000
}
}`
const body2 = `{
"dimming": {
"brightness":30.0
},
"dynamics": {
"duration": 1000
}
}`
console.log("Send Glow (up and down) @", delay)
setTimeout(sendRequest, 1000, body)
setTimeout(sendRequest, 2000, body2)
}
module.exports = { setBlue, setPink, glow }

65
main.js Normal file
View File

@ -0,0 +1,65 @@
const Bezier = require("./bezier");
const hue = require("./lib");
// hue.setBlue()
// hue.setPink()
// Linear Rendering (Working)
// for(let i = 0; i < 101; i++){
// if(i % 2 === 0){
// setTimeout(hue.setBlue, i*100)
// }
// else{
// setTimeout(hue.setPink, i*100)
// }
// }
// console.warn("Done Scheduling Events")
// If bezzier doesn't work, go back to lame segmentation
// for(let i = 0; i < 101; i++){
// if(i % 2 === 0){
// if(i < 10){
// setTimeout(hue.setBlue, i*100)
// }
// else if (10 < i < 20){
// }
// else if (20 < )
// }
// else{
// setTimeout(hue.setPink, i*100)
// }
// }
// console.warn("Done Scheduling Events")
// Linear Rendering (Working)
const b = new Bezier(0, 400, 1000, 100, 100, 100, 1000, 400);
let points = b.getLUT();
// + (50 * points[i].x)
for (let i = 0; i < points.length; i++) {
console.log(i * points[i].y);
}
for (let i = 0; i < 100; i++) {
var delay = i * points[i].y;
if (i % 2 === 0) {
setTimeout(hue.setBlue, delay, delay);
} else {
setTimeout(hue.setPink, delay, delay);
}
}
setTimeout(
() => {
hue.glow();
for (let i = 1; i < 100; i++) {
setTimeout(hue.glow, i * 2000, i * 2000);
}
},
delay,
delay,
);
console.warn("Done Scheduling Events");