Polling vs events at same frequency

Posted on
  • If there is an event which fires at a regular frequency, is it more battery efficient to watch that event vs. polling at the same frequency?

  • setWatch can watch only pins, so if your "event" is pin change then it is the best choice as it fires exactly at the right moment and does not miss. How would you do the polling, can you show the code? polling feels complex, what would be the advantage?

  • I don't know what the advantage would be, if any. I ask because I was looking at the source of the Sleep Phase Alarm app and realised it uses polls for the accelerometer reading instead of using Bangle.on("accel", ...).

  • l0oks like it needs to feed calc_ess periodically, the event on change would probably not give you such regular timing. Anyway interval doing such math every 80ms can drain a battery but looks like it run for max. 30 minutes before sceduled alarm so may not drain too much. also it never stops that 80ms interval (relying on load() to clean up?)

  • The event is very regular. I tried the app and I woke up late with a flat battery, which is why I looked at the source. I don't think it is only running for max 30 minutes. If I am not misreading something it does the calculations all night but only triggers the alarm max 30 minutes before the alarm is due to go off. I think only starting the calculations a certain amount of time before the alarm is due would spare the battery, but I wondered whether changing the way it gets the accelerometer values would help too.

  • 0h, you're right, so that is wasteful indeed, doing it only in the interval of possible wake up would be enough. as for poll vs event, it doesn't matter if the event is fired with same frequency or setInterval is used, in both cases it drains battery by same amount of cpu computation. moving the math to native code could save a lot. Not sure how current step counting works but the trick to save power is to let the accelerometer work in batched mode - let it fill buffer while cpu sleeps and handle the buffer of more samples in one go

  • The step counter doesn't do that . I was going to ask about it, because I had a look at the documentation for the accelerometer and saw that we could, but I'm not sure if it is plausible because something else might consume the buffer before the step counter does (don't "taps" require the accelerometer too?). Or can the accelerometer provide the latest value while still filling the buffer until the step counter needs it?

  • Just to say I'd always recommend using the event rather than polling.

    While in reality there may not be a big difference in power usage, if you're polling you risk getting the same reading twice (or skipping one) if there's a minor difference in timing between the accelerometer and the interval.

    Also, it looks to me like calc_ess does some things in pure JS that Espruino already provides utility functions for. E.sum and E.variance should do what's needed on the ess_values array really quickly.

    There is now the health event every 10 minutes which has a 'movement' field too, and it's possible that could be used instead with basically zero extra calculation needed.

  • Do we know if the authors of the app are active on the forum?

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

Polling vs events at same frequency

Posted by Avatar for myownself @myownself

Actions