Skip to content

Commit 61a46f6

Browse files
authored
Merge pull request #8 from FoamyGuy/performance_boost
Performance boost and simpletest update
2 parents 48bea36 + 8921a7c commit 61a46f6

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

adafruit_progressbar.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,17 @@ 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):
121124
for _h in range(2, self._height - 2):
122125
self._bitmap[_w, _h] = 0
123126
else:
124-
# fully fill progress bar color
125-
for _w in range(2, self._width * value - 2):
127+
# fill from the previous x pixel to the new x pixel
128+
_prev_pixel = max(2, int(self._width * self._progress_val - 3))
129+
_new_pixel = min(int(self._width * value - 2), int(self._width * 1.0 - 3))
130+
for _w in range(_prev_pixel, _new_pixel + 1):
126131
for _h in range(2, self._height - 2):
127132
self._bitmap[_w, _h] = 2
128133
self._progress_val = value

examples/progressbar_simpletest.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
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
30+
# range end is exclusive so we need to use 1 bigger than max number that we want
31+
for current_progress in range(0, 101, 1):
32+
print("Progress: {}%".format(current_progress))
33+
progress_bar.progress = current_progress / 100 # convert to decimal
3634
time.sleep(0.01)
35+
time.sleep(0.3)
36+
progress_bar.progress = 0.0
37+
time.sleep(0.3)

0 commit comments

Comments
 (0)