refactor MicroPython changes to helper

This commit is contained in:
szelenka 2025-04-16 10:08:48 -04:00
parent 2fb6c7d79c
commit 6fc310f56d
4 changed files with 83 additions and 47 deletions

View file

@ -29,6 +29,9 @@ __version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
from adafruit_led_animation import MS_PER_SECOND, monotonic_ms
from adafruit_led_animation.helper import (
pixel_object_show, pixel_object_auto_write_set
)
class Animation:
@ -41,8 +44,7 @@ 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
if hasattr(self.pixel_object, "auto_write"):
self.pixel_object.auto_write = False
pixel_object_auto_write_set(pixel_object, False)
self._peers = [self] + peers if peers is not None else [self]
self._speed_ms = 0
self._color = None
@ -117,10 +119,7 @@ class Animation:
"""
Displays the updated pixels. Called during animates with changes.
"""
if hasattr(self.pixel_object, "show"):
self.pixel_object.show()
elif hasattr(self.pixel_object, "write"):
self.pixel_object.write()
pixel_object_show(self.pixel_object)
@property
def peers(self):
@ -158,10 +157,7 @@ class Animation:
Fills the pixel object with a color.
"""
self.pixel_object.fill(color)
if hasattr(self.pixel_object, "show"):
self.pixel_object.show()
elif hasattr(self.pixel_object, "write"):
self.pixel_object.write()
pixel_object_show(self.pixel_object)
@property
def color(self):

View file

@ -26,7 +26,11 @@ Implementation Notes
"""
from micropython import const
from .helper import PixelMap, horizontal_strip_gridmap, vertical_strip_gridmap
from .helper import (
PixelMap, horizontal_strip_gridmap, vertical_strip_gridmap,
pixel_object_show, pixel_object_auto_write, pixel_object_auto_write_set,
pixel_object_brightness, pixel_object_brightness_set
)
HORIZONTAL = const(1)
@ -130,7 +134,7 @@ class PixelGrid:
else:
raise ValueError("PixelGrid assignment needs a sub-index or x,y coordinate")
if hasattr(self._pixels, "auto_write") and self._pixels.auto_write:
if pixel_object_auto_write(self._pixels):
self.show()
def __getitem__(self, index):
@ -150,13 +154,12 @@ class PixelGrid:
"""
brightness from the underlying strip.
"""
return self._pixels.brightness if hasattr(self._pixels, "brightness") else 1.0
return pixel_object_brightness(self._pixels)
@brightness.setter
def brightness(self, brightness):
if hasattr(self._pixels, "brightness"):
# pylint: disable=attribute-defined-outside-init
self._pixels.brightness = min(max(brightness, 0.0), 1.0)
# pylint: disable=attribute-defined-outside-init
pixel_object_brightness_set(self._pixels, brightness)
def fill(self, color):
"""
@ -171,22 +174,19 @@ class PixelGrid:
"""
Shows the pixels on the underlying strip.
"""
if hasattr(self._pixels, "show"):
self._pixels.show()
elif hasattr(self._pixels, "write"):
self._pixels.write()
pixel_object_show(self._pixels)
@property
def auto_write(self):
"""
auto_write from the underlying strip.
"""
return hasattr(self._pixels, "auto_write") and self._pixels.auto_write
return pixel_object_auto_write(self._pixels)
@auto_write.setter
def auto_write(self, value):
if hasattr(self._pixels, "auto_write"):
self._pixels.auto_write = value
# pylint: disable=attribute-defined-outside-init
pixel_object_auto_write_set(self._pixels, value)
def reverse_x_mapper(width, mapper):

View file

@ -30,6 +30,7 @@ __version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
from adafruit_led_animation.animation import Animation
from adafruit_led_animation.helper import pixel_object_show
class AnimationGroup:
@ -146,7 +147,7 @@ class AnimationGroup:
for member in self._members:
if isinstance(member, Animation):
if last_strip != member.pixel_object:
self._pixel_object_show(member.pixel_object)
pixel_object_show(member.pixel_object)
last_strip = member.pixel_object
else:
member.show()
@ -158,17 +159,6 @@ class AnimationGroup:
ret = True
return ret
def _pixel_object_show(self, pixel_object):
"""
Show the pixel object. This is a helper function to handle both
MicroPython and CircuitPython.
:param pixel_object: The pixel object to show/write to.
"""
if hasattr(pixel_object, "show"):
pixel_object.show()
elif hasattr(pixel_object, "write"):
pixel_object.write()
@property
def color(self):
"""

View file

@ -138,7 +138,7 @@ class PixelMap:
else:
self._set_pixels(index, val)
if hasattr(self._pixels, "auto_write") and self._pixels.auto_write:
if pixel_object_auto_write(self._pixels):
self.show()
def __getitem__(self, index):
@ -161,13 +161,12 @@ class PixelMap:
"""
brightness from the underlying strip.
"""
return self._pixels.brightness if hasattr(self._pixels, "brightness") else 1.0
return pixel_object_brightness(self._pixels)
@brightness.setter
def brightness(self, brightness):
if hasattr(self._pixels, "brightness"):
# pylint: disable=attribute-defined-outside-init
self._pixels.brightness = min(max(brightness, 0.0), 1.0)
# pylint: disable=attribute-defined-outside-init
pixel_object_brightness_set(self._pixels, brightness)
def fill(self, color):
"""
@ -183,22 +182,19 @@ class PixelMap:
"""
Shows the pixels on the underlying strip.
"""
if hasattr(self._pixels, "show"):
self._pixels.show()
elif hasattr(self._pixels, "write"):
self._pixels.write()
pixel_object_show(self._pixels)
@property
def auto_write(self):
"""
auto_write from the underlying strip.
"""
return hasattr(self._pixels, "auto_write") and self._pixels.auto_write
return pixel_object_auto_write(self._pixels)
@auto_write.setter
def auto_write(self, value):
if hasattr(self._pixels, "auto_write"):
self._pixels.auto_write = value
# pylint: disable=attribute-defined-outside-init
pixel_object_auto_write_set(self._pixels, value)
@classmethod
def vertical_lines(cls, pixel_object, width, height, gridmap):
@ -315,3 +311,57 @@ class PixelSubset(PixelMap):
pixel_ranges=[[n] for n in range(start, end)],
individual_pixels=True,
)
def pixel_object_show(pixel_object):
"""
Show the pixel object. This is a helper function to handle both
MicroPython and CircuitPython.
:param pixel_object: The pixel object to show/write to.
"""
if hasattr(pixel_object, "show"):
pixel_object.show()
elif hasattr(pixel_object, "write"):
pixel_object.write()
def pixel_object_auto_write(pixel_object):
"""
Get the auto_write property of the pixel object.
:param pixel_object: The pixel object to get the auto_write property from.
:return: The auto_write property of the pixel object.
"""
if hasattr(pixel_object, "auto_write"):
return pixel_object.auto_write
return False
def pixel_object_auto_write_set(pixel_object, value):
"""
Set the auto_write property of the pixel object.
:param pixel_object: The pixel object to set the auto_write property on.
:param value: The value to set the auto_write property to.
"""
if hasattr(pixel_object, "auto_write"):
pixel_object.auto_write = value
def pixel_object_brightness(pixel_object):
"""
Get the brightness property of the pixel object.
:param pixel_object: The pixel object to get the brightness property from.
:return: The brightness property of the pixel object.
"""
if hasattr(pixel_object, "brightness"):
return pixel_object.brightness
return 1.0
def pixel_object_brightness_set(pixel_object, value):
"""
Set the brightness property of the pixel object.
:param pixel_object: The pixel object to set the brightness property on.
:param value: The value to set the brightness property to.
"""
if hasattr(pixel_object, "brightness"):
pixel_object.brightness = value