Skip to content

Commit 14205d8

Browse files
authored
Cookiecutter the library and update to match NeoPixel API. (#1)
Cookiecutter the library and update to match NeoPixel API.
1 parent 2619924 commit 14205d8

11 files changed

+523
-102
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_build
2+
__pycache__

CODE_OF_CONDUCT.md

+74
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2016 Adafruit Industries
3+
Copyright (c) 2016, 2017 Adafruit Industries
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

-2
This file was deleted.

README.rst

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
Adafruit CircuitPython DotStar
3+
==============================
4+
5+
.. image:: https://readthedocs.org/projects/adafruit-circuitpython-neopixel/badge/?version=latest
6+
:target: https://circuitpython.readthedocs.io/projects/neopixel/en/latest/
7+
:alt: Documentation Status
8+
9+
.. image :: https://badges.gitter.im/adafruit/circuitpython.svg
10+
:target: https://gitter.im/adafruit/circuitpython?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge
11+
:alt: Gitter
12+
13+
.. image :: https://img.shields.io/discord/327254708534116352.svg
14+
:target: https://adafru.it/discord
15+
:alt: Discord
16+
17+
Higher level DotStar driver that presents the strip as a sequence. It is the
18+
same api as the `NeoPixel library <https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel>`_.
19+
20+
Colors are stored as tuples by default. However, you can also use int hex syntax
21+
to set values similar to colors on the web. For example, ``0x100000`` (``#100000``
22+
on the web) is equivalent to ``(0x10, 0, 0)``.
23+
24+
.. note:: The int hex API represents the brightness of the white pixel when
25+
present by setting the RGB channels to identical values. For example, full
26+
white is 0xffffff but is actually (0, 0, 0, 0xff) in the tuple syntax. Setting
27+
a pixel value with an int will use the white pixel if the RGB channels are
28+
identical. For full, independent, control of each color component use the
29+
tuple syntax.
30+
31+
Dependencies
32+
=============
33+
This driver depends on:
34+
35+
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
36+
37+
Please ensure all dependencies are available on the CircuitPython filesystem.
38+
This is easily achieved by downloading
39+
`the Adafruit library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_.
40+
41+
Usage Example
42+
=============
43+
44+
This example demonstrates the library with the single built-in DotStar on the
45+
`Trinket M0 <https://www.adafruit.com/product/3500>`_ and
46+
`Gemma M0 <https://www.adafruit.com/product/3501>`_.
47+
48+
.. code-block:: python
49+
50+
import board
51+
import adafruit_dotstar
52+
53+
pixels = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
54+
pixels[0] = (10, 0, 0)
55+
56+
Contributing
57+
============
58+
59+
Contributions are welcome! Please read our `Code of Conduct
60+
<https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel/blob/master/CODE_OF_CONDUCT.md>`_
61+
before contributing to help this project stay welcoming.
62+
63+
API Reference
64+
=============
65+
66+
.. toctree::
67+
:maxdepth: 2
68+
69+
api

adafruit_dotstar.py

+224
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2016 Damien P. George (original Neopixel object)
4+
# Copyright (c) 2017 Ladyada
5+
# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
25+
"""
26+
`adafruit_dotstar` - DotStar strip driver
27+
====================================================
28+
29+
* Author(s): Damien P. George, Limor Fried & Scott Shawcroft
30+
"""
31+
import busio
32+
import digitalio
33+
import time
34+
35+
class DotStar:
36+
"""
37+
A sequence of dotstars.
38+
39+
:param ~microcontroller.Pin clock: The pin to output dotstar clock on.
40+
:param ~microcontroller.Pin data: The pin to output dotstar data on.
41+
:param int n: The number of dotstars in the chain
42+
:param float brightness: Brightness of the pixels between 0.0 and 1.0
43+
:param bool auto_write: True if the neopixels should immediately change when set. If False, `show` must be called explicitly.
44+
45+
46+
Example for Gemma M0:
47+
48+
.. code-block:: python
49+
50+
import adafruit_dotstar
51+
import time
52+
from board import *
53+
54+
RED = 0x100000
55+
56+
with adafruit_dotstar.DotStar(APA102_SCK, APA102_MOSI, 1) as pixels:
57+
pixels[0] = RED
58+
time.sleep(2)
59+
"""
60+
61+
def __init__(self, clock, data, n, brightness=1.0, auto_write=True):
62+
self.spi = None
63+
try:
64+
self.spi = busio.SPI(clock, MOSI=data)
65+
while not self.spi.try_lock():
66+
pass
67+
self.spi.configure(baudrate=4000000)
68+
except ValueError:
69+
self.dpin = digitalio.DigitalInOut(data)
70+
self.cpin = digitalio.DigitalInOut(clock)
71+
self.dpin.direction = digitalio.Direction.OUTPUT
72+
self.cpin.direction = digitalio.Direction.OUTPUT
73+
self.cpin.value = False
74+
self.n = n
75+
self.start_header = 4
76+
# Supply one extra clock cycle for each two pixels in the strip.
77+
self.end_header = n // 16
78+
if n % 16 != 0:
79+
n += 1
80+
self.buf = bytearray(n * 4 + self.start_header + self.end_header)
81+
# Four empty bytes to start.
82+
for i in range(self.start_header):
83+
self.buf[i] = 0x00
84+
# Mark the beginnings of each pixel.
85+
for i in range(n):
86+
self.buf[self.start_header + 4 * i] = 0xff
87+
# 0xff bytes at the end.
88+
for i in range(self.end_header):
89+
self.buf[len(self.buf) - 1 - i] = 0xff
90+
self.brightness = brightness
91+
self.auto_write = auto_write
92+
93+
def deinit(self):
94+
"""Blank out the DotStars and release the resources."""
95+
self.auto_write = False
96+
for i in range(self.start_header, len(self.buf) - self.end_header):
97+
# Preserve the pixel markers.
98+
if i % 4 == 0:
99+
continue
100+
self.buf[i] = 0
101+
self.show()
102+
if self.spi:
103+
self.spi.deinit()
104+
else:
105+
self.dpin.deinit()
106+
self.cpin.deinit()
107+
108+
def __enter__(self):
109+
return self
110+
111+
def __exit__(self, exception_type, exception_value, traceback):
112+
self.deinit()
113+
114+
def __repr__(self):
115+
return "[" + ", ".join([str(x) for x in self]) + "]"
116+
117+
def _set_item(self, index, value):
118+
offset = index * 4 + self.start_header
119+
r = 0
120+
g = 0
121+
b = 0
122+
if type(value) == int:
123+
r = value >> 16
124+
g = (value >> 8) & 0xff
125+
b = value & 0xff
126+
else:
127+
r, g, b = value
128+
# Each pixel starts with 0xFF, then red/green/blue. Although the data
129+
# sheet suggests using a global brightness in the first byte, we don't
130+
# do that because it causes further issues with persistence of vision
131+
# projects.
132+
self.buf[offset] = 0xff
133+
self.buf[offset + 1] = b
134+
self.buf[offset + 2] = g
135+
self.buf[offset + 3] = r
136+
137+
def __setitem__(self, index, val):
138+
if isinstance(index, slice):
139+
start, stop, step = index.indices(len(self))
140+
length = stop - start
141+
if step != 0:
142+
length = math.ceil(length / step)
143+
if len(val) != length:
144+
raise ValueError("Slice and input sequence size do not match.")
145+
for val_i, in_i in enumerate(range(start, stop, step)):
146+
self._set_item(in_i, val[val_i])
147+
else:
148+
self._set_item(index, val)
149+
150+
if self.auto_write:
151+
self.show()
152+
153+
def __getitem__(self, index):
154+
if isinstance(index, slice):
155+
out = []
156+
for in_i in range(*index.indices(len(self.buf) // self.bpp)):
157+
out.append(tuple(self.buf[in_i * 4 + (3 - i) + self.start_header]
158+
for i in range(3)))
159+
return out
160+
if index < 0:
161+
index += len(self)
162+
if index >= self.n or index < 0:
163+
raise IndexError
164+
offset = index * 4
165+
return tuple(self.buf[offset + (3 - i) + self.start_header]
166+
for i in range(3))
167+
168+
def __len__(self):
169+
return self.n
170+
171+
@property
172+
def brightness(self):
173+
"""Overall brightness of the pixel"""
174+
return self._brightness
175+
176+
@brightness.setter
177+
def brightness(self, brightness):
178+
self._brightness = min(max(brightness, 0.0), 1.0)
179+
180+
def fill(self, color):
181+
"""Colors all pixels the given ***color***."""
182+
auto_write = self.auto_write
183+
self.auto_write = False
184+
for i in range(len(self)):
185+
self[i] = color
186+
if auto_write:
187+
self.show()
188+
self.auto_write = auto_write
189+
190+
def ds_writebytes(self, buf):
191+
for b in buf:
192+
for i in range(8):
193+
self.cpin.value = True
194+
self.dpin.value = (b & 0x80)
195+
self.cpin.value = False
196+
b = b << 1
197+
198+
def show(self):
199+
"""Shows the new colors on the pixels themselves if they haven't already
200+
been autowritten.
201+
202+
The colors may or may not be showing after this function returns because
203+
it may be done asynchronously."""
204+
# Create a second output buffer if we need to compute brightness
205+
buf = self.buf
206+
if self.brightness < 0.99:
207+
buf = bytearray(n * bpp + 8)
208+
# Four empty bytes to start.
209+
for i in range(4):
210+
buf[i] = 0x00
211+
for i in range(4, len(self.buf) - 4):
212+
if i % 4 == 0:
213+
buf[i] = self.buf
214+
continue
215+
buf[i] = self.buf[i] * self._brightness
216+
# Four 0xff bytes at the end.
217+
for i in range(4):
218+
buf[len(self.buf) - 4 + i] = 0xff
219+
220+
if self.spi:
221+
self.spi.write(buf)
222+
else:
223+
self.ds_writebytes(buf)
224+
self.cpin.value = False

api.rst

+5
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_dotstar
5+
:members:

0 commit comments

Comments
 (0)