Add support for MicroPython
This commit is contained in:
parent
83b87ef867
commit
990e77d862
6 changed files with 75 additions and 25 deletions
|
|
@ -28,13 +28,22 @@ try:
|
||||||
return monotonic_ns() // NANOS_PER_MS
|
return monotonic_ns() // NANOS_PER_MS
|
||||||
|
|
||||||
except (ImportError, NotImplementedError):
|
except (ImportError, NotImplementedError):
|
||||||
import time
|
try:
|
||||||
|
from time import monotonic
|
||||||
|
|
||||||
def monotonic_ms():
|
def monotonic_ms():
|
||||||
"""
|
"""
|
||||||
Implementation of monotonic_ms for platforms without time.monotonic_ns
|
Implementation of monotonic_ms for platforms without time.monotonic_ns
|
||||||
"""
|
"""
|
||||||
return int(time.monotonic() * MS_PER_SECOND)
|
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)
|
NANOS_PER_MS = const(1000000)
|
||||||
|
|
|
||||||
7
adafruit_led_animation/animation/__init__.py
Normal file → Executable file
7
adafruit_led_animation/animation/__init__.py
Normal file → Executable file
|
|
@ -41,6 +41,7 @@ class Animation:
|
||||||
# pylint: disable=too-many-arguments
|
# pylint: disable=too-many-arguments
|
||||||
def __init__(self, pixel_object, speed, color, peers=None, paused=False, name=None):
|
def __init__(self, pixel_object, speed, color, peers=None, paused=False, name=None):
|
||||||
self.pixel_object = pixel_object
|
self.pixel_object = pixel_object
|
||||||
|
if hasattr(self.pixel_object, "auto_write"):
|
||||||
self.pixel_object.auto_write = False
|
self.pixel_object.auto_write = False
|
||||||
self._peers = [self] + peers if peers is not None else [self]
|
self._peers = [self] + peers if peers is not None else [self]
|
||||||
self._speed_ms = 0
|
self._speed_ms = 0
|
||||||
|
|
@ -116,7 +117,10 @@ class Animation:
|
||||||
"""
|
"""
|
||||||
Displays the updated pixels. Called during animates with changes.
|
Displays the updated pixels. Called during animates with changes.
|
||||||
"""
|
"""
|
||||||
|
if hasattr(self.pixel_object, "show"):
|
||||||
self.pixel_object.show()
|
self.pixel_object.show()
|
||||||
|
elif hasattr(self.pixel_object, "write"):
|
||||||
|
self.pixel_object.write()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def peers(self):
|
def peers(self):
|
||||||
|
|
@ -154,7 +158,10 @@ class Animation:
|
||||||
Fills the pixel object with a color.
|
Fills the pixel object with a color.
|
||||||
"""
|
"""
|
||||||
self.pixel_object.fill(color)
|
self.pixel_object.fill(color)
|
||||||
|
if hasattr(self.pixel_object, "show"):
|
||||||
self.pixel_object.show()
|
self.pixel_object.show()
|
||||||
|
elif hasattr(self.pixel_object, "write"):
|
||||||
|
self.pixel_object.write()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def color(self):
|
def color(self):
|
||||||
|
|
|
||||||
23
adafruit_led_animation/color.py
Normal file → Executable file
23
adafruit_led_animation/color.py
Normal file → Executable file
|
|
@ -24,7 +24,28 @@ Implementation Notes
|
||||||
https://circuitpython.org/downloads
|
https://circuitpython.org/downloads
|
||||||
"""
|
"""
|
||||||
# Makes colorwheel() available.
|
# 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 = (255, 0, 0)
|
||||||
"""Red."""
|
"""Red."""
|
||||||
|
|
|
||||||
|
|
@ -130,7 +130,7 @@ class PixelGrid:
|
||||||
else:
|
else:
|
||||||
raise ValueError("PixelGrid assignment needs a sub-index or x,y coordinate")
|
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()
|
self.show()
|
||||||
|
|
||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
|
|
@ -150,10 +150,11 @@ class PixelGrid:
|
||||||
"""
|
"""
|
||||||
brightness from the underlying strip.
|
brightness from the underlying strip.
|
||||||
"""
|
"""
|
||||||
return self._pixels.brightness
|
return self._pixels.brightness if hasattr(self._pixels, "brightness") else 1.0
|
||||||
|
|
||||||
@brightness.setter
|
@brightness.setter
|
||||||
def brightness(self, brightness):
|
def brightness(self, brightness):
|
||||||
|
if hasattr(self._pixels, "brightness"):
|
||||||
# pylint: disable=attribute-defined-outside-init
|
# pylint: disable=attribute-defined-outside-init
|
||||||
self._pixels.brightness = min(max(brightness, 0.0), 1.0)
|
self._pixels.brightness = min(max(brightness, 0.0), 1.0)
|
||||||
|
|
||||||
|
|
@ -170,17 +171,21 @@ class PixelGrid:
|
||||||
"""
|
"""
|
||||||
Shows the pixels on the underlying strip.
|
Shows the pixels on the underlying strip.
|
||||||
"""
|
"""
|
||||||
|
if hasattr(self._pixels, "show"):
|
||||||
self._pixels.show()
|
self._pixels.show()
|
||||||
|
elif hasattr(self._pixels, "write"):
|
||||||
|
self._pixels.write()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def auto_write(self):
|
def auto_write(self):
|
||||||
"""
|
"""
|
||||||
auto_write from the underlying strip.
|
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
|
@auto_write.setter
|
||||||
def auto_write(self, value):
|
def auto_write(self, value):
|
||||||
|
if hasattr(self._pixels, "auto_write"):
|
||||||
self._pixels.auto_write = value
|
self._pixels.auto_write = value
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -146,7 +146,10 @@ class AnimationGroup:
|
||||||
for member in self._members:
|
for member in self._members:
|
||||||
if isinstance(member, Animation):
|
if isinstance(member, Animation):
|
||||||
if last_strip != member.pixel_object:
|
if last_strip != member.pixel_object:
|
||||||
|
if hasattr(member.pixel_object, "show"):
|
||||||
member.pixel_object.show()
|
member.pixel_object.show()
|
||||||
|
elif hasattr(member.pixel_object, "write"):
|
||||||
|
member.pixel_object.write()
|
||||||
last_strip = member.pixel_object
|
last_strip = member.pixel_object
|
||||||
else:
|
else:
|
||||||
member.show()
|
member.show()
|
||||||
|
|
|
||||||
|
|
@ -138,7 +138,7 @@ class PixelMap:
|
||||||
else:
|
else:
|
||||||
self._set_pixels(index, val)
|
self._set_pixels(index, val)
|
||||||
|
|
||||||
if self._pixels.auto_write:
|
if hasattr(self._pixels, "auto_write") and self._pixels.auto_write:
|
||||||
self.show()
|
self.show()
|
||||||
|
|
||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
|
|
@ -161,10 +161,11 @@ class PixelMap:
|
||||||
"""
|
"""
|
||||||
brightness from the underlying strip.
|
brightness from the underlying strip.
|
||||||
"""
|
"""
|
||||||
return self._pixels.brightness
|
return self._pixels.brightness if hasattr(self._pixels, "brightness") else 1.0
|
||||||
|
|
||||||
@brightness.setter
|
@brightness.setter
|
||||||
def brightness(self, brightness):
|
def brightness(self, brightness):
|
||||||
|
if hasattr(self._pixels, "brightness"):
|
||||||
# pylint: disable=attribute-defined-outside-init
|
# pylint: disable=attribute-defined-outside-init
|
||||||
self._pixels.brightness = min(max(brightness, 0.0), 1.0)
|
self._pixels.brightness = min(max(brightness, 0.0), 1.0)
|
||||||
|
|
||||||
|
|
@ -182,17 +183,21 @@ class PixelMap:
|
||||||
"""
|
"""
|
||||||
Shows the pixels on the underlying strip.
|
Shows the pixels on the underlying strip.
|
||||||
"""
|
"""
|
||||||
|
if hasattr(self._pixels, "show"):
|
||||||
self._pixels.show()
|
self._pixels.show()
|
||||||
|
elif hasattr(self._pixels, "write"):
|
||||||
|
self._pixels.write()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def auto_write(self):
|
def auto_write(self):
|
||||||
"""
|
"""
|
||||||
auto_write from the underlying strip.
|
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
|
@auto_write.setter
|
||||||
def auto_write(self, value):
|
def auto_write(self, value):
|
||||||
|
if hasattr(self._pixels, "auto_write"):
|
||||||
self._pixels.auto_write = value
|
self._pixels.auto_write = value
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue