Skip to content

Add JSON characteristic #162

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

Merged
merged 11 commits into from
Apr 23, 2022
Merged

Conversation

mraleson
Copy link
Contributor

@mraleson mraleson commented Apr 16, 2022

Adds a JSON characteristic so python arrays, dicts, etc can be set/get easily on a connected ble device. This was very helpful for fast prototyping and debugging. I found it difficult to send large values of the UART due to the small buffer, lack of flow control, reliable messaging etc. It also has an advantage over making many individual characteristics, the Nordic chip I was using on the Feather Sense was unable to support the number of individual characteristics I needed without running out of memory. I also considered a msgpack variant, but it didn't save enough space for me to justify the lack of debugging utility. One consideration is the size is limited by the characteristic buffer size.

@mraleson mraleson force-pushed the json-characteristic branch from 37bff64 to 65f350b Compare April 16, 2022 20:31
@FoamyGuy
Copy link
Contributor

Hi @mraleson, Thanks for working on this. It seems like some neat and useful functionality.

I would recommend to also go ahead and add a new script inside of the examples/ directory that shows an example usage of this new type of characteristic (or two scripts if it needs two things to communicate between similar to some of the other examples).

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting idea, and adds convenience.

__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"


class JsonCharacteristic(Characteristic):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you make this a subclass of StringCharacteristic, that would seem to save a few encoding/decoding steps?

Copy link
Contributor Author

@mraleson mraleson Apr 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this initially, but because of the packing/unpacking of initial_value in init and also in the setter getter inheriting from StringCharacteristic it ended up not really saving any typing. It also added an additional inheritance layer that hid attributes like max_length.

__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"


class JsonCharacteristic(Characteristic):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use JSON instead of Json. The former is typical in existing Python libraries, including those that are included in CPython.

Suggested change
class JsonCharacteristic(Characteristic):
class JSONCharacteristic(Characteristic):

`json`
====================================================

This module provides Json characteristic.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this grammatical, and expand on what is going on a bit more.



class JsonCharacteristic(Characteristic):
"""Json string characteristic for JSON serializable values of a limited size (max_length)."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Json string characteristic for JSON serializable values of a limited size (max_length)."""
"""JSON string characteristic for JSON serializable values of a limited size (max_length)."""

#
# SPDX-License-Identifier: MIT

# Read sensor readings from peripheral BLE device using JSON characteristic.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix grammar -> "a JSON characteristic" , or similar.

@mraleson
Copy link
Contributor Author

mraleson commented Apr 19, 2022

Hi all, I updated the code with some examples and I took a stab at resolving the requested changes. I haven't had a chance to test again with all of the changes in their entirety. Please let me know if there is a process you follow for testing and I can do that as well.

@mraleson mraleson requested a review from dhalbert April 19, 2022 21:12
@tekktrik
Copy link
Member

Hey @mraleson looks like this is failing on pre-commit, you may want to run that again if you're done making changes per review :)

@dhalbert
Copy link
Collaborator

examples/ble_json_service.py:12:0: R0903: Too few public methods (0/1) (too-few-public-methods)

You can add a # pylint: disable=too-few-public-methods to get around this.

@mraleson
Copy link
Contributor Author

Thank you @tekktrik @dhalbert, all of the CI issues should be fixed.

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dhalbert dhalbert merged commit 13c5292 into adafruit:main Apr 23, 2022
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Apr 24, 2022
Updating https://github.com/adafruit/Adafruit_CircuitPython_BLE to 8.3.0 from 8.2.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_BLE#162 from mraleson/json-characteristic
  > Patch: Replaced discord badge image
  > Update .gitignore
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