Skip to content

Commit ea4b835

Browse files
committed
Update documentation
1 parent 656c642 commit ea4b835

File tree

5 files changed

+119
-63
lines changed

5 files changed

+119
-63
lines changed

adafruit_progressbar/__init__.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ def __init__(
8080
position,
8181
size,
8282
start_value=0.0,
83+
value=0,
8384
bar_color=0x00FF00,
8485
outline_color=0xFFFFFF,
8586
fill_color=0x000000,
8687
border_thickness=1,
8788
show_margin=False,
88-
value_range=(0.0, 1.0),
89+
value_range=(0, 100),
8990
):
9091

9192
self._widget_size = size
@@ -100,6 +101,7 @@ def __init__(
100101
self._border_thickness = border_thickness
101102
self._show_margin = show_margin
102103
self._range = value_range
104+
self._value = value
103105

104106
super().__init__(
105107
self._bitmap,
@@ -176,6 +178,24 @@ def fill(self, color):
176178
self._palette[2] = color
177179
self._palette.make_opaque(0)
178180

181+
@property
182+
def value(self):
183+
"""
184+
The current value of the control, used to determine its progress/ratio
185+
:return: int/float
186+
"""
187+
return self._value
188+
189+
@value.setter
190+
def value(self, value):
191+
self._value = value
192+
# Convert value to float since we may be dealing with
193+
# integer types, and we can't work with integer division
194+
# to get a ratio (position) of "value" within range.
195+
self.progress = (float(value - self._range[0])) / (
196+
abs(self._range[0]) + abs(self._range[1])
197+
)
198+
179199
@property
180200
def progress(self):
181201
"""Gets the current displayed value of the widget."""

adafruit_progressbar/horizontalprogressbar.py

+76-10
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,88 @@
1212
"""
1313

1414
# import displayio
15-
from . import ProgressBarBase # , FillDirection
15+
from . import ProgressBarBase, FillDirection
1616

1717

1818
class HorizontalProgressBar(ProgressBarBase):
19-
"""
20-
A progress bar that goes horizontally.
19+
"""A dynamic progress bar widget.
20+
21+
The anchor position is the position where the control would start if it
22+
were being read visually or on paper, where the (0, 0) position is
23+
the lower-left corner for ascending progress bars (fills from the bottom to
24+
to the top in vertical bars, or from the left to the right in horizontal
25+
progress bars), upper-left corner for descending progress bars (fills from
26+
the top to the bottom).
27+
28+
Using the diagrams below, the bar will fill in the following directions::
29+
30+
-----------------------------
31+
| Horizontal | Vertical |
32+
----------------------------------------------
33+
| Ascending | 1-3 to 2-4 | 3-4 to 1-2 |
34+
----------------------------------------------
35+
| Descending | 2-4 to 1-3 | 1-2 to 3-4 |
36+
----------------------------------------------
37+
38+
Vertical Horizontal
39+
40+
1--2 1-----------------------2
41+
| | | |
42+
| | | |
43+
| | 3-----------------------4
44+
| |
45+
3--4
46+
47+
:param anchor_position: The anchor coordinates of the progress bar.
48+
:type anchor_position: Tuple[int, int]
49+
:param size: The size in (width, height) of the progress bar
50+
:type size: Tuple[int, int]
51+
:param progress: The percentage of the progress bar.
52+
:type progress: float
53+
:param bar_color: The color of the progress bar. Can be a hex
54+
value for color.
55+
:param outline_color: The outline of the progress bar. Can be a hex
56+
value for color.
57+
:type outline_color: int
58+
:param border_thickness: Used for the outline_color
59+
:type border_thickness: int
60+
:param show_margin: Whether or not to have a margin between the border and
61+
the fill, or not.
62+
:type show_margin: bool
63+
:param direction: The direction of the fill
64+
:type direction: FillDirection
65+
2166
"""
2267

23-
def __init__(self):
24-
"""
25-
Get stuff, horizontally!
26-
:param args:
27-
:param kwargs:
28-
"""
68+
# pylint: disable=bad-option-value, unused-argument, too-many-arguments
69+
def __init__(
70+
self,
71+
anchor_position,
72+
size,
73+
min_value=0,
74+
max_value=100,
75+
progress=0.0,
76+
value=0,
77+
bar_color=0x00FF00,
78+
outline_color=0xFFFFFF,
79+
fill_color=0x444444,
80+
border_thickness=1,
81+
show_margin=True,
82+
direction=FillDirection.DEFAULT,
83+
):
2984

30-
super().__init__((0, 0), (100, 20), 0.0)
85+
super().__init__(
86+
anchor_position,
87+
size,
88+
progress,
89+
bar_color,
90+
outline_color,
91+
fill_color,
92+
value,
93+
border_thickness=border_thickness,
94+
show_margin=show_margin,
95+
value_range=(min_value, max_value),
96+
)
3197

3298
def render(self, _previous_value, _new_value, _progress_value) -> None:
3399
"""

adafruit_progressbar/progressbar.py

+5-41
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"""
2323

2424
# imports
25-
from horizontalprogressbar import HorizontalProgressBar
25+
from adafruit_progressbar.horizontalprogressbar import HorizontalProgressBar
2626

2727

2828
# pylint: disable=too-many-arguments, too-few-public-methods
@@ -68,49 +68,13 @@ def __init__(
6868
super().__init__(
6969
(x, y),
7070
(width, height),
71+
0,
72+
100,
7173
progress,
74+
0,
7275
bar_color,
7376
outline_color,
74-
0x444444,
77+
0x000000,
7578
border_thickness=stroke,
7679
show_margin=True,
77-
value_range=(0.0, 1.0),
7880
)
79-
80-
# _outline_color: int # The colour used for the border of the widget
81-
82-
def render(self, _previous_value, _new_value, _progress_value) -> None:
83-
"""
84-
The rendering mechanism to display the newly set value.
85-
86-
:param _previous_value: The value from which we are updating
87-
:type _previous_value: object
88-
:param _new_value: The value to which we are updating
89-
:type _new_value: object
90-
:param _progress_value: The value of the progress, or ratio between the new value and the
91-
maximum value
92-
:type _progress_value: float
93-
:rtype None:
94-
"""
95-
96-
if _previous_value == _new_value:
97-
return # Do nothing if there's nothing to update
98-
99-
if _previous_value > _new_value:
100-
# Remove color in range from width*value+margin to width-margin
101-
# from right to left
102-
_prev_pixel = max(2, int(self.widget_width * self.progress - 2))
103-
_new_pixel = max(int(self.widget_width * _new_value - 2), 2)
104-
for _w in range(_prev_pixel, _new_pixel - 1, -1):
105-
for _h in range(2, self.widget_height - 2):
106-
self._bitmap[_w, _h] = 0
107-
else:
108-
# fill from the previous x pixel to the new x pixel
109-
_prev_pixel = max(2, int(self.widget_width * self.progress - 3))
110-
_new_pixel = min(
111-
int(self.widget_width * _new_value - 2),
112-
int(self.widget_width * 1.0 - 3),
113-
)
114-
for _w in range(_prev_pixel, _new_pixel + 1):
115-
for _h in range(2, self.widget_height - 2):
116-
self._bitmap[_w, _h] = 2

adafruit_progressbar/verticalprogressbar.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
Dynamic progress bar widget for CircuitPython displays
1010
1111
12-
* Author(s): Brent Rubell
13-
Hugo Dahl
12+
* Author(s): Brent Rubell, Hugo Dahl
1413
1514
Implementation Notes
1615
--------------------
@@ -40,13 +39,13 @@ class VerticalProgressBar(ProgressBarBase):
4039
4140
Using the diagrams below, the bar will fill in the following directions::
4241
43-
-----------------------------
44-
| Horizontal | Vertical |
45-
----------------------------------------------
46-
| Ascending | 1-3 to 2-4 | 3-4 to 1-2 |
47-
----------------------------------------------
48-
| Descending | 2-4 to 1-3 | 1-2 to 3-4 |
49-
----------------------------------------------
42+
-----------------------------
43+
| Horizontal | Vertical |
44+
----------------------------------------------
45+
| Ascending | 1-3 to 2-4 | 3-4 to 1-2 |
46+
----------------------------------------------
47+
| Descending | 2-4 to 1-3 | 1-2 to 3-4 |
48+
----------------------------------------------
5049
5150
Vertical Horizontal
5251
@@ -86,9 +85,10 @@ def __init__(
8685
min_value=0,
8786
max_value=100,
8887
progress=0.0,
88+
value=0,
8989
bar_color=0x00FF00,
9090
outline_color=0xFFFFFF,
91-
fill_color=None,
91+
fill_color=0x444444,
9292
stroke=1,
9393
margin=False,
9494
direction=FillDirection.DEFAULT,
@@ -130,12 +130,13 @@ def __init__(
130130
anchor_position,
131131
size,
132132
progress,
133+
value,
133134
bar_color,
134135
outline_color,
135136
fill_color,
136137
border_thickness=stroke,
137138
show_margin=True,
138-
value_range=(0.0, 1.0),
139+
value_range=(min_value, max_value),
139140
)
140141

141142
# pylint: disable=too-many-locals

docs/api.rst

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ API Definition
66
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
77
.. use this format as the module name: "adafruit_foo.foo"
88
9+
.. automodule:: adafruit_progressbar.horizontalprogressbar
10+
:members:
11+
:show-inheritance:
12+
:inherited-members:
13+
914
.. automodule:: adafruit_progressbar.progressbar
1015
:members:
1116
:show-inheritance:

0 commit comments

Comments
 (0)