Does the Espruino support sleep mode?

Posted on
  • I am trying toevaluate whether the Espruino is suitable for my project.
    I may be asking an obvious question, but I could not find the answer in the documentation.

    In the event loop if nothing happens, does the processer enter the sleep mode to conserve power?

    Does the javascript interpreter let you go into sleep mode and wake up through a command/ subroutine call?

    thank you in advance for any answers.

    with kind regards,
    peter

  • Yes that's explained here. The reference is there and it depends on the particular board you are using: Espruino's do it, others might not...
    Works like a charm on an Espruino 1.4: a pair of batteries lasts for much more than a month (2 years?) !
    You can get a feed back each time your board is awaken by using setSleepIndicator. So as long as it doesn't sleep, the led will stay illuminated.
    In fact that's one of the main benefits of Espruino's interpreter to automatically have the board go to sleep once everything pending has been done and wake up if an interrupt / clock's alarm happens.

  • In Espruino there is no notion of a loop such as in Arduino and a-like. Espruino initializes and runs through your code you upload and then to sleep / idle. Take a look at the various code examples in the tutorials, and forums, reference items, etc. What you see is setups of events and related callbacks.

    Espruino gets really active only when some hardware or timed software event occurs (except you code - by mistake - an infinite loop and prevents it from going sleep/idle).

    For a scenario, assume Espruino should do something every time after your pressed a button (release a momentary on push button), let's say toggle another pin to which an LED is connected. Assume LED is connected that it on when the pin is high (on).

    For the example we will use Esprino's conveniently on-board user BTN button, which is connected to pin B12 and Ground. (BTN - and also BTN1 - are just other 'names' / symbols / references of pin B12). For the LED we use one of the even so conveniently on-board red, green, and blue LEDs - LED1..LED3 - connected to pins A13..A15 and Ground. (Again, LED1..LED3 are just other references for pins A13..A15).

    The code for that looks like this:

    // toggle blue on-board LED LED3 / pin !5
    var toggleLED = function() { 
      if (digitalRead(LED3) === 1) {
          LED3.reset();
      } else {
          LED3.set();
             
      }
    };
    
    // watch pin BTN / B12 pin and for every rising edge toggle the LED
    setWatch(toggleLED, BTN,
             { repeat:true, edge:'rising', debounce:10 });
    

    To make things a bit more interesting, we say that the LED should toggle anyway every 5 seconds. This would then be a software event (driven of course by a hardware timer event ). To achieve this we add the following snipped to our code:

    // set an interval / timer to toggle the LED every 5 seconds
    var iv = setInterval(toggleLED,3000);
    

    Of course, you understand that the button release and interval events happen independent but the effect on the LED is the same.

    Based on this example you can create a reaction time tester or meter:

    Every time the LED comes on, the user has to press the button, and the code measures the time between the LED comes on and the user presses the button.

    Further modifications can be made that a randomized time passes after the button has been released for the LED to go on again...

    For more uses of events and timers see Software Buttons - Many buttons from just one hardware button. The software button uses dynamically created single trigger rising and falling edge events and timeouts.

    Any other device sets up events with callbacks - that a key beauty of Espruino and its secret of extreme power saving.

  • This video describes Javascript event-loop it's in the context of JS in the browser. Espruino has some different IO events to the browser but shares others like timers. I might help get the gist of how Espruino handles stuff.

    Help, I'm stuck in an event-loop

  • As @asez73 says, Espruino handles sleep automatically - you just need to tell it to enable it with setDeepSleep(1), because when enabled the accuracy of the timer drops to around 1/32,000 of a sec when it's waking from sleep (that's the freq of the low frequency oscillator).

    I also need to update the power consumption page - the rev 1v4 board (which is in all the distributors now) has much better power consumption (0.03mA) because of a different voltage regulator.

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

Does the Espruino support sleep mode?

Posted by Avatar for priemen @priemen

Actions