Compare commits
34 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db9029db43 | ||
|
|
1db962ec60 | ||
|
|
df83258866 | ||
|
|
6ae038b32b | ||
|
|
4ca25f1640 | ||
|
|
ce4589de9b | ||
|
|
5eba1d5654 | ||
|
|
32dbf82455 | ||
|
|
8b1b8f7247 | ||
|
|
dcdc685619 | ||
|
|
b14810192b | ||
|
|
05399dc3c2 | ||
|
|
7e56b88e52 | ||
|
|
1e6ff577e0 | ||
|
|
dfdfbc9bca | ||
|
|
705e95fd7c | ||
|
|
922ee96fce | ||
|
|
01acd5cd8d | ||
|
|
8595da1256 | ||
|
|
fed1d75d02 | ||
|
|
1d05783b40 | ||
|
|
c5a144b25e | ||
|
|
0e57c865b1 | ||
|
|
cb75fd507e | ||
|
|
ed15cc86a8 | ||
|
|
06c0e11488 | ||
|
|
e68b2f18ef | ||
|
|
e6e935221e | ||
|
|
ca621f455c | ||
|
|
513572f1d7 | ||
|
|
95a18d36bd | ||
|
|
6caf7c5d1e | ||
|
|
e70fc483b2 | ||
|
|
227ebf8b84 |
24 changed files with 521 additions and 243 deletions
57
.github/workflows/build.yml
vendored
Normal file
57
.github/workflows/build.yml
vendored
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
name: Build CI
|
||||
|
||||
on: [pull_request, push]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Dump GitHub context
|
||||
env:
|
||||
GITHUB_CONTEXT: ${{ toJson(github) }}
|
||||
run: echo "$GITHUB_CONTEXT"
|
||||
- name: Translate Repo Name For Build Tools filename_prefix
|
||||
id: repo-name
|
||||
run: |
|
||||
echo ::set-output name=repo-name::$(
|
||||
echo ${{ github.repository }} |
|
||||
awk -F '\/' '{ print tolower($2) }' |
|
||||
tr '_' '-'
|
||||
)
|
||||
- name: Set up Python 3.6
|
||||
uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: 3.6
|
||||
- name: Versions
|
||||
run: |
|
||||
python3 --version
|
||||
- name: Checkout Current Repo
|
||||
uses: actions/checkout@v1
|
||||
with:
|
||||
submodules: true
|
||||
- name: Checkout tools repo
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
repository: adafruit/actions-ci-circuitpython-libs
|
||||
path: actions-ci
|
||||
- name: Install dependencies
|
||||
# (e.g. - apt-get: gettext, etc; pip: circuitpython-build-tools, requirements.txt; etc.)
|
||||
run: |
|
||||
source actions-ci/install.sh
|
||||
- name: Pip install pylint, black, & Sphinx
|
||||
run: |
|
||||
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
|
||||
- name: Library version
|
||||
run: git describe --dirty --always --tags
|
||||
- name: Check formatting
|
||||
run: |
|
||||
black --check --target-version=py35 .
|
||||
- name: PyLint
|
||||
run: |
|
||||
pylint $( find . -path './adafruit*.py' )
|
||||
([[ ! -d "examples" ]] || pylint --disable=missing-docstring,invalid-name,bad-whitespace $( find . -path "./examples/*.py" ))
|
||||
- name: Build assets
|
||||
run: circuitpython-build-bundles --filename_prefix ${{ steps.repo-name.outputs.repo-name }} --library_location .
|
||||
- name: Build docs
|
||||
working-directory: docs
|
||||
run: sphinx-build -E -W -b html . _build/html
|
||||
81
.github/workflows/release.yml
vendored
Normal file
81
.github/workflows/release.yml
vendored
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
name: Release Actions
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
|
||||
jobs:
|
||||
upload-release-assets:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Dump GitHub context
|
||||
env:
|
||||
GITHUB_CONTEXT: ${{ toJson(github) }}
|
||||
run: echo "$GITHUB_CONTEXT"
|
||||
- name: Translate Repo Name For Build Tools filename_prefix
|
||||
id: repo-name
|
||||
run: |
|
||||
echo ::set-output name=repo-name::$(
|
||||
echo ${{ github.repository }} |
|
||||
awk -F '\/' '{ print tolower($2) }' |
|
||||
tr '_' '-'
|
||||
)
|
||||
- name: Set up Python 3.6
|
||||
uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: 3.6
|
||||
- name: Versions
|
||||
run: |
|
||||
python3 --version
|
||||
- name: Checkout Current Repo
|
||||
uses: actions/checkout@v1
|
||||
with:
|
||||
submodules: true
|
||||
- name: Checkout tools repo
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
repository: adafruit/actions-ci-circuitpython-libs
|
||||
path: actions-ci
|
||||
- name: Install deps
|
||||
run: |
|
||||
source actions-ci/install.sh
|
||||
- name: Build assets
|
||||
run: circuitpython-build-bundles --filename_prefix ${{ steps.repo-name.outputs.repo-name }} --library_location .
|
||||
- name: Upload Release Assets
|
||||
# the 'official' actions version does not yet support dynamically
|
||||
# supplying asset names to upload. @csexton's version chosen based on
|
||||
# discussion in the issue below, as its the simplest to implement and
|
||||
# allows for selecting files with a pattern.
|
||||
# https://github.com/actions/upload-release-asset/issues/4
|
||||
#uses: actions/upload-release-asset@v1.0.1
|
||||
uses: csexton/release-asset-action@master
|
||||
with:
|
||||
pattern: "bundles/*"
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
upload-pypi:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Check For setup.py
|
||||
id: need-pypi
|
||||
run: |
|
||||
echo ::set-output name=setup-py::$( find . -wholename './setup.py' )
|
||||
- name: Set up Python
|
||||
if: contains(steps.need-pypi.outputs.setup-py, 'setup.py')
|
||||
uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Install dependencies
|
||||
if: contains(steps.need-pypi.outputs.setup-py, 'setup.py')
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install setuptools wheel twine
|
||||
- name: Build and publish
|
||||
if: contains(steps.need-pypi.outputs.setup-py, 'setup.py')
|
||||
env:
|
||||
TWINE_USERNAME: ${{ secrets.pypi_username }}
|
||||
TWINE_PASSWORD: ${{ secrets.pypi_password }}
|
||||
run: |
|
||||
python setup.py sdist
|
||||
twine upload dist/*
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -2,5 +2,4 @@ __pycache__
|
|||
_build
|
||||
*.pyc
|
||||
.env
|
||||
build*
|
||||
bundles
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ confidence=
|
|||
# no Warning level messages displayed, use"--disable=all --enable=classes
|
||||
# --disable=W"
|
||||
# disable=import-error,print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call
|
||||
disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error
|
||||
disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,bad-continuation
|
||||
|
||||
# Enable the message, report, category or checker with the given id(s). You can
|
||||
# either give multiple identifier separated by comma (,) or put this option
|
||||
|
|
|
|||
33
.travis.yml
33
.travis.yml
|
|
@ -1,33 +0,0 @@
|
|||
dist: xenial
|
||||
language: python
|
||||
python:
|
||||
- '3.6'
|
||||
cache:
|
||||
pip: true
|
||||
deploy:
|
||||
- provider: releases
|
||||
api_key: "$GITHUB_TOKEN"
|
||||
file_glob: true
|
||||
file: "$TRAVIS_BUILD_DIR/bundles/*"
|
||||
skip_cleanup: true
|
||||
overwrite: true
|
||||
on:
|
||||
tags: true
|
||||
- provider: pypi
|
||||
user: adafruit-travis
|
||||
on:
|
||||
tags: true
|
||||
password:
|
||||
secure: bhCvvxDVGvUAHHUWVNFYe6L34x+whmKkRrfSfpYcTTtcYsFZ4Zx91j1QZVcAJLL76+JmmYcM9MzAIETIGg8u3Y0zkGIMVNiVN3Jq+u4X3rOvkdPNlnd3aeFGluOIn5tVtax3UXU4FcMhVLmQHJwCMlmxF8NNxvnogTlwCddZNAQxBzi+lVoIgoGPHl+tFeYeZ2prgbQMpgT9EvHVcNipQztUlKWXG95WEI1COKz0e59PTSvuLXnLQM9WQ0cfUTKynVq1Ipxg19Dd9UV4DQvRZZ8kex60VdNs/JsaBF/QmZhEgF8UbGjz2AW9J8Yvkp+qxKRb8XaJXbZ51clQqkEw45P6E6eYPsje7uSFL38zCweP8MYPdJnNFi5FnHiwMVdiMmE4gkklag4M3ClrylZRZYVCdAjw1fNqkqGFk0JtzSXL4eINran8lUeE7fVWO8XgAX9gN6845Kstv/Tl2DhvEW6lln8+CgpnYk0q3tfh8JATUGuNbN1K4t2ApQO3jo6nvIgPctkNF7cbfR7EgzmtlkYXj0F+urB/615V44PWxI5ECfvQ1LzhcR0+Fpn9VjgvFc/tcUn8gSas/yMCM9s+UsyaIVhwvfVLGn+ip2sViKo+4Z8Csm512O/EklE7Y4Y4rQsCvcFHv7ctJXsgRi8XS4NaXXUL3YdHddk2N0zQx78=
|
||||
|
||||
install:
|
||||
- pip install -r requirements.txt
|
||||
- pip install circuitpython-build-tools Sphinx sphinx-rtd-theme
|
||||
- pip install --force-reinstall pylint==1.9.2
|
||||
script:
|
||||
- pylint adafruit_bluefruit_connect/*.py
|
||||
- ([[ ! -d "examples" ]] || pylint --disable=missing-docstring,invalid-name,bad-whitespace
|
||||
examples/*.py)
|
||||
- circuitpython-build-bundles --filename_prefix adafruit-circuitpython-bluefruitconnect
|
||||
--library_location .
|
||||
- cd docs && sphinx-build -E -W -b html . _build/html && cd ..
|
||||
|
|
@ -34,6 +34,8 @@ Examples of unacceptable behavior by participants include:
|
|||
* Excessive or unwelcome helping; answering outside the scope of the question
|
||||
asked
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Promoting or spreading disinformation, lies, or conspiracy theories against
|
||||
a person, group, organisation, project, or community
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic
|
||||
address, without explicit permission
|
||||
|
|
@ -72,10 +74,10 @@ You may report in the following ways:
|
|||
In any situation, you may send an email to <support@adafruit.com>.
|
||||
|
||||
On the Adafruit Discord, you may send an open message from any channel
|
||||
to all Community Helpers by tagging @community helpers. You may also send an
|
||||
open message from any channel, or a direct message to @kattni#1507,
|
||||
@tannewt#4653, @Dan Halbert#1614, @cater#2442, @sommersoft#0222, or
|
||||
@Andon#8175.
|
||||
to all Community Moderators by tagging @community moderators. You may
|
||||
also send an open message from any channel, or a direct message to
|
||||
@kattni#1507, @tannewt#4653, @Dan Halbert#1614, @cater#2442,
|
||||
@sommersoft#0222, @Mr. Certainly#0472 or @Andon#8175.
|
||||
|
||||
Email and direct message reports will be kept confidential.
|
||||
|
||||
|
|
|
|||
82
README.rst
82
README.rst
|
|
@ -6,11 +6,11 @@ Introduction
|
|||
:alt: Documentation Status
|
||||
|
||||
.. image:: https://img.shields.io/discord/327254708534116352.svg
|
||||
:target: https://discord.gg/nBQh6qu
|
||||
:target: https://adafru.it/discord
|
||||
:alt: Discord
|
||||
|
||||
.. image:: https://travis-ci.com/adafruit/Adafruit_CircuitPython_BluefruitConnect.svg?branch=master
|
||||
:target: https://travis-ci.com/adafruit/Adafruit_CircuitPython_BluefruitConnect
|
||||
.. image:: https://github.com/adafruit/Adafruit_CircuitPython_BluefruitConnect/workflows/Build%20CI/badge.svg
|
||||
:target: https://github.com/adafruit/Adafruit_CircuitPython_BluefruitConnect/actions/
|
||||
:alt: Build Status
|
||||
|
||||
This module helps you to communicate with the Adafruit Bluefruit Connect app or use its protocols.
|
||||
|
|
@ -25,6 +25,31 @@ Please ensure all dependencies are available on the CircuitPython filesystem.
|
|||
This is easily achieved by downloading
|
||||
`the Adafruit library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_.
|
||||
|
||||
Installing from PyPI
|
||||
====================
|
||||
|
||||
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
|
||||
PyPI <https://pypi.org/project/adafruit-circuitpython-bluefruitconnect/>`_. To install for current user:
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
pip3 install adafruit-circuitpython-bluefruitconnect
|
||||
|
||||
To install system-wide (this may be required in some cases):
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
sudo pip3 install adafruit-circuitpython-bluefruitconnect
|
||||
|
||||
To install in a virtual environment in your current project:
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
mkdir project-name && cd project-name
|
||||
python3 -m venv .env
|
||||
source .env/bin/activate
|
||||
pip3 install adafruit-circuitpython-bluefruitconnect
|
||||
|
||||
Usage Example
|
||||
=============
|
||||
|
||||
|
|
@ -53,52 +78,7 @@ Contributions are welcome! Please read our `Code of Conduct
|
|||
<https://github.com/adafruit/Adafruit_CircuitPython_ble/blob/master/CODE_OF_CONDUCT.md>`_
|
||||
before contributing to help this project stay welcoming.
|
||||
|
||||
Building locally
|
||||
================
|
||||
Documentation
|
||||
=============
|
||||
|
||||
Zip release files
|
||||
-----------------
|
||||
|
||||
To build this library locally you'll need to install the
|
||||
`circuitpython-build-tools <https://github.com/adafruit/circuitpython-build-tools>`_ package.
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
python3 -m venv .env
|
||||
source .env/bin/activate
|
||||
pip install circuitpython-build-tools
|
||||
|
||||
Once installed, make sure you are in the virtual environment:
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
source .env/bin/activate
|
||||
|
||||
Then run the build:
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
circuitpython-build-bundles --filename_prefix adafruit-circuitpython-bluefruitconnect --library_location .
|
||||
|
||||
Sphinx documentation
|
||||
-----------------------
|
||||
|
||||
Sphinx is used to build the documentation based on rST files and comments in the code. First,
|
||||
install dependencies (feel free to reuse the virtual environment from above):
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
python3 -m venv .env
|
||||
source .env/bin/activate
|
||||
pip install Sphinx sphinx-rtd-theme
|
||||
|
||||
Now, once you have the virtual environment activated:
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
cd docs
|
||||
sphinx-build -E -W -b html . _build/html
|
||||
|
||||
This will output the documentation to ``docs/_build/html``. Open the index.html in your browser to
|
||||
view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to
|
||||
locally verify it will pass.
|
||||
For information on building library documentation, please check out `this guide <https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1>`_.
|
||||
|
|
|
|||
|
|
@ -33,13 +33,14 @@ import struct
|
|||
|
||||
from .packet import Packet
|
||||
|
||||
|
||||
class _XYZPacket(Packet):
|
||||
"""A packet of x, y, z float values. Used for several different Bluefruit controller packets."""
|
||||
|
||||
_FMT_PARSE = '<xxfffx'
|
||||
_FMT_PARSE = "<xxfffx"
|
||||
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
|
||||
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
|
||||
_FMT_CONSTRUCT = '<2sfff'
|
||||
_FMT_CONSTRUCT = "<2sfff"
|
||||
# _TYPE_HEADER is set by each concrete subclass.
|
||||
|
||||
def __init__(self, x, y, z):
|
||||
|
|
@ -53,8 +54,9 @@ class _XYZPacket(Packet):
|
|||
def to_bytes(self):
|
||||
"""Return the bytes needed to send this packet.
|
||||
"""
|
||||
partial_packet = struct.pack(self._FMT_CONSTRUCT, self._TYPE_HEADER,
|
||||
self._x, self._y, self._z)
|
||||
partial_packet = struct.pack(
|
||||
self._FMT_CONSTRUCT, self._TYPE_HEADER, self._x, self._y, self._z
|
||||
)
|
||||
return self.add_checksum(partial_packet)
|
||||
|
||||
@property
|
||||
|
|
|
|||
|
|
@ -31,11 +31,12 @@ Bluefruit Connect App Accelerometer data packet.
|
|||
|
||||
from ._xyz_packet import _XYZPacket
|
||||
|
||||
|
||||
class AccelerometerPacket(_XYZPacket):
|
||||
"""A packet of x, y, z float values from an accelerometer."""
|
||||
|
||||
# Everything else is handled by _XYZPacket.
|
||||
_TYPE_HEADER = b'!A'
|
||||
_TYPE_HEADER = b"!A"
|
||||
|
||||
|
||||
# Register this class with the superclass. This allows the user to import only what is needed.
|
||||
|
|
|
|||
|
|
@ -34,32 +34,33 @@ import struct
|
|||
|
||||
from .packet import Packet
|
||||
|
||||
|
||||
class ButtonPacket(Packet):
|
||||
"""A packet containing a button name and its state."""
|
||||
|
||||
BUTTON_1 = '1'
|
||||
BUTTON_1 = "1"
|
||||
"""Code for Button 1 on the Bluefruit LE Connect app Control Pad screen."""
|
||||
BUTTON_2 = '2'
|
||||
BUTTON_2 = "2"
|
||||
"""Button 2."""
|
||||
BUTTON_3 = '3'
|
||||
BUTTON_3 = "3"
|
||||
"""Button 3."""
|
||||
BUTTON_4 = '4'
|
||||
BUTTON_4 = "4"
|
||||
"""Button 4."""
|
||||
#pylint: disable= invalid-name
|
||||
UP = '5'
|
||||
# pylint: disable= invalid-name
|
||||
UP = "5"
|
||||
"""Up Button."""
|
||||
DOWN = '6'
|
||||
DOWN = "6"
|
||||
"""Down Button."""
|
||||
LEFT = '7'
|
||||
LEFT = "7"
|
||||
"""Left Button."""
|
||||
RIGHT = '8'
|
||||
RIGHT = "8"
|
||||
"""Right Button."""
|
||||
|
||||
_FMT_PARSE = '<xxssx'
|
||||
_FMT_PARSE = "<xxssx"
|
||||
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
|
||||
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
|
||||
_FMT_CONSTRUCT = '<2sss'
|
||||
_TYPE_HEADER = b'!B'
|
||||
_FMT_CONSTRUCT = "<2sss"
|
||||
_TYPE_HEADER = b"!B"
|
||||
|
||||
def __init__(self, button, pressed):
|
||||
"""Construct a ButtonPacket from a button name and the button's state.
|
||||
|
|
@ -83,14 +84,18 @@ class ButtonPacket(Packet):
|
|||
pylint makes it difficult to call this method _parse(), hence the name.
|
||||
"""
|
||||
button, pressed = struct.unpack(cls._FMT_PARSE, packet)
|
||||
if not pressed in b'01':
|
||||
if not pressed in b"01":
|
||||
raise ValueError("Bad button press/release value")
|
||||
return cls(chr(button[0]), pressed == b'1')
|
||||
return cls(chr(button[0]), pressed == b"1")
|
||||
|
||||
def to_bytes(self):
|
||||
"""Return the bytes needed to send this packet."""
|
||||
partial_packet = struct.pack(self._FMT_CONSTRUCT, self._TYPE_HEADER,
|
||||
self._button, b'1' if self._pressed else b'0')
|
||||
partial_packet = struct.pack(
|
||||
self._FMT_CONSTRUCT,
|
||||
self._TYPE_HEADER,
|
||||
self._button,
|
||||
b"1" if self._pressed else b"0",
|
||||
)
|
||||
return self.add_checksum(partial_packet)
|
||||
|
||||
@property
|
||||
|
|
|
|||
|
|
@ -33,14 +33,15 @@ import struct
|
|||
|
||||
from .packet import Packet
|
||||
|
||||
|
||||
class ColorPacket(Packet):
|
||||
"""A packet containing an RGB color value."""
|
||||
|
||||
_FMT_PARSE = '<xx3Bx'
|
||||
_FMT_PARSE = "<xx3Bx"
|
||||
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
|
||||
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
|
||||
_FMT_CONSTRUCT = '<2s3B'
|
||||
_TYPE_HEADER = b'!C'
|
||||
_FMT_CONSTRUCT = "<2s3B"
|
||||
_TYPE_HEADER = b"!C"
|
||||
|
||||
def __init__(self, color):
|
||||
"""Construct a ColorPacket from a 3-tuple of RGB values,
|
||||
|
|
@ -50,7 +51,7 @@ class ColorPacket(Packet):
|
|||
or an int color value ``0xRRGGBB``
|
||||
"""
|
||||
if isinstance(color, int):
|
||||
self._color = tuple(color.to_bytes('BBB', 'big'))
|
||||
self._color = tuple(color.to_bytes("BBB", "big"))
|
||||
elif len(color) == 3 and all(0 <= c <= 255 for c in color):
|
||||
self._color = color
|
||||
else:
|
||||
|
|
@ -67,7 +68,9 @@ class ColorPacket(Packet):
|
|||
def to_bytes(self):
|
||||
"""Return the bytes needed to send this packet.
|
||||
"""
|
||||
partial_packet = struct.pack(self._FMT_CONSTRUCT, self._TYPE_HEADER, *self._color)
|
||||
partial_packet = struct.pack(
|
||||
self._FMT_CONSTRUCT, self._TYPE_HEADER, *self._color
|
||||
)
|
||||
return self.add_checksum(partial_packet)
|
||||
|
||||
@property
|
||||
|
|
|
|||
|
|
@ -31,11 +31,12 @@ Bluefruit Connect App Gyro data packet.
|
|||
|
||||
from ._xyz_packet import _XYZPacket
|
||||
|
||||
|
||||
class GyroPacket(_XYZPacket):
|
||||
"""A packet of x, y, z float values from a gyroscope."""
|
||||
|
||||
# Everything else is handled by _XYZPacket.
|
||||
_TYPE_HEADER = b'!G'
|
||||
_TYPE_HEADER = b"!G"
|
||||
|
||||
|
||||
# Register this class with the superclass. This allows the user to import only what is needed.
|
||||
|
|
|
|||
|
|
@ -33,14 +33,15 @@ import struct
|
|||
|
||||
from .packet import Packet
|
||||
|
||||
|
||||
class LocationPacket(Packet):
|
||||
"""A packet of latitude, longitude, and altitude values."""
|
||||
|
||||
_FMT_PARSE = '<xxfffx'
|
||||
_FMT_PARSE = "<xxfffx"
|
||||
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
|
||||
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
|
||||
_FMT_CONSTRUCT = '<2sfff'
|
||||
_TYPE_HEADER = b'!L'
|
||||
_FMT_CONSTRUCT = "<2sfff"
|
||||
_TYPE_HEADER = b"!L"
|
||||
|
||||
def __init__(self, latitude, longitude, altitude):
|
||||
"""Construct a LocationPacket from the given values."""
|
||||
|
|
@ -51,8 +52,13 @@ class LocationPacket(Packet):
|
|||
def to_bytes(self):
|
||||
"""Return the bytes needed to send this packet.
|
||||
"""
|
||||
partial_packet = struct.pack(self._FMT_CONSTRUCT, self._TYPE_HEADER,
|
||||
self._latitude, self._longitude, self._altitude)
|
||||
partial_packet = struct.pack(
|
||||
self._FMT_CONSTRUCT,
|
||||
self._TYPE_HEADER,
|
||||
self._latitude,
|
||||
self._longitude,
|
||||
self._altitude,
|
||||
)
|
||||
return self.add_checksum(partial_packet)
|
||||
|
||||
@property
|
||||
|
|
@ -70,5 +76,6 @@ class LocationPacket(Packet):
|
|||
"""The altitude value."""
|
||||
return self._altitude
|
||||
|
||||
|
||||
# Register this class with the superclass. This allows the user to import only what is needed.
|
||||
LocationPacket.register_packet_type()
|
||||
|
|
|
|||
|
|
@ -31,11 +31,12 @@ Bluefruit Connect App Magnetometer data packet.
|
|||
|
||||
from ._xyz_packet import _XYZPacket
|
||||
|
||||
|
||||
class MagnetometerPacket(_XYZPacket):
|
||||
"""A packet of x, y, z float values from a magnetometer."""
|
||||
|
||||
# Everything else is handled by _XYZPacket.
|
||||
_TYPE_HEADER = b'!M'
|
||||
_TYPE_HEADER = b"!M"
|
||||
|
||||
|
||||
# Register this class with the superclass. This allows the user to import only what is needed.
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ Bluefruit Connect App packet superclass
|
|||
|
||||
import struct
|
||||
|
||||
|
||||
class Packet:
|
||||
"""
|
||||
A Bluefruit app controller packet. A packet consists of these bytes, in order:
|
||||
|
|
@ -80,7 +81,7 @@ class Packet:
|
|||
# In case this was called from a subclass, make sure the parsed
|
||||
# type matches up with the current class.
|
||||
if not issubclass(packet_class, cls):
|
||||
raise ValueError('Packet type is not a {}'.format(cls.__name__))
|
||||
raise ValueError("Packet type is not a {}".format(cls.__name__))
|
||||
|
||||
if len(packet) != packet_class.PACKET_LENGTH:
|
||||
raise ValueError("Wrong length packet")
|
||||
|
|
@ -110,14 +111,13 @@ class Packet:
|
|||
if not start:
|
||||
# Timeout: nothing read.
|
||||
return None
|
||||
if start == b'!':
|
||||
if start == b"!":
|
||||
# Found start of packet.
|
||||
packet_type = stream.read(1)
|
||||
if not packet_type:
|
||||
# Timeout: nothing more read.
|
||||
return None
|
||||
else:
|
||||
break
|
||||
break
|
||||
# Didn't find a packet start. Loop and try again.
|
||||
|
||||
header = start + packet_type
|
||||
|
|
@ -142,7 +142,7 @@ class Packet:
|
|||
@staticmethod
|
||||
def checksum(partial_packet):
|
||||
"""Compute checksum for bytes, not including the checksum byte itself."""
|
||||
return ~sum(partial_packet) & 0xff
|
||||
return ~sum(partial_packet) & 0xFF
|
||||
|
||||
def add_checksum(self, partial_packet):
|
||||
"""Compute the checksum of partial_packet and return a new bytes
|
||||
|
|
|
|||
|
|
@ -33,16 +33,17 @@ import struct
|
|||
|
||||
from ._xyz_packet import _XYZPacket
|
||||
|
||||
|
||||
class QuaternionPacket(_XYZPacket):
|
||||
"""A packet of x, y, z float values. Used for several different Bluefruit controller packets."""
|
||||
|
||||
# Use _XYZPacket to handle x, y, z, and add w.
|
||||
|
||||
_FMT_PARSE = '<xxffffx'
|
||||
_FMT_PARSE = "<xxffffx"
|
||||
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
|
||||
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
|
||||
_FMT_CONSTRUCT = '<2sffff'
|
||||
_TYPE_HEADER = b'!Q'
|
||||
_FMT_CONSTRUCT = "<2sffff"
|
||||
_TYPE_HEADER = b"!Q"
|
||||
|
||||
def __init__(self, x, y, z, w):
|
||||
"""Construct a QuaternionPacket from the given x, y, z, and w float values."""
|
||||
|
|
@ -52,8 +53,9 @@ class QuaternionPacket(_XYZPacket):
|
|||
def to_bytes(self):
|
||||
"""Return the bytes needed to send this packet.
|
||||
"""
|
||||
partial_packet = struct.pack(self._FMT_CONSTRUCT, self._TYPE_HEADER,
|
||||
self._x, self._y, self._z, self._w)
|
||||
partial_packet = struct.pack(
|
||||
self._FMT_CONSTRUCT, self._TYPE_HEADER, self._x, self._y, self._z, self._w
|
||||
)
|
||||
return partial_packet + self.checksum(partial_packet)
|
||||
|
||||
@property
|
||||
|
|
@ -61,5 +63,6 @@ class QuaternionPacket(_XYZPacket):
|
|||
"""The w value."""
|
||||
return self._w
|
||||
|
||||
|
||||
# Register this class with the superclass. This allows the user to import only what is needed.
|
||||
QuaternionPacket.register_packet_type()
|
||||
|
|
|
|||
112
docs/conf.py
112
docs/conf.py
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
import os
|
||||
import sys
|
||||
sys.path.insert(0, os.path.abspath('..'))
|
||||
|
||||
sys.path.insert(0, os.path.abspath(".."))
|
||||
|
||||
# -- General configuration ------------------------------------------------
|
||||
|
||||
|
|
@ -10,10 +11,10 @@ sys.path.insert(0, os.path.abspath('..'))
|
|||
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
||||
# ones.
|
||||
extensions = [
|
||||
'sphinx.ext.autodoc',
|
||||
'sphinx.ext.intersphinx',
|
||||
'sphinx.ext.napoleon',
|
||||
'sphinx.ext.todo',
|
||||
"sphinx.ext.autodoc",
|
||||
"sphinx.ext.intersphinx",
|
||||
"sphinx.ext.napoleon",
|
||||
"sphinx.ext.todo",
|
||||
]
|
||||
|
||||
# TODO: Please Read!
|
||||
|
|
@ -23,29 +24,32 @@ extensions = [
|
|||
autodoc_mock_imports = ["bleio"]
|
||||
|
||||
|
||||
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}
|
||||
intersphinx_mapping = {
|
||||
"python": ("https://docs.python.org/3.4", None),
|
||||
"CircuitPython": ("https://circuitpython.readthedocs.io/en/latest/", None),
|
||||
}
|
||||
|
||||
# Add any paths that contain templates here, relative to this directory.
|
||||
templates_path = ['_templates']
|
||||
templates_path = ["_templates"]
|
||||
|
||||
source_suffix = '.rst'
|
||||
source_suffix = ".rst"
|
||||
|
||||
# The master toctree document.
|
||||
master_doc = 'index'
|
||||
master_doc = "index"
|
||||
|
||||
# General information about the project.
|
||||
project = u'Adafruit Bluefruit Connect Library'
|
||||
copyright = u'2019 Dan Halbert'
|
||||
author = u'Dan Halbert'
|
||||
project = "Adafruit Bluefruit Connect Library"
|
||||
copyright = "2019 Dan Halbert"
|
||||
author = "Dan Halbert"
|
||||
|
||||
# The version info for the project you're documenting, acts as replacement for
|
||||
# |version| and |release|, also used in various other places throughout the
|
||||
# built documents.
|
||||
#
|
||||
# The short X.Y version.
|
||||
version = u'1.0'
|
||||
version = "1.0"
|
||||
# The full version, including alpha/beta/rc tags.
|
||||
release = u'1.0'
|
||||
release = "1.0"
|
||||
|
||||
# The language for content autogenerated by Sphinx. Refer to documentation
|
||||
# for a list of supported languages.
|
||||
|
|
@ -57,7 +61,7 @@ language = None
|
|||
# List of patterns, relative to source directory, that match files and
|
||||
# directories to ignore when looking for source files.
|
||||
# This patterns also effect to html_static_path and html_extra_path
|
||||
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', '.env', 'CODE_OF_CONDUCT.md']
|
||||
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", ".env", "CODE_OF_CONDUCT.md"]
|
||||
|
||||
# The reST default role (used for this markup: `text`) to use for all
|
||||
# documents.
|
||||
|
|
@ -69,7 +73,7 @@ default_role = "any"
|
|||
add_function_parentheses = True
|
||||
|
||||
# The name of the Pygments (syntax highlighting) style to use.
|
||||
pygments_style = 'sphinx'
|
||||
pygments_style = "sphinx"
|
||||
|
||||
# If true, `todo` and `todoList` produce output, else they produce nothing.
|
||||
todo_include_todos = False
|
||||
|
|
@ -84,59 +88,62 @@ napoleon_numpy_docstring = False
|
|||
# The theme to use for HTML and HTML Help pages. See the documentation for
|
||||
# a list of builtin themes.
|
||||
#
|
||||
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
|
||||
on_rtd = os.environ.get("READTHEDOCS", None) == "True"
|
||||
|
||||
if not on_rtd: # only import and set the theme if we're building docs locally
|
||||
try:
|
||||
import sphinx_rtd_theme
|
||||
html_theme = 'sphinx_rtd_theme'
|
||||
html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), '.']
|
||||
|
||||
html_theme = "sphinx_rtd_theme"
|
||||
html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), "."]
|
||||
except:
|
||||
html_theme = 'default'
|
||||
html_theme_path = ['.']
|
||||
html_theme = "default"
|
||||
html_theme_path = ["."]
|
||||
else:
|
||||
html_theme_path = ['.']
|
||||
html_theme_path = ["."]
|
||||
|
||||
# Add any paths that contain custom static files (such as style sheets) here,
|
||||
# relative to this directory. They are copied after the builtin static files,
|
||||
# so a file named "default.css" will overwrite the builtin "default.css".
|
||||
html_static_path = ['_static']
|
||||
html_static_path = ["_static"]
|
||||
|
||||
# The name of an image file (relative to this directory) to use as a favicon of
|
||||
# the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
|
||||
# pixels large.
|
||||
#
|
||||
html_favicon = '_static/favicon.ico'
|
||||
html_favicon = "_static/favicon.ico"
|
||||
|
||||
# Output file base name for HTML help builder.
|
||||
htmlhelp_basename = 'AdafruitBluefruitConnectLibrarydoc'
|
||||
htmlhelp_basename = "AdafruitBluefruitConnectLibrarydoc"
|
||||
|
||||
# -- Options for LaTeX output ---------------------------------------------
|
||||
|
||||
latex_elements = {
|
||||
# The paper size ('letterpaper' or 'a4paper').
|
||||
#
|
||||
# 'papersize': 'letterpaper',
|
||||
|
||||
# The font size ('10pt', '11pt' or '12pt').
|
||||
#
|
||||
# 'pointsize': '10pt',
|
||||
|
||||
# Additional stuff for the LaTeX preamble.
|
||||
#
|
||||
# 'preamble': '',
|
||||
|
||||
# Latex figure (float) alignment
|
||||
#
|
||||
# 'figure_align': 'htbp',
|
||||
# The paper size ('letterpaper' or 'a4paper').
|
||||
#
|
||||
# 'papersize': 'letterpaper',
|
||||
# The font size ('10pt', '11pt' or '12pt').
|
||||
#
|
||||
# 'pointsize': '10pt',
|
||||
# Additional stuff for the LaTeX preamble.
|
||||
#
|
||||
# 'preamble': '',
|
||||
# Latex figure (float) alignment
|
||||
#
|
||||
# 'figure_align': 'htbp',
|
||||
}
|
||||
|
||||
# Grouping the document tree into LaTeX files. List of tuples
|
||||
# (source start file, target name, title,
|
||||
# author, documentclass [howto, manual, or own class]).
|
||||
latex_documents = [
|
||||
(master_doc, 'AdafruitBluefruitConnectLibrary.tex', u'Adafruit Bluefruit Connect Library Documentation',
|
||||
author, 'manual'),
|
||||
(
|
||||
master_doc,
|
||||
"AdafruitBluefruitConnectLibrary.tex",
|
||||
"Adafruit Bluefruit Connect Library Documentation",
|
||||
author,
|
||||
"manual",
|
||||
),
|
||||
]
|
||||
|
||||
# -- Options for manual page output ---------------------------------------
|
||||
|
|
@ -144,8 +151,13 @@ latex_documents = [
|
|||
# One entry per manual page. List of tuples
|
||||
# (source start file, name, description, authors, manual section).
|
||||
man_pages = [
|
||||
(master_doc, 'AdafruitBluefruitConnectLibrary', u'Adafruit Bluefruit Connect Library Documentation',
|
||||
[author], 1)
|
||||
(
|
||||
master_doc,
|
||||
"AdafruitBluefruitConnectLibrary",
|
||||
"Adafruit Bluefruit Connect Library Documentation",
|
||||
[author],
|
||||
1,
|
||||
)
|
||||
]
|
||||
|
||||
# -- Options for Texinfo output -------------------------------------------
|
||||
|
|
@ -154,7 +166,13 @@ man_pages = [
|
|||
# (source start file, target name, title, author,
|
||||
# dir menu entry, description, category)
|
||||
texinfo_documents = [
|
||||
(master_doc, 'AdafruitBluefruitConnectLibrary', u'Adafruit Bluefruit Connect Library Documentation',
|
||||
author, 'AdafruitBluefruitConnectLibrary', 'One line description of project.',
|
||||
'Miscellaneous'),
|
||||
(
|
||||
master_doc,
|
||||
"AdafruitBluefruitConnectLibrary",
|
||||
"Adafruit Bluefruit Connect Library Documentation",
|
||||
author,
|
||||
"AdafruitBluefruitConnectLibrary",
|
||||
"One line description of project.",
|
||||
"Miscellaneous",
|
||||
),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -3,12 +3,24 @@ Simple test
|
|||
|
||||
Ensure your device works with this simple test.
|
||||
|
||||
.. literalinclude:: ../examples/bluefruit_connect_simpletest.py
|
||||
:caption: examples/bluefruit_connect_simpletest.py
|
||||
.. literalinclude:: ../examples/bluefruitconnect_simpletest.py
|
||||
:caption: examples/bluefruitconnect_simpletest.py
|
||||
:linenos:
|
||||
|
||||
This test demonstrates controlling an Adafruit Crickit board with Bluetooth.
|
||||
This example demonstrates receiving button presses from the Control Pad.
|
||||
|
||||
.. literalinclude:: ../examples/bluefruit_connect_accelerometer_packet_test.py
|
||||
:caption: examples/bluefruit_connect_accelerometer_packet_test.py
|
||||
.. literalinclude:: ../examples/bluefruitconnect_controlpad.py
|
||||
:caption: examples/bluefruitconnect_controlpad.py
|
||||
:linenos:
|
||||
|
||||
This example demonstrates receiving sensor data from the Controller.
|
||||
|
||||
.. literalinclude:: ../examples/bluefruitconnect_sensors.py
|
||||
:caption: examples/bluefruitconnect_sensors.py
|
||||
:linenos:
|
||||
|
||||
This example demonstrates receiving text from the UART interface.
|
||||
|
||||
.. literalinclude:: ../examples/bluefruitconnect_uart.py
|
||||
:caption: examples/bluefruitconnect_uart.py
|
||||
:linenos:
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
# Stream accelerometer data from your phone or tablet.
|
||||
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
|
||||
# Connect and go to the Controller screen. Turn on
|
||||
# STREAM SENSOR DATA -> Accelerometer to send data from the device's
|
||||
# accelerometer. See how it matches what this prints.
|
||||
|
||||
from adafruit_ble.uart import UARTServer
|
||||
from adafruit_bluefruit_connect.packet import Packet
|
||||
# Only the packet classes that are imported will be known to Packet.
|
||||
from adafruit_bluefruit_connect.accelerometer_packet import AccelerometerPacket
|
||||
|
||||
uart_server = UARTServer()
|
||||
|
||||
while True:
|
||||
# Advertise when not connected.
|
||||
uart_server.start_advertising()
|
||||
while not uart_server.connected:
|
||||
pass
|
||||
|
||||
while uart_server.connected:
|
||||
packet = Packet.from_stream(uart_server)
|
||||
if isinstance(packet, AccelerometerPacket):
|
||||
print(packet.x, packet.y, packet.z)
|
||||
57
examples/bluefruitconnect_controlpad.py
Normal file
57
examples/bluefruitconnect_controlpad.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
# Basic structure example for using the BLE Connect Control Pad
|
||||
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
|
||||
# Connect, and then select Controller-> Control Pad.
|
||||
|
||||
from adafruit_ble import BLERadio
|
||||
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
||||
from adafruit_ble.services.nordic import UARTService
|
||||
from adafruit_bluefruit_connect.packet import Packet
|
||||
|
||||
# Only the packet classes that are imported will be known to Packet.
|
||||
from adafruit_bluefruit_connect.button_packet import ButtonPacket
|
||||
|
||||
ble = BLERadio()
|
||||
uart_server = UARTService()
|
||||
advertisement = ProvideServicesAdvertisement(uart_server)
|
||||
|
||||
while True:
|
||||
print("WAITING...")
|
||||
# Advertise when not connected.
|
||||
ble.start_advertising(advertisement)
|
||||
while not ble.connected:
|
||||
pass
|
||||
|
||||
# Connected
|
||||
ble.stop_advertising()
|
||||
print("CONNECTED")
|
||||
|
||||
# Loop and read packets
|
||||
while ble.connected:
|
||||
|
||||
# Keeping trying until a good packet is received
|
||||
try:
|
||||
packet = Packet.from_stream(uart_server)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
# Only handle button packets
|
||||
if isinstance(packet, ButtonPacket) and packet.pressed:
|
||||
if packet.button == ButtonPacket.UP:
|
||||
print("Button UP")
|
||||
if packet.button == ButtonPacket.DOWN:
|
||||
print("Button DOWN")
|
||||
if packet.button == ButtonPacket.LEFT:
|
||||
print("Button LEFT")
|
||||
if packet.button == ButtonPacket.RIGHT:
|
||||
print("Button RIGHT")
|
||||
if packet.button == ButtonPacket.BUTTON_1:
|
||||
print("Button 1")
|
||||
if packet.button == ButtonPacket.BUTTON_2:
|
||||
print("Button 2")
|
||||
if packet.button == ButtonPacket.BUTTON_3:
|
||||
print("Button 3")
|
||||
if packet.button == ButtonPacket.BUTTON_4:
|
||||
print("Button 4")
|
||||
|
||||
# Disconnected
|
||||
print("DISCONNECTED")
|
||||
62
examples/bluefruitconnect_sensors.py
Normal file
62
examples/bluefruitconnect_sensors.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# Basic structure example for using the BLE Connect Controller sensors
|
||||
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
|
||||
# Connect, and then select Controller and enable the sensors
|
||||
|
||||
from adafruit_ble import BLERadio
|
||||
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
||||
from adafruit_ble.services.nordic import UARTService
|
||||
from adafruit_bluefruit_connect.packet import Packet
|
||||
|
||||
# Only the packet classes that are imported will be known to Packet.
|
||||
from adafruit_bluefruit_connect.accelerometer_packet import AccelerometerPacket
|
||||
from adafruit_bluefruit_connect.gyro_packet import GyroPacket
|
||||
from adafruit_bluefruit_connect.location_packet import LocationPacket
|
||||
from adafruit_bluefruit_connect.magnetometer_packet import MagnetometerPacket
|
||||
from adafruit_bluefruit_connect.quaternion_packet import QuaternionPacket
|
||||
|
||||
ble = BLERadio()
|
||||
uart_server = UARTService()
|
||||
advertisement = ProvideServicesAdvertisement(uart_server)
|
||||
|
||||
while True:
|
||||
print("WAITING...")
|
||||
# Advertise when not connected.
|
||||
ble.start_advertising(advertisement)
|
||||
while not ble.connected:
|
||||
pass
|
||||
|
||||
# Connected
|
||||
ble.stop_advertising()
|
||||
print("CONNECTED")
|
||||
|
||||
# Loop and read packets
|
||||
while ble.connected:
|
||||
|
||||
# Keeping trying until a good packet is received
|
||||
try:
|
||||
packet = Packet.from_stream(uart_server)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
# Accelerometer
|
||||
if isinstance(packet, AccelerometerPacket):
|
||||
print("Accelerometer:", packet.x, packet.y, packet.z)
|
||||
|
||||
# Gyro
|
||||
if isinstance(packet, GyroPacket):
|
||||
print("Gyro:", packet.x, packet.y, packet.z)
|
||||
|
||||
# Location
|
||||
if isinstance(packet, LocationPacket):
|
||||
print("Location:", packet.latitude, packet.longitude, packet.altitude)
|
||||
|
||||
# Magnetometer
|
||||
if isinstance(packet, MagnetometerPacket):
|
||||
print("Magnetometer", packet.x, packet.y, packet.z)
|
||||
|
||||
# Quaternion
|
||||
if isinstance(packet, QuaternionPacket):
|
||||
print("Quaternion:", packet.x, packet.y, packet.z, packet.w)
|
||||
|
||||
# Disconnected
|
||||
print("DISCONNECTED")
|
||||
|
|
@ -2,20 +2,25 @@
|
|||
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
|
||||
# Connect, and then select colors on the Controller->Color Picker screen.
|
||||
|
||||
from adafruit_ble.uart import UARTServer
|
||||
from adafruit_ble import BLERadio
|
||||
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
||||
from adafruit_ble.services.nordic import UARTService
|
||||
from adafruit_bluefruit_connect.packet import Packet
|
||||
|
||||
# Only the packet classes that are imported will be known to Packet.
|
||||
from adafruit_bluefruit_connect.color_packet import ColorPacket
|
||||
|
||||
uart_server = UARTServer()
|
||||
ble = BLERadio()
|
||||
uart_server = UARTService()
|
||||
advertisement = ProvideServicesAdvertisement(uart_server)
|
||||
|
||||
while True:
|
||||
# Advertise when not connected.
|
||||
uart_server.start_advertising()
|
||||
while not uart_server.connected:
|
||||
ble.start_advertising(advertisement)
|
||||
while not ble.connected:
|
||||
pass
|
||||
|
||||
while uart_server.connected:
|
||||
while ble.connected:
|
||||
packet = Packet.from_stream(uart_server)
|
||||
if isinstance(packet, ColorPacket):
|
||||
print(packet.color)
|
||||
47
examples/bluefruitconnect_uart.py
Normal file
47
examples/bluefruitconnect_uart.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
# Basic example for using the BLE Connect UART
|
||||
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
|
||||
# Connect, and then select UART. Any text received FROM the connected device
|
||||
# will be displayed. Periodically, text is sent TO the connected device.
|
||||
|
||||
import time
|
||||
from adafruit_ble import BLERadio
|
||||
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
||||
from adafruit_ble.services.nordic import UARTService
|
||||
|
||||
SEND_RATE = 10 # how often in seconds to send text
|
||||
|
||||
ble = BLERadio()
|
||||
uart_server = UARTService()
|
||||
advertisement = ProvideServicesAdvertisement(uart_server)
|
||||
|
||||
count = 0
|
||||
while True:
|
||||
print("WAITING...")
|
||||
# Advertise when not connected.
|
||||
ble.start_advertising(advertisement)
|
||||
while not ble.connected:
|
||||
pass
|
||||
|
||||
# Connected
|
||||
ble.stop_advertising()
|
||||
print("CONNECTED")
|
||||
|
||||
# Loop and read packets
|
||||
last_send = time.monotonic()
|
||||
while ble.connected:
|
||||
# INCOMING (RX) check for incoming text
|
||||
if uart_server.in_waiting:
|
||||
raw_bytes = uart_server.read(uart_server.in_waiting)
|
||||
text = raw_bytes.decode().strip()
|
||||
# print("raw bytes =", raw_bytes)
|
||||
print("RX:", text)
|
||||
# OUTGOING (TX) periodically send text
|
||||
if time.monotonic() - last_send > SEND_RATE:
|
||||
text = "COUNT = {}\r\n".format(count)
|
||||
print("TX:", text.strip())
|
||||
uart_server.write(text.encode())
|
||||
count += 1
|
||||
last_send = time.monotonic()
|
||||
|
||||
# Disconnected
|
||||
print("DISCONNECTED")
|
||||
51
setup.py
51
setup.py
|
|
@ -7,6 +7,7 @@ https://github.com/pypa/sampleproject
|
|||
|
||||
# Always prefer setuptools over distutils
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
# To use a consistent encoding
|
||||
from codecs import open
|
||||
from os import path
|
||||
|
|
@ -14,48 +15,38 @@ from os import path
|
|||
here = path.abspath(path.dirname(__file__))
|
||||
|
||||
# Get the long description from the README file
|
||||
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
|
||||
with open(path.join(here, "README.rst"), encoding="utf-8") as f:
|
||||
long_description = f.read()
|
||||
|
||||
setup(
|
||||
name='adafruit-circuitpython-bluefruitconnect',
|
||||
|
||||
name="adafruit-circuitpython-bluefruitconnect",
|
||||
use_scm_version=True,
|
||||
setup_requires=['setuptools_scm'],
|
||||
|
||||
description='CircuitPython library for use with the Adafruit Bluefruit Connect apps.',
|
||||
|
||||
setup_requires=["setuptools_scm"],
|
||||
description="CircuitPython library for use with the Adafruit Bluefruit Connect apps.",
|
||||
long_description=long_description,
|
||||
long_description_content_type='text/x-rst',
|
||||
|
||||
long_description_content_type="text/x-rst",
|
||||
# The project's main homepage.
|
||||
url='https://github.com/adafruit/Adafruit_CircuitPython_BluefruitConnect',
|
||||
|
||||
url="https://github.com/adafruit/Adafruit_CircuitPython_BluefruitConnect",
|
||||
# Author details
|
||||
author='Adafruit Industries',
|
||||
author_email='circuitpython@adafruit.com',
|
||||
|
||||
install_requires=['Adafruit-Blinka'],
|
||||
|
||||
author="Adafruit Industries",
|
||||
author_email="circuitpython@adafruit.com",
|
||||
install_requires=["Adafruit-Blinka"],
|
||||
# Choose your license
|
||||
license='MIT',
|
||||
|
||||
license="MIT",
|
||||
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
|
||||
classifiers=[
|
||||
'Development Status :: 3 - Alpha',
|
||||
'Intended Audience :: Developers',
|
||||
'Topic :: Software Development :: Libraries',
|
||||
'Topic :: System :: Hardware',
|
||||
'License :: OSI Approved :: MIT License',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Programming Language :: Python :: 3.4',
|
||||
'Programming Language :: Python :: 3.5',
|
||||
"Development Status :: 3 - Alpha",
|
||||
"Intended Audience :: Developers",
|
||||
"Topic :: Software Development :: Libraries",
|
||||
"Topic :: System :: Hardware",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3.4",
|
||||
"Programming Language :: Python :: 3.5",
|
||||
],
|
||||
|
||||
# What does your project relate to?
|
||||
keywords='adafruit ble bluefruit bluetooth micropython circuitpython',
|
||||
|
||||
keywords="adafruit ble bluefruit bluetooth micropython circuitpython",
|
||||
# You can just specify the packages manually here if your project is
|
||||
# simple. Or you can use find_packages().
|
||||
py_modules=['adafruit_bluefruit_connect'],
|
||||
packages=["adafruit_bluefruit_connect"],
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue