lancaster-university / codal-microbit-v2

CODAL target for the micro:bit v2.x series of devices
MIT License
44 stars 52 forks source link

MakeCode power extension programme does not go to sleep #203

Closed microbit-carlos closed 1 year ago

microbit-carlos commented 2 years ago

Description

This programme does seem to go to sleep, neither in USB or battery. Although it's possible it goes to sleep but something wakes it up immediately.

Changing the [request low power] for a [request low power for [10000] ms] block does work.

Test Programme

image
input.onButtonPressed(Button.A, function () {
    basic.showString("A")
    power.lowPowerRequest()
})
basic.forever(function () {
    basic.showNumber(0)
    basic.showIcon(IconNames.Heart)
})

Hardware & Software

Tested on a V2.2 using the micro:bit power management extension and MakeCode live.

MakeCode info:

Extension:

Share link and hex:

JohnVidler commented 2 years ago

Placing this in the v0.2.41 release for now, mainly to facilitate tracking it

martinwork commented 2 years ago

The power manager blocks low power mode if there are no wake up sources or if a timed wake up is due to trigger soon. A button handler does not automatically create a wake up source. See https://makecode.microbit.org/_Xwxhku8zqVUm. The forever loop has a pause because of https://github.com/lancaster-university/codal-microbit-v2/issues/201#issuecomment-1214165440 image

microbit-carlos commented 2 years ago

Thanks for the clarification Martin! So by adding the [full power on [button A]] block it then goes to sleep.

Was this done for a specific technical reason? In my opinion it should still go to sleep even if a wake up source wasn't configured, but there might be a reason why that's not preferable?

martinwork commented 2 years ago

In my opinion it should still go to sleep even if a wake up source wasn't configured

I guess we didn't think of a use for that. This controls whether sleep happens. https://github.com/lancaster-university/codal-microbit-v2/blob/master/source/MicroBitPowerManager.cpp#L708

It looks like the only technical consideration is if a time based wake up is about to trigger. In fact, I think it will always wake up when the USB is connected, and we didn't think we needed a control for that.

JohnVidler commented 2 years ago

I think this behaviour is now as expected as of 0.2.42, but I'm going to leave this open until I've confirmed this with a beta MakeCode build.

microbit-carlos commented 2 years ago

MakeCode test build with CODAL 0.2.42:

microbit-carlos commented 2 years ago

I think this behaviour is now as expected as of 0.2.42, but I'm going to leave this open until I've confirmed this with a beta MakeCode build.

I think the behaviour described in the original comments (not going to sleep unless a wake up source is configured) was by design in the extension? (which we might want to change)

Has something changed in CODAL to change this?

microbit-carlos commented 2 years ago

Testing this with the latest CODAL and it doesn't look like CODAL is going to sleep, so not something within the extension, but something to change in CODAL:

#include "MicroBit.h"

MicroBit uBit;

int main() {
    uBit.init();

    while (true) {
        uBit.power.deepSleep();
        uBit.display.print("A");
        uBit.sleep(500);
    }
}

MICROBIT.hex.zip

martinwork commented 2 years ago

See https://github.com/lancaster-university/codal-microbit-v2/blob/master/source/MicroBitPowerManager.cpp#L740

It's probably returning here https://github.com/lancaster-university/codal-microbit-v2/blob/master/source/MicroBitPowerManager.cpp#L773

JohnVidler commented 2 years ago

Done. Essentially removed the checks for going to a 'blind' deep sleep, and allow the board to do it. Furthermore, its now possible to wake the board with serial data from USB by default, so there should always be a wake source of some kind by default, even if the user never configures one (unless they manually disable wake-on-USB, of course)

JohnVidler commented 2 years ago

This is all now merged into master, pending being tagged - but I'll leave this open until we've built againts PXT and confirmed that this is solved... but I think this is now solved :)

microbit-carlos commented 2 years ago

Test URL for v0.2.43: https://makecode.microbit.org/app/37f6efa929bcadb89d3f5b689c0cf3cae27e49d1-df227f1388

microbit-carlos commented 1 year ago

@JohnVidler can you test this with the latest CODAL pxt test deployment?

microbit-carlos commented 1 year ago

This working in the latest CODAL/MakeCode.