Skip to content

Commit e6a0c73

Browse files
committed
Refactor for duplicate code
Changes working for initial (incrementing) direction
1 parent 999a8da commit e6a0c73

File tree

4 files changed

+61
-16
lines changed

4 files changed

+61
-16
lines changed

adafruit_progressbar/__init__.py

+25-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
# imports
2525
try:
26-
from typing import Tuple, Union
26+
from typing import Tuple, Union, List
2727
except ImportError:
2828
pass # No harm if the module isn't located
2929
import displayio
@@ -476,6 +476,10 @@ def get_value_ratio(self, value: Union[int, float]) -> float:
476476
def _get_value_sizes(cls, _old_ratio: float, _new_ratio: float) -> Tuple[int, int]:
477477
return 0, 0
478478

479+
@classmethod
480+
def _get_max_fill_size(cls) -> int:
481+
return 0
482+
479483
def _get_ratios(
480484
self, _old_value: Union[int, float], _new_value: Union[int, float]
481485
) -> Tuple[float, float]:
@@ -487,11 +491,13 @@ def _adjust_size_for_range_limits(
487491
# If we have *ANY* value other than "zero" (minimum), we should
488492
# have at least one element showing
489493
if _new_value_size == 0 and _new_value > self.minimum:
494+
print(f"Adjust 1 up for {_new_value}")
490495
_new_value_size = 1
491496

492497
# Conversely, if we have *ANY* value other than 100% (maximum),
493498
# we should NOT show a full bar.
494-
if _new_value_size == self.fill_height() and _new_value < self.maximum:
499+
if _new_value_size == self._get_max_fill_size() and _new_value < self.maximum:
500+
print(f"Adjust 1 down for {_new_value}")
495501
_new_value_size -= 1
496502

497503
return _new_value_size
@@ -549,32 +555,38 @@ def _render(
549555
# Default values for increasing value
550556
_color = 2
551557
_incr = 1
552-
_start = max(_old_value_size + _render_offset, _render_offset)
553-
_end = max(_new_value_size, 0) + _render_offset
558+
_start = max(_old_value_size, 0)
559+
_end = max(_new_value_size, 0)
554560

555561
if _old_value_size >= _new_value_size:
556562
# Override defaults to be decreasing
557563
_color = 0 # Clear
558564
_incr = -1 # Iterate range downward
559-
_start = max(_old_value_size + _render_offset, _render_offset)
560-
_end = max(_new_value_size + _render_offset, _render_offset) - 1
565+
_start = max(_old_value_size, 0) - 1
566+
_end = max(_new_value_size, 0) - 1
561567
# If we're setting to minimum, make sure we're clearing by
562-
# starting one "bar" higher
568+
# starting one "bar" further
563569
if _new_value == self.minimum:
564570
_start += 1
565571

566-
if self._invert_fill_direction():
567-
_ref_pos = self.widget_height - 1
568-
_end = _ref_pos - _end # Those pesky "off-by-one" issues
569-
_start = _ref_pos - _start
570-
_incr = -1 if _start > _end else 1
571-
_color = 0 if _old_value > _new_value else 2
572+
# if self._invert_fill_direction():
573+
# _ref_pos = self.widget_height - _render_offset
574+
# self._debug("Ref pos: ", _ref_pos)
575+
# _end = _ref_pos - _end # Those pesky "off-by-one" issues
576+
# _start = _ref_pos - _start
577+
# _incr = -1 if _start > _end else 1
578+
# _color = 0 if _old_value > _new_value else 2
572579

573580
vert_start, vert_end, vert_incr = self._get_vertical_fill(_start, _end, _incr)
574581
horiz_start, horiz_end, horiz_incr = self._get_horizontal_fill(
575582
_start, _end, _incr
576583
)
577584

585+
vert_start += _render_offset
586+
vert_end += _render_offset
587+
horiz_start += _render_offset
588+
horiz_end += _render_offset
589+
578590
for vertical_position in range(vert_start, vert_end, vert_incr):
579591
for horizontal_position in range(horiz_start, horiz_end, horiz_incr):
580592
self._bitmap[horizontal_position, vertical_position] = _color

adafruit_progressbar/horizontalprogressbar.py

+3
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,6 @@ def _get_horizontal_fill(
151151

152152
def _invert_fill_direction(self) -> bool:
153153
return self._direction == HorizontalFillDirection.RIGHT_TO_LEFT
154+
155+
def _get_max_fill_size(self):
156+
return self.fill_width()

examples/progressbar_magtag_simpletest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import board
1010
import displayio
1111
import digitalio
12-
from adafruit_progressbar.progressbar import ProgressBar
12+
from adafruit_progressbar.progressbar import HorizontalProgressBar
1313

1414
# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
1515
# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
@@ -34,7 +34,7 @@
3434
y = display.height // 3
3535

3636
# Create a new progress_bar object at (x, y)
37-
progress_bar = ProgressBar(
37+
progress_bar = HorizontalProgressBar(
3838
x, y, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x666666, outline_color=0xFFFFFF
3939
)
4040

examples/progressbar_matrixportal.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
# CONTROLS
1818

19+
import digitalio
20+
1921
from adafruit_progressbar.horizontalprogressbar import (
2022
HorizontalProgressBar,
2123
HorizontalFillDirection,
@@ -72,7 +74,12 @@
7274

7375
# Another progress bar, with explicit range and fill from the right
7476
ranged_bar = HorizontalProgressBar(
75-
(2, 20), (40, 8), value=40, min_value=0, max_value=100
77+
(2, 20),
78+
(40, 8),
79+
value=40,
80+
min_value=0,
81+
max_value=100,
82+
direction=HorizontalFillDirection.RIGHT_TO_LEFT,
7683
)
7784
group.insert(1, ranged_bar)
7885

@@ -107,22 +114,38 @@
107114

108115
group.insert(3, countdown_bar)
109116
# group.insert(0, countdown_bar)
117+
118+
print("Progress bars added. Starting demo...")
119+
120+
print("Using countdown bar")
121+
110122
for timer in range(countdown_bar.maximum, countdown_bar.minimum, -1):
111123
bar_color_to_set = (0x20 * (6 - timer) + 20, (0x20 * (timer - 1)) + 20, 0x10)
112124
countdown_bar.bar_color = bar_color_to_set
113125
countdown_bar.value = timer
114126
time.sleep(1)
115127

128+
print("Removing countdown bar")
129+
116130
countdown_bar.value = 0
117131
group.remove(countdown_bar)
118132

119133
progress_bar_value = 0.0
120134
progress_bar_incr = 3.0
121135

136+
button1 = digitalio.DigitalInOut(board.BUTTON_UP)
137+
button1.switch_to_input(digitalio.Pull.UP)
138+
button2 = digitalio.DigitalInOut(board.BUTTON_DOWN)
139+
button2.switch_to_input(digitalio.Pull.UP)
140+
141+
122142
print("Start forever loop")
123143
while True:
124144

145+
print("Setting progress bar value to", progress_bar_value)
146+
125147
progress_bar.value = progress_bar_value
148+
ranged_bar.value = progress_bar_value
126149
progress_bar_value += progress_bar_incr
127150

128151
if progress_bar_value > progress_bar.maximum:
@@ -133,4 +156,11 @@
133156
progress_bar_value = progress_bar.minimum
134157
progress_bar_incr *= -1
135158

159+
# button_pressed = False
160+
# while False == button_pressed:
161+
# button_pressed = button_pressed or not (button1.value and button2.value)
162+
# time.sleep(0.1)
163+
164+
# print("Step")
165+
button_pressed = False
136166
time.sleep(0.5)

0 commit comments

Comments
 (0)