Add support for MicroPython

This commit is contained in:
szelenka 2025-04-16 08:53:41 -04:00
parent 83b87ef867
commit 990e77d862
6 changed files with 75 additions and 25 deletions

View file

@ -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)

13
adafruit_led_animation/animation/__init__.py Normal file → Executable file
View file

@ -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):

23
adafruit_led_animation/color.py Normal file → Executable file
View file

@ -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."""

View file

@ -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):

View file

@ -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()

View file

@ -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):