Skip to content

Commit 990e77d

Browse files
Add support for MicroPython
1 parent 83b87ef commit 990e77d

File tree

6 files changed

+76
-26
lines changed

6 files changed

+76
-26
lines changed

adafruit_led_animation/__init__.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,22 @@ def monotonic_ms():
2828
return monotonic_ns() // NANOS_PER_MS
2929

3030
except (ImportError, NotImplementedError):
31-
import time
32-
33-
def monotonic_ms():
34-
"""
35-
Implementation of monotonic_ms for platforms without time.monotonic_ns
36-
"""
37-
return int(time.monotonic() * MS_PER_SECOND)
31+
try:
32+
from time import monotonic
33+
34+
def monotonic_ms():
35+
"""
36+
Implementation of monotonic_ms for platforms without time.monotonic_ns
37+
"""
38+
return int(monotonic() * MS_PER_SECOND)
39+
except (ImportError, NotImplementedError):
40+
from time import time_ns
41+
42+
def monotonic_ms():
43+
"""
44+
Implementation of monotonic_ms for platforms without time.monotonic_ns or time.monotonic
45+
"""
46+
return time_ns() // NANOS_PER_MS
3847

3948

4049
NANOS_PER_MS = const(1000000)

adafruit_led_animation/animation/__init__.py

100644100755
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class Animation:
4141
# pylint: disable=too-many-arguments
4242
def __init__(self, pixel_object, speed, color, peers=None, paused=False, name=None):
4343
self.pixel_object = pixel_object
44-
self.pixel_object.auto_write = False
44+
if hasattr(self.pixel_object, "auto_write"):
45+
self.pixel_object.auto_write = False
4546
self._peers = [self] + peers if peers is not None else [self]
4647
self._speed_ms = 0
4748
self._color = None
@@ -116,7 +117,10 @@ def show(self):
116117
"""
117118
Displays the updated pixels. Called during animates with changes.
118119
"""
119-
self.pixel_object.show()
120+
if hasattr(self.pixel_object, "show"):
121+
self.pixel_object.show()
122+
elif hasattr(self.pixel_object, "write"):
123+
self.pixel_object.write()
120124

121125
@property
122126
def peers(self):
@@ -154,7 +158,10 @@ def fill(self, color):
154158
Fills the pixel object with a color.
155159
"""
156160
self.pixel_object.fill(color)
157-
self.pixel_object.show()
161+
if hasattr(self.pixel_object, "show"):
162+
self.pixel_object.show()
163+
elif hasattr(self.pixel_object, "write"):
164+
self.pixel_object.write()
158165

159166
@property
160167
def color(self):

adafruit_led_animation/color.py

100644100755
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,28 @@
2424
https://circuitpython.org/downloads
2525
"""
2626
# Makes colorwheel() available.
27-
from rainbowio import colorwheel # pylint: disable=unused-import
27+
try:
28+
from rainbowio import colorwheel # pylint: disable=unused-import
29+
except (ImportError, ModuleNotFoundError):
30+
def colorwheel(pos):
31+
# ref: https://github.com/adafruit/circuitpython/blob/main/shared-module/rainbowio/__init__.c
32+
pos = pos - ((pos // 256) * 256)
33+
shift1 = 0
34+
shift2 = 0
35+
if (pos < 85):
36+
shift1 = 8
37+
shift2 = 16
38+
elif (pos < 170):
39+
pos -= 85
40+
shift1 = 0
41+
shift2 = 8
42+
else:
43+
pos -= 170
44+
shift1 = 16
45+
shift2 = 0
46+
p = (int)(pos * 3)
47+
p = p if (p < 256) else 255
48+
return (p << shift1) | ((255 - p) << shift2)
2849

2950
RED = (255, 0, 0)
3051
"""Red."""

adafruit_led_animation/grid.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def __setitem__(self, index, val):
130130
else:
131131
raise ValueError("PixelGrid assignment needs a sub-index or x,y coordinate")
132132

133-
if self._pixels.auto_write:
133+
if hasattr(self._pixels, "auto_write") and self._pixels.auto_write:
134134
self.show()
135135

136136
def __getitem__(self, index):
@@ -150,12 +150,13 @@ def brightness(self):
150150
"""
151151
brightness from the underlying strip.
152152
"""
153-
return self._pixels.brightness
153+
return self._pixels.brightness if hasattr(self._pixels, "brightness") else 1.0
154154

155155
@brightness.setter
156156
def brightness(self, brightness):
157-
# pylint: disable=attribute-defined-outside-init
158-
self._pixels.brightness = min(max(brightness, 0.0), 1.0)
157+
if hasattr(self._pixels, "brightness"):
158+
# pylint: disable=attribute-defined-outside-init
159+
self._pixels.brightness = min(max(brightness, 0.0), 1.0)
159160

160161
def fill(self, color):
161162
"""
@@ -170,18 +171,22 @@ def show(self):
170171
"""
171172
Shows the pixels on the underlying strip.
172173
"""
173-
self._pixels.show()
174+
if hasattr(self._pixels, "show"):
175+
self._pixels.show()
176+
elif hasattr(self._pixels, "write"):
177+
self._pixels.write()
174178

175179
@property
176180
def auto_write(self):
177181
"""
178182
auto_write from the underlying strip.
179183
"""
180-
return self._pixels.auto_write
184+
return hasattr(self._pixels, "auto_write") and self._pixels.auto_write
181185

182186
@auto_write.setter
183187
def auto_write(self, value):
184-
self._pixels.auto_write = value
188+
if hasattr(self._pixels, "auto_write"):
189+
self._pixels.auto_write = value
185190

186191

187192
def reverse_x_mapper(width, mapper):

adafruit_led_animation/group.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ def animate(self, show=True):
146146
for member in self._members:
147147
if isinstance(member, Animation):
148148
if last_strip != member.pixel_object:
149-
member.pixel_object.show()
149+
if hasattr(member.pixel_object, "show"):
150+
member.pixel_object.show()
151+
elif hasattr(member.pixel_object, "write"):
152+
member.pixel_object.write()
150153
last_strip = member.pixel_object
151154
else:
152155
member.show()

adafruit_led_animation/helper.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def __setitem__(self, index, val):
138138
else:
139139
self._set_pixels(index, val)
140140

141-
if self._pixels.auto_write:
141+
if hasattr(self._pixels, "auto_write") and self._pixels.auto_write:
142142
self.show()
143143

144144
def __getitem__(self, index):
@@ -161,12 +161,13 @@ def brightness(self):
161161
"""
162162
brightness from the underlying strip.
163163
"""
164-
return self._pixels.brightness
164+
return self._pixels.brightness if hasattr(self._pixels, "brightness") else 1.0
165165

166166
@brightness.setter
167167
def brightness(self, brightness):
168-
# pylint: disable=attribute-defined-outside-init
169-
self._pixels.brightness = min(max(brightness, 0.0), 1.0)
168+
if hasattr(self._pixels, "brightness"):
169+
# pylint: disable=attribute-defined-outside-init
170+
self._pixels.brightness = min(max(brightness, 0.0), 1.0)
170171

171172
def fill(self, color):
172173
"""
@@ -182,18 +183,22 @@ def show(self):
182183
"""
183184
Shows the pixels on the underlying strip.
184185
"""
185-
self._pixels.show()
186+
if hasattr(self._pixels, "show"):
187+
self._pixels.show()
188+
elif hasattr(self._pixels, "write"):
189+
self._pixels.write()
186190

187191
@property
188192
def auto_write(self):
189193
"""
190194
auto_write from the underlying strip.
191195
"""
192-
return self._pixels.auto_write
196+
return hasattr(self._pixels, "auto_write") and self._pixels.auto_write
193197

194198
@auto_write.setter
195199
def auto_write(self, value):
196-
self._pixels.auto_write = value
200+
if hasattr(self._pixels, "auto_write"):
201+
self._pixels.auto_write = value
197202

198203
@classmethod
199204
def vertical_lines(cls, pixel_object, width, height, gridmap):

0 commit comments

Comments
 (0)