Skip to content

Commit dd1c9a3

Browse files
committed
update the progress method to only fill pixels that need to be changed. change simpletest to use for loop counting ints to avoid floating point math issue
1 parent 48bea36 commit dd1c9a3

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

adafruit_progressbar.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,18 @@ def progress(self, value):
117117
), "Progress value must be a floating point value."
118118
if self._progress_val > value:
119119
# uncolorize range from width*value+margin to width-margin
120-
for _w in range(int(value * self._width + 2), self._width - 2):
120+
# from right to left
121+
_prev_pixel = max(2, int(self._width * self._progress_val - 2))
122+
_new_pixel = max(int(self._width * value - 2), 2)
123+
for _w in range(_prev_pixel, _new_pixel - 1, -1):
124+
print("w {}".format(_w))
121125
for _h in range(2, self._height - 2):
122126
self._bitmap[_w, _h] = 0
123127
else:
124-
# fully fill progress bar color
125-
for _w in range(2, self._width * value - 2):
128+
# fill from the previous x pixel to the new x pixel
129+
_prev_pixel = max(2, int(self._width * self._progress_val - 3))
130+
_new_pixel = min(int(self._width * value - 2), int(self._width * 1.0 - 3))
131+
for _w in range(_prev_pixel, _new_pixel + 1):
126132
for _h in range(2, self._height - 2):
127133
self._bitmap[_w, _h] = 2
128134
self._progress_val = value

examples/progressbar_simpletest.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727

2828
current_progress = 0.0
2929
while True:
30-
while current_progress <= 1.0:
31-
print("Progress: {}%".format(current_progress * 100))
32-
progress_bar.progress = current_progress
33-
current_progress += 0.05
34-
if current_progress >= 1.0:
35-
current_progress = 0.0
36-
time.sleep(0.01)
30+
for current_progress in range(0, 21):
31+
print("Progress: {}%".format(current_progress * 0.05))
32+
progress_bar.progress = current_progress * 0.05
33+
time.sleep(0.02)
34+
time.sleep(0.3)
35+
progress_bar.progress = 0.0
36+
time.sleep(0.3)

0 commit comments

Comments
 (0)