Skip to content

Commit eb86bf0

Browse files
committed
Fixed errors from pylint and black
1 parent d12b7fb commit eb86bf0

File tree

1 file changed

+59
-17
lines changed

1 file changed

+59
-17
lines changed

adafruit_led_animation/animation/volume.py

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@
3434

3535
from adafruit_led_animation.animation import Animation
3636

37+
38+
def map_range(x, in_min, in_max, out_min, out_max):
39+
"""
40+
Maps a number from one range to another.
41+
:return: Returns value mapped to new range
42+
:rtype: float
43+
"""
44+
mapped = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
45+
if out_min <= out_max:
46+
return max(min(mapped, out_max), out_min)
47+
48+
return min(max(mapped, out_max), out_min)
49+
50+
3751
class Volume(Animation):
3852
"""
3953
Animate the brightness and number of pixels based on volume.
@@ -44,31 +58,59 @@ class Volume(Animation):
4458
:param float max_volume: what volume is considered maximum where everything is lit up
4559
"""
4660

47-
def __init__(self, pixel_object, speed, brightest_color, decoder, max_volume=500, name=None):
61+
# pylint: disable=too-many-arguments
62+
def __init__(
63+
self, pixel_object, speed, brightest_color, decoder, max_volume=500, name=None
64+
):
4865
self._decoder = decoder
4966
self._num_pixels = len(pixel_object)
5067
self._max_volume = max_volume
51-
self._brigthest_color = brightest_color
68+
self._brightest_color = brightest_color
5269
super().__init__(pixel_object, speed, brightest_color, name=name)
5370

54-
def _set_color(self, brightest_color):
55-
self.colors = [brightest_color]
56-
57-
def map_range(self, x, in_min, in_max, out_min, out_max):
58-
mapped = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
59-
if out_min <= out_max:
60-
return max(min(mapped, out_max), out_min)
61-
62-
return min(max(mapped, out_max), out_min)
71+
def set_brightest_color(self, brightest_color):
72+
"""
73+
Animate the brightness and number of pixels based on volume.
74+
:param brightest_color: Color at max volume ``(r, g, b)`` tuple, or ``0x000000`` hex format
75+
"""
76+
self._brightest_color = brightest_color
6377

6478
def draw(self):
65-
red = int(self.map_range(self._decoder.rms_level, 0, self._max_volume, 0, self._brigthest_color[0]))
66-
green = int(self.map_range(self._decoder.rms_level, 0, self._max_volume, 0, self._brigthest_color[1]))
67-
blue = int(self.map_range(self._decoder.rms_level, 0, self._max_volume, 0, self._brigthest_color[2]))
79+
red = int(
80+
map_range(
81+
self._decoder.rms_level,
82+
0,
83+
self._max_volume,
84+
0,
85+
self._brightest_color[0],
86+
)
87+
)
88+
green = int(
89+
map_range(
90+
self._decoder.rms_level,
91+
0,
92+
self._max_volume,
93+
0,
94+
self._brightest_color[1],
95+
)
96+
)
97+
blue = int(
98+
map_range(
99+
self._decoder.rms_level,
100+
0,
101+
self._max_volume,
102+
0,
103+
self._brightest_color[2],
104+
)
105+
)
68106

69-
lit_pixels = int(self.map_range(self._decoder.rms_level, 0, self._max_volume, 0, self._num_pixels))
107+
lit_pixels = int(
108+
map_range(self._decoder.rms_level, 0, self._max_volume, 0, self._num_pixels)
109+
)
70110
if lit_pixels > self._num_pixels:
71111
lit_pixels = self._num_pixels
72112

73-
self.pixel_object[0:lit_pixels] = [(red,green,blue)] * lit_pixels
74-
self.pixel_object[lit_pixels:self._num_pixels] = [(0,0,0)] * (self._num_pixels-lit_pixels)
113+
self.pixel_object[0:lit_pixels] = [(red, green, blue)] * lit_pixels
114+
self.pixel_object[lit_pixels : self._num_pixels] = [(0, 0, 0)] * (
115+
self._num_pixels - lit_pixels
116+
)

0 commit comments

Comments
 (0)