Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isPressed() returns false when button is pressed down while resetting the board #388

Open
microbit-carlos opened this issue Nov 2, 2023 · 3 comments
Labels
Milestone

Comments

@microbit-carlos
Copy link
Collaborator

Simple program, on startup if Button A is pressed it displays the letter A, otherwise ?:

#include "MicroBit.h"

MicroBit uBit;

int main() {
    uBit.init();

    if (uBit.buttonA.isPressed()) {
        uBit.display.print('A');
    } else {
        uBit.display.print('?');
    }
    while (true) uBit.sleep(1000);
}

MICROBIT.hex.zip

  • With the board powered off, keeping button A pressed down and inserting the USB cable shows A
  • While powered on, keeping the button A pressed down, and pressing the reset button shows ?
@microbit-carlos
Copy link
Collaborator Author

microbit-carlos commented Nov 2, 2023

With this provided codal.json file, the issue is resolved with MICROBIT_BLE_PAIRING_MODE=1, but breaks with MICROBIT_BLE_PAIRING_MODE=0.

{
    "target": {
        "name": "codal-microbit-v2",
        "url": "https://github.com/lancaster-university/codal-microbit-v2",
        "branch": "master",
        "type": "git"
    },
    "config":{
        "DEVICE_BLE": 1,
        "MICROBIT_BLE_ENABLED" : 0,
        "MICROBIT_BLE_PAIRING_MODE": 0
    }
}

@microbit-carlos
Copy link
Collaborator Author

With MICROBIT_BLE_PAIRING_MODE =0, adding uBit.sleep(100) before the uBit.buttonA.isPressed() (which happens in uBit.init() when pairing mode is enabled) seems to resolve the issue. 🤔

@microbit-carlos
Copy link
Collaborator Author

microbit-carlos commented Dec 10, 2024

This is related to:

Essentially it takes 8 ticks (32ms) before isPressed() can return true, and the first call happens in the Button constructor.

The issue for button A and B were introduced in v0.2.60, in commit d083143. This is because as part of that refactor there was two 10ms delays (one in the Acc and another in the Mag constructors), but the LSM only needs ~7ms, so this was reduced from 20ms to 7ms. This reduced the uBit initialisation time by 13ms, which is long enough for this issue to appear. To confirm this, adding a 12ms delay before buttonA.isPressed() is enough for it to return the correct value.

The reason the issue is not triggered when MICROBIT_BLE_PAIRING_MODE is enabled is because this feature adds additional time in the uBit.init(), when it checks the button state for entering pairing mode when A+B are pressed on startup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants