From a531e9e55cab0aa62a5d5f35f4e9a5c11d10e336 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sat, 18 Jul 2020 19:27:15 -0500 Subject: [PATCH 1/3] trying to make use of vectorio instead of making a bitmap --- adafruit_progressbar.py | 85 ++++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 23 deletions(-) diff --git a/adafruit_progressbar.py b/adafruit_progressbar.py index 5619144..c940012 100755 --- a/adafruit_progressbar.py +++ b/adafruit_progressbar.py @@ -40,12 +40,14 @@ # imports import displayio +import vectorio __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/brentru/Adafruit_CircuitPython_ProgressBar.git" + # pylint: disable=too-many-arguments, too-few-public-methods -class ProgressBar(displayio.TileGrid): +class ProgressBar(displayio.Group): """A dynamic progress bar widget. :param int x: The x-position of the top left corner. @@ -63,18 +65,18 @@ class ProgressBar(displayio.TileGrid): # pylint: disable=invalid-name def __init__( - self, - x, - y, - width, - height, - progress=0.0, - bar_color=0x00FF00, - outline_color=0xFFFFFF, - stroke=1, + self, + x, + y, + width, + height, + progress=0.0, + bar_color=0x00FF00, + outline_color=0xFFFFFF, + stroke=1, ): assert isinstance(progress, float), "Progress must be a floating point value." - self._bitmap = displayio.Bitmap(width, height, 3) + self._palette = displayio.Palette(3) self._palette[0] = 0x0 self._palette[1] = outline_color @@ -84,9 +86,44 @@ def __init__( self._height = height self._progress_val = progress - self.progress = self._progress_val # draw outline rectangle + _outline_palette = displayio.Palette(2) + _outline_palette.make_transparent(0) + _outline_palette[1] = self._palette[1] + + self._outline_rect = vectorio.Polygon( + points=[(0, 0), (self._width, 0), (self._width, self._height), (0, self._height)]) + self._outline_rect_shape = vectorio.VectorShape(shape=self._outline_rect, x=x, y=y, + pixel_shader=_outline_palette) + + _inner_outline_palette = displayio.Palette(2) + _inner_outline_palette[1] = self._palette[0] + _inner_outline_palette.make_transparent(0) + self._inner_outline_rect = vectorio.Polygon( + points=[(0, 0), (self._width - 2, 0), (self._width - 2, self._height - 2), (0, self._height - 2)]) + self._inner_outline_rect_shape = vectorio.VectorShape(shape=self._inner_outline_rect, x=x + 1, y=y + 1, + pixel_shader=_inner_outline_palette) + + self._fill_bar_max = self._width - 4 + _fill_bar_palette = displayio.Palette(2) + _fill_bar_palette[1] = self._palette[2] + _fill_bar_palette.make_transparent(0) + self._fill_bar_rect = vectorio.Polygon( + points=[ + (0, 0), + (int(self._fill_bar_max * self._progress_val), 0), + (int(self._fill_bar_max * self._progress_val), self._height - 4), + (0, self._height - 4)]) + self._fill_bar_rect_shape = vectorio.VectorShape(shape=self._fill_bar_rect, x=x + 2, y=y + 2, + pixel_shader=_fill_bar_palette) + + super().__init__(max_size=3, scale=1, x=x, y=y) + self.append(self._outline_rect_shape) + self.append(self._inner_outline_rect_shape) + self.append(self._fill_bar_rect_shape) + + """ for _w in range(width): for line in range(stroke): self._bitmap[_w, line] = 1 @@ -95,7 +132,9 @@ def __init__( for line in range(stroke): self._bitmap[line, _h] = 1 self._bitmap[width - 1 - line, _h] = 1 + super().__init__(self._bitmap, pixel_shader=self._palette, x=x, y=y) + """ @property def progress(self): @@ -111,21 +150,21 @@ def progress(self, value): :param float value: Progress bar value. """ + assert value <= 1.0, "Progress value may not be > 100%" assert isinstance( value, float ), "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): - 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): - for _h in range(2, self._height - 2): - self._bitmap[_w, _h] = 2 - self._progress_val = value + + _new_points = [ + (0, 0), + (int(self._fill_bar_max * value), 0), + (int(self._fill_bar_max * value), self._height - 4), + (0, self._height - 4) + ] + print(_new_points) + self._fill_bar_rect.points = _new_points + @property def fill(self): From f5e7a600bbaf44b5f230ffc2a9b91c30c84a2af0 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Wed, 22 Jul 2020 21:59:45 -0500 Subject: [PATCH 2/3] vectorio changes working --- adafruit_progressbar.py | 93 +++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/adafruit_progressbar.py b/adafruit_progressbar.py index c940012..71a08aa 100755 --- a/adafruit_progressbar.py +++ b/adafruit_progressbar.py @@ -63,17 +63,17 @@ class ProgressBar(displayio.Group): """ - # pylint: disable=invalid-name + # pylint: disable=invalid-name,too-many-instance-attributes def __init__( - self, - x, - y, - width, - height, - progress=0.0, - bar_color=0x00FF00, - outline_color=0xFFFFFF, - stroke=1, + self, + x, + y, + width, + height, + progress=0.0, + bar_color=0x00FF00, + outline_color=0xFFFFFF, + stroke=1, ): assert isinstance(progress, float), "Progress must be a floating point value." @@ -86,26 +86,43 @@ def __init__( self._height = height self._progress_val = progress - + self._stroke = stroke # draw outline rectangle _outline_palette = displayio.Palette(2) _outline_palette.make_transparent(0) _outline_palette[1] = self._palette[1] self._outline_rect = vectorio.Polygon( - points=[(0, 0), (self._width, 0), (self._width, self._height), (0, self._height)]) - self._outline_rect_shape = vectorio.VectorShape(shape=self._outline_rect, x=x, y=y, - pixel_shader=_outline_palette) - + points=[ + (0, 0), + (self._width, 0), + (self._width, self._height), + (0, self._height), + ] + ) + self._outline_rect_shape = vectorio.VectorShape( + shape=self._outline_rect, x=0, y=0, pixel_shader=_outline_palette + ) + # draw inner outline rectangle _inner_outline_palette = displayio.Palette(2) _inner_outline_palette[1] = self._palette[0] _inner_outline_palette.make_transparent(0) self._inner_outline_rect = vectorio.Polygon( - points=[(0, 0), (self._width - 2, 0), (self._width - 2, self._height - 2), (0, self._height - 2)]) - self._inner_outline_rect_shape = vectorio.VectorShape(shape=self._inner_outline_rect, x=x + 1, y=y + 1, - pixel_shader=_inner_outline_palette) - - self._fill_bar_max = self._width - 4 + points=[ + (0, 0), + (self._width - 1 - stroke, 0), + (self._width - 1 - stroke, self._height - 1 - stroke), + (0, self._height - 1 - stroke), + ] + ) + self._inner_outline_rect_shape = vectorio.VectorShape( + shape=self._inner_outline_rect, + x=stroke // 2 + 1, + y=stroke // 2 + 1, + pixel_shader=_inner_outline_palette, + ) + # draw fill bar + self._fill_bar_max = self._width - 3 - stroke _fill_bar_palette = displayio.Palette(2) _fill_bar_palette[1] = self._palette[2] _fill_bar_palette.make_transparent(0) @@ -113,29 +130,25 @@ def __init__( points=[ (0, 0), (int(self._fill_bar_max * self._progress_val), 0), - (int(self._fill_bar_max * self._progress_val), self._height - 4), - (0, self._height - 4)]) - self._fill_bar_rect_shape = vectorio.VectorShape(shape=self._fill_bar_rect, x=x + 2, y=y + 2, - pixel_shader=_fill_bar_palette) + ( + int(self._fill_bar_max * self._progress_val), + self._height - 3 - stroke, + ), + (0, self._height - 3 - stroke), + ] + ) + self._fill_bar_rect_shape = vectorio.VectorShape( + shape=self._fill_bar_rect, + x=1 + (stroke // 2 + 1), + y=1 + (stroke // 2 + 1), + pixel_shader=_fill_bar_palette, + ) super().__init__(max_size=3, scale=1, x=x, y=y) self.append(self._outline_rect_shape) self.append(self._inner_outline_rect_shape) self.append(self._fill_bar_rect_shape) - """ - for _w in range(width): - for line in range(stroke): - self._bitmap[_w, line] = 1 - self._bitmap[_w, height - 1 - line] = 1 - for _h in range(height): - for line in range(stroke): - self._bitmap[line, _h] = 1 - self._bitmap[width - 1 - line, _h] = 1 - - super().__init__(self._bitmap, pixel_shader=self._palette, x=x, y=y) - """ - @property def progress(self): """The percentage of the progress bar expressed as a @@ -159,13 +172,11 @@ def progress(self, value): _new_points = [ (0, 0), (int(self._fill_bar_max * value), 0), - (int(self._fill_bar_max * value), self._height - 4), - (0, self._height - 4) + (int(self._fill_bar_max * value), self._height - 3 - self._stroke), + (0, self._height - 3 - self._stroke), ] - print(_new_points) self._fill_bar_rect.points = _new_points - @property def fill(self): """The fill of the progress bar. Can be a hex value for a color or ``None`` for From 4daf660285eb9ee242affa12bc4d5faeb30c9bd0 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Wed, 22 Jul 2020 22:33:50 -0500 Subject: [PATCH 3/3] add vectorio to mock imports for docs --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 111d888..7e536d7 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -20,7 +20,7 @@ # Uncomment the below if you use native CircuitPython modules such as # digitalio, micropython and busio. List the modules you use. Without it, the # autodoc module docs will fail to generate with a warning. -autodoc_mock_imports = ["displayio"] +autodoc_mock_imports = ["displayio", "vectorio"] intersphinx_mapping = { "python": ("https://docs.python.org/3.4", None),