|
| 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.fluctuation_effect` |
| 8 | +================================================================================ |
| 9 | +
|
| 10 | +Add the colorwheel effect 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 | +from rainbowio import colorwheel |
| 25 | +from adafruit_itertools.adafruit_itertools import cycle |
| 26 | +from displayio_effects import WidgetType, WIDGET_TYPE_ATTR |
| 27 | + |
| 28 | +__version__ = "0.0.0-auto.0" |
| 29 | +__repo__ = "https://github.com/tekktrik/CircuitPython_Org_DisplayIO_Effects.git" |
| 30 | + |
| 31 | + |
| 32 | +COLORWHEEL_WIDGET_VALUES = { |
| 33 | + WidgetType.DIAL: { |
| 34 | + "path": ("_needle", "_palette"), |
| 35 | + "index" : 0, |
| 36 | + }, |
| 37 | + WidgetType.GAUGE: { |
| 38 | + "path": ("_palette"), |
| 39 | + "index": 2, |
| 40 | + }, |
| 41 | +} |
| 42 | + |
| 43 | +COLORWHEEL_COLORS = cycle([colorwheel(color_value) for color_value in range(256)]) |
| 44 | + |
| 45 | + |
| 46 | +def update_colorwheel(self): |
| 47 | + """Updates the widget value and propagates the fluctuation effect refresh""" |
| 48 | + |
| 49 | + palette_map = getattr(self, WIDGET_TYPE_ATTR) |
| 50 | + palette_attr = self |
| 51 | + for attr_path in palette_map["path"]: |
| 52 | + palette_attr = getattr(palette_attr, attr_path) |
| 53 | + palette_attr[palette_map["index"]] = next(COLORWHEEL_COLORS) |
| 54 | + |
| 55 | + |
| 56 | +def hook_colorwheel_effect(widget_class, widget_type): |
| 57 | + """Adds the colorwheel effect for the given class |
| 58 | +
|
| 59 | + :param widget_class: The widget class that should have this effect hooked |
| 60 | + into it |
| 61 | + :param int widget_type: The enum value of this widget type, must be a |
| 62 | + valid ~WidgetType |
| 63 | +
|
| 64 | + For example, to hook this into the ``Dial`` widget, you would use the |
| 65 | + following code: |
| 66 | +
|
| 67 | + .. code-block:: python |
| 68 | +
|
| 69 | + from displayio_dial import Dial |
| 70 | + from displayio_effects import WidgetType, colorwheel_effect |
| 71 | +
|
| 72 | + fluctuation_effect.hook_colorwheel_effect(Dial, WidgetType.DIAL) |
| 73 | +
|
| 74 | + """ |
| 75 | + |
| 76 | + if not COLORWHEEL_WIDGET_VALUES.get(widget_type): |
| 77 | + raise ValueError( |
| 78 | + "The given widget does not have the ability to use this effect" |
| 79 | + ) |
| 80 | + |
| 81 | + setattr(widget_class, WIDGET_TYPE_ATTR, widget_type) |
| 82 | + |
| 83 | + setattr(widget_class, "update_colorwheel", update_colorwheel) |
0 commit comments