Skip to content

Commit 44a086d

Browse files
committed
Finalize hooking effect to widgets
1 parent 10659e5 commit 44a086d

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

displayio_effects/throttle_effect.py

+44-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-FileCopyrightText: Copyright (c) 2022 Alec Delaney for CircuitPython Organization
33
#
44
# SPDX-License-Identifier: MIT
5+
# pylint: disable=protected-access
56
"""
67
`displayio_effects.throttle_effect`
78
================================================================================
@@ -33,25 +34,29 @@ def throttle_effect(self):
3334
"""
3435
return self._throttle_setting
3536

37+
3638
@throttle_effect.setter
3739
def throttle_effect(self, setting):
3840
if setting < 0:
3941
raise ValueError("Throttle effect setting must be larger than 0")
4042
if setting:
41-
self._throttle_hold_value = self.value
43+
self._throttle_hold_value = getattr(self, self._value_name)
4244
self._throttle_setting = setting
4345

46+
4447
@property
4548
def throttle_effect_move_rate(self):
4649
"""The speed at which the throttle effect moves the widget vaalue
4750
per update"""
4851

4952
return self._throttle_move_rate
5053

54+
5155
@throttle_effect_move_rate.setter
5256
def throttle_effect_move_rate(self, rate):
5357
self._throttle_move_rate = rate
5458

59+
5560
def throttle_update(self):
5661
"""Updates the widget value and propagates the throttle effect refresh"""
5762

@@ -62,33 +67,54 @@ def throttle_update(self):
6267
if self._throttle_destination in (None, self._throttle_hold_value):
6368
limit_bound = self._throttle_setting * 10
6469
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
6771
)
6872

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
7378
)
79+
setattr(self, self._value_name, value)
7480

7581
threshold_check = (
76-
self.value >= self._throttle_destination
82+
value >= self._throttle_destination
7783
if self._throttle_destination >= self._throttle_hold_value
78-
else self.value <= self._throttle_destination
84+
else value <= self._throttle_destination
7985
)
8086
if threshold_check:
8187
self._throttle_destination = self._throttle_hold_value
8288

83-
def hook_throttle_effect(*widget_classes):
84-
for widget_class in widget_classes:
8589

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)
88114

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)
93119

94-
setattr(widget_class, "throttle_update", throttle_update)
120+
setattr(widget_class, "throttle_update", throttle_update)

0 commit comments

Comments
 (0)