01.07.2016, 07:46
Small example: play a beep sound each time 1/1/1 value is set to true. Note that it will only work with the upcoming firmware.
Code:
$(function(){
var locked;
var ctx = new(window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext);
function beep(duration, freq, cb) {
var osc = ctx.createOscillator();
osc.frequency.value = freq;
osc.connect(ctx.destination);
osc.start ? osc.start(0) : osc.noteOn(0);
setTimeout(function () {
osc.disconnect();
if (typeof cb == "function") {
cb();
}
}, duration);
}
/*
beep each time value of 1/1/1 is true
note that 3rd listen parameter must be set to true
to execute the callback each time new value arrives
otherwise callback is executed only when value changes
*/
grp.listen('1/1/1', function(object, state) {
if (state == 'value' && object.value && !locked) {
locked = true;
beep(200, 432, function() {
locked = false;
});
}
}, true);
});