34
34
35
35
from adafruit_led_animation .animation import Animation
36
36
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
+
37
51
class Volume (Animation ):
38
52
"""
39
53
Animate the brightness and number of pixels based on volume.
@@ -44,31 +58,59 @@ class Volume(Animation):
44
58
:param float max_volume: what volume is considered maximum where everything is lit up
45
59
"""
46
60
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
+ ):
48
65
self ._decoder = decoder
49
66
self ._num_pixels = len (pixel_object )
50
67
self ._max_volume = max_volume
51
- self ._brigthest_color = brightest_color
68
+ self ._brightest_color = brightest_color
52
69
super ().__init__ (pixel_object , speed , brightest_color , name = name )
53
70
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
63
77
64
78
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
+ )
68
106
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
+ )
70
110
if lit_pixels > self ._num_pixels :
71
111
lit_pixels = self ._num_pixels
72
112
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