-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verified everything works using the Metro M0 Express in CircuitPython 1.0.0.
- Loading branch information
Showing
14 changed files
with
613 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,3 +99,4 @@ ENV/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Travis CI configuration for automated .mpy file generation. | ||
# Author: Tony DiCola | ||
# License: Public Domain | ||
# This configuration will work with Travis CI (travis-ci.org) to automacially | ||
# build .mpy files for CircuitPython when a new tagged release is created. This | ||
# file is relatively generic and can be shared across multiple repositories by | ||
# following these steps: | ||
# 1. Copy this file into a .travis.yml file in the root of the repository. | ||
# 2. Change the deploy > file section below to list each of the .mpy files | ||
# that should be generated. The config will automatically look for | ||
# .py files with the same name as the source for generating the .mpy files. | ||
# Note that the .mpy extension should be lower case! | ||
# 3. Commit the .travis.yml file and push it to GitHub. | ||
# 4. Go to travis-ci.org and find the repository (it needs to be setup to access | ||
# your github account, and your github account needs access to write to the | ||
# repo). Flip the 'ON' switch on for Travis and the repo, see the Travis | ||
# docs for more details: https://docs.travis-ci.com/user/getting-started/ | ||
# 5. Get a GitHub 'personal access token' which has at least 'public_repo' or | ||
# 'repo' scope: https://help.github.com/articles/creating-an-access-token-for-command-line-use/ | ||
# Keep this token safe and secure! Anyone with the token will be able to | ||
# access and write to your GitHub repositories. Travis will use the token | ||
# to attach the .mpy files to the release. | ||
# 6. In the Travis CI settings for the repository that was enabled find the | ||
# environment variable editing page: https://docs.travis-ci.com/user/environment-variables/#Defining-Variables-in-Repository-Settings | ||
# Add an environment variable named GITHUB_TOKEN and set it to the value | ||
# of the GitHub personal access token above. Keep 'Display value in build | ||
# log' flipped off. | ||
# 7. That's it! Tag a release and Travis should go to work to add .mpy files | ||
# to the release. It takes about a 2-3 minutes for a worker to spin up, | ||
# build mpy-cross, and add the binaries to the release. | ||
language: generic | ||
|
||
sudo: true | ||
|
||
deploy: | ||
provider: releases | ||
api_key: $GITHUB_TOKEN | ||
file: | ||
- "hcsr04.mpy" | ||
skip_cleanup: true | ||
on: | ||
tags: true | ||
|
||
before_install: | ||
- sudo apt-get -yqq update | ||
- sudo apt-get install -y build-essential git python python-pip | ||
- git clone https://github.com/adafruit/circuitpython.git | ||
- make -C circuitpython/mpy-cross | ||
- export PATH=$PATH:$PWD/circuitpython/mpy-cross/ | ||
- sudo pip install shyaml | ||
|
||
before_deploy: | ||
- shyaml get-values deploy.file < .travis.yml | sed 's/.mpy/.py/' | xargs -L1 mpy-cross |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Contributor Covenant Code of Conduct | ||
|
||
## Our Pledge | ||
|
||
In the interest of fostering an open and welcoming environment, we as | ||
contributors and maintainers pledge to making participation in our project and | ||
our community a harassment-free experience for everyone, regardless of age, body | ||
size, disability, ethnicity, gender identity and expression, level of experience, | ||
nationality, personal appearance, race, religion, or sexual identity and | ||
orientation. | ||
|
||
## Our Standards | ||
|
||
Examples of behavior that contributes to creating a positive environment | ||
include: | ||
|
||
* Using welcoming and inclusive language | ||
* Being respectful of differing viewpoints and experiences | ||
* Gracefully accepting constructive criticism | ||
* Focusing on what is best for the community | ||
* Showing empathy towards other community members | ||
|
||
Examples of unacceptable behavior by participants include: | ||
|
||
* The use of sexualized language or imagery and unwelcome sexual attention or | ||
advances | ||
* Trolling, insulting/derogatory comments, and personal or political attacks | ||
* Public or private harassment | ||
* Publishing others' private information, such as a physical or electronic | ||
address, without explicit permission | ||
* Other conduct which could reasonably be considered inappropriate in a | ||
professional setting | ||
|
||
## Our Responsibilities | ||
|
||
Project maintainers are responsible for clarifying the standards of acceptable | ||
behavior and are expected to take appropriate and fair corrective action in | ||
response to any instances of unacceptable behavior. | ||
|
||
Project maintainers have the right and responsibility to remove, edit, or | ||
reject comments, commits, code, wiki edits, issues, and other contributions | ||
that are not aligned to this Code of Conduct, or to ban temporarily or | ||
permanently any contributor for other behaviors that they deem inappropriate, | ||
threatening, offensive, or harmful. | ||
|
||
## Scope | ||
|
||
This Code of Conduct applies both within project spaces and in public spaces | ||
when an individual is representing the project or its community. Examples of | ||
representing a project or community include using an official project e-mail | ||
address, posting via an official social media account, or acting as an appointed | ||
representative at an online or offline event. Representation of a project may be | ||
further defined and clarified by project maintainers. | ||
|
||
## Enforcement | ||
|
||
Instances of abusive, harassing, or otherwise unacceptable behavior may be | ||
reported by contacting the project team at support@adafruit.com. All | ||
complaints will be reviewed and investigated and will result in a response that | ||
is deemed necessary and appropriate to the circumstances. The project team is | ||
obligated to maintain confidentiality with regard to the reporter of an incident. | ||
Further details of specific enforcement policies may be posted separately. | ||
|
||
Project maintainers who do not follow or enforce the Code of Conduct in good | ||
faith may face temporary or permanent repercussions as determined by other | ||
members of the project's leadership. | ||
|
||
## Attribution | ||
|
||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, | ||
available at [http://contributor-covenant.org/version/1/4][version] | ||
|
||
[homepage]: http://contributor-covenant.org | ||
[version]: http://contributor-covenant.org/version/1/4/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[[source]] | ||
url = "https://pypi.python.org/simple" | ||
verify_ssl = true | ||
|
||
[dev-packages] | ||
sphinx = "*" | ||
sphinx-rtd-theme = "*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
CircuitPython HC-SR04 Driver | ||
============================ | ||
|
||
|docs| |version| |ci| |license_type| | ||
|
||
.. image:: hcsr04.jpg | ||
:width: 300px | ||
|
||
The HC-SR04 is an inexpensive solution for measuring distances using microcontrollers. This library provides a simple | ||
driver for controlling these sensors from `CircuitPython`_, Adafruit's port of `MicroPython <http://micropython.org/>`_. | ||
|
||
|
||
Installation | ||
------------ | ||
|
||
This driver depends on `CircuitPython <https://github.com/adafruit/circuitpython>`_ and is designed for use with an | ||
HC-SR04 ultrasonic range sensor. You'll also need to 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>`_. | ||
|
||
.. warning:: | ||
|
||
The HC-SR04 uses 5V logic, so you will have to use a `level shifter | ||
<https://www.adafruit.com/product/2653?q=level%20shifter&>`_ between it | ||
and your CircuitPython board (which uses 3.3V logic). | ||
|
||
.. note:: | ||
|
||
If you want to use an HC-SR04 with `MicroPython <http://micropython.org/>`_, I recommend checking out `this library | ||
<https://github.com/andrey-git/micropython-hcsr04>`_. | ||
|
||
|
||
Quick Start | ||
----------- | ||
|
||
You'll need to dedicate two pins to communicating with the HC-SR04. The sensor communicates in a very rudimentary | ||
manner, so it doesn't matter which pins you choose, as long as they're digital IO pins (pins that start with "``D``" | ||
are digital). | ||
|
||
There are two ways of instantiating a :class:`~hcsr04.HCSR04` object: with or without using a context manager. | ||
|
||
.. note:: | ||
|
||
It is technically possible to communicate with the HC-SR04 using only one wire since the trigger and echo signals | ||
aren't ever active at the same time. Once I have a chance to determine a safe way to do this, I plan to add this as | ||
a feature to the library. | ||
|
||
.. seealso:: | ||
|
||
`Adafruit's guide on Lifetime and ContextManagers <https://circuitpython.readthedocs.io/en/latest/docs/design_guide.html#lifetime-and-contextmanagers>`_ | ||
Gives more info on using context managers with CircuitPython drivers. | ||
|
||
:any:`board` | ||
A list of pins available on your device. To view this list, first `get a REPL | ||
<http://circuitpython.readthedocs.io/en/latest/docs/pyboard/tutorial/repl.html>`_ (the guide linked was written | ||
for the pyboard, but it still works), then input the following: | ||
|
||
:: | ||
|
||
import board | ||
dir(board) | ||
|
||
Without a Context Manager | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
In the example below, we create the :class:`~hcsr04.HCSR04` object directly, get the distance every 2 seconds, then | ||
de-initialize the device. | ||
|
||
:: | ||
|
||
from hcsr04 import HCSR04 | ||
sonar = HCSR04(trig, echo) | ||
try: | ||
while True: | ||
print(sonar.dist_cm()) | ||
sleep(2) | ||
except KeyboardInterrupt: | ||
pass | ||
sonar.deinit() | ||
|
||
|
||
With a Context Manager | ||
^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
In the example below, we use a context manager (the :any:`with <with>` statement) to create the :class:`~hcsr04.HCSR04` | ||
instance, again get the distance every 2 seconds, but then the context manager handles de-initializing the device for | ||
us. | ||
|
||
:: | ||
|
||
from hcsr04 import HCSR04 | ||
with HCSR04(trig, echo) as sonar: | ||
try: | ||
while True: | ||
print(sonar.dist_cm()) | ||
sleep(2) | ||
except KeyboardInterrupt: | ||
pass | ||
|
||
|
||
API Reference | ||
------------- | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
api | ||
|
||
|
||
Contributing | ||
------------ | ||
|
||
Contributions are welcome! Please read our `Code of Conduct | ||
<https://github.com/adafruit/Adafruit_CircuitPython_HCSR04/blob/master/CODE_OF_CONDUCT.md>`_ | ||
before contributing to help this project stay welcoming. | ||
|
||
|
||
License | ||
------- | ||
|
||
This project is licensed under the `MIT License <https://github.com/mmabey/CircuitPython_HCSR04/blob/master/LICENSE>`_. | ||
|
||
|
||
.. |docs| image:: https://readthedocs.org/projects/adafruit-soundboard/badge/ | ||
:alt: Documentation Status | ||
:target: `Read the Docs`_ | ||
|
||
.. |version| image:: https://img.shields.io/github/release/mmabey/CircuitPython_HCSR04/all.svg | ||
:alt: Release Version | ||
:target: https://github.com/mmabey/CircuitPython_HCSR04 | ||
|
||
.. |ci| image:: https://travis-ci.org/mmabey/CircuitPython_HCSR04.svg | ||
:alt: CI Build Status | ||
:target: https://travis-ci.org/mmabey/CircuitPython_HCSR04 | ||
|
||
.. |license_type| image:: https://img.shields.io/github/license/mmabey/CircuitPython_HCSR04.svg | ||
:alt: License: MIT | ||
:target: `GitHub`_ | ||
|
||
.. _GitHub: https://github.com/mmabey/CircuitPython_HCSR04 | ||
|
||
.. _CircuitPython: https://github.com/adafruit/circuitpython | ||
|
||
.. _Read the Docs: http://circuitpython-hcsr04.readthedocs.io/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Minimal makefile for Sphinx documentation | ||
# | ||
|
||
# You can set these variables from the command line. | ||
SPHINXOPTS = | ||
SPHINXBUILD = python -msphinx | ||
SPHINXPROJ = CircuitPythonHCSR04Library | ||
SOURCEDIR = . | ||
BUILDDIR = _build | ||
|
||
# Put it first so that "make" without argument is like "make help". | ||
help: | ||
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) | ||
|
||
.PHONY: help Makefile | ||
|
||
# Catch-all target: route all unknown targets to Sphinx using the new | ||
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). | ||
%: Makefile | ||
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
``hcsr04`` | ||
========== | ||
|
||
.. automodule:: hcsr04 | ||
:members: |
Oops, something went wrong.