Skip to content

Commit b867283

Browse files
authored
Merge pull request #74 from FoamyGuy/dynamic_arc
allow dynamic arc. update pylint
2 parents 2f95ac1 + 7f83f59 commit b867283

File tree

5 files changed

+189
-23
lines changed

5 files changed

+189
-23
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repos:
1818
- id: end-of-file-fixer
1919
- id: trailing-whitespace
2020
- repo: https://github.com/pycqa/pylint
21-
rev: v2.17.4
21+
rev: v3.3.1
2222
hooks:
2323
- id: pylint
2424
name: pylint (library code)

.pylintrc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ confidence=
5555
# no Warning level messages displayed, use"--disable=all --enable=classes
5656
# --disable=W"
5757
# disable=import-error,raw-checker-failed,bad-inline-option,locally-disabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,deprecated-str-translate-call
58-
disable=raw-checker-failed,bad-inline-option,locally-disabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,import-error,pointless-string-statement,unspecified-encoding
58+
disable=raw-checker-failed,bad-inline-option,locally-disabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,import-error,pointless-string-statement,unspecified-encoding,too-many-arguments,too-many-positional-arguments
5959

6060
# Enable the message, report, category or checker with the given id(s). You can
6161
# either give multiple identifier separated by comma (,) or put this option
@@ -360,9 +360,6 @@ valid-metaclass-classmethod-first-arg=mcs
360360

361361
[DESIGN]
362362

363-
# Maximum number of arguments for function / method
364-
max-args=5
365-
366363
# Maximum number of attributes for a class (see R0902).
367364
# max-attributes=7
368365
max-attributes=11

adafruit_display_shapes/arc.py

Lines changed: 121 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,133 @@ def __init__(
8080
**kwargs,
8181
) -> None:
8282
super().__init__(*args, **kwargs)
83+
8384
# shift direction by angle/2
84-
direction = direction - angle / 2
85+
self._direction = direction - angle / 2
86+
self._radius = radius
87+
self._angle = angle
88+
self._segments = segments
89+
self._outline = outline
90+
self._fill = fill
91+
self._arc_width = arc_width
92+
self.palette = None
93+
self.vector_polygon = None
94+
self.outline_polygon = None
95+
96+
self._init_arc()
97+
98+
def _init_arc(self):
8599
# create outer points
86100
points = []
87-
for i in range(segments + 1):
88-
alpha = (i * angle / segments + direction) / 180 * math.pi
89-
x0 = int(radius * math.cos(alpha))
90-
y0 = -int(radius * math.sin(alpha))
101+
for i in range(self._segments + 1):
102+
alpha = (i * self._angle / self._segments + self._direction) / 180 * math.pi
103+
x0 = int(self._radius * math.cos(alpha))
104+
y0 = -int(self._radius * math.sin(alpha))
91105
points.append((x0, y0))
92106

93107
# create inner points
94-
if arc_width > 1:
95-
for i in range(segments, -1, -1):
96-
alpha = (i * angle / segments + direction) / 180 * math.pi
97-
x0 = int((radius - arc_width) * math.cos(alpha))
98-
y0 = -int((radius - arc_width) * math.sin(alpha))
108+
if self._arc_width > 1:
109+
for i in range(self._segments, -1, -1):
110+
alpha = (
111+
(i * self._angle / self._segments + self._direction) / 180 * math.pi
112+
)
113+
x0 = int((self._radius - self._arc_width) * math.cos(alpha))
114+
y0 = -int((self._radius - self._arc_width) * math.sin(alpha))
99115
points.append((x0, y0))
100116

101117
# create polygon(s) and add to ourselves
102-
if arc_width > 1 and HAVE_VECTORIO and fill is not None:
103-
palette = displayio.Palette(1)
104-
palette[0] = fill
105-
self.append(vectorio.Polygon(pixel_shader=palette, points=points, x=0, y=0))
106-
if outline is not None:
107-
self.append(Polygon(points, outline=outline, colors=1, close=arc_width > 1))
118+
if self._arc_width > 1 and HAVE_VECTORIO and self._fill is not None:
119+
if self.palette is None:
120+
self.palette = displayio.Palette(1)
121+
self.palette[0] = self._fill
122+
if self.vector_polygon is None:
123+
self.vector_polygon = vectorio.Polygon(
124+
pixel_shader=self.palette, points=points, x=0, y=0
125+
)
126+
self.append(self.vector_polygon)
127+
else:
128+
self.vector_polygon.points = points
129+
130+
if self._outline is not None:
131+
if self.outline_polygon is None:
132+
self.outline_polygon = Polygon(
133+
points, outline=self._outline, colors=1, close=self._arc_width > 1
134+
)
135+
else:
136+
self.remove(self.outline_polygon)
137+
self.outline_polygon = Polygon(
138+
points, outline=self._outline, colors=1, close=self._arc_width > 1
139+
)
140+
self.append(self.outline_polygon)
141+
142+
@property
143+
def direction(self):
144+
"""Which direction the arc is pointing"""
145+
return self._direction
146+
147+
@direction.setter
148+
def direction(self, value):
149+
self._direction = value
150+
self._direction = value - self.angle / 2
151+
self._init_arc()
152+
153+
@property
154+
def radius(self):
155+
"""Radius of the arc"""
156+
return self._radius
157+
158+
@radius.setter
159+
def radius(self, value):
160+
self._radius = value
161+
self._init_arc()
162+
163+
@property
164+
def angle(self):
165+
"""How wide the curve of the arc is in degrees"""
166+
return self._angle
167+
168+
@angle.setter
169+
def angle(self, value):
170+
self._angle = value
171+
self._init_arc()
172+
173+
@property
174+
def segments(self):
175+
"""Number of segments of the arc, more segments make smoother
176+
rounded parts but use more time and memory"""
177+
return self._segments
178+
179+
@segments.setter
180+
def segments(self, value):
181+
self._segments = value
182+
self._init_arc()
183+
184+
@property
185+
def outline(self):
186+
"""The outline color. None for no outline"""
187+
return self._outline
188+
189+
@outline.setter
190+
def outline(self, value):
191+
self._outline = value
192+
self._init_arc()
193+
194+
@property
195+
def fill(self):
196+
"""The fill color. None for no fill"""
197+
return self._fill
198+
199+
@fill.setter
200+
def fill(self, value):
201+
self._fill = value
202+
self._init_arc()
203+
204+
@property
205+
def arc_width(self):
206+
"""The thickness of the arc in pixels"""
207+
return self._arc_width
208+
209+
@arc_width.setter
210+
def arc_width(self, value):
211+
self._arc_width = value
212+
self._init_arc()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
Illustrates how to dynamically update arcs over time.
5+
"""
6+
import time
7+
import board
8+
9+
import displayio
10+
from adafruit_display_shapes.arc import Arc
11+
from adafruit_display_shapes.circle import Circle
12+
13+
# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
14+
# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
15+
# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
16+
display = board.DISPLAY
17+
18+
w2 = int(display.width / 2)
19+
h2 = int(display.height / 2)
20+
21+
WHITE = 0xFFFFFF
22+
RED = 0xFF0000
23+
GREEN = 0x00FF00
24+
BLUE = 0x0000FF
25+
26+
# Make the display context
27+
group = displayio.Group()
28+
display.root_group = group
29+
30+
# little circle in the center of all arcs
31+
circle = Circle(w2, h2, 5, fill=0x00FF00)
32+
group.append(circle)
33+
34+
# red arc , 10 pixels wide
35+
arc1 = Arc(
36+
x=w2,
37+
y=h2,
38+
radius=min(display.width, display.height) / 4,
39+
angle=90,
40+
direction=90,
41+
segments=20,
42+
arc_width=10,
43+
fill=RED,
44+
)
45+
group.append(arc1)
46+
47+
# blue arc (or pie)
48+
arc2 = Arc(
49+
x=w2,
50+
y=h2,
51+
radius=min(display.width, display.height) / 6,
52+
angle=90,
53+
direction=0,
54+
segments=10,
55+
arc_width=min(display.width, display.height) / 6 - 5,
56+
outline=BLUE,
57+
)
58+
group.append(arc2)
59+
60+
while True:
61+
for i in range(360 // 40 + 1):
62+
arc1.angle = i * 40
63+
arc2.direction = i * 40
64+
time.sleep(0.05)
65+
print(len(arc2))

examples/display_shapes_sparkline_ticks.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@
165165
x_start = sparkline1.x - 5
166166
x_end = sparkline1.x
167167
y_both = int(round(sparkline1.y + (i * (chart_height) / (total_ticks))))
168-
if y_both > sparkline1.y + chart_height - 1:
169-
y_both = sparkline1.y + chart_height - 1
168+
y_both = min(y_both, sparkline1.y + chart_height - 1)
170169
my_group.append(Line(x_start, y_both, x_end, y_both, color=line_color))
171170

172171

0 commit comments

Comments
 (0)