From dd1c9a3a1e56e4fb45067a9488c253f7cd3a98e2 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sat, 18 Jul 2020 12:35:59 -0500 Subject: [PATCH 1/4] 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 --- adafruit_progressbar.py | 12 +++++++++--- examples/progressbar_simpletest.py | 14 +++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/adafruit_progressbar.py b/adafruit_progressbar.py index 5619144..c564905 100755 --- a/adafruit_progressbar.py +++ b/adafruit_progressbar.py @@ -117,12 +117,18 @@ def progress(self, value): ), "Progress value must be a floating point value." if self._progress_val > value: # uncolorize range from width*value+margin to width-margin - for _w in range(int(value * self._width + 2), self._width - 2): + # from right to left + _prev_pixel = max(2, int(self._width * self._progress_val - 2)) + _new_pixel = max(int(self._width * value - 2), 2) + for _w in range(_prev_pixel, _new_pixel - 1, -1): + print("w {}".format(_w)) for _h in range(2, self._height - 2): self._bitmap[_w, _h] = 0 else: - # fully fill progress bar color - for _w in range(2, self._width * value - 2): + # fill from the previous x pixel to the new x pixel + _prev_pixel = max(2, int(self._width * self._progress_val - 3)) + _new_pixel = min(int(self._width * value - 2), int(self._width * 1.0 - 3)) + for _w in range(_prev_pixel, _new_pixel + 1): for _h in range(2, self._height - 2): self._bitmap[_w, _h] = 2 self._progress_val = value diff --git a/examples/progressbar_simpletest.py b/examples/progressbar_simpletest.py index e5bb43d..0e48fe3 100644 --- a/examples/progressbar_simpletest.py +++ b/examples/progressbar_simpletest.py @@ -27,10 +27,10 @@ current_progress = 0.0 while True: - while current_progress <= 1.0: - print("Progress: {}%".format(current_progress * 100)) - progress_bar.progress = current_progress - current_progress += 0.05 - if current_progress >= 1.0: - current_progress = 0.0 - time.sleep(0.01) + for current_progress in range(0, 21): + print("Progress: {}%".format(current_progress * 0.05)) + progress_bar.progress = current_progress * 0.05 + time.sleep(0.02) + time.sleep(0.3) + progress_bar.progress = 0.0 + time.sleep(0.3) From 81be954658a8169d5ae04b1e1a4455dfdc92f7eb Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sat, 18 Jul 2020 13:09:13 -0500 Subject: [PATCH 2/4] remove debugging print that was accidentally left in --- adafruit_progressbar.py | 1 - 1 file changed, 1 deletion(-) diff --git a/adafruit_progressbar.py b/adafruit_progressbar.py index c564905..6eea67c 100755 --- a/adafruit_progressbar.py +++ b/adafruit_progressbar.py @@ -121,7 +121,6 @@ def progress(self, value): _prev_pixel = max(2, int(self._width * self._progress_val - 2)) _new_pixel = max(int(self._width * value - 2), 2) for _w in range(_prev_pixel, _new_pixel - 1, -1): - print("w {}".format(_w)) for _h in range(2, self._height - 2): self._bitmap[_w, _h] = 0 else: From ace28ff6ca96c320383752244817aa3eb42ebb74 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sat, 18 Jul 2020 15:00:43 -0500 Subject: [PATCH 3/4] update simpletest --- examples/progressbar_simpletest.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/progressbar_simpletest.py b/examples/progressbar_simpletest.py index 0e48fe3..60dc342 100644 --- a/examples/progressbar_simpletest.py +++ b/examples/progressbar_simpletest.py @@ -27,9 +27,10 @@ current_progress = 0.0 while True: - for current_progress in range(0, 21): - print("Progress: {}%".format(current_progress * 0.05)) - progress_bar.progress = current_progress * 0.05 + # range end is exclusive so we need to use 1 bigger than max number that we want + for current_progress in range(0, 101, 5): + print("Progress: {}%".format(current_progress)) + progress_bar.progress = current_progress / 100 # convert to decimal time.sleep(0.02) time.sleep(0.3) progress_bar.progress = 0.0 From 8921a7c11fcb2f53ed81a69c20e7475fd67f0e67 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sat, 18 Jul 2020 15:04:57 -0500 Subject: [PATCH 4/4] count by 1 instead of 5 in the simpletest --- examples/progressbar_simpletest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/progressbar_simpletest.py b/examples/progressbar_simpletest.py index 60dc342..70952d3 100644 --- a/examples/progressbar_simpletest.py +++ b/examples/progressbar_simpletest.py @@ -28,10 +28,10 @@ current_progress = 0.0 while True: # range end is exclusive so we need to use 1 bigger than max number that we want - for current_progress in range(0, 101, 5): + for current_progress in range(0, 101, 1): print("Progress: {}%".format(current_progress)) progress_bar.progress = current_progress / 100 # convert to decimal - time.sleep(0.02) + time.sleep(0.01) time.sleep(0.3) progress_bar.progress = 0.0 time.sleep(0.3)