Skip to content

Adding in code #1

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 1 commit into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions CODE_OF_CONDUCT.md
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/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Carter Nelson for adafruit

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 0 additions & 2 deletions README.md

This file was deleted.

89 changes: 89 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

Introduction
============

.. image:: https://readthedocs.org/projects/adafruit-circuitpython-ds2413/badge/?version=latest

:target: https://circuitpython.readthedocs.io/projects/ds2413/en/latest/

:alt: Documentation Status

.. image :: https://img.shields.io/discord/327254708534116352.svg
:target: https://discord.gg/nBQh6qu
:alt: Discord

CircuitPython driver for the DS2413 one wire 2 channel GPIO breakout.

Dependencies
=============
This driver depends on:

* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
* `Adafruit OneWire <https://github.com/adafruit/Adafruit_CircuitPython_OneWire>`_

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>`_.

Usage Example
=============

.. code-block:: python

import time
import board
from adafruit_onewire.bus import OneWireBus
import adafruit_ds2413

ow_bus = OneWireBus(board.D2)
ds = adafruit_ds2413.DS2413(ow_bus, ow_bus.scan()[0])

led = ds.IOA
button = ds.IOB
button.direction = adafruit_ds2413.INPUT

while not button.value:
led.value = True
time.sleep(0.5)
led.value = False
time.sleep(0.5)


API Reference
=============

.. toctree::
:maxdepth: 2

api

Contributing
============

Contributions are welcome! Please read our `Code of Conduct
<https://github.com/adafruit/Adafruit_CircuitPython_DS2413/blob/master/CODE_OF_CONDUCT.md>`_
before contributing to help this project stay welcoming.

Building locally
================

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-ds2413 --library_location .
151 changes: 151 additions & 0 deletions adafruit_ds2413.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# The MIT License (MIT)
#
# Copyright (c) 2017 Carter Nelson for adafruit
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# pylint: disable=C0103
"""
`adafruit_DS2413`
====================================================

CircuitPython driver for the DS2413 one wire 2 channel GPIO breakout.

* Author(s): Carter Nelson
"""

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DS2413.git"

from adafruit_onewire.device import OneWireDevice
from micropython import const

_DS2413_ACCESS_READ = b'\xF5'
_DS2413_ACCESS_WRITE = b'\x5A'
_DS2413_ACK_SUCCESS = b'\xAA'
_DS2413_ACK_ERROR = b'\xFF'
INPUT = const(0)
OUTPUT = const(1)

class DS2413Pin():
"""Class which provides interface to single DS2413 GPIO pin."""

def __init__(self, number, host, direction=OUTPUT):
if number not in (0, 1):
raise ValueError("Incorrect pin number.")
self._number = number
self._host = host
self._mask = 1 << (number * 2)
self._direction = None # create it, and then...
self.direction = direction # set it through setter

@property
def direction(self):
"""The direction of the pin, either INPUT or OUTPUT."""
return self._direction

@direction.setter
def direction(self, direction):
if direction not in (INPUT, OUTPUT):
raise ValueError("Incorrect direction setting.")
self._direction = OUTPUT
self.value = False
self._direction = direction

@property
def value(self):
"""The pin state if configured as INPUT. The output latch state
if configured as OUTPUT. True is HIGH/ON, False is LOW/OFF."""
# return Pin State if configured for INPUT
# return Latch State if configured for OUTPUT
# NOTE: logic is re-inverted to make it more normally
return not self._host.pio_state & (self._mask << self._direction)

@value.setter
def value(self, state):
# This only makes sense if the pin is configured for OUTPUT.
if self._direction == INPUT:
raise RuntimeError("Can't set value when pin is set to input.")
# We jump through some hoops in order to only set/clear the bit
# for the channel associated with this pin object.
current = self._host.pio_state
# PIOB Output Latch PIOA Output Latch
new = (current >> 2 & 0x02) | (current >> 1 & 0x01)
# To switch the output transistor on, the corresponding bit value is 0.
# To switch the output transistor off (non-conducting) the bit must be 1.
if state:
# clear it (transistor = ON)
new &= ~(1 << self._number)
else:
# set it (transistor = OFF)
new |= 1 << self._number
self._host.pio_state = new

class DS2413():
"""Class which provides interface to DS2413 GPIO breakout."""

def __init__(self, bus, address):
if address.family_code == 0x3A:
self._address = address
self._device = OneWireDevice(bus, address)
self._buf = bytearray(3)
self._IOA = None
self._IOB = None
else:
raise RuntimeError('Incorrect family code in device address.')

@property
def IOA(self):
"""The pin object for channel A."""
if self._IOA is None:
self._IOA = DS2413Pin(0, self)
return self._IOA

@property
def IOB(self):
"""The pin object for channel B."""
if self._IOB is None:
self._IOB = DS2413Pin(1, self)
return self._IOB

@property
def pio_state(self):
"""The state of both PIO channels."""
return self._read_status()

@pio_state.setter
def pio_state(self, value):
return self._write_latches(value)

def _read_status(self):
with self._device as dev:
dev.write(_DS2413_ACCESS_READ)
dev.readinto(self._buf, end=1)
return self._buf[0]

def _write_latches(self, value):
# top six bits must be 1
value |= 0xFC
self._buf[0] = value
self._buf[1] = ~value & 0xFF
with self._device as dev:
dev.write(_DS2413_ACCESS_WRITE)
dev.write(self._buf, end=2)
dev.readinto(self._buf, end=1)
if not self._buf[0] == ord(_DS2413_ACK_SUCCESS):
raise RuntimeError('ACK failure.')
5 changes: 5 additions & 0 deletions api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

.. If you created a package, create one automodule per module in the package.

.. automodule:: adafruit_ds2413
:members:
Loading