Skip to content

Commit 31d4e04

Browse files
committed
add code
1 parent 25fafc4 commit 31d4e04

File tree

5 files changed

+134
-56
lines changed

5 files changed

+134
-56
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ install:
4242
- pip install --force-reinstall pylint==1.9.2
4343

4444
script:
45-
- pylint adafruit_neopixel_spi.py
45+
- pylint neopixel_spi.py
4646
- ([[ ! -d "examples" ]] || pylint --disable=missing-docstring,invalid-name,bad-whitespace examples/*.py)
4747
- circuitpython-build-bundles --filename_prefix adafruit-circuitpython-neopixel_spi --library_location .
4848
- cd docs && sphinx-build -E -W -b html . _build/html && cd ..

adafruit_neopixel_spi.py

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

docs/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
55
.. use this format as the module name: "adafruit_foo.foo"
66
7-
.. automodule:: adafruit_neopixel_spi
7+
.. automodule:: neopixel_spi
88
:members:

neopixel_spi.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2019 Carter Nelson for Adafruit Industries
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+
"""
23+
`neopixel_spi`
24+
================================================================================
25+
26+
SPI driven CircuitPython driver for NeoPixels.
27+
28+
29+
* Author(s): Carter Nelson
30+
31+
Implementation Notes
32+
--------------------
33+
34+
**Hardware:**
35+
36+
* Hardware SPI port required on host platform.
37+
38+
**Software and Dependencies:**
39+
40+
* Adafruit Blinka:
41+
https://github.com/adafruit/Adafruit_Blinka
42+
43+
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
44+
"""
45+
46+
from neopixel import *
47+
48+
__version__ = "0.0.0-auto.0"
49+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel_SPI.git"
50+
51+
class NeoPixel_SPI(NeoPixel):
52+
"""
53+
A sequence of neopixels.
54+
55+
:param ~busio.SPI spi: The SPI bus to output neopixel data on.
56+
:param int n: The number of neopixels in the chain
57+
:param int bpp: Bytes per pixel. 3 for RGB and 4 for RGBW pixels.
58+
:param float brightness: Brightness of the pixels between 0.0 and 1.0 where 1.0 is full
59+
brightness
60+
:param bool auto_write: True if the neopixels should immediately change when set. If False,
61+
`show` must be called explicitly.
62+
:param tuple pixel_order: Set the pixel color channel order. GRBW is set by default.
63+
64+
Example:
65+
66+
.. code-block:: python
67+
68+
import board
69+
import neopixel
70+
71+
pixels = neopixel.NeoPixel_SPI(board.SPI(), 10)
72+
pixels.fill(0xff0000)
73+
"""
74+
#pylint: disable=invalid-name, super-init-not-called
75+
76+
FREQ = 6400000 # 800kHz * 8, actual may be different
77+
TRST = 80e-6 # Reset code low level time
78+
79+
def __init__(self, spi, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None):
80+
from adafruit_bus_device.spi_device import SPIDevice
81+
self._spi = SPIDevice(spi, baudrate=self.FREQ)
82+
with self._spi as spibus:
83+
try:
84+
# get actual SPI frequency
85+
freq = spibus.frequency
86+
except AttributeError:
87+
# use nominal
88+
freq = self.FREQ
89+
self.RESET = bytes([0]*round(freq*self.TRST))
90+
self.n = n
91+
if pixel_order is None:
92+
self.order = GRBW
93+
self.bpp = bpp
94+
else:
95+
self.order = pixel_order
96+
self.bpp = len(self.order)
97+
self.buf = bytearray(self.n * self.bpp)
98+
self.spibuf = bytearray(8*len(self.buf))
99+
# Set auto_write to False temporarily so brightness setter does _not_
100+
# call show() while in __init__.
101+
self.auto_write = False
102+
self.brightness = brightness
103+
self.auto_write = auto_write
104+
105+
def deinit(self):
106+
"""Blank out the NeoPixels."""
107+
for i in range(len(self.buf)):
108+
self.buf[i] = 0
109+
self.show()
110+
111+
def show(self):
112+
"""Shows the new colors on the pixels themselves if they haven't already
113+
been autowritten."""
114+
self._transmogrify()
115+
with self._spi as spi:
116+
# write out special byte sequence surrounded by RESET
117+
# leading RESET needed for cases where MOSI rests HI
118+
spi.write(self.RESET + self.spibuf + self.RESET)
119+
120+
def _transmogrify(self):
121+
"""Turn every BIT of buf into a special BYTE pattern."""
122+
k = 0
123+
for byte in self.buf:
124+
byte = int(byte * self.brightness)
125+
# MSB first
126+
for i in range(7, -1, -1):
127+
if byte >> i & 0x01:
128+
self.spibuf[k] = 0b11110000 # A NeoPixel 1 bit
129+
else:
130+
self.spibuf[k] = 0b11000000 # A NeoPixel 0 bit
131+
k += 1

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@
6161
# simple. Or you can use find_packages().
6262
# TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER,
6363
# CHANGE `py_modules=['...']` TO `packages=['...']`
64-
py_modules=['adafruit_neopixel_spi'],
64+
py_modules=['neopixel_spi'],
6565
)

0 commit comments

Comments
 (0)