Skip to content

Commit e46aefb

Browse files
committed
Some refactoring. Getting negative values working
1 parent ea4b835 commit e46aefb

File tree

4 files changed

+87
-41
lines changed

4 files changed

+87
-41
lines changed

adafruit_progressbar/__init__.py

+30-18
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ class ProgressBarBase(displayio.TileGrid):
4444
:type position: Tuple[int, int]
4545
:param size: The size (width, height) of the progress bar
4646
:type size: Tuple[int, int]
47-
:param start_value: The beginning value of the progress bar. This value
48-
is displayed when the progress bar is first visible,
49-
if it hasn't been updated.
50-
:type start_value: float
5147
:param bar_color: The color of the bar representing the value. This can
5248
be a hexadecimal value for color (0x224466).
5349
Default: 0x00FF00 (Solid green)
@@ -74,12 +70,11 @@ class ProgressBarBase(displayio.TileGrid):
7470
:type value_range: Tuple[int, int] or Tuple[float, float]
7571
"""
7672

77-
# pylint: disable=too-many-arguments
73+
# pylint: disable=too-many-arguments, too-many-instance-attributes
7874
def __init__(
7975
self,
8076
position,
8177
size,
82-
start_value=0.0,
8378
value=0,
8479
bar_color=0x00FF00,
8580
outline_color=0xFFFFFF,
@@ -91,8 +86,7 @@ def __init__(
9186

9287
self._widget_size = size
9388
self._position = position
94-
self._progress = start_value
95-
print(f"Size: {size} - WS: {self.widget_size}")
89+
9690
self._bitmap = displayio.Bitmap(size[0], size[1], 3)
9791
self._palette = displayio.Palette(3)
9892
self._palette[0] = fill_color
@@ -101,7 +95,17 @@ def __init__(
10195
self._border_thickness = border_thickness
10296
self._show_margin = show_margin
10397
self._range = value_range
104-
self._value = value
98+
self._progress = 0.0
99+
100+
# Setup value and old_value to handle the change to the new
101+
# initial value later.
102+
self._value = self.minimum
103+
self._old_value = self.minimum
104+
105+
self._margin = 0
106+
107+
if self._show_margin:
108+
self._margin = 1
105109

106110
super().__init__(
107111
self._bitmap,
@@ -111,6 +115,8 @@ def __init__(
111115
)
112116

113117
self._draw_outline()
118+
self.render(self.minimum, self.minimum + 1, 0)
119+
self.value = value
114120

115121
# _bitmap: displayio.Bitmap # The bitmap used for the bar/value
116122
# _position: (int, int) # The (x,y) coordinates of the top-left corner
@@ -138,11 +144,6 @@ def widget_height(self):
138144
"""The total height of the widget, in pixels. Includes the border and margin."""
139145
return self.widget_size[1]
140146

141-
@property
142-
def _outline_color(self):
143-
"""The colour of the border/outline of the widget"""
144-
return self._palette[1]
145-
146147
@property
147148
def x(self):
148149
"""The horizontal (x) position of the top-left corner of the widget."""
@@ -188,6 +189,15 @@ def value(self):
188189

189190
@value.setter
190191
def value(self, value):
192+
"""Sets the current value of the progress within the min-max range
193+
194+
:param value: The new value for the progress status
195+
:type value: int/float
196+
"""
197+
# Save off the previous value, so we can pass it in the
198+
# call to "Render"
199+
print(f"Updating value from {self._value} to {value}")
200+
self._old_value = self._value
191201
self._value = value
192202
# Convert value to float since we may be dealing with
193203
# integer types, and we can't work with integer division
@@ -210,15 +220,17 @@ def border_thickness(self):
210220
def progress(self, value):
211221
"""The current displayed value of the widget.
212222
213-
:param float value: The new value which should be displayed by the progress
223+
:param value: The new value which should be displayed by the progress
214224
bar. Must be between 0.0-1.0
225+
:type value: float
215226
"""
216-
_old_value = self.progress
217227
# If we're using floats, from 0.0 to 1.0, using 4 decimal places allows us to handle values
218228
# as precise as 0.23456, which evaluates to a percentage value of 23.45% (with rounding)
219229
self._progress = round(value, 4)
220-
print(f"Calling render() with ({_old_value}, {self.progress}, {self.progress})")
221-
self.render(_old_value, self.progress, self.progress)
230+
print(
231+
f"Calling render() with ({self._old_value}, {self.value}, {self.progress})"
232+
)
233+
self.render(self._old_value, self.value, self.progress)
222234

223235
@property
224236
def range(self):

adafruit_progressbar/horizontalprogressbar.py

+53-12
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ class HorizontalProgressBar(ProgressBarBase):
4848
:type anchor_position: Tuple[int, int]
4949
:param size: The size in (width, height) of the progress bar
5050
:type size: Tuple[int, int]
51-
:param progress: The percentage of the progress bar.
52-
:type progress: float
5351
:param bar_color: The color of the progress bar. Can be a hex
5452
value for color.
5553
:param outline_color: The outline of the progress bar. Can be a hex
@@ -85,16 +83,16 @@ def __init__(
8583
super().__init__(
8684
anchor_position,
8785
size,
88-
progress,
86+
value,
8987
bar_color,
9088
outline_color,
9189
fill_color,
92-
value,
93-
border_thickness=border_thickness,
94-
show_margin=show_margin,
95-
value_range=(min_value, max_value),
90+
border_thickness,
91+
show_margin,
92+
(min_value, max_value),
9693
)
9794

95+
# pylint: disable=too-many-locals
9896
def render(self, _previous_value, _new_value, _progress_value) -> None:
9997
"""
10098
The rendering mechanism to display the newly set value.
@@ -109,20 +107,63 @@ def render(self, _previous_value, _new_value, _progress_value) -> None:
109107
:rtype None:
110108
"""
111109

112-
if _previous_value == _new_value:
113-
return # Do nothing if there's nothing to update
110+
_padding = self.border_thickness
111+
112+
if self._margin:
113+
_padding += 1
114+
115+
_border_thickness = self.border_thickness
116+
_border_size = (
117+
_border_thickness * 2
118+
) # Size of the border on both sides of the control (1),
119+
# in both directions (left-to-right and top-to-bottom)
120+
121+
_fill_width = (
122+
self.widget_width - (2 * _padding) - _border_size
123+
) # Count padding on left and right
124+
_fill_height = (
125+
self.widget_height - (2 * _padding) - _border_size
126+
) # Count padding on the top and bottom
127+
128+
_prev_prog = float(_previous_value - self.minimum) / (
129+
abs(self.minimum) + abs(self.maximum)
130+
)
131+
132+
print(f"Progress: from {_prev_prog} to {_progress_value}")
133+
_prev_value_size = int(_prev_prog * _fill_height)
134+
_new_value_size = int(_progress_value * _fill_height)
135+
136+
# If we have *ANY* value other than "zero" (minimum), we should
137+
# have at least one element showing
138+
if _new_value_size == 0 and _new_value > self.minimum:
139+
_new_value_size = 1
140+
141+
# Conversely, if we have *ANY* value other than 100% (maximum),
142+
# we should NOT show a full bar.
143+
144+
if _new_value_size == _fill_height and _new_value < self.maximum:
145+
_new_value_size -= 1
146+
147+
# Default values for increasing value
148+
_color = 2
149+
_incr = 1
150+
_start_offset = _padding + _border_thickness
151+
_start = max(_prev_value_size, _start_offset)
152+
_end = max(_new_value_size, 0) + _start_offset
114153

115154
if _previous_value > _new_value:
155+
print("prev > new")
116156
# Remove color in range from width*value+margin to width-margin
117157
# from right to left
118-
_prev_pixel = max(2, int(self.widget_width * self.progress - 2))
119-
_new_pixel = max(int(self.widget_width * _new_value - 2), 2)
158+
_prev_pixel = max(2, int(self.widget_width * _previous_value - _padding))
159+
_new_pixel = max(int(self.widget_width * _new_value - _padding), 2)
120160
for _w in range(_prev_pixel, _new_pixel - 1, -1):
121161
for _h in range(2, self.widget_height - 2):
122162
self._bitmap[_w, _h] = 0
123163
else:
164+
print("prev <= new")
124165
# fill from the previous x pixel to the new x pixel
125-
_prev_pixel = max(2, int(self.widget_width * self.progress - 3))
166+
_prev_pixel = max(2, int(self.widget_width * _previous_value - 3))
126167
_new_pixel = min(
127168
int(self.widget_width * _new_value - 2),
128169
int(self.widget_width * 1.0 - 3),

adafruit_progressbar/progressbar.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class ProgressBar(HorizontalProgressBar):
3737
:type width: int
3838
:param height: The height of the progress bar.
3939
:type height: int
40-
:param progress: The percentage of the progress bar.
41-
:type progress: float
4240
:param bar_color: The color of the progress bar. Can be a hex
4341
value for color.
4442
:param outline_color: The outline of the progress bar. Can be a hex
@@ -70,8 +68,7 @@ def __init__(
7068
(width, height),
7169
0,
7270
100,
73-
progress,
74-
0,
71+
(progress * 100), # Progress vs. max above
7572
bar_color,
7673
outline_color,
7774
0x000000,

adafruit_progressbar/verticalprogressbar.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def __init__(
8484
size,
8585
min_value=0,
8686
max_value=100,
87-
progress=0.0,
8887
value=0,
8988
bar_color=0x00FF00,
9089
outline_color=0xFFFFFF,
@@ -129,7 +128,6 @@ def __init__(
129128
super().__init__(
130129
anchor_position,
131130
size,
132-
progress,
133131
value,
134132
bar_color,
135133
outline_color,
@@ -155,14 +153,12 @@ def render(self, _old_value, _new_value, _progress_value):
155153
:return: None
156154
:rtype: None
157155
"""
158-
_padding = 0
159-
160-
print(f"Drawing a visual of progress value {_progress_value}")
156+
_padding = self._border_thickness
161157

162158
if self._margin:
163-
_padding = 1
159+
_padding += 1
164160

165-
_border_thickness = 1
161+
_border_thickness = self.border_thickness
166162
_border_size = (
167163
_border_thickness * 2
168164
) # Size of the border on both sides of the control (1),

0 commit comments

Comments
 (0)