Skip to content

Adding auto_write and show() #15

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
Nov 20, 2018
Merged
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
45 changes: 43 additions & 2 deletions adafruit_trellism4.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
class _NeoPixelArray:
"""Creates a NeoPixel array for use in the ``TrellisM4Express`` class."""
def __init__(self, pin, *, width, height, rotation=0):
self._neopixel = neopixel.NeoPixel(pin, width * height, auto_write=False)
self._neopixel = neopixel.NeoPixel(pin, width * height, auto_write=True)
if rotation % 90 != 0:
raise ValueError("Only 90 degree rotations supported")
self._rotation = rotation % 360
Expand Down Expand Up @@ -82,8 +82,50 @@ def __setitem__(self, index, value):
raise IndexError("Pixel assignment outside available coordinates.")

self._neopixel[offset] = value

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

Use when ``auto_write`` is set to ``False``.

The colors may or may not be showing after this function returns because
it may be done asynchronously.
"""
self._neopixel.show()

@property
def auto_write(self):
"""
True if the neopixels should immediately change when set. If False,
``show`` must be called explicitly.

This example disables ``auto_write``, sets every pixel, calls ``show()``, then
re-enables ``auto-write``.

.. code-block:: python

import adafruit_trellism4

trellis = adafruit_trellism4.TrellisM4Express()

trellis.pixels.auto_write = False

for x in range(trellis.pixels.width):
for y in range(trellis.pixels.height):
trellis.pixels[x, y] = (0, 255, 0)

trellis.pixels.show() # must call show() when auto_write == False

trellis.pixels.auto_write = True
"""
return self._neopixel.auto_write

@auto_write.setter
def auto_write(self, val):
self._neopixel.auto_write = val

@property
def brightness(self):
"""
Expand Down Expand Up @@ -125,7 +167,6 @@ def fill(self, color):

"""
self._neopixel.fill(color)
self._neopixel.show()

@property
def width(self):
Expand Down