2222"""
2323
2424import random
25+ from displayio_effects import WidgetType , WIDGET_TYPE_ATTR
2526
2627__version__ = "0.0.0-auto.0"
2728__repo__ = "https://github.com/tekktrik/CircuitPython_Org_DisplayIO_Effects.git"
2829
2930
31+ FLUCTUATION_WIDGET_VALUES = {
32+ WidgetType .DIAL : "value" ,
33+ WidgetType .GAUGE : "level" ,
34+ }
35+
36+
37+ def _get_value_name (instance ):
38+ widget_type = getattr (instance , WIDGET_TYPE_ATTR )
39+ return FLUCTUATION_WIDGET_VALUES [widget_type ]
40+
41+
3042@property
3143def fluctuation_amplitude (self ):
3244 """The furtherest the fluctuation effect can randomly set the widget value relative
@@ -37,10 +49,11 @@ def fluctuation_amplitude(self):
3749
3850@fluctuation_amplitude .setter
3951def fluctuation_amplitude (self , amplitude ):
52+ value_name = _get_value_name (self )
4053 if amplitude < 0 :
4154 raise ValueError ("Fluctuation effect setting must be larger than 0" )
4255 if amplitude :
43- self ._fluctuation_hold_value = getattr (self , self . _value_name )
56+ self ._fluctuation_hold_value = getattr (self , value_name )
4457 self ._fluctuation_amplitude = amplitude
4558
4659
@@ -60,6 +73,8 @@ def fluctuation_move_rate(self, rate):
6073def update_fluctuation (self ):
6174 """Updates the widget value and propagates the fluctuation effect refresh"""
6275
76+ value_name = _get_value_name (self )
77+
6378 if self ._fluctuation_amplitude == 0 :
6479 self ._fluctuation_destination = None
6580 return
@@ -71,13 +86,13 @@ def update_fluctuation(self):
7186 + self ._fluctuation_hold_value
7287 )
7388
74- value = getattr (self , self . _value_name )
89+ value = getattr (self , value_name )
7590 value = (
7691 value + self ._fluctuation_move_rate
7792 if self ._fluctuation_destination > value
7893 else value - self ._fluctuation_move_rate
7994 )
80- setattr (self , self . _value_name , value )
95+ setattr (self , value_name , value )
8196
8297 threshold_check = (
8398 value >= self ._fluctuation_destination
@@ -88,34 +103,39 @@ def update_fluctuation(self):
88103 self ._fluctuation_destination = self ._fluctuation_hold_value
89104
90105
91- def hook_fluctuation_effect (widget_class , value_name ):
106+ def hook_fluctuation_effect (widget_class , widget_type ):
92107 """Adds the fluctuation effect for the given class
93108
94- :param widget_classes : The widgets that should have this effect hooked
95- into them.
96- :param str value_name : The name of the attribute that sets the "value"
97- for this widget
109+ :param widget_class : The widget class that should have this effect hooked
110+ into it
111+ :param int widget_type : The enum value of this widget type, must be a
112+ valid ~WidgetType
98113
99114 For example, to hook this into the ``Dial`` widget, you would use the
100115 following code:
101116
102117 .. code-block:: python
103118
104119 from displayio_dial import Dial
105- from displayio_effects import fluctuation_effect
120+ from displayio_effects import WidgetType, fluctuation_effect
106121
107- fluctuation_effect.hook_fluctuation_effect(Dial, "value" )
122+ fluctuation_effect.hook_fluctuation_effect(Dial, WidgetType.DIAL )
108123
109124 """
110125
111- setattr (widget_class , "_value_name" , value_name )
126+ if not FLUCTUATION_WIDGET_VALUES .get (widget_type ):
127+ raise ValueError (
128+ "The given widget does not have the ability to use this effect"
129+ )
130+
131+ setattr (widget_class , WIDGET_TYPE_ATTR , widget_type )
112132
113133 setattr (widget_class , "_fluctuation_destination" , None )
114134 setattr (widget_class , "_fluctuation_hold_value" , 0 )
115135
116136 setattr (widget_class , "fluctuation_amplitude" , fluctuation_amplitude )
117137 setattr (widget_class , "_fluctuation_amplitude" , 0 )
118138 setattr (widget_class , "fluctuation_move_rate" , fluctuation_move_rate )
119- setattr (widget_class , "_fluctuation_move_rate" , 0.1 )
139+ setattr (widget_class , "_fluctuation_move_rate" , 0.01 )
120140
121141 setattr (widget_class , "update_fluctuation" , update_fluctuation )
0 commit comments