This is a working example I have come up with with your help, maybe it's of any use to others.
/*
Example of an Espruino Object that fires an event when the button on pin D17 is pressed.
*/
var P8 = {
init : function(){
setWatch(function(){
P8.emit("button","D17");
},D17 ,{ repeat:true,debounce:25 }
);
}
}; // end of P8 Object
// ============================= test of P8 object ===============
P8.init();
P8.on('button', function(btn){ print("Button",btn, "press detected");});
Output of the script when the button is pressed:
Button D17 press detected
In this example the init function needs to be called at start of the main script. Not sure if there is a way to move this init into the Object, but this works for me.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
This is a working example I have come up with with your help, maybe it's of any use to others.
Output of the script when the button is pressed:
Button D17 press detected
In this example the init function needs to be called at start of the main script. Not sure if there is a way to move this init into the Object, but this works for me.