Skip to content

New adafruit_ws2801.py #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 16 commits into from
Mar 19, 2018
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
201 changes: 201 additions & 0 deletions adafruit_ws2801.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# The MIT License (MIT)
#
# Copyright (c) 2016 Damien P. George (original Neopixel object)
# Copyright (c) 2017 Ladyada
# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
# Copyright (c) 2018 Kevin Walters
#
# 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.

"""
`adafruit_ws2801` - WS2801 LED pixel string driver
====================================================

* Author(s): Damien P. George, Limor Fried & Scott Shawcroft, Kevin Walters
"""
import math

import busio
import digitalio

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

### based on https://github.com/adafruit/Adafruit_CircuitPython_DotStar

class WS2801:
"""
A sequence of WS2801 controlled LEDs.

:param ~microcontroller.Pin clock: The pin to output dotstar clock on.
:param ~microcontroller.Pin data: The pin to output dotstar data on.
:param int n: The number of LEDs in the chain.
:param float brightness: The brightness between 0.0 and (default) 1.0.
:param bool auto_write: True if the dotstars should immediately change when
set. If False, `show` must be called explicitly.


Example for Gemma M0:

.. code-block:: python

import adafruit_ws2801
import time
from board import *

RED = 0x100000

with adafruit_ws2801.WS2801(board.D2, board.D0, 25, brightness=1.0) as pixels:
pixels[0] = RED
time.sleep(2)
"""

def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True):
self._spi = None
try:
self._spi = busio.SPI(clock, MOSI=data)
while not self._spi.try_lock():
pass
self._spi.configure(baudrate=1000*1000)
except ValueError:
self.dpin = digitalio.DigitalInOut(data)
self.cpin = digitalio.DigitalInOut(clock)
self.dpin.direction = digitalio.Direction.OUTPUT
self.cpin.direction = digitalio.Direction.OUTPUT
self.cpin.value = False
self._n = n
self._buf = bytearray(n * 3)
### TODO - review brightness oddity inherited from adafruit_dotstar
self._brightness = 1.0
self.brightness = brightness
self.auto_write = auto_write
### TODO - review/consider adding GRB support like that in c++ version

def deinit(self):
"""Blank out the DotStars and release the resources."""
self.auto_write = False
black=(0,0,0)
self.fill(black)
self.show()
if self._spi:
self._spi.deinit()
else:
self.dpin.deinit()
self.cpin.deinit()

def __enter__(self):
return self

def __exit__(self, exception_type, exception_value, traceback):
self.deinit()

def __repr__(self):
return "[" + ", ".join([str(x) for x in self]) + "]"

def _set_item(self, index, value):
offset = index * 3
if isinstance(value, int):
r = value >> 16
g = (value >> 8) & 0xff
b = value & 0xff
else:
r, g, b = value
# red/green/blue order for WS2801
self._buf[offset] = r
self._buf[offset + 1] = g
self._buf[offset + 2] = b

def __setitem__(self, index, val):
if isinstance(index, slice):
start, stop, step = index.indices(self._n)
length = stop - start
if step != 0:
length = math.ceil(length / step)
if len(val) != length:
raise ValueError("Slice and input sequence size do not match.")
for val_i, in_i in enumerate(range(start, stop, step)):
self._set_item(in_i, val[val_i])
else:
self._set_item(index, val)

if self.auto_write:
self.show()

def __getitem__(self, index):
if isinstance(index, slice):
out = []
for in_i in range(*index.indices(self._n)):
out.append(
tuple(self._buf[in_i * 3 + (2 - i)] for i in range(3)))
return out
if index < 0:
index += len(self)
if index >= self._n or index < 0:
raise IndexError
offset = index * 3
return tuple(self._buf[offset + (2 - i)] for i in range(3))

def __len__(self):
return self._n

@property
def brightness(self):
"""Overall brightness of the pixel"""
return self._brightness

@brightness.setter
def brightness(self, brightness):
self._brightness = min(max(brightness, 0.0), 1.0)

def fill(self, color):
"""Colors all pixels the given ***color***."""
auto_write = self.auto_write
self.auto_write = False
for i, _ in enumerate(self):
self[i] = color
if auto_write:
self.show()
self.auto_write = auto_write

def _ds_writebytes(self, buf):
for b in buf:
for _ in range(8):
self.cpin.value = True
self.dpin.value = (b & 0x80)
self.cpin.value = False
b = b << 1

def show(self):
"""Shows the new colors on the pixels themselves if they haven't already
been autowritten.

The colors may or may not be showing after this function returns because
it may be done asynchronously."""
# Create a second output buffer if we need to compute brightness
buf = self._buf
if self.brightness < 1.0:
buf = bytearray(len(self._buf))
for i in range(self._n):
buf[i] = int(self._buf[i] * self._brightness)

if self._spi:
self._spi.write(buf)
else:
self._ds_writebytes(buf)
self.cpin.value = False