2
2
# SPDX-FileCopyrightText: Copyright (c) 2022 Alec Delaney for CircuitPython Organization
3
3
#
4
4
# SPDX-License-Identifier: MIT
5
+ # pylint: disable=protected-access
5
6
"""
6
7
`displayio_effects.throttle_effect`
7
8
================================================================================
@@ -33,25 +34,29 @@ def throttle_effect(self):
33
34
"""
34
35
return self ._throttle_setting
35
36
37
+
36
38
@throttle_effect .setter
37
39
def throttle_effect (self , setting ):
38
40
if setting < 0 :
39
41
raise ValueError ("Throttle effect setting must be larger than 0" )
40
42
if setting :
41
- self ._throttle_hold_value = self . value
43
+ self ._throttle_hold_value = getattr ( self , self . _value_name )
42
44
self ._throttle_setting = setting
43
45
46
+
44
47
@property
45
48
def throttle_effect_move_rate (self ):
46
49
"""The speed at which the throttle effect moves the widget vaalue
47
50
per update"""
48
51
49
52
return self ._throttle_move_rate
50
53
54
+
51
55
@throttle_effect_move_rate .setter
52
56
def throttle_effect_move_rate (self , rate ):
53
57
self ._throttle_move_rate = rate
54
58
59
+
55
60
def throttle_update (self ):
56
61
"""Updates the widget value and propagates the throttle effect refresh"""
57
62
@@ -62,33 +67,54 @@ def throttle_update(self):
62
67
if self ._throttle_destination in (None , self ._throttle_hold_value ):
63
68
limit_bound = self ._throttle_setting * 10
64
69
self ._throttle_destination = (
65
- random .uniform (- limit_bound , limit_bound ) / 10
66
- + self ._throttle_hold_value
70
+ random .uniform (- limit_bound , limit_bound ) / 10 + self ._throttle_hold_value
67
71
)
68
72
69
- self .value = (
70
- self .value + self ._throttle_move_rate
71
- if self ._throttle_destination > self .value
72
- else self .value - self ._throttle_move_rate
73
+ value = getattr (self , self ._value_name )
74
+ value = (
75
+ value + self ._throttle_move_rate
76
+ if self ._throttle_destination > value
77
+ else value - self ._throttle_move_rate
73
78
)
79
+ setattr (self , self ._value_name , value )
74
80
75
81
threshold_check = (
76
- self . value >= self ._throttle_destination
82
+ value >= self ._throttle_destination
77
83
if self ._throttle_destination >= self ._throttle_hold_value
78
- else self . value <= self ._throttle_destination
84
+ else value <= self ._throttle_destination
79
85
)
80
86
if threshold_check :
81
87
self ._throttle_destination = self ._throttle_hold_value
82
88
83
- def hook_throttle_effect (* widget_classes ):
84
- for widget_class in widget_classes :
85
89
86
- setattr (widget_class , "_throttle_destination" , None )
87
- setattr (widget_class , "_throttle_value" , 0 )
90
+ def hook_throttle_effect (widget_class , value_name ):
91
+ """Adds the throttle effect for the given classes
92
+
93
+ :param widget_classes: The widgets that should have this effect hooked
94
+ into them.
95
+ :param str value_name: The name of the attribute that sets the "value"
96
+ for this widget
97
+
98
+ For example, to hook this into the ``Dial`` widget, you would use the
99
+ following code:
100
+
101
+ .. code-block:: python
102
+
103
+ from displayio_dial import Dial
104
+ from displayio_effects import throttle_effect
105
+
106
+ throttle_effect.hook_throttle_effect(Dial, "value")
107
+
108
+ """
109
+
110
+ setattr (widget_class , "_value_name" , value_name )
111
+
112
+ setattr (widget_class , "_throttle_destination" , None )
113
+ setattr (widget_class , "_throttle_value" , 0 )
88
114
89
- setattr (widget_class , "throttle_effect" , throttle_effect )
90
- setattr (widget_class , "_throttle_effect" , 0 )
91
- setattr (widget_class , "throttle_effect_move_rate" , throttle_effect_move_rate )
92
- setattr (widget_class , "_throttle_move_rate" , 0.1 )
115
+ setattr (widget_class , "throttle_effect" , throttle_effect )
116
+ setattr (widget_class , "_throttle_effect" , 0 )
117
+ setattr (widget_class , "throttle_effect_move_rate" , throttle_effect_move_rate )
118
+ setattr (widget_class , "_throttle_move_rate" , 0.1 )
93
119
94
- setattr (widget_class , "throttle_update" , throttle_update )
120
+ setattr (widget_class , "throttle_update" , throttle_update )
0 commit comments