Skip to content

Conversation

@laurensvalk
Copy link
Member

pbio/main: Wait for Bluetooth end at pbio.

Other than safely freeing allocated memory, application code shouldn't have to
worry about deinitializing the Bluetooth driver, so move it towards the pbio
layer. Disconnecting from peripherals at the end is not unlike stopping motors etc


pybricks.pupdevices.lwp3device: Use finalizers.
pybricks.common: Pass parent object to keypad and light.
pybricks.pupdevices.Remote: Clean up self references.

For technical and historical reasons, the Remote and LWP3Device classes were a bit of a mess, with various different singletons and object types that were almost but not quite working like MicroPython objects. On top of this, the object types were partially shared between the Remote and LWP3Device class. We were using static buffers in various places to deal with scheduled pbio/tasks, which we no longer need to do.

This refactors the module to use real MicroPython objects with the normal self_in convention wherever we can. We can make a reference to the global instance so that the driver callbacks know where to put the data, and use the finalizer to unset it for safety. Then we don't need separate logic for the LWP3Device notification buffer either, which is just allocated as part of the object.

Now that each method has a self reference, we can relatively easily upgrade this code to use more than one peripheral in the future.

Implement poweroff
The Bluetooth chip is now powered off on shutdown. This was a remaining task in #388.

Other than safely freeing allocated memory, application code shouldn't have to
worry about deinitializing the Bluetooth driver, so move it towards the pbio
layer. Disconnecting from peripherals at the end is not unlike stopping motors etc.
Finish an old todo about using finalizers to clean up observe data and lwp3 notification buffer.
There may be more than one in general, so pass relevant reference.
For technical and historical reasons, these classes were a bit of a mess, with various different singletons and object types that were almost but not quite working like MicroPython objects. On top of this, the object types were partially shared between the Remote and LWP3Device class.

This refactors the module to use real MicroPython objects with the normal self_in convention wherever we can. We can make a reference to the global instance so that the driver callbacks know where to put the data, and use the finalizer to unset it for safety. Then we don't need separate logic for the LWP3Device notification buffer either, which is just allocated as part of the object.

Now that each method has a self reference, we can relatively easily upgrade this code to use more than one peripheral in the future.
Before the Bluetooth overhaul, we were just powering down. In the recent commits, this was not yet implemented, so the hub would stay connected or keep advertising once turned off and charging.

Poweroff is now back, but we also gracefully disconnect first to be nice to Pybricks Code.
@coveralls
Copy link

Coverage Status

coverage: 59.39%. first build
when pulling 66ae6c4 on btfinalizer
into 0b30a63 on master.

@laurensvalk laurensvalk merged commit 66ae6c4 into master Sep 29, 2025
32 checks passed
@laurensvalk laurensvalk deleted the btfinalizer branch September 29, 2025 18:19
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.

3 participants