diff --git a/adafruit_led_animation/__init__.py b/adafruit_led_animation/__init__.py index ff60e91..d9247bb 100644 --- a/adafruit_led_animation/__init__.py +++ b/adafruit_led_animation/__init__.py @@ -28,13 +28,22 @@ try: return monotonic_ns() // NANOS_PER_MS except (ImportError, NotImplementedError): - import time + try: + from time import monotonic - def monotonic_ms(): - """ - Implementation of monotonic_ms for platforms without time.monotonic_ns - """ - return int(time.monotonic() * MS_PER_SECOND) + def monotonic_ms(): + """ + Implementation of monotonic_ms for platforms without time.monotonic_ns + """ + return int(monotonic() * MS_PER_SECOND) + except (ImportError, NotImplementedError): + from time import time_ns + + def monotonic_ms(): + """ + Implementation of monotonic_ms for platforms without time.monotonic_ns or time.monotonic + """ + return time_ns() // NANOS_PER_MS NANOS_PER_MS = const(1000000) diff --git a/adafruit_led_animation/animation/__init__.py b/adafruit_led_animation/animation/__init__.py old mode 100644 new mode 100755 index 87e1bf2..ca5f831 --- a/adafruit_led_animation/animation/__init__.py +++ b/adafruit_led_animation/animation/__init__.py @@ -41,7 +41,8 @@ class Animation: # pylint: disable=too-many-arguments def __init__(self, pixel_object, speed, color, peers=None, paused=False, name=None): self.pixel_object = pixel_object - self.pixel_object.auto_write = False + if hasattr(self.pixel_object, "auto_write"): + self.pixel_object.auto_write = False self._peers = [self] + peers if peers is not None else [self] self._speed_ms = 0 self._color = None @@ -116,7 +117,10 @@ class Animation: """ Displays the updated pixels. Called during animates with changes. """ - self.pixel_object.show() + if hasattr(self.pixel_object, "show"): + self.pixel_object.show() + elif hasattr(self.pixel_object, "write"): + self.pixel_object.write() @property def peers(self): @@ -154,7 +158,10 @@ class Animation: Fills the pixel object with a color. """ self.pixel_object.fill(color) - self.pixel_object.show() + if hasattr(self.pixel_object, "show"): + self.pixel_object.show() + elif hasattr(self.pixel_object, "write"): + self.pixel_object.write() @property def color(self): diff --git a/adafruit_led_animation/color.py b/adafruit_led_animation/color.py old mode 100644 new mode 100755 index 9f8b17a..2d36ccc --- a/adafruit_led_animation/color.py +++ b/adafruit_led_animation/color.py @@ -24,7 +24,28 @@ Implementation Notes https://circuitpython.org/downloads """ # Makes colorwheel() available. -from rainbowio import colorwheel # pylint: disable=unused-import +try: + from rainbowio import colorwheel # pylint: disable=unused-import +except (ImportError, ModuleNotFoundError): + def colorwheel(pos): + # ref: https://github.com/adafruit/circuitpython/blob/main/shared-module/rainbowio/__init__.c + pos = pos - ((pos // 256) * 256) + shift1 = 0 + shift2 = 0 + if (pos < 85): + shift1 = 8 + shift2 = 16 + elif (pos < 170): + pos -= 85 + shift1 = 0 + shift2 = 8 + else: + pos -= 170 + shift1 = 16 + shift2 = 0 + p = (int)(pos * 3) + p = p if (p < 256) else 255 + return (p << shift1) | ((255 - p) << shift2) RED = (255, 0, 0) """Red.""" diff --git a/adafruit_led_animation/grid.py b/adafruit_led_animation/grid.py index b15254c..a944bb3 100644 --- a/adafruit_led_animation/grid.py +++ b/adafruit_led_animation/grid.py @@ -130,7 +130,7 @@ class PixelGrid: else: raise ValueError("PixelGrid assignment needs a sub-index or x,y coordinate") - if self._pixels.auto_write: + if hasattr(self._pixels, "auto_write") and self._pixels.auto_write: self.show() def __getitem__(self, index): @@ -150,12 +150,13 @@ class PixelGrid: """ brightness from the underlying strip. """ - return self._pixels.brightness + return self._pixels.brightness if hasattr(self._pixels, "brightness") else 1.0 @brightness.setter def brightness(self, brightness): - # pylint: disable=attribute-defined-outside-init - self._pixels.brightness = min(max(brightness, 0.0), 1.0) + if hasattr(self._pixels, "brightness"): + # pylint: disable=attribute-defined-outside-init + self._pixels.brightness = min(max(brightness, 0.0), 1.0) def fill(self, color): """ @@ -170,18 +171,22 @@ class PixelGrid: """ Shows the pixels on the underlying strip. """ - self._pixels.show() + if hasattr(self._pixels, "show"): + self._pixels.show() + elif hasattr(self._pixels, "write"): + self._pixels.write() @property def auto_write(self): """ auto_write from the underlying strip. """ - return self._pixels.auto_write + return hasattr(self._pixels, "auto_write") and self._pixels.auto_write @auto_write.setter def auto_write(self, value): - self._pixels.auto_write = value + if hasattr(self._pixels, "auto_write"): + self._pixels.auto_write = value def reverse_x_mapper(width, mapper): diff --git a/adafruit_led_animation/group.py b/adafruit_led_animation/group.py index 2377d1d..f0c3d3e 100644 --- a/adafruit_led_animation/group.py +++ b/adafruit_led_animation/group.py @@ -146,7 +146,10 @@ class AnimationGroup: for member in self._members: if isinstance(member, Animation): if last_strip != member.pixel_object: - member.pixel_object.show() + if hasattr(member.pixel_object, "show"): + member.pixel_object.show() + elif hasattr(member.pixel_object, "write"): + member.pixel_object.write() last_strip = member.pixel_object else: member.show() diff --git a/adafruit_led_animation/helper.py b/adafruit_led_animation/helper.py index b033b58..a22d03c 100644 --- a/adafruit_led_animation/helper.py +++ b/adafruit_led_animation/helper.py @@ -138,7 +138,7 @@ class PixelMap: else: self._set_pixels(index, val) - if self._pixels.auto_write: + if hasattr(self._pixels, "auto_write") and self._pixels.auto_write: self.show() def __getitem__(self, index): @@ -161,12 +161,13 @@ class PixelMap: """ brightness from the underlying strip. """ - return self._pixels.brightness + return self._pixels.brightness if hasattr(self._pixels, "brightness") else 1.0 @brightness.setter def brightness(self, brightness): - # pylint: disable=attribute-defined-outside-init - self._pixels.brightness = min(max(brightness, 0.0), 1.0) + if hasattr(self._pixels, "brightness"): + # pylint: disable=attribute-defined-outside-init + self._pixels.brightness = min(max(brightness, 0.0), 1.0) def fill(self, color): """ @@ -182,18 +183,22 @@ class PixelMap: """ Shows the pixels on the underlying strip. """ - self._pixels.show() + if hasattr(self._pixels, "show"): + self._pixels.show() + elif hasattr(self._pixels, "write"): + self._pixels.write() @property def auto_write(self): """ auto_write from the underlying strip. """ - return self._pixels.auto_write + return hasattr(self._pixels, "auto_write") and self._pixels.auto_write @auto_write.setter def auto_write(self, value): - self._pixels.auto_write = value + if hasattr(self._pixels, "auto_write"): + self._pixels.auto_write = value @classmethod def vertical_lines(cls, pixel_object, width, height, gridmap):