Skip to content

Commit 0bf2830

Browse files
committed
add dial simpletest example
1 parent a477a03 commit 0bf2830

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

adafruit_displayio_layout/widgets/dial.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ def __init__(
104104
start_angle=None,
105105
clip_needle=False,
106106
# trims off the needle outside of the dial region, used for sweep_angles < 180
107-
needle_width=3,
107+
needle_width=7,
108108
# triangle with this base width, best if this is odd
109109
needle_color=0x880000,
110110
value=None,
111111
value_font=None,
112-
display_value=True,
112+
display_value=False,
113113
value_color=0xFF0000,
114114
value_format_string=":0.0f",
115115
min_value=0.0,
@@ -436,6 +436,8 @@ def _create_needle(self):
436436
self._needle_width = round(
437437
self._needle_width_requested * self._dial_radius / needle_length_showing
438438
)
439+
else:
440+
self._needle_width = self._needle_width_requested
439441

440442
def _update_value(self):
441443

@@ -493,8 +495,8 @@ def _draw_position(self, position):
493495
# Get the position offset from the motion function
494496
angle_offset = self._get_offset_position(position)
495497

496-
d_x = self._needle_width / 2 * math.cos(angle_offset)
497-
d_y = self._needle_width / 2 * math.sin(angle_offset)
498+
d_x = (self._needle_width / 2) * math.cos(angle_offset)
499+
d_y = (self._needle_width / 2) * math.sin(angle_offset)
498500

499501
x_0 = round(self._dial_center[0] - d_x)
500502
y_0 = round(self._dial_center[1] - d_y)
@@ -593,7 +595,8 @@ def draw_ticks(
593595
sweep_angle,
594596
tick_color_index=1,
595597
):
596-
"""Helper function for drawing ticks on the dial widget.
598+
"""Helper function for drawing ticks on the dial widget. Can be used to
599+
customize the dial face.
597600
598601
:param displayio.Bitmap target_bitmap: Bitmap where ticks will be drawn into
599602
:param (int,int) dial_center: the (x,y) pixel location in the bitmap of
@@ -662,7 +665,8 @@ def draw_labels(
662665
rotate_labels=True,
663666
tick_label_scale=1.0,
664667
):
665-
"""Helper function for drawing text labels on the dial widget.
668+
"""Helper function for drawing text labels on the dial widget. Can be used
669+
to customize the dial face.
666670
667671
:param displayio.Bitmap target_bitmap: Bitmap where ticks will be drawn into
668672
:param Font font: the font to be used to draw the tick mark text labels
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# SPDX-FileCopyrightText: 2021 Kevin Matocha
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#############################
5+
# This is a trial of the Dial using Adafruit_DisplayIO_Layout
6+
#
7+
8+
import time
9+
import board
10+
import displayio
11+
from adafruit_displayio_layout.widgets.dial import Dial
12+
13+
from adafruit_bitmap_font import bitmap_font
14+
15+
glyphs = "0123456789"
16+
17+
# Fonts used for the Dial tick labels
18+
tick_font_file = "fonts/BitstreamVeraSans-Bold-16.pcf"
19+
tick_font = bitmap_font.load_font(tick_font_file)
20+
tick_font.load_glyphs(glyphs)
21+
22+
display = board.DISPLAY # create the display on the PyPortal or Clue (for example)
23+
# otherwise change this to setup the display
24+
# for display chip driver and pinout you have (e.g. ILI9341)
25+
26+
27+
# Define the minimum and maximum values for the dial
28+
minimum_value = 0
29+
maximum_value = 100
30+
31+
# Create a Dial widget
32+
my_dial = Dial(
33+
x=20, # set x-position of the dial inside of my_group
34+
y=20, # set y-position of the dial inside of my_group
35+
width=180, # requested width of the di
36+
height=180,
37+
padding=25, # add 25 pixels around the dial to make room for labels
38+
start_angle=-120, # left angle position at -120 degrees
39+
sweep_angle=240, # total sweep angle of 240 degrees
40+
min_value=minimum_value, # set the minimum value shown on the dial
41+
max_value=maximum_value, # set the maximum value shown on the dial
42+
tick_label_font=tick_font, #
43+
)
44+
45+
my_group = displayio.Group(max_size=1)
46+
my_group.append(my_dial)
47+
48+
display.show(my_group) # add high level Group to the display
49+
50+
step_size = 1
51+
52+
while True:
53+
54+
# run the dial from minimum to maximum
55+
for this_value in range(minimum_value, maximum_value + 1, step_size):
56+
my_dial.value = this_value
57+
display.refresh() # force the display to refresh
58+
time.sleep(0.5)
59+
60+
# run the dial from maximum to minimum
61+
for this_value in range(maximum_value, minimum_value - 1, -step_size):
62+
my_dial.value = this_value
63+
display.refresh() # force the display to refresh
64+
time.sleep(0.5)

0 commit comments

Comments
 (0)