• Hi,

    I would like to create my own event handlers similar like the Bangle event handlers, f.i. the "step" handler.

    Bangle.on('step', function(up) {
      print("step was made");

    Tried this example from https://www.w3schools.com/nodejs/nodejs_­events.asp

    var events = require('events');
    var eventEmitter = new events.EventEmitter();
    //Create an event handler:
    var myEventHandler = function () {
      console.log('I hear a scream!');
    //Assign the event handler to an event:
    eventEmitter.on('scream', myEventHandler);
    //Fire the 'scream' event:

    But this results in the error:

    I read in the Espruino documentation here http://www.espruino.com/Reference#l_Obje­ct_emit about Object.emit and Object.on but can not find any reference or example on how to setup an eventEmitter in Espruino.

    I'm rather new to Espruino/Javascript so maybe there's an obvious answer. Can someone point me in the right direction?

  • There are some examples. I recommend that you download/clone the BangleApps repo, https://github.com/espruino/BangleApps. And use a decent IDE/text editor that can search through all the projects in there.
    I searched for keyword "emit" and found an example.


    From briefly looking at the examples, its enough to call

    var myobj = new Object();

    and later


    The "Object" class is a compatible EventEmitter by default.

  • Thanks for the tip, why didn’t I think of that.

    Have been searching the doc and forum for some clues how to do, but found nothing. Would be great for others if a simple example is added to the doc, which is by the way excellent.

    Will try to make a working example today and post it here.


  • There are lots of details on the reference page

    eg object class where you find .emit() and .on().

  • Hi Marck,

    Yes, I saw the object class with emit and on in the dock, realised that this was what I needed, but had no idea how to setup the object. I'm an Arduino guy, so this object stuff is new to me. So for Espruino beginners an working example really helps.

  • Would code examples added to http://www.espruino.com/Reference#l_Obje­ct_on and http://www.espruino.com/Reference#l_Obje­ct_emit help at all? That would be an easy change in a reasonably obvious place...

  • Based on the tip from @d3nd3-o0 I changed the example in message #1 to:

    var eventEmitter = new Object();
    //Create an event handler:
    var myEventHandler = function () {
      console.log('I hear a scream!');
    //Assign the event handler to an event:
    eventEmitter.on('scream', myEventHandler);
    //Fire the 'scream' event:

    The result:
    I hear a scream!

    So that works!

    So, there is no need in Espruino to require an events module, instead you just create an object, and use that as event emitter.

    Thanks to @d3nd3-o0 for guiding me in the right direction!

  • @gordon. YES, a simple example like in my previous post would have helped me getting this working. And YES, adding such an example to the doc for on/emit is the perfect place.


  • So have fun and use this forum for further questions, you will find many members that are willing to support others.

    But keep in mind if you are not using original boards you should send a donation to Gordon.

  • Thanks for the nice example, would have saved many hours of my life ;-)

    My apologies for being thick, but the example on Github will automagically appear in the Espruino Reference, right?

  • Thanks for the "nudge", DONE!

  • @Gordon, the names used in the brief explanation / example is the issue... 'We' who are used to objects and the event emissioning and listening (as for example NodeJS has implemented it), the frugal examples are 'understood' / 'interpreted' the right way, last but not least the names are used all over the place in such environments and type and purpose of their elements/arguments are clear to us:


    Just adding some comments already helps:

    o.on("data",foo); // o: any object, "data": name/id of the event
    o.on("data",bar); // foo,bar: listening functions, accepting 2nd... arg of .emit()
    o.emit("data","Hallo"); // triggering listening functions passing one arg

    There would be more to say, but your changes in doc make it understandable, and also the link to node js doc helps. A hint about what to search for would be even more helpful: Google / Search the web for 'node js emit on'

  • Ok - I've just added some comments :)

  • 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(){
        },D17 ,{ repeat:true,debounce:25 }
    }; // end of P8 Object
    // ============================= test of P8 object  ===============
    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.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

How the create an event handler similar to Bangle.on("step"...........)

Posted by Avatar for gerardwr @gerardwr