22
22
"""
23
23
24
24
import random
25
+ from displayio_effects import WidgetType , WIDGET_TYPE_ATTR
25
26
26
27
__version__ = "0.0.0-auto.0"
27
28
__repo__ = "https://github.com/tekktrik/CircuitPython_Org_DisplayIO_Effects.git"
28
29
29
30
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
+
30
42
@property
31
43
def fluctuation_amplitude (self ):
32
44
"""The furtherest the fluctuation effect can randomly set the widget value relative
@@ -37,10 +49,11 @@ def fluctuation_amplitude(self):
37
49
38
50
@fluctuation_amplitude .setter
39
51
def fluctuation_amplitude (self , amplitude ):
52
+ value_name = _get_value_name (self )
40
53
if amplitude < 0 :
41
54
raise ValueError ("Fluctuation effect setting must be larger than 0" )
42
55
if amplitude :
43
- self ._fluctuation_hold_value = getattr (self , self . _value_name )
56
+ self ._fluctuation_hold_value = getattr (self , value_name )
44
57
self ._fluctuation_amplitude = amplitude
45
58
46
59
@@ -60,6 +73,8 @@ def fluctuation_move_rate(self, rate):
60
73
def update_fluctuation (self ):
61
74
"""Updates the widget value and propagates the fluctuation effect refresh"""
62
75
76
+ value_name = _get_value_name (self )
77
+
63
78
if self ._fluctuation_amplitude == 0 :
64
79
self ._fluctuation_destination = None
65
80
return
@@ -71,13 +86,13 @@ def update_fluctuation(self):
71
86
+ self ._fluctuation_hold_value
72
87
)
73
88
74
- value = getattr (self , self . _value_name )
89
+ value = getattr (self , value_name )
75
90
value = (
76
91
value + self ._fluctuation_move_rate
77
92
if self ._fluctuation_destination > value
78
93
else value - self ._fluctuation_move_rate
79
94
)
80
- setattr (self , self . _value_name , value )
95
+ setattr (self , value_name , value )
81
96
82
97
threshold_check = (
83
98
value >= self ._fluctuation_destination
@@ -88,34 +103,39 @@ def update_fluctuation(self):
88
103
self ._fluctuation_destination = self ._fluctuation_hold_value
89
104
90
105
91
- def hook_fluctuation_effect (widget_class , value_name ):
106
+ def hook_fluctuation_effect (widget_class , widget_type ):
92
107
"""Adds the fluctuation effect for the given class
93
108
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
98
113
99
114
For example, to hook this into the ``Dial`` widget, you would use the
100
115
following code:
101
116
102
117
.. code-block:: python
103
118
104
119
from displayio_dial import Dial
105
- from displayio_effects import fluctuation_effect
120
+ from displayio_effects import WidgetType, fluctuation_effect
106
121
107
- fluctuation_effect.hook_fluctuation_effect(Dial, "value" )
122
+ fluctuation_effect.hook_fluctuation_effect(Dial, WidgetType.DIAL )
108
123
109
124
"""
110
125
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 )
112
132
113
133
setattr (widget_class , "_fluctuation_destination" , None )
114
134
setattr (widget_class , "_fluctuation_hold_value" , 0 )
115
135
116
136
setattr (widget_class , "fluctuation_amplitude" , fluctuation_amplitude )
117
137
setattr (widget_class , "_fluctuation_amplitude" , 0 )
118
138
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 )
120
140
121
141
setattr (widget_class , "update_fluctuation" , update_fluctuation )
0 commit comments