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

Allow Bluetooth to be toggled off/on with Bluetooth button. #247

Merged
merged 10 commits into from
Jun 6, 2024

Conversation

laurensvalk
Copy link
Member

Implements toggling Bluetooth on and off with the Bluetooth button on Spike Prime and Mindstorms Robot Inventor.

See pybricks/support#1615 for context, use cases, and user interface considerations.

This is a slightly more minimalist approach compared to #195. It adds PBIO_PYBRICKS_STATUS_BLUETOOTH_BLE_ENABLED to the protocol instead of PBIO_PYBRICKS_STATUS_BLUETOOTH_BUTTON_PRESSED and PBIO_PYBRICKS_STATUS_BLUETOOTH_SHUTDOWN.

The hmi is implemented similar to the existing code for the start/stop program button, and separated from the actual Bluetooth start/stop logic. The hmi may request a Bluetooth toggle, but it is up to sys/bluetooth whether to do anything with that. This prevents Bluetooth from being toggled if is currently being used or while a program is running.

The PBIO_PYBRICKS_STATUS_BLUETOOTH_BLE_ENABLED flag is only used for raising exceptions when the user tries to create remote peripheral or broadcast/observe.

Persistency is a separate task that can be added later. This was holding up previous merges, but we should be able to get started without it.

Thanks to everyone for asking about this feature and thanks to @nkarstens for creating #195!

The code:
   if pressed then user_program_start(true)

made it read as if this call was starting the code on pressing the button, which isn't quite what this is doing.

So rename to make this clearer, since we'll be adding a similar routine for the Bluetooth button UI.
@coveralls
Copy link

Coverage Status

coverage: 56.467% (-0.1%) from 56.563%
when pulling 395315f on toggle-bluetooth
into 79a0e01 on master.

It doesn't take that many bytes and it helps keep the code much cleaner.
Using advertising as the debounce state only applies when Bluetooth is enabled.
Being not-connected naturally extends to USB when we add it.
This is less fragile than starting and stopping the process.
This reverts 8c8340b.

At this point, we don't need an extra status flag. Bluetooth is the only communication method, and we can't communicate a status if it is disabled.

So we can avoid changing the protocol for now. We can add appropriate flags if we add USB support, in which case communicating the active connection type may make sense.
@coveralls
Copy link

Coverage Status

coverage: 56.454% (-0.1%) from 56.563%
when pulling 3e29eb3 on toggle-bluetooth
into 79a0e01 on master.

@laurensvalk
Copy link
Member Author

In rare occasions it can get stuck at the bottom of the pbsys/bluetooth process.

This seems odd as it would have traversed this path too all the time so far when running a really short program. Or maybe we just haven't noticed it before?

        // reset Bluetooth chip
        pbsys_status_light_bluetooth_set_color(PBIO_COLOR_GREEN); // debug light
        pbdrv_bluetooth_power_on(false);
        PROCESS_WAIT_WHILE(pbdrv_bluetooth_is_ready());             // light can get stuck staying green
        pbsys_status_light_bluetooth_set_color(PBIO_COLOR_RED); // debug light

@nothingtodowu
Copy link

I have a question that if I want set bluetooth off by default, how to change this? Thanks.

@laurensvalk
Copy link
Member Author

I have a question that if I want set bluetooth off by default, how to change this? Thanks.

You no longer need to use builds from this pull request.

You can use the nightly build which already includes this feature.

The setting also persists. So Bluetooth will still be off when you reboot the hub.

@nothingtodowu
Copy link

Thanks a lot.

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

Successfully merging this pull request may close these issues.

4 participants