Improve Bluetooth de-initialization #389
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
RemoteandLWP3Deviceclasses 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 scheduledpbio/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.