Skip to content

Commit f2e13a1

Browse files
authored
Merge pull request #1 from caternuson/master
Adding in code
2 parents 2f26809 + e058003 commit f2e13a1

File tree

11 files changed

+550
-2
lines changed

11 files changed

+550
-2
lines changed

CODE_OF_CONDUCT.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, gender identity and expression, level of experience,
9+
nationality, personal appearance, race, religion, or sexual identity and
10+
orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at [email protected]. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at [http://contributor-covenant.org/version/1/4][version]
72+
73+
[homepage]: http://contributor-covenant.org
74+
[version]: http://contributor-covenant.org/version/1/4/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Carter Nelson for adafruit
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.rst

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
Introduction
3+
============
4+
5+
.. image:: https://readthedocs.org/projects/adafruit-circuitpython-ds2413/badge/?version=latest
6+
7+
:target: https://circuitpython.readthedocs.io/projects/ds2413/en/latest/
8+
9+
:alt: Documentation Status
10+
11+
.. image :: https://img.shields.io/discord/327254708534116352.svg
12+
:target: https://discord.gg/nBQh6qu
13+
:alt: Discord
14+
15+
CircuitPython driver for the DS2413 one wire 2 channel GPIO breakout.
16+
17+
Dependencies
18+
=============
19+
This driver depends on:
20+
21+
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
22+
* `Adafruit OneWire <https://github.com/adafruit/Adafruit_CircuitPython_OneWire>`_
23+
24+
Please ensure all dependencies are available on the CircuitPython filesystem.
25+
This is easily achieved by downloading
26+
`the Adafruit library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_.
27+
28+
Usage Example
29+
=============
30+
31+
.. code-block:: python
32+
33+
import time
34+
import board
35+
from adafruit_onewire.bus import OneWireBus
36+
import adafruit_ds2413
37+
38+
ow_bus = OneWireBus(board.D2)
39+
ds = adafruit_ds2413.DS2413(ow_bus, ow_bus.scan()[0])
40+
41+
led = ds.IOA
42+
button = ds.IOB
43+
button.direction = adafruit_ds2413.INPUT
44+
45+
while not button.value:
46+
led.value = True
47+
time.sleep(0.5)
48+
led.value = False
49+
time.sleep(0.5)
50+
51+
52+
API Reference
53+
=============
54+
55+
.. toctree::
56+
:maxdepth: 2
57+
58+
api
59+
60+
Contributing
61+
============
62+
63+
Contributions are welcome! Please read our `Code of Conduct
64+
<https://github.com/adafruit/Adafruit_CircuitPython_DS2413/blob/master/CODE_OF_CONDUCT.md>`_
65+
before contributing to help this project stay welcoming.
66+
67+
Building locally
68+
================
69+
70+
To build this library locally you'll need to install the
71+
`circuitpython-build-tools <https://github.com/adafruit/circuitpython-build-tools>`_ package.
72+
73+
.. code-block:: shell
74+
75+
python3 -m venv .env
76+
source .env/bin/activate
77+
pip install circuitpython-build-tools
78+
79+
Once installed, make sure you are in the virtual environment:
80+
81+
.. code-block:: shell
82+
83+
source .env/bin/activate
84+
85+
Then run the build:
86+
87+
.. code-block:: shell
88+
89+
circuitpython-build-bundles --filename_prefix adafruit-circuitpython-ds2413 --library_location .

adafruit_ds2413.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2017 Carter Nelson for adafruit
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
# pylint: disable=C0103
23+
"""
24+
`adafruit_DS2413`
25+
====================================================
26+
27+
CircuitPython driver for the DS2413 one wire 2 channel GPIO breakout.
28+
29+
* Author(s): Carter Nelson
30+
"""
31+
32+
__version__ = "0.0.0-auto.0"
33+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DS2413.git"
34+
35+
from adafruit_onewire.device import OneWireDevice
36+
from micropython import const
37+
38+
_DS2413_ACCESS_READ = b'\xF5'
39+
_DS2413_ACCESS_WRITE = b'\x5A'
40+
_DS2413_ACK_SUCCESS = b'\xAA'
41+
_DS2413_ACK_ERROR = b'\xFF'
42+
INPUT = const(0)
43+
OUTPUT = const(1)
44+
45+
class DS2413Pin():
46+
"""Class which provides interface to single DS2413 GPIO pin."""
47+
48+
def __init__(self, number, host, direction=OUTPUT):
49+
if number not in (0, 1):
50+
raise ValueError("Incorrect pin number.")
51+
self._number = number
52+
self._host = host
53+
self._mask = 1 << (number * 2)
54+
self._direction = None # create it, and then...
55+
self.direction = direction # set it through setter
56+
57+
@property
58+
def direction(self):
59+
"""The direction of the pin, either INPUT or OUTPUT."""
60+
return self._direction
61+
62+
@direction.setter
63+
def direction(self, direction):
64+
if direction not in (INPUT, OUTPUT):
65+
raise ValueError("Incorrect direction setting.")
66+
self._direction = OUTPUT
67+
self.value = False
68+
self._direction = direction
69+
70+
@property
71+
def value(self):
72+
"""The pin state if configured as INPUT. The output latch state
73+
if configured as OUTPUT. True is HIGH/ON, False is LOW/OFF."""
74+
# return Pin State if configured for INPUT
75+
# return Latch State if configured for OUTPUT
76+
# NOTE: logic is re-inverted to make it more normally
77+
return not self._host.pio_state & (self._mask << self._direction)
78+
79+
@value.setter
80+
def value(self, state):
81+
# This only makes sense if the pin is configured for OUTPUT.
82+
if self._direction == INPUT:
83+
raise RuntimeError("Can't set value when pin is set to input.")
84+
# We jump through some hoops in order to only set/clear the bit
85+
# for the channel associated with this pin object.
86+
current = self._host.pio_state
87+
# PIOB Output Latch PIOA Output Latch
88+
new = (current >> 2 & 0x02) | (current >> 1 & 0x01)
89+
# To switch the output transistor on, the corresponding bit value is 0.
90+
# To switch the output transistor off (non-conducting) the bit must be 1.
91+
if state:
92+
# clear it (transistor = ON)
93+
new &= ~(1 << self._number)
94+
else:
95+
# set it (transistor = OFF)
96+
new |= 1 << self._number
97+
self._host.pio_state = new
98+
99+
class DS2413():
100+
"""Class which provides interface to DS2413 GPIO breakout."""
101+
102+
def __init__(self, bus, address):
103+
if address.family_code == 0x3A:
104+
self._address = address
105+
self._device = OneWireDevice(bus, address)
106+
self._buf = bytearray(3)
107+
self._IOA = None
108+
self._IOB = None
109+
else:
110+
raise RuntimeError('Incorrect family code in device address.')
111+
112+
@property
113+
def IOA(self):
114+
"""The pin object for channel A."""
115+
if self._IOA is None:
116+
self._IOA = DS2413Pin(0, self)
117+
return self._IOA
118+
119+
@property
120+
def IOB(self):
121+
"""The pin object for channel B."""
122+
if self._IOB is None:
123+
self._IOB = DS2413Pin(1, self)
124+
return self._IOB
125+
126+
@property
127+
def pio_state(self):
128+
"""The state of both PIO channels."""
129+
return self._read_status()
130+
131+
@pio_state.setter
132+
def pio_state(self, value):
133+
return self._write_latches(value)
134+
135+
def _read_status(self):
136+
with self._device as dev:
137+
dev.write(_DS2413_ACCESS_READ)
138+
dev.readinto(self._buf, end=1)
139+
return self._buf[0]
140+
141+
def _write_latches(self, value):
142+
# top six bits must be 1
143+
value |= 0xFC
144+
self._buf[0] = value
145+
self._buf[1] = ~value & 0xFF
146+
with self._device as dev:
147+
dev.write(_DS2413_ACCESS_WRITE)
148+
dev.write(self._buf, end=2)
149+
dev.readinto(self._buf, end=1)
150+
if not self._buf[0] == ord(_DS2413_ACK_SUCCESS):
151+
raise RuntimeError('ACK failure.')

api.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
.. If you created a package, create one automodule per module in the package.
3+
4+
.. automodule:: adafruit_ds2413
5+
:members:

0 commit comments

Comments
 (0)