Skip to content

Commit b03f8cb

Browse files
committed
Switch to using enumerated widget types
1 parent 199f434 commit b03f8cb

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

displayio_effects/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2+
# SPDX-FileCopyrightText: Copyright (c) 2022 Alec Delaney for CircuitPython Organization
3+
#
4+
# SPDX-License-Identifier: MIT
5+
# pylint: disable=protected-access
6+
"""
7+
`displayio_effects`
8+
================================================================================
9+
10+
Add the some flair to your widgets!
11+
12+
13+
* Author(s): Alec Delaney
14+
15+
Implementation Notes
16+
--------------------
17+
18+
**Software and Dependencies:**
19+
20+
* Adafruit CircuitPython firmware for the supported boards:
21+
https://circuitpython.org/downloads
22+
"""
23+
24+
class WidgetType:
25+
"""Enum values for customizable widget types"""
26+
27+
DIAL = 0
28+
GAUGE = 1

displayio_effects/fluctuation_effect.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@
2222
"""
2323

2424
import random
25+
from displayio_effects import WidgetType
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+
3037
@property
3138
def fluctuation_amplitude(self):
3239
"""The furtherest the fluctuation effect can randomly set the widget value relative
@@ -88,26 +95,31 @@ def update_fluctuation(self):
8895
self._fluctuation_destination = self._fluctuation_hold_value
8996

9097

91-
def hook_fluctuation_effect(widget_class, value_name):
98+
def hook_fluctuation_effect(widget_class, widget_type):
9299
"""Adds the fluctuation effect for the given class
93100
94-
:param widget_classes: The widgets that should have this effect hooked
101+
:param widget_class: The widget that should have this effect hooked
95102
into them.
96-
:param str value_name: The name of the attribute that sets the "value"
97-
for this widget
103+
:param int widget_type: The enum value of this widget type, must be a
104+
valid ~WidgetType
98105
99106
For example, to hook this into the ``Dial`` widget, you would use the
100107
following code:
101108
102109
.. code-block:: python
103110
104111
from displayio_dial import Dial
105-
from displayio_effects import fluctuation_effect
112+
from
113+
from displayio_effects import WidgetType, fluctuation_effect
106114
107-
fluctuation_effect.hook_fluctuation_effect(Dial, "value")
115+
fluctuation_effect.hook_fluctuation_effect(Dial, WidgetType.DIAL)
108116
109117
"""
110118

119+
value_name = FLUCTUATION_WIDGET_VALUES.get(widget_type)
120+
if not value_name:
121+
raise ValueError("The given widget does not have the ability to use this effect")
122+
111123
setattr(widget_class, "_value_name", value_name)
112124

113125
setattr(widget_class, "_fluctuation_destination", None)

0 commit comments

Comments
 (0)