|
31 | 31 | # * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
|
32 | 32 | """
|
33 | 33 |
|
34 |
| -# imports |
| 34 | +import random |
35 | 35 |
|
36 | 36 | __version__ = "0.0.0-auto.0"
|
37 | 37 | __repo__ = "https://github.com/tekktrik/CircuitPython_Org_DisplayIO_Effects.git"
|
| 38 | + |
| 39 | + |
| 40 | +@property |
| 41 | +def throttle_effect(self): |
| 42 | + """The furtherest the throttle effect can randomly set the dial value relative |
| 43 | + to its true position, in either direction. |
| 44 | + """ |
| 45 | + return self._throttle_setting |
| 46 | + |
| 47 | +@throttle_effect.setter |
| 48 | +def throttle_effect(self, setting): |
| 49 | + if setting < 0: |
| 50 | + raise ValueError("Throttle effect setting must be larger than 0") |
| 51 | + if setting: |
| 52 | + self._throttle_hold_value = self.value |
| 53 | + self._throttle_setting = setting |
| 54 | + |
| 55 | +@property |
| 56 | +def throttle_effect_move_rate(self): |
| 57 | + """The speed at which the throttle effect moves the dial per update""" |
| 58 | + return self._throttle_move_rate |
| 59 | + |
| 60 | +@throttle_effect_move_rate.setter |
| 61 | +def throttle_effect_move_rate(self, rate): |
| 62 | + self._throttle_move_rate = rate |
| 63 | + |
| 64 | +def throttle_update(self): |
| 65 | + """Updates the gauge and propagates the throttle effect refresh""" |
| 66 | + |
| 67 | + if self._throttle_setting == 0: |
| 68 | + self._throttle_destination = None |
| 69 | + return |
| 70 | + |
| 71 | + if self._throttle_destination in (None, self._throttle_hold_value): |
| 72 | + limit_bound = self._throttle_setting * 10 |
| 73 | + self._throttle_destination = ( |
| 74 | + random.uniform(-limit_bound, limit_bound) / 10 |
| 75 | + + self._throttle_hold_value |
| 76 | + ) |
| 77 | + |
| 78 | + self.value = ( |
| 79 | + self.value + self._throttle_move_rate |
| 80 | + if self._throttle_destination > self.value |
| 81 | + else self.value - self._throttle_move_rate |
| 82 | + ) |
| 83 | + |
| 84 | + threshold_check = ( |
| 85 | + self.value >= self._throttle_destination |
| 86 | + if self._throttle_destination >= self._throttle_hold_value |
| 87 | + else self.value <= self._throttle_destination |
| 88 | + ) |
| 89 | + if threshold_check: |
| 90 | + self._throttle_destination = self._throttle_hold_value |
| 91 | + |
| 92 | +def hook_throttle_effect(*widget_classes): |
| 93 | + for widget_class in widget_classes: |
| 94 | + |
| 95 | + setattr(widget_class, "_throttle_destination", None) |
| 96 | + setattr(widget_class, "_throttle_value", 0) |
| 97 | + |
| 98 | + setattr(widget_class, "throttle_effect", throttle_effect) |
| 99 | + setattr(widget_class, "_throttle_effect", 0) |
| 100 | + setattr(widget_class, "throttle_effect_move_rate", throttle_effect_move_rate) |
| 101 | + setattr(widget_class, "_throttle_move_rate", 0.1) |
| 102 | + |
| 103 | + setattr(widget_class, "throttle_update", throttle_update) |
0 commit comments