Skip to content

Commit 21f55ef

Browse files
authored
Merge pull request #26 from kmatch98/dial_rotation
Add limit_rotation option to constrain needle rotation
2 parents 70a1f0e + 5733d1d commit 21f55ef

File tree

1 file changed

+40
-26
lines changed
  • adafruit_displayio_layout/widgets

1 file changed

+40
-26
lines changed

adafruit_displayio_layout/widgets/dial.py

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,47 +48,56 @@ class Dial(Widget):
4848
4949
:param int width: requested width, in pixels
5050
:param int height: requested height, in pixels
51-
:param int padding: keepout padding amount around the border, in pixels
51+
:param int padding: keepout padding amount around the border, in pixels,
52+
default is 12
5253
53-
:param float sweep_angle: dial rotation, in degrees, maximum value is 360 degrees
54-
:param float start_angle: starting angle, in degrees. Defaults
55-
to `None` for symmetry along vertical axis. Vertical is defined as 0 degrees.
54+
:param float sweep_angle: dial rotation, in degrees, maximum value is 360 degrees,
55+
default is 90 degrees
56+
:param float start_angle: starting angle, in degrees. Set to `None` for symmetry along
57+
vertical axis. Vertical is defined as 0 degrees.
5658
Negative values are counter-clockwise degrees; positive values
57-
are clockwise degrees.
59+
are clockwise degrees. Defaults to `None`.
5860
59-
:param float min_value: the minimum value displayed on the dial
60-
:param float max_value: the maximum value displayed the dial
61+
:param float min_value: the minimum value displayed on the dial, default is 0.0
62+
:param float max_value: the maximum value displayed the dial, default is 100.0
6163
:param float value: the value to display (if None, defaults to ``min_value``)
6264
6365
:param Boolean display_value: set `True` to display a value label on the dial
64-
:param Font value_font: the font for the value label
65-
:param int value_color: the color for the value label
66+
:param Font value_font: the font for the value label, defaults to
67+
``terminalio.FONT``
68+
:param int value_color: the color for the value label, defaults to 0xFF0000
6669
:param str value_format_string: the format string for displaying the value label
6770
(defaults to ':0.0f' to show the value rounded to the nearest whole number)
68-
:param (float,float) value_label_anchor_point: anchor point on the label
69-
:param (float,float) value_label_anchor_point_on_widget: anchor point on the widget where the
70-
label will be placed
71+
:param (float,float) value_label_anchor_point: anchor point on the label, default
72+
value is (0.5, -1.0) where the y-value of -1.0 signifies the text baseline
73+
:param (float,float) value_label_anchor_point_on_widget: anchor point on the
74+
widget where the label will be placed, default value is (0.5, 0.5)
7175
72-
:param int needle_width: requested pixel width of the triangular needle
76+
:param int needle_width: requested pixel width of the triangular needle,
77+
default = 7
7378
:param int needle_color: color value for the needle, defaults to red (0xFF0000)
74-
75-
:param int tick_color: tick line color (24-bit hex value)
76-
:param int major_ticks: number of major ticks
77-
:param int major_tick_stroke: major tick line stroke width, in pixels
78-
:param int major_tick_length: major tick length, in pixels
79-
:param str major_tick_labels: array of strings for the major tick labels
79+
:param Boolean limit_rotation: Set True to limit needle rotation to between the
80+
``min_value`` and ``max_value``, set to False for unlimited rotation, default is True
81+
82+
:param int tick_color: tick line color (24-bit hex value), defaults to 0xFFFFFF
83+
:param int major_ticks: number of major ticks, default = 5
84+
:param int major_tick_stroke: major tick line stroke width, in pixels, default = 3
85+
:param int major_tick_length: major tick length, in pixels, default = 10
86+
:param str major_tick_labels: array of strings for the major tick labels,
87+
default is ("0", "25", "50", "75", "100")
8088
:param float tick_label_scale: the scaling of the tick labels, default = 1.0
81-
:param Font tick_label_font: font to be used for major tick labels
82-
:param int tick_label_color: color for the major tick labels
89+
:param Font tick_label_font: font to be used for major tick labels, default
90+
is ``terminalio.FONT``
91+
:param int tick_label_color: color for the major tick labels, default is 0xFFFFFF
8392
:param Boolean angle_tick_labels: set True to rotate the major tick labels to
84-
match the tick angle
93+
match the tick angle, default is True
8594
86-
:param int minor_ticks: number of minor ticks (per major tick)
87-
:param int minor_tick_stroke: minor tick line stroke width, in pixels
88-
:param int minor_tick_length: minor tick length, in pixels
95+
:param int minor_ticks: number of minor ticks (per major tick), default = 5
96+
:param int minor_tick_stroke: minor tick line stroke width, in pixels, default = 1
97+
:param int minor_tick_length: minor tick length, in pixels, default = 5
8998
9099
:param int background_color: background color (RGB tuple
91-
or 24-bit hex value), set None for transparent
100+
or 24-bit hex value), set `None` for transparent, default is `None`
92101
93102
94103
:param (float,float) anchor_point: (X,Y) values from 0.0 to 1.0 to define the dial's
@@ -169,6 +178,7 @@ def __init__(
169178
needle_width=7,
170179
# triangle with this base width, best if this is odd
171180
needle_color=0x880000,
181+
limit_rotation=True,
172182
value=None,
173183
value_font=None,
174184
display_value=False,
@@ -245,6 +255,7 @@ def __init__(
245255
self._clip_needle = clip_needle
246256
self._needle_width_requested = needle_width
247257
self._needle_color = needle_color
258+
self._limit_rotation = limit_rotation
248259
self._background_color = background_color
249260

250261
self._major_tick_labels = major_tick_labels
@@ -552,6 +563,9 @@ def _get_offset_position(self, position):
552563
return angle_offset
553564

554565
def _update_needle(self, value):
566+
if self._limit_rotation: # constrain between min_value and max_value
567+
value = max(min(self._value, self._max_value), self._min_value)
568+
555569
self._draw_position(
556570
value / (self._max_value - self._min_value)
557571
) # convert to position (0.0 to 1.0)

0 commit comments

Comments
 (0)