Last active
February 9, 2019 23:49
-
-
Save oschettler/28f1e9d5f966972181382c59cb0a28cd to your computer and use it in GitHub Desktop.
Control an Elgato Avea Bulb with a Puck.JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Control an Elgato Avea Bulb: | |
* | |
* Single click - toggle on/off | |
* Double click - cycle through three brightness levels | |
* | |
* License: MIT | |
* (c) 2019 Olav Schettler <olav@schettler.net> | |
*/ | |
var values = [ | |
[ 0x57, 0xff, 0x3 ], | |
[ 0x57, 0xff, 0x7 ], | |
[ 0x57, 0xff, 0xf ] | |
], | |
brightness = 2, | |
click_timer = null, | |
active_timer = null, | |
click_count = 0, | |
on = false, | |
active = false; | |
function setLight(isOn) { | |
if (active) { | |
console.log("Ueberrannt"); | |
LED1.write(true); | |
setTimeout(function() { LED1.write(false); }, 20); | |
return; | |
} | |
active = true; | |
active_timer = setTimeout(function() { | |
active = false; | |
console.log("Timeout"); | |
}, 10000); | |
var gatt; | |
// Paul: f4:b8:5e:95:7c:20 | |
NRF.connect("78:a5:04:50:d6:53").then(function(g) { | |
// ^^^^^^^^^^^^^^^^^ your light's address here | |
LED2.write(true); | |
setTimeout(function() { LED2.write(false); }, 20); | |
console.log("Verbunden"); | |
gatt = g; | |
return gatt.getPrimaryService("f815e810-456c-6761-746f-4d756e696368"); | |
}, function(e) { | |
LED1.write(true); | |
setTimeout(function() { LED1.write(false); }, 20); | |
console.log("Nicht verbunden: " + e); | |
}).then(function(service) { | |
return service.getCharacteristic("f815e811-456c-6761-746f-4d756e696368"); | |
}).then(function(characteristic) { | |
return characteristic.writeValue( | |
isOn | |
? values[brightness] | |
: [0x57, 0, 0] | |
); | |
}).then(function() { | |
gatt.disconnect(); | |
clearTimeout(active_timer); | |
active = false; | |
console.log("Fertig!"); | |
}); | |
} | |
setWatch(function() { | |
click_count++; | |
if (click_timer === null) { | |
click_timer = setTimeout(function() { | |
clearTimeout(click_timer); | |
if (click_count < 2) { | |
on = !on; | |
} | |
else { | |
brightness++; | |
if (brightness >= values.length) { | |
brightness = 0; | |
} | |
console.log("Brightness: " + brightness); | |
on = true; | |
} | |
setLight(on); | |
click_timer = null; | |
click_count = 0; | |
}, 400); | |
} | |
}, BTN, { repeat:true, edge:"rising", debounce:50 }); | |
/** | |
* Future: long press | |
setWatch(function(e) { | |
if(e.time-e.lastTime > 0.5){ // on long press slide goes one step back | |
sendPrev(); | |
} | |
}, BTN, {edge:"falling", debounce:50, repeat:true}); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment