From 89e804b08065c9000c6b89318bf07ef7dd843df8 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Tue, 26 Nov 2024 12:32:32 -0600 Subject: [PATCH 01/27] button_base annotations in place Added annotations for the various functions. Corrected some documentation errors as well, regarding what the selected_label actually does. Additionally added terminalio.FONT as the default font. --- adafruit_button/button_base.py | 72 ++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 8a7709c..9d76b11 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -22,9 +22,15 @@ """ from adafruit_display_text.bitmap_label import Label from displayio import Group +import terminalio +try: + from typing import Optional, Union, List, Tuple, Any + from fontio import FontProtocol +except ImportError: + pass -def _check_color(color): +def _check_color(color: Optional[Union[int, tuple[int, int, int]]]) -> Optional[int]: # if a tuple is supplied, convert it to a RGB number if isinstance(color, tuple): r, g, b = color @@ -36,31 +42,31 @@ class ButtonBase(Group): # pylint: disable=too-many-instance-attributes """Superclass for creating UI buttons for ``displayio``. - :param x: The x position of the button. - :param y: The y position of the button. - :param width: The width of the button in tiles. - :param height: The height of the button in tiles. - :param name: A name, or miscellaneous string that is stored on the button. - :param label: The text that appears inside the button. Defaults to not displaying the label. - :param label_font: The button label font. - :param label_color: The color of the button label text. Defaults to 0x0. - :param selected_label: Text that appears when selected + :param int x: The x position of the button. + :param int y: The y position of the button. + :param int width: The width of the button in tiles. + :param int height: The height of the button in tiles. + :param str name: A name, or miscellaneous string that is stored on the button. + :param str label: The text that appears inside the button. Defaults to not displaying the label. + :param FontProtocol label_font: The button label font. + :param int label_color: The color of the button label text. Defaults to 0x0. + :param selected_label: The color of button label text when the button is selected. """ def __init__( self, *, - x, - y, - width, - height, - name=None, - label=None, - label_font=None, - label_color=0x0, - selected_label=None, - label_scale=None - ): + x: int, + y: int, + width: int, + height: int, + name: Optional[str] = None, + label: Optional[str] = None, + label_font: Optional[FontProtocol] = None, + label_color: Optional[int] = 0x0, + selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + label_scale: Optional[int] = 1, + ) -> None: super().__init__(x=x, y=y) self.x = x self.y = y @@ -73,15 +79,15 @@ def __init__( self._label_color = label_color self._label_font = label_font self._selected_label = _check_color(selected_label) - self._label_scale = label_scale or 1 + self._label_scale = label_scale @property - def label(self): + def label(self) -> Optional[Tuple[str, str]]: """The text label of the button""" return getattr(self._label, "text", None) @label.setter - def label(self, newtext): + def label(self, newtext: str) -> None: if self._label and self and (self[-1] == self._label): self.pop() @@ -90,7 +96,7 @@ def label(self, newtext): return # nothing to do! if not self._label_font: - raise RuntimeError("Please provide label font") + self._label_font = terminalio.FONT self._label = Label(self._label_font, text=newtext, scale=self._label_scale) dims = list(self._label.bounding_box) dims[2] *= self._label.scale @@ -115,17 +121,17 @@ def label(self, newtext): if (self.selected_label is None) and (self._label_color is not None): self.selected_label = (~self._label_color) & 0xFFFFFF - def _subclass_selected_behavior(self, value): - # Subclasses should overide this! + def _subclass_selected_behavior(self, value: Optional[Any]) -> None: + # Subclasses should override this! pass @property - def selected(self): + def selected(self) -> bool: """Selected inverts the colors.""" return self._selected @selected.setter - def selected(self, value): + def selected(self, value: bool) -> None: if value == self._selected: return # bail now, nothing more to do self._selected = value @@ -140,20 +146,20 @@ def selected(self, value): self._subclass_selected_behavior(value) @property - def selected_label(self): + def selected_label(self) -> int: """The font color of the button when selected""" return self._selected_label @selected_label.setter - def selected_label(self, new_color): + def selected_label(self, new_color: int) -> None: self._selected_label = _check_color(new_color) @property - def label_color(self): + def label_color(self) -> int: """The font color of the button""" return self._label_color @label_color.setter - def label_color(self, new_color): + def label_color(self, new_color: int) -> None: self._label_color = _check_color(new_color) self._label.color = self._label_color From fdf13e9a170d8db5341cd79ad6019ca3680071f3 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:10:05 -0600 Subject: [PATCH 02/27] Add more annotations, improve documentation Added type annotations for button.py, as well as correcting/updating documentation. --- adafruit_button/button.py | 109 ++++++++++++++++++--------------- adafruit_button/button_base.py | 22 ++++--- 2 files changed, 73 insertions(+), 58 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 45c87f7..5f31705 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -26,36 +26,49 @@ from adafruit_display_shapes.roundrect import RoundRect from adafruit_button.button_base import ButtonBase, _check_color +try: + from typing import Optional, Union, Tuple, Any, List + from fontio import FontProtocol +except ImportError: + pass + __version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git" class Button(ButtonBase): # pylint: disable=too-many-instance-attributes, too-many-locals - """Helper class for creating UI buttons for ``displayio``. - - :param x: The x position of the button. - :param y: The y position of the button. - :param width: The width of the button in pixels. - :param height: The height of the button in pixels. - :param name: The name of the button. + """Helper class for creating UI buttons for ``displayio``. Provides the following + buttons: + RECT: A rectangular button. SHAWDOWRECT adds a drop shadow. + ROUNDRECT: A rectangular button with rounded corners. SHADOWROUNDRECT adds + a drop shadow. + + :param int x: The x position of the button. + :param int y: The y position of the button. + :param int width: The width of the button in pixels. + :param int height: The height of the button in pixels. + :param str name: The name of the button. :param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT. Defaults to RECT. - :param fill_color: The color to fill the button. Defaults to 0xFFFFFF. - :param outline_color: The color of the outline of the button. - :param label: The text that appears inside the button. Defaults to not displaying the label. - :param label_font: The button label font. - :param label_color: The color of the button label text. Defaults to 0x0. - :param selected_fill: Inverts the fill color. - :param selected_outline: Inverts the outline color. - :param selected_label: Inverts the label color. + :param int|Tuple(int, int, int) fill_color: The color to fill the button. Defaults to 0xFFFFFF. + :param int|Tuple(int, int, int) outline_color: The color of the outline of the button. + :param str label: The text that appears inside the button. Defaults to not displaying the label. + :param FontProtocol label_font: The button label font. Defaults to terminalio.FONT + :param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0. + :param int|Tuple(int, int, int) selected_fill: The fill color when the button is selected. + Defaults to the inverse of the fill_color. + :param int|Tuple(int, int, int) selected_outline: The outline color when the button is selected. + Defaults to the inverse of outline_color. + :param selected_label: The label color when the button is selected. + Defaults to inverting the label_color. """ - def _empty_self_group(self): + def _empty_self_group(self) -> None: while len(self) > 0: self.pop() - def _create_body(self): + def _create_body(self) -> None: if (self.outline_color is not None) or (self.fill_color is not None): if self.style == Button.RECT: self.body = Rect( @@ -117,21 +130,21 @@ def _create_body(self): def __init__( self, *, - x, - y, - width, - height, - name=None, - style=RECT, - fill_color=0xFFFFFF, - outline_color=0x0, - label=None, - label_font=None, - label_color=0x0, - selected_fill=None, - selected_outline=None, - selected_label=None, - label_scale=None + x: int, + y: int, + width: int, + height: int, + name: Optional[str] = None, + style = RECT, + fill_color: Optional[Union[int, tuple[int, int, int]]] = 0xFFFFFF, + outline_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, + label: Optional[str] = None, + label_font: Optional[FontProtocol] = None, + label_color: Optional[int] = 0x0, + selected_fill: Optional[Union[int, tuple[int, int , int]]] = None, + selected_outline: Optional[Union[int, tuple[int, int, int]]] = None, + selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + label_scale: Optional[int] = 1 ): super().__init__( x=x, @@ -167,7 +180,7 @@ def __init__( self.label = label - def _subclass_selected_behavior(self, value): + def _subclass_selected_behavior(self, value: Optional[Any]) -> None: if self._selected: new_fill = self.selected_fill new_out = self.selected_outline @@ -180,7 +193,7 @@ def _subclass_selected_behavior(self, value): self.body.outline = new_out @property - def group(self): + def group(self) -> "Button": """Return self for compatibility with old API.""" print( "Warning: The group property is being deprecated. " @@ -189,7 +202,7 @@ def group(self): ) return self - def contains(self, point): + def contains(self, point: List[int]) -> bool: """Used to determine if a point is contained within a button. For example, ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for determining that a button has been touched. @@ -199,56 +212,56 @@ def contains(self, point): ) @property - def fill_color(self): + def fill_color(self) -> Optional[int]: """The fill color of the button body""" return self._fill_color @fill_color.setter - def fill_color(self, new_color): + def fill_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: self._fill_color = _check_color(new_color) if not self.selected: self.body.fill = self._fill_color @property - def outline_color(self): + def outline_color(self) -> Optional[int]: """The outline color of the button body""" return self._outline_color @outline_color.setter - def outline_color(self, new_color): + def outline_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: self._outline_color = _check_color(new_color) if not self.selected: self.body.outline = self._outline_color @property - def selected_fill(self): + def selected_fill(self) -> Optional[int]: """The fill color of the button body when selected""" return self._selected_fill @selected_fill.setter - def selected_fill(self, new_color): + def selected_fill(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: self._selected_fill = _check_color(new_color) if self.selected: self.body.fill = self._selected_fill @property - def selected_outline(self): + def selected_outline(self) -> Optional[int]: """The outline color of the button body when selected""" return self._selected_outline @selected_outline.setter - def selected_outline(self, new_color): + def selected_outline(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: self._selected_outline = _check_color(new_color) if self.selected: self.body.outline = self._selected_outline @property - def width(self): + def width(self) -> int: """The width of the button""" return self._width @width.setter - def width(self, new_width): + def width(self, new_width: int) -> None: self._width = new_width self._empty_self_group() self._create_body() @@ -257,12 +270,12 @@ def width(self, new_width): self.label = self.label @property - def height(self): + def height(self) -> int: """The height of the button""" return self._height @height.setter - def height(self, new_height): + def height(self, new_height: int) -> None: self._height = new_height self._empty_self_group() self._create_body() @@ -270,7 +283,7 @@ def height(self, new_height): self.append(self.body) self.label = self.label - def resize(self, new_width, new_height): + def resize(self, new_width: int, new_height: int) -> None: """Resize the button to the new width and height given :param new_width int the desired width :param new_height int the desired height diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 9d76b11..d7bd329 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -24,7 +24,7 @@ from displayio import Group import terminalio try: - from typing import Optional, Union, List, Tuple, Any + from typing import Optional, Union, Tuple, Any from fontio import FontProtocol except ImportError: pass @@ -48,9 +48,10 @@ class ButtonBase(Group): :param int height: The height of the button in tiles. :param str name: A name, or miscellaneous string that is stored on the button. :param str label: The text that appears inside the button. Defaults to not displaying the label. - :param FontProtocol label_font: The button label font. - :param int label_color: The color of the button label text. Defaults to 0x0. - :param selected_label: The color of button label text when the button is selected. + :param FontProtocol label_font: The button label font. Defaults to terminalio.FONT + :param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0. + :param int|Tuple(int, int, int) selected_label: The color of button label text when the button is selected. + :param int label_scale: The scale factor used for the label. Defaults to 1. """ def __init__( @@ -127,7 +128,7 @@ def _subclass_selected_behavior(self, value: Optional[Any]) -> None: @property def selected(self) -> bool: - """Selected inverts the colors.""" + """Returns whether the button is selected.""" return self._selected @selected.setter @@ -146,20 +147,21 @@ def selected(self, value: bool) -> None: self._subclass_selected_behavior(value) @property - def selected_label(self) -> int: - """The font color of the button when selected""" + def selected_label(self) -> Optional[Union[int, tuple[int, int, int]]]: + """The font color of the button when selected. + If no color is specified it defaults to the inverse of the label_color""" return self._selected_label @selected_label.setter - def selected_label(self, new_color: int) -> None: + def selected_label(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: self._selected_label = _check_color(new_color) @property - def label_color(self) -> int: + def label_color(self) -> Optional[Union[int, tuple[int, int, int]]]: """The font color of the button""" return self._label_color @label_color.setter - def label_color(self, new_color: int) -> None: + def label_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: self._label_color = _check_color(new_color) self._label.color = self._label_color From 94e60a7b018b5ae46f9846fef3742f91bc987d8b Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Wed, 27 Nov 2024 15:25:21 -0600 Subject: [PATCH 03/27] Type annotations to SpriteButton, Further documentation changes. Completed type annotations for the sprite_button.py file as well as further improvements to documentation. --- adafruit_button/button.py | 4 +- adafruit_button/button_base.py | 13 ++++-- adafruit_button/sprite_button.py | 80 ++++++++++++++++++-------------- 3 files changed, 56 insertions(+), 41 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 5f31705..6940a10 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -53,8 +53,8 @@ class Button(ButtonBase): Defaults to RECT. :param int|Tuple(int, int, int) fill_color: The color to fill the button. Defaults to 0xFFFFFF. :param int|Tuple(int, int, int) outline_color: The color of the outline of the button. - :param str label: The text that appears inside the button. Defaults to not displaying the label. - :param FontProtocol label_font: The button label font. Defaults to terminalio.FONT + :param str label: The text that appears inside the button. + :param FontProtocol label_font: The button label font. Defaults to ''terminalio.FONT'' :param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0. :param int|Tuple(int, int, int) selected_fill: The fill color when the button is selected. Defaults to the inverse of the fill_color. diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index d7bd329..9131229 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -1,4 +1,5 @@ # SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries +# SPDX-FileCopyrightText: 2024 Channing Ramos # # SPDX-License-Identifier: MIT @@ -9,7 +10,7 @@ UI Buttons for displayio -* Author(s): Limor Fried +* Author(s): Limor Fried, Channing Ramos Implementation Notes -------------------- @@ -47,10 +48,12 @@ class ButtonBase(Group): :param int width: The width of the button in tiles. :param int height: The height of the button in tiles. :param str name: A name, or miscellaneous string that is stored on the button. - :param str label: The text that appears inside the button. Defaults to not displaying the label. - :param FontProtocol label_font: The button label font. Defaults to terminalio.FONT - :param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0. + :param str label: The text that appears inside the button. + :param FontProtocol label_font: The button label font. Defaults to ''terminalio.FONT'' + :param int|Tuple(int, int, int) label_color: The color of the button label text. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. :param int|Tuple(int, int, int) selected_label: The color of button label text when the button is selected. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color. :param int label_scale: The scale factor used for the label. Defaults to 1. """ @@ -83,7 +86,7 @@ def __init__( self._label_scale = label_scale @property - def label(self) -> Optional[Tuple[str, str]]: + def label(self) -> Optional[str]: """The text label of the button""" return getattr(self._label, "text", None) diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 67fd9ee..d9ab283 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -1,4 +1,5 @@ # SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries +# SPDX-FileCopyrightText: 2024 Channing Ramos # # SPDX-License-Identifier: MIT @@ -9,7 +10,7 @@ Bitmap 3x3 Spritesheet based UI Button for displayio -* Author(s): Tim Cocks +* Author(s): Tim Cocks, Channing Ramos Implementation Notes -------------------- @@ -24,40 +25,51 @@ from adafruit_imageload import load from adafruit_button.button_base import ButtonBase +try: + from typing import Optional, Union, Tuple, Any, List + from fontio import FontProtocol +except ImportError: + pass class SpriteButton(ButtonBase): - """Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``. - - :param x: The x position of the button. - :param y: The y position of the button. - :param width: The width of the button in tiles. - :param height: The height of the button in tiles. - :param name: A name, or miscellaneous string that is stored on the button. - :param label: The text that appears inside the button. Defaults to not displaying the label. - :param label_font: The button label font. - :param label_color: The color of the button label text. Defaults to 0x0. - :param selected_label: Text that appears when selected - :param string bmp_path: The path of the 3x3 spritesheet Bitmap file - :param string selected_bmp_path: The path of the 3x3 spritesheet Bitmap file to use when pressed - :param int or tuple transparent_index: Index(s) that will be made transparent on the Palette + """Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``. Compatible with any format + supported by ''adafruit_imageload''. + + :param int x: The x position of the button. + :param int y: The y position of the button. + :param int width: The width of the button in tiles. + :param int height: The height of the button in tiles. + :param Optional[str] name: A name, or miscellaneous string that is stored on the button. + :param Optional[str] label: The text that appears inside the button. + :param Optional[FontProtocol] label_font: The button label font. + :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. Accepts either + an integer or a tuple of 3 integers representing RGB values. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of the button label text when the button + is selected. Accepts either an integer or a tuple of 3 integers representing RGB values. Defaults to the inverse of + label_color. + :param str bmp_path: The path of the 3x3 spritesheet mage file + :param Optional[str] selected_bmp_path: The path of the 3x3 spritesheet image file to use when pressed + :param Optional[Union[int, Tuple]] transparent_index: Palette index(s) that will be set to transparent. PNG files have these index(s) + set automatically. Not compatible with JPG files. + :param Optional[int] label_scale: The scale multiplier of the button label. Defaults to 1. """ def __init__( self, *, - x, - y, - width, - height, - name=None, - label=None, - label_font=None, - label_color=0x0, - selected_label=None, - bmp_path=None, - selected_bmp_path=None, - transparent_index=None, - label_scale=None + x: int, + y: int, + width: int, + height: int, + name: Optional[str] = None, + label: Optional[str] = None, + label_font: Optional[FontProtocol] = None, + label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, + selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + bmp_path: str = None, + selected_bmp_path: Optional[str] = None, + transparent_index: Optional[Union[int, tuple]] = None, + label_scale: Optional[int] = 1 ): if bmp_path is None: raise ValueError("Please supply bmp_path. It cannot be None.") @@ -104,16 +116,16 @@ def __init__( self.label = label @property - def width(self): - """The width of the button""" + def width(self) -> int: + """The width of the button. Read-Only""" return self._width @property - def height(self): - """The height of the button""" + def height(self) -> int: + """The height of the button. Read-Only""" return self._height - def contains(self, point): + def contains(self, point: list[int]) -> bool: """Used to determine if a point is contained within a button. For example, ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for determining that a button has been touched. @@ -122,7 +134,7 @@ def contains(self, point): self.y <= point[1] <= self.y + self.height ) - def _subclass_selected_behavior(self, value): + def _subclass_selected_behavior(self, value: Optional[Any]) -> None: if self._selected: if self._selected_bmp is not None: self._btn_tilegrid.bitmap = self._selected_bmp From 86c6491aa69ab632b3b63e7ef0c0c6fa7dad91d7 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Wed, 27 Nov 2024 15:32:09 -0600 Subject: [PATCH 04/27] Annotations correction Corrected some wrong annotations. --- adafruit_button/button_base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 9131229..830dc13 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -150,21 +150,21 @@ def selected(self, value: bool) -> None: self._subclass_selected_behavior(value) @property - def selected_label(self) -> Optional[Union[int, tuple[int, int, int]]]: + def selected_label(self) -> int: """The font color of the button when selected. If no color is specified it defaults to the inverse of the label_color""" return self._selected_label @selected_label.setter - def selected_label(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: + def selected_label(self, new_color: Union[int, tuple[int, int, int]]) -> None: self._selected_label = _check_color(new_color) @property - def label_color(self) -> Optional[Union[int, tuple[int, int, int]]]: + def label_color(self) -> int: """The font color of the button""" return self._label_color @label_color.setter - def label_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: + def label_color(self, new_color: Union[int, tuple[int, int, int]]) -> None: self._label_color = _check_color(new_color) self._label.color = self._label_color From 11a570c3ab232f3f02bd6564c5b8cc3adcf783e9 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Wed, 27 Nov 2024 19:49:31 -0600 Subject: [PATCH 05/27] Second Documentation Pass --- adafruit_button/button_base.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 830dc13..8854b67 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -31,7 +31,7 @@ pass -def _check_color(color: Optional[Union[int, tuple[int, int, int]]]) -> Optional[int]: +def _check_color(color: Optional[Union[int, tuple[int, int, int]]]) -> int: # if a tuple is supplied, convert it to a RGB number if isinstance(color, tuple): r, g, b = color @@ -47,14 +47,13 @@ class ButtonBase(Group): :param int y: The y position of the button. :param int width: The width of the button in tiles. :param int height: The height of the button in tiles. - :param str name: A name, or miscellaneous string that is stored on the button. - :param str label: The text that appears inside the button. - :param FontProtocol label_font: The button label font. Defaults to ''terminalio.FONT'' - :param int|Tuple(int, int, int) label_color: The color of the button label text. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. - :param int|Tuple(int, int, int) selected_label: The color of button label text when the button is selected. + :param Optional[str] name: A name, or miscellaneous string that is stored on the button. + :param Optional[str] label: The text that appears inside the button. + :param Optional[FontProtocol] label_font: The button label font. Defaults to ''terminalio.FONT'' + :param Optional[int] label_color: The color of the button label text. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of button label text when the button is selected. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color. - :param int label_scale: The scale factor used for the label. Defaults to 1. + :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1. """ def __init__( From ae6ced5c49781daa75d55614be6adf65cb33f774 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Wed, 27 Nov 2024 19:56:30 -0600 Subject: [PATCH 06/27] Updated label_color for compatibility label_color now accepts a tuple as well as an int for color, bringing its behavior in line with selected_label --- adafruit_button/button_base.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 8854b67..54ed1c6 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -50,7 +50,8 @@ class ButtonBase(Group): :param Optional[str] name: A name, or miscellaneous string that is stored on the button. :param Optional[str] label: The text that appears inside the button. :param Optional[FontProtocol] label_font: The button label font. Defaults to ''terminalio.FONT'' - :param Optional[int] label_color: The color of the button label text. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. Defaults to 0x0. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color. :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of button label text when the button is selected. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color. :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1. @@ -66,7 +67,7 @@ def __init__( name: Optional[str] = None, label: Optional[str] = None, label_font: Optional[FontProtocol] = None, - label_color: Optional[int] = 0x0, + label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, selected_label: Optional[Union[int, tuple[int, int, int]]] = None, label_scale: Optional[int] = 1, ) -> None: @@ -79,7 +80,7 @@ def __init__( self._selected = False self.name = name self._label = label - self._label_color = label_color + self._label_color = _check_color(label_color) self._label_font = label_font self._selected_label = _check_color(selected_label) self._label_scale = label_scale From 252cf7d4a3e0a645514f0ada2d17a6e897845978 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 13:59:42 -0600 Subject: [PATCH 07/27] Button.py second pass Second pass to finalize changes --- adafruit_button/button.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 6940a10..b15d683 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -48,20 +48,24 @@ class Button(ButtonBase): :param int y: The y position of the button. :param int width: The width of the button in pixels. :param int height: The height of the button in pixels. - :param str name: The name of the button. + :param Optional[str] name: The name of the button. :param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT. Defaults to RECT. - :param int|Tuple(int, int, int) fill_color: The color to fill the button. Defaults to 0xFFFFFF. - :param int|Tuple(int, int, int) outline_color: The color of the outline of the button. - :param str label: The text that appears inside the button. - :param FontProtocol label_font: The button label font. Defaults to ''terminalio.FONT'' - :param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0. - :param int|Tuple(int, int, int) selected_fill: The fill color when the button is selected. - Defaults to the inverse of the fill_color. - :param int|Tuple(int, int, int) selected_outline: The outline color when the button is selected. - Defaults to the inverse of outline_color. - :param selected_label: The label color when the button is selected. - Defaults to inverting the label_color. + :param Optional[Union[int, Tuple[int, int, int]]] fill_color: The color to fill the button. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0xFFFFFF. + :param Optional[Union[int, Tuple[int, int, int]]] outline_color: The color of the outline of the button. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. + :param Optional[str] label: The text that appears inside the button. + :param Optional[FontProtocol] label_font: The button label font. Defaults to ''terminalio.FONT'' + :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] selected_fill: The fill color when the button is selected. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to the inverse of the fill_color. + :param Optional[Union[int, Tuple[int, int, int]]] selected_outline: The outline color when the button is selected. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to the inverse of outline_color. + :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The label color when the button is selected. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to inverting the label_color. + :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1. """ def _empty_self_group(self) -> None: @@ -140,7 +144,7 @@ def __init__( outline_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, label: Optional[str] = None, label_font: Optional[FontProtocol] = None, - label_color: Optional[int] = 0x0, + label_color: Optional[Union[int, tuple[int, int , int]]] = 0x0, selected_fill: Optional[Union[int, tuple[int, int , int]]] = None, selected_outline: Optional[Union[int, tuple[int, int, int]]] = None, selected_label: Optional[Union[int, tuple[int, int, int]]] = None, @@ -217,7 +221,7 @@ def fill_color(self) -> Optional[int]: return self._fill_color @fill_color.setter - def fill_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: + def fill_color(self, new_color: Union[int, tuple[int, int, int]]) -> None: self._fill_color = _check_color(new_color) if not self.selected: self.body.fill = self._fill_color @@ -228,7 +232,7 @@ def outline_color(self) -> Optional[int]: return self._outline_color @outline_color.setter - def outline_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: + def outline_color(self, new_color: Union[int, tuple[int, int, int]]) -> None: self._outline_color = _check_color(new_color) if not self.selected: self.body.outline = self._outline_color @@ -239,7 +243,7 @@ def selected_fill(self) -> Optional[int]: return self._selected_fill @selected_fill.setter - def selected_fill(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: + def selected_fill(self, new_color: Union[int, tuple[int, int, int]]) -> None: self._selected_fill = _check_color(new_color) if self.selected: self.body.fill = self._selected_fill @@ -250,7 +254,7 @@ def selected_outline(self) -> Optional[int]: return self._selected_outline @selected_outline.setter - def selected_outline(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: + def selected_outline(self, new_color: Union[int, tuple[int, int, int]]) -> None: self._selected_outline = _check_color(new_color) if self.selected: self.body.outline = self._selected_outline From acb718a1b58c90b8551d4174bb61f79a672c8b90 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 14:02:58 -0600 Subject: [PATCH 08/27] Fixed transparent_index The transparent index is now set to the index number given, rather than just using 0. --- adafruit_button/sprite_button.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index d9ab283..8dabb80 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -100,7 +100,7 @@ def __init__( for _index in transparent_index: self._selected_bmp_palette.make_transparent(_index) elif isinstance(transparent_index, int): - self._selected_bmp_palette.make_transparent(0) + self._selected_bmp_palette.make_transparent(transparent_index) self._btn_tilegrid = inflate_tilegrid( bmp_obj=self._bmp, From a11ed732f2d16678eaebfaa7d78a60869f40aa4d Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:22:09 -0600 Subject: [PATCH 09/27] Update Licensing Added my name to the licensing portion. --- adafruit_button/button.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index b15d683..d86a7b2 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -1,4 +1,5 @@ # SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries +# SPDX-FileCopyrightText: 2024 Channing Ramos # # SPDX-License-Identifier: MIT @@ -9,7 +10,7 @@ UI Buttons for displayio -* Author(s): Limor Fried +* Author(s): Limor Fried, Channing Ramos Implementation Notes -------------------- From b2759a25e0897879e3a6f949d6005bced183dcd2 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:47:32 -0600 Subject: [PATCH 10/27] Formatting Correction - button.py Correct formatting errors for the button.py file to pass ruff checks. --- adafruit_button/button.py | 43 +++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index d86a7b2..333b2eb 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -54,18 +54,21 @@ class Button(ButtonBase): Defaults to RECT. :param Optional[Union[int, Tuple[int, int, int]]] fill_color: The color to fill the button. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0xFFFFFF. - :param Optional[Union[int, Tuple[int, int, int]]] outline_color: The color of the outline of the button. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] outline_color: The color of the outline of the + button. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. :param Optional[str] label: The text that appears inside the button. :param Optional[FontProtocol] label_font: The button label font. Defaults to ''terminalio.FONT'' - :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. - :param Optional[Union[int, Tuple[int, int, int]]] selected_fill: The fill color when the button is selected. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to the inverse of the fill_color. - :param Optional[Union[int, Tuple[int, int, int]]] selected_outline: The outline color when the button is selected. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to the inverse of outline_color. - :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The label color when the button is selected. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to inverting the label_color. + :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label + text. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] selected_fill: The fill color when the + button is selected. Accepts an int or a tuple of 3 integers representing RGB values. + Defaults to the inverse of the fill_color. + :param Optional[Union[int, Tuple[int, int, int]]] selected_outline: The outline color when the + button is selected. Accepts an int or a tuple of 3 integers representing RGB values. + Defaults to the inverse of outline_color. + :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The label color when the + button is selected. Accepts an int or a tuple of 3 integers representing RGB values. + Defaults to inverting the label_color. :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1. """ @@ -141,14 +144,14 @@ def __init__( height: int, name: Optional[str] = None, style = RECT, - fill_color: Optional[Union[int, tuple[int, int, int]]] = 0xFFFFFF, - outline_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, + fill_color: Optional[Union[int, Tuple[int, int, int]]] = 0xFFFFFF, + outline_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0, label: Optional[str] = None, label_font: Optional[FontProtocol] = None, - label_color: Optional[Union[int, tuple[int, int , int]]] = 0x0, - selected_fill: Optional[Union[int, tuple[int, int , int]]] = None, - selected_outline: Optional[Union[int, tuple[int, int, int]]] = None, - selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + label_color: Optional[Union[int, Tuple[int, int , int]]] = 0x0, + selected_fill: Optional[Union[int, Tuple[int, int , int]]] = None, + selected_outline: Optional[Union[int, Tuple[int, int, int]]] = None, + selected_label: Optional[Union[int, Tuple[int, int, int]]] = None, label_scale: Optional[int] = 1 ): super().__init__( @@ -222,7 +225,7 @@ def fill_color(self) -> Optional[int]: return self._fill_color @fill_color.setter - def fill_color(self, new_color: Union[int, tuple[int, int, int]]) -> None: + def fill_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._fill_color = _check_color(new_color) if not self.selected: self.body.fill = self._fill_color @@ -233,7 +236,7 @@ def outline_color(self) -> Optional[int]: return self._outline_color @outline_color.setter - def outline_color(self, new_color: Union[int, tuple[int, int, int]]) -> None: + def outline_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._outline_color = _check_color(new_color) if not self.selected: self.body.outline = self._outline_color @@ -244,7 +247,7 @@ def selected_fill(self) -> Optional[int]: return self._selected_fill @selected_fill.setter - def selected_fill(self, new_color: Union[int, tuple[int, int, int]]) -> None: + def selected_fill(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._selected_fill = _check_color(new_color) if self.selected: self.body.fill = self._selected_fill @@ -255,7 +258,7 @@ def selected_outline(self) -> Optional[int]: return self._selected_outline @selected_outline.setter - def selected_outline(self, new_color: Union[int, tuple[int, int, int]]) -> None: + def selected_outline(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._selected_outline = _check_color(new_color) if self.selected: self.body.outline = self._selected_outline From 2dd2d8ba1a6e4ab625d1761a4754503bf5c8a6c2 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:54:49 -0600 Subject: [PATCH 11/27] Formatting corrections for button_base.py Correct formatting to avoid ruff errors. --- adafruit_button/button_base.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 54ed1c6..907aaf7 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -49,11 +49,13 @@ class ButtonBase(Group): :param int height: The height of the button in tiles. :param Optional[str] name: A name, or miscellaneous string that is stored on the button. :param Optional[str] label: The text that appears inside the button. - :param Optional[FontProtocol] label_font: The button label font. Defaults to ''terminalio.FONT'' - :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. Defaults to 0x0. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color. - :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of button label text when the button is selected. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color. + :param Optional[FontProtocol] label_font: The button label font. + Defaults to ''terminalio.FONT'' + :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the label text. + Defaults to 0x0. Accepts an int or a tuple of 3 integers representing RGB values. + :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of the label text + when the button is selected. Accepts an int or a tuple of 3 integers representing RGB values. + Defaults to an inverse of label_color. :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1. """ @@ -67,8 +69,8 @@ def __init__( name: Optional[str] = None, label: Optional[str] = None, label_font: Optional[FontProtocol] = None, - label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, - selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + label_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0, + selected_label: Optional[Union[int, Tuple[int, int, int]]] = None, label_scale: Optional[int] = 1, ) -> None: super().__init__(x=x, y=y) @@ -156,7 +158,7 @@ def selected_label(self) -> int: return self._selected_label @selected_label.setter - def selected_label(self, new_color: Union[int, tuple[int, int, int]]) -> None: + def selected_label(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._selected_label = _check_color(new_color) @property @@ -165,6 +167,6 @@ def label_color(self) -> int: return self._label_color @label_color.setter - def label_color(self, new_color: Union[int, tuple[int, int, int]]) -> None: + def label_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._label_color = _check_color(new_color) self._label.color = self._label_color From 3f1434dcdc8faa1bb5067010f352b18ced3e3308 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:03:00 -0600 Subject: [PATCH 12/27] Formatting for sprite_button Resolving ruff errors for sprite_button.py, some minor formatting elsewhere. --- adafruit_button/button.py | 19 ++++++++++--------- adafruit_button/sprite_button.py | 29 +++++++++++++++-------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 333b2eb..7eec017 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -53,22 +53,23 @@ class Button(ButtonBase): :param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT. Defaults to RECT. :param Optional[Union[int, Tuple[int, int, int]]] fill_color: The color to fill the button. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0xFFFFFF. - :param Optional[Union[int, Tuple[int, int, int]]] outline_color: The color of the outline of the - button. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0xFFFFFF. + :param Optional[Union[int, Tuple[int, int, int]]] outline_color: The color of the outline of + the button. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. :param Optional[str] label: The text that appears inside the button. - :param Optional[FontProtocol] label_font: The button label font. Defaults to ''terminalio.FONT'' + :param Optional[FontProtocol] label_font: The button label font. Defaults to + ''terminalio.FONT'' :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label - text. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. + text. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. :param Optional[Union[int, Tuple[int, int, int]]] selected_fill: The fill color when the - button is selected. Accepts an int or a tuple of 3 integers representing RGB values. - Defaults to the inverse of the fill_color. + button is selected. Accepts an int or a tuple of 3 integers representing RGB values. + Defaults to the inverse of the fill_color. :param Optional[Union[int, Tuple[int, int, int]]] selected_outline: The outline color when the button is selected. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to the inverse of outline_color. :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The label color when the - button is selected. Accepts an int or a tuple of 3 integers representing RGB values. - Defaults to inverting the label_color. + button is selected. Accepts an int or a tuple of 3 integers representing RGB values. + Defaults to inverting the label_color. :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1. """ diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 8dabb80..25119a4 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -32,8 +32,8 @@ pass class SpriteButton(ButtonBase): - """Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``. Compatible with any format - supported by ''adafruit_imageload''. + """Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``. + Compatible with any format supported by ''adafruit_imageload''. :param int x: The x position of the button. :param int y: The y position of the button. @@ -42,15 +42,16 @@ class SpriteButton(ButtonBase): :param Optional[str] name: A name, or miscellaneous string that is stored on the button. :param Optional[str] label: The text that appears inside the button. :param Optional[FontProtocol] label_font: The button label font. - :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. Accepts either - an integer or a tuple of 3 integers representing RGB values. Defaults to 0x0. - :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of the button label text when the button - is selected. Accepts either an integer or a tuple of 3 integers representing RGB values. Defaults to the inverse of - label_color. + :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the label text. + Accepts either an integer or a tuple of 3 integers representing RGB values. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of the button label + text when the button is selected. Accepts either an integer or a tuple of 3 integers + representing RGB values. Defaults to the inverse of label_color. :param str bmp_path: The path of the 3x3 spritesheet mage file - :param Optional[str] selected_bmp_path: The path of the 3x3 spritesheet image file to use when pressed - :param Optional[Union[int, Tuple]] transparent_index: Palette index(s) that will be set to transparent. PNG files have these index(s) - set automatically. Not compatible with JPG files. + :param Optional[str] selected_bmp_path: The path of the 3x3 spritesheet image file to use when + pressed + :param Optional[Union[int, Tuple]] transparent_index: Palette index(s) that will be set to + transparent. PNG files have these index(s) set automatically. Not compatible with JPG files. :param Optional[int] label_scale: The scale multiplier of the button label. Defaults to 1. """ @@ -64,11 +65,11 @@ def __init__( name: Optional[str] = None, label: Optional[str] = None, label_font: Optional[FontProtocol] = None, - label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, - selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + label_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0, + selected_label: Optional[Union[int, Tuple[int, int, int]]] = None, bmp_path: str = None, selected_bmp_path: Optional[str] = None, - transparent_index: Optional[Union[int, tuple]] = None, + transparent_index: Optional[Union[int, Tuple]] = None, label_scale: Optional[int] = 1 ): if bmp_path is None: @@ -125,7 +126,7 @@ def height(self) -> int: """The height of the button. Read-Only""" return self._height - def contains(self, point: list[int]) -> bool: + def contains(self, point: List[int]) -> bool: """Used to determine if a point is contained within a button. For example, ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for determining that a button has been touched. From a3ba72102d1caa7ccdccf48b9e3f16530ac79f73 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:13:31 -0600 Subject: [PATCH 13/27] CRLF to LF line ending conversion PyCharm has a mind of its own for settings. --- adafruit_button/button.py | 6 +++--- adafruit_button/button_base.py | 2 +- adafruit_button/sprite_button.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 7eec017..5bafa3e 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -294,8 +294,8 @@ def height(self, new_height: int) -> None: def resize(self, new_width: int, new_height: int) -> None: """Resize the button to the new width and height given - :param new_width int the desired width - :param new_height int the desired height + :param int new_width: The desired width in pixels. + :param int new_height: he desired height in pixels. """ self._width = new_width self._height = new_height @@ -303,4 +303,4 @@ def resize(self, new_width: int, new_height: int) -> None: self._create_body() if self.body: self.append(self.body) - self.label = self.label + self.label = self.label \ No newline at end of file diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 907aaf7..0c84237 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -169,4 +169,4 @@ def label_color(self) -> int: @label_color.setter def label_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._label_color = _check_color(new_color) - self._label.color = self._label_color + self._label.color = self._label_color \ No newline at end of file diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 25119a4..389fe95 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -142,4 +142,4 @@ def _subclass_selected_behavior(self, value: Optional[Any]) -> None: self._btn_tilegrid.pixel_shader = self._selected_bmp_palette else: self._btn_tilegrid.bitmap = self._bmp - self._btn_tilegrid.pixel_shader = self._bmp_palette + self._btn_tilegrid.pixel_shader = self._bmp_palette \ No newline at end of file From db3d9e5e4abf834a345c530ca1c64226bd66097d Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:19:00 -0600 Subject: [PATCH 14/27] Black formatting Ran against local installation of black after correcting my own config. --- adafruit_button/button.py | 8 ++++---- adafruit_button/button_base.py | 3 ++- adafruit_button/sprite_button.py | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 5bafa3e..3a8bdd3 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -144,13 +144,13 @@ def __init__( width: int, height: int, name: Optional[str] = None, - style = RECT, + style=RECT, fill_color: Optional[Union[int, Tuple[int, int, int]]] = 0xFFFFFF, outline_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0, label: Optional[str] = None, label_font: Optional[FontProtocol] = None, - label_color: Optional[Union[int, Tuple[int, int , int]]] = 0x0, - selected_fill: Optional[Union[int, Tuple[int, int , int]]] = None, + label_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0, + selected_fill: Optional[Union[int, Tuple[int, int, int]]] = None, selected_outline: Optional[Union[int, Tuple[int, int, int]]] = None, selected_label: Optional[Union[int, Tuple[int, int, int]]] = None, label_scale: Optional[int] = 1 @@ -303,4 +303,4 @@ def resize(self, new_width: int, new_height: int) -> None: self._create_body() if self.body: self.append(self.body) - self.label = self.label \ No newline at end of file + self.label = self.label diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 0c84237..58f429b 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -24,6 +24,7 @@ from adafruit_display_text.bitmap_label import Label from displayio import Group import terminalio + try: from typing import Optional, Union, Tuple, Any from fontio import FontProtocol @@ -169,4 +170,4 @@ def label_color(self) -> int: @label_color.setter def label_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._label_color = _check_color(new_color) - self._label.color = self._label_color \ No newline at end of file + self._label.color = self._label_color diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 389fe95..3b5feff 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -31,6 +31,7 @@ except ImportError: pass + class SpriteButton(ButtonBase): """Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``. Compatible with any format supported by ''adafruit_imageload''. @@ -142,4 +143,4 @@ def _subclass_selected_behavior(self, value: Optional[Any]) -> None: self._btn_tilegrid.pixel_shader = self._selected_bmp_palette else: self._btn_tilegrid.bitmap = self._bmp - self._btn_tilegrid.pixel_shader = self._bmp_palette \ No newline at end of file + self._btn_tilegrid.pixel_shader = self._bmp_palette From a00436d37cc1f832332a51893b4d08354a09a0bf Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:48:08 -0600 Subject: [PATCH 15/27] Allow setting label colors with Tuples Corrects issues with converting tuples of RGB values to ints, which Label objects require. --- adafruit_button/button.py | 4 ++-- adafruit_button/button_base.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 3a8bdd3..1ad1aa0 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -179,9 +179,9 @@ def __init__( self._selected_outline = _check_color(selected_outline) if self.selected_fill is None and fill_color is not None: - self.selected_fill = (~self._fill_color) & 0xFFFFFF + self.selected_fill = (~_check_color(self._fill_color)) & 0xFFFFFF if self.selected_outline is None and outline_color is not None: - self.selected_outline = (~self._outline_color) & 0xFFFFFF + self.selected_outline = (~_check_color(self._outline_color)) & 0xFFFFFF self._create_body() if self.body: diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 58f429b..167a667 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -126,7 +126,7 @@ def label(self, newtext: str) -> None: self.append(self._label) if (self.selected_label is None) and (self._label_color is not None): - self.selected_label = (~self._label_color) & 0xFFFFFF + self.selected_label = (~_check_color(self._label_color)) & 0xFFFFFF def _subclass_selected_behavior(self, value: Optional[Any]) -> None: # Subclasses should override this! From fd29d0f496920039da9d68307b6ff0bd2ea4a210 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 17:34:40 -0600 Subject: [PATCH 16/27] Update conf.py Point it to the new builtin libraries used, terminalio and fontio. --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 8e24264..56878d6 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -27,7 +27,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", "bitmaptools"] +autodoc_mock_imports = ["displayio", "bitmaptools", "terminalio", "fontio"] intersphinx_mapping = { From 044c03acbf34fe17f6f2691250ea2351a1682249 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:53:21 -0600 Subject: [PATCH 17/27] Exposed Name attribute The name attribute is not a property that can be read or set. --- adafruit_button/button_base.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 8a7709c..c8cc35d 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -68,7 +68,7 @@ def __init__( self._height = height self._font = label_font self._selected = False - self.name = name + self._name = name self._label = label self._label_color = label_color self._label_font = label_font @@ -157,3 +157,12 @@ def label_color(self): def label_color(self, new_color): self._label_color = _check_color(new_color) self._label.color = self._label_color + + @property + def name(self) -> str: + """The name of the button""" + return self._name + + @name.setter + def name(self, new_name: str) -> None: + self._name = new_name From cf32fd3d3baf8527698c7ead98c7472cee29ecd5 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 4 Dec 2024 14:55:05 -0600 Subject: [PATCH 18/27] type annotations --- adafruit_button/button.py | 71 ++++++++++++++++++-------------- adafruit_button/button_base.py | 42 +++++++++++-------- adafruit_button/sprite_button.py | 40 ++++++++++-------- 3 files changed, 86 insertions(+), 67 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 45c87f7..22ffe55 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -26,6 +26,13 @@ from adafruit_display_shapes.roundrect import RoundRect from adafruit_button.button_base import ButtonBase, _check_color +try: + from typing import Optional, Union + from fontio import FontProtocol + from displayio import Group +except ImportError: + pass + __version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git" @@ -117,22 +124,22 @@ def _create_body(self): def __init__( self, *, - x, - y, - width, - height, - name=None, - style=RECT, - fill_color=0xFFFFFF, - outline_color=0x0, - label=None, - label_font=None, - label_color=0x0, - selected_fill=None, - selected_outline=None, - selected_label=None, - label_scale=None - ): + x: int, + y: int, + width: int, + height: int, + name: Optional[str] = None, + style: int = RECT, + fill_color: Optional[Union[int, tuple[int, int, int]]] = 0xFFFFFF, + outline_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, + label: Optional[str] = None, + label_font: Optional[FontProtocol] = None, + label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, + selected_fill: Optional[Union[int, tuple[int, int, int]]] = None, + selected_outline: Optional[Union[int, tuple[int, int, int]]] = None, + selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + label_scale: Optional[int] = None + ) -> None: super().__init__( x=x, y=y, @@ -167,7 +174,7 @@ def __init__( self.label = label - def _subclass_selected_behavior(self, value): + def _subclass_selected_behavior(self, value: bool) -> None: if self._selected: new_fill = self.selected_fill new_out = self.selected_outline @@ -180,7 +187,7 @@ def _subclass_selected_behavior(self, value): self.body.outline = new_out @property - def group(self): + def group(self) -> Group: """Return self for compatibility with old API.""" print( "Warning: The group property is being deprecated. " @@ -189,7 +196,7 @@ def group(self): ) return self - def contains(self, point): + def contains(self, point: tuple[int, int]) -> bool: """Used to determine if a point is contained within a button. For example, ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for determining that a button has been touched. @@ -199,56 +206,56 @@ def contains(self, point): ) @property - def fill_color(self): + def fill_color(self) -> int: """The fill color of the button body""" return self._fill_color @fill_color.setter - def fill_color(self, new_color): + def fill_color(self, new_color: int) -> None: self._fill_color = _check_color(new_color) if not self.selected: self.body.fill = self._fill_color @property - def outline_color(self): + def outline_color(self) -> int: """The outline color of the button body""" return self._outline_color @outline_color.setter - def outline_color(self, new_color): + def outline_color(self, new_color: int) -> None: self._outline_color = _check_color(new_color) if not self.selected: self.body.outline = self._outline_color @property - def selected_fill(self): + def selected_fill(self) -> int: """The fill color of the button body when selected""" return self._selected_fill @selected_fill.setter - def selected_fill(self, new_color): + def selected_fill(self, new_color: int) -> None: self._selected_fill = _check_color(new_color) if self.selected: self.body.fill = self._selected_fill @property - def selected_outline(self): + def selected_outline(self) -> int: """The outline color of the button body when selected""" return self._selected_outline @selected_outline.setter - def selected_outline(self, new_color): + def selected_outline(self, new_color: int) -> None: self._selected_outline = _check_color(new_color) if self.selected: self.body.outline = self._selected_outline @property - def width(self): + def width(self) -> int: """The width of the button""" return self._width @width.setter - def width(self, new_width): + def width(self, new_width: int) -> None: self._width = new_width self._empty_self_group() self._create_body() @@ -257,12 +264,12 @@ def width(self, new_width): self.label = self.label @property - def height(self): + def height(self) -> int: """The height of the button""" return self._height @height.setter - def height(self, new_height): + def height(self, new_height: int) -> None: self._height = new_height self._empty_self_group() self._create_body() @@ -270,7 +277,7 @@ def height(self, new_height): self.append(self.body) self.label = self.label - def resize(self, new_width, new_height): + def resize(self, new_width: int, new_height: int) -> None: """Resize the button to the new width and height given :param new_width int the desired width :param new_height int the desired height diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 8a7709c..c906c90 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -23,6 +23,12 @@ from adafruit_display_text.bitmap_label import Label from displayio import Group +try: + from typing import Optional, Union + from fontio import FontProtocol +except ImportError: + pass + def _check_color(color): # if a tuple is supplied, convert it to a RGB number @@ -50,16 +56,16 @@ class ButtonBase(Group): def __init__( self, *, - x, - y, - width, - height, - name=None, - label=None, - label_font=None, - label_color=0x0, - selected_label=None, - label_scale=None + x: int, + y: int, + width: int, + height: int, + name: Optional[str] = None, + label: Optional[str] = None, + label_font: Optional[FontProtocol] = None, + label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, + selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + label_scale: Optional[int] = None ): super().__init__(x=x, y=y) self.x = x @@ -76,12 +82,12 @@ def __init__( self._label_scale = label_scale or 1 @property - def label(self): + def label(self) -> str: """The text label of the button""" return getattr(self._label, "text", None) @label.setter - def label(self, newtext): + def label(self, newtext: str) -> None: if self._label and self and (self[-1] == self._label): self.pop() @@ -120,12 +126,12 @@ def _subclass_selected_behavior(self, value): pass @property - def selected(self): + def selected(self) -> bool: """Selected inverts the colors.""" return self._selected @selected.setter - def selected(self, value): + def selected(self, value: bool) -> None: if value == self._selected: return # bail now, nothing more to do self._selected = value @@ -140,20 +146,20 @@ def selected(self, value): self._subclass_selected_behavior(value) @property - def selected_label(self): + def selected_label(self) -> int: """The font color of the button when selected""" return self._selected_label @selected_label.setter - def selected_label(self, new_color): + def selected_label(self, new_color: int) -> None: self._selected_label = _check_color(new_color) @property - def label_color(self): + def label_color(self) -> int: """The font color of the button""" return self._label_color @label_color.setter - def label_color(self, new_color): + def label_color(self, new_color: int): self._label_color = _check_color(new_color) self._label.color = self._label_color diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 67fd9ee..f1ca06e 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -24,6 +24,12 @@ from adafruit_imageload import load from adafruit_button.button_base import ButtonBase +try: + from typing import Optional, Union, Tuple + from fontio import FontProtocol +except ImportError: + pass + class SpriteButton(ButtonBase): """Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``. @@ -45,19 +51,19 @@ class SpriteButton(ButtonBase): def __init__( self, *, - x, - y, - width, - height, - name=None, - label=None, - label_font=None, - label_color=0x0, - selected_label=None, - bmp_path=None, - selected_bmp_path=None, - transparent_index=None, - label_scale=None + x: int, + y: int, + width: int, + height: int, + name: Optional[str] = None, + label: Optional[str] = None, + label_font: Optional[FontProtocol] = None, + label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, + selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + bmp_path: Optional[str] = None, + selected_bmp_path: Optional[str] = None, + transparent_index: Optional[int] = None, + label_scale: Optional[int] = None ): if bmp_path is None: raise ValueError("Please supply bmp_path. It cannot be None.") @@ -104,16 +110,16 @@ def __init__( self.label = label @property - def width(self): + def width(self) -> int: """The width of the button""" return self._width @property - def height(self): + def height(self) -> int: """The height of the button""" return self._height - def contains(self, point): + def contains(self, point: Tuple[int, int]): """Used to determine if a point is contained within a button. For example, ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for determining that a button has been touched. @@ -122,7 +128,7 @@ def contains(self, point): self.y <= point[1] <= self.y + self.height ) - def _subclass_selected_behavior(self, value): + def _subclass_selected_behavior(self, value: bool) -> None: if self._selected: if self._selected_bmp is not None: self._btn_tilegrid.bitmap = self._selected_bmp From daf6bc2405e208cf2a560f07b65300dce29d4c3b Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 4 Dec 2024 15:29:24 -0600 Subject: [PATCH 19/27] use ruff --- .gitattributes | 11 +++ .pre-commit-config.yaml | 42 +++----- README.rst | 6 +- adafruit_button/__init__.py | 1 + adafruit_button/button.py | 15 ++- adafruit_button/button_base.py | 17 ++-- adafruit_button/sprite_button.py | 9 +- docs/conf.py | 8 +- examples/display_button_color_properties.py | 3 +- examples/display_button_customfont.py | 4 +- examples/display_button_simpletest.py | 3 +- examples/display_button_soundboard.py | 2 + .../display_button_spritebutton_simpletest.py | 4 +- ...spritebutton_tft_featherwing_simpletest.py | 8 +- ruff.toml | 99 +++++++++++++++++++ 15 files changed, 164 insertions(+), 68 deletions(-) create mode 100644 .gitattributes create mode 100644 ruff.toml diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..21c125c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries +# +# SPDX-License-Identifier: Unlicense + +.py text eol=lf +.rst text eol=lf +.txt text eol=lf +.yaml text eol=lf +.toml text eol=lf +.license text eol=lf +.md text eol=lf diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 374676d..f27b786 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,42 +1,22 @@ # SPDX-FileCopyrightText: 2020 Diego Elio Pettenò +# SPDX-FileCopyrightText: 2024 Justin Myers # # SPDX-License-Identifier: Unlicense repos: - - repo: https://github.com/python/black - rev: 23.3.0 - hooks: - - id: black - - repo: https://github.com/fsfe/reuse-tool - rev: v1.1.2 - hooks: - - id: reuse - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: check-yaml - id: end-of-file-fixer - id: trailing-whitespace - - repo: https://github.com/pycqa/pylint - rev: v3.3.1 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.3.4 hooks: - - id: pylint - name: pylint (library code) - types: [python] - args: - - --disable=consider-using-f-string - exclude: "^(docs/|examples/|tests/|setup.py$)" - - id: pylint - name: pylint (example code) - description: Run pylint rules on "examples/*.py" files - types: [python] - files: "^examples/" - args: - - --disable=missing-docstring,invalid-name,consider-using-f-string,duplicate-code - - id: pylint - name: pylint (test code) - description: Run pylint rules on "tests/*.py" files - types: [python] - files: "^tests/" - args: - - --disable=missing-docstring,consider-using-f-string,duplicate-code + - id: ruff-format + - id: ruff + args: ["--fix"] + - repo: https://github.com/fsfe/reuse-tool + rev: v3.0.1 + hooks: + - id: reuse diff --git a/README.rst b/README.rst index 7828bef..f88b195 100644 --- a/README.rst +++ b/README.rst @@ -13,9 +13,9 @@ Introduction :target: https://github.com/adafruit/Adafruit_CircuitPython_Display_Button/actions :alt: Build Status -.. image:: https://img.shields.io/badge/code%20style-black-000000.svg - :target: https://github.com/psf/black - :alt: Code Style: Black +.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json + :target: https://github.com/astral-sh/ruff + :alt: Code Style: Ruff UI Buttons for displayio diff --git a/adafruit_button/__init__.py b/adafruit_button/__init__.py index 2fa5ba8..f521f2e 100644 --- a/adafruit_button/__init__.py +++ b/adafruit_button/__init__.py @@ -20,4 +20,5 @@ https://github.com/adafruit/circuitpython/releases """ + from adafruit_button.button import Button diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 22ffe55..80ed36a 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -21,15 +21,17 @@ """ -from micropython import const from adafruit_display_shapes.rect import Rect from adafruit_display_shapes.roundrect import RoundRect +from micropython import const + from adafruit_button.button_base import ButtonBase, _check_color try: from typing import Optional, Union - from fontio import FontProtocol + from displayio import Group + from fontio import FontProtocol except ImportError: pass @@ -38,7 +40,6 @@ class Button(ButtonBase): - # pylint: disable=too-many-instance-attributes, too-many-locals """Helper class for creating UI buttons for ``displayio``. :param x: The x position of the button. @@ -84,9 +85,7 @@ def _create_body(self): outline=self._outline_color, ) elif self.style == Button.SHADOWRECT: - self.shadow = Rect( - 2, 2, self.width - 2, self.height - 2, fill=self.outline_color - ) + self.shadow = Rect(2, 2, self.width - 2, self.height - 2, fill=self.outline_color) self.body = Rect( 0, 0, @@ -121,7 +120,7 @@ def _create_body(self): SHADOWRECT = const(2) SHADOWROUNDRECT = const(3) - def __init__( + def __init__( # noqa: PLR0913 Too many arguments self, *, x: int, @@ -138,7 +137,7 @@ def __init__( selected_fill: Optional[Union[int, tuple[int, int, int]]] = None, selected_outline: Optional[Union[int, tuple[int, int, int]]] = None, selected_label: Optional[Union[int, tuple[int, int, int]]] = None, - label_scale: Optional[int] = None + label_scale: Optional[int] = None, ) -> None: super().__init__( x=x, diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index c906c90..7796bf4 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -20,11 +20,13 @@ https://github.com/adafruit/circuitpython/releases """ + from adafruit_display_text.bitmap_label import Label from displayio import Group try: from typing import Optional, Union + from fontio import FontProtocol except ImportError: pass @@ -39,7 +41,6 @@ def _check_color(color): class ButtonBase(Group): - # pylint: disable=too-many-instance-attributes """Superclass for creating UI buttons for ``displayio``. :param x: The x position of the button. @@ -53,7 +54,7 @@ class ButtonBase(Group): :param selected_label: Text that appears when selected """ - def __init__( + def __init__( # noqa: PLR0913 Too many arguments self, *, x: int, @@ -65,7 +66,7 @@ def __init__( label_font: Optional[FontProtocol] = None, label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, selected_label: Optional[Union[int, tuple[int, int, int]]] = None, - label_scale: Optional[int] = None + label_scale: Optional[int] = None, ): super().__init__(x=x, y=y) self.x = x @@ -102,10 +103,8 @@ def label(self, newtext: str) -> None: dims[2] *= self._label.scale dims[3] *= self._label.scale if dims[2] >= self.width or dims[3] >= self.height: - while len(self._label.text) > 1 and ( - dims[2] >= self.width or dims[3] >= self.height - ): - self._label.text = "{}.".format(self._label.text[:-2]) + while len(self._label.text) > 1 and (dims[2] >= self.width or dims[3] >= self.height): + self._label.text = f"{self._label.text[:-2]}." dims = list(self._label.bounding_box) dims[2] *= self._label.scale dims[3] *= self._label.scale @@ -113,9 +112,7 @@ def label(self, newtext: str) -> None: raise RuntimeError("Button not large enough for label") self._label.x = (self.width - dims[2]) // 2 self._label.y = self.height // 2 - self._label.color = ( - self._label_color if not self.selected else self._selected_label - ) + self._label.color = self._label_color if not self.selected else self._selected_label self.append(self._label) if (self.selected_label is None) and (self._label_color is not None): diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index f1ca06e..5feb312 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -20,12 +20,15 @@ https://github.com/adafruit/circuitpython/releases """ -from adafruit_imageload.tilegrid_inflator import inflate_tilegrid + from adafruit_imageload import load +from adafruit_imageload.tilegrid_inflator import inflate_tilegrid + from adafruit_button.button_base import ButtonBase try: - from typing import Optional, Union, Tuple + from typing import Optional, Tuple, Union + from fontio import FontProtocol except ImportError: pass @@ -48,7 +51,7 @@ class SpriteButton(ButtonBase): :param int or tuple transparent_index: Index(s) that will be made transparent on the Palette """ - def __init__( + def __init__( # noqa: PLR0913 Too many arguments self, *, x: int, diff --git a/docs/conf.py b/docs/conf.py index 8e24264..ed174ba 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,12 +1,10 @@ -# -*- coding: utf-8 -*- - # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # # SPDX-License-Identifier: MIT +import datetime import os import sys -import datetime sys.path.insert(0, os.path.abspath("..")) @@ -48,9 +46,7 @@ creation_year = "2019" current_year = str(datetime.datetime.now().year) year_duration = ( - current_year - if current_year == creation_year - else creation_year + " - " + current_year + current_year if current_year == creation_year else creation_year + " - " + current_year ) copyright = year_duration + " Limor Fried" author = "Limor Fried" diff --git a/examples/display_button_color_properties.py b/examples/display_button_color_properties.py index d8bf92f..9ad73a0 100644 --- a/examples/display_button_color_properties.py +++ b/examples/display_button_color_properties.py @@ -6,10 +6,11 @@ properties after the button has been initialized. """ +import adafruit_touchscreen import board import displayio import terminalio -import adafruit_touchscreen + from adafruit_button import Button # use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.) diff --git a/examples/display_button_customfont.py b/examples/display_button_customfont.py index 189af96..eb6a3ae 100644 --- a/examples/display_button_customfont.py +++ b/examples/display_button_customfont.py @@ -5,10 +5,12 @@ """ import os + +import adafruit_touchscreen import board import displayio from adafruit_bitmap_font import bitmap_font -import adafruit_touchscreen + from adafruit_button import Button # use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.) diff --git a/examples/display_button_simpletest.py b/examples/display_button_simpletest.py index fa388f3..f8d8546 100644 --- a/examples/display_button_simpletest.py +++ b/examples/display_button_simpletest.py @@ -4,10 +4,11 @@ Simple button example. """ +import adafruit_touchscreen import board import displayio import terminalio -import adafruit_touchscreen + from adafruit_button import Button # use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.) diff --git a/examples/display_button_soundboard.py b/examples/display_button_soundboard.py index 2fd0bad..ef52c8a 100644 --- a/examples/display_button_soundboard.py +++ b/examples/display_button_soundboard.py @@ -5,7 +5,9 @@ """ import time + from adafruit_pyportal import PyPortal + from adafruit_button import Button SHOW_BUTTONS = False diff --git a/examples/display_button_spritebutton_simpletest.py b/examples/display_button_spritebutton_simpletest.py index 838ed30..1836d67 100644 --- a/examples/display_button_spritebutton_simpletest.py +++ b/examples/display_button_spritebutton_simpletest.py @@ -1,10 +1,12 @@ # SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries # SPDX-License-Identifier: MIT import time + +import adafruit_touchscreen import board import displayio -import adafruit_touchscreen import terminalio + from adafruit_button.sprite_button import SpriteButton # These pins are used as both analog and digital! XL, XR and YU must be analog diff --git a/examples/display_button_spritebutton_tft_featherwing_simpletest.py b/examples/display_button_spritebutton_tft_featherwing_simpletest.py index bac2cf7..006fd51 100644 --- a/examples/display_button_spritebutton_tft_featherwing_simpletest.py +++ b/examples/display_button_spritebutton_tft_featherwing_simpletest.py @@ -1,12 +1,14 @@ # SPDX-FileCopyrightText: 2024 DJDevon3 # SPDX-License-Identifier: MIT import time -import displayio -import terminalio + +import adafruit_stmpe610 # TFT Featherwing V1 touch driver import board import digitalio +import displayio +import terminalio from adafruit_hx8357 import HX8357 # TFT Featherwing display driver -import adafruit_stmpe610 # TFT Featherwing V1 touch driver + from adafruit_button.sprite_button import SpriteButton # 3.5" TFT Featherwing is 480x320 diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 0000000..db37c83 --- /dev/null +++ b/ruff.toml @@ -0,0 +1,99 @@ +# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +target-version = "py38" +line-length = 100 + +[lint] +select = ["I", "PL", "UP"] + +extend-select = [ + "D419", # empty-docstring + "E501", # line-too-long + "W291", # trailing-whitespace + "PLC0414", # useless-import-alias + "PLC2401", # non-ascii-name + "PLC2801", # unnecessary-dunder-call + "PLC3002", # unnecessary-direct-lambda-call + "E999", # syntax-error + "PLE0101", # return-in-init + "F706", # return-outside-function + "F704", # yield-outside-function + "PLE0116", # continue-in-finally + "PLE0117", # nonlocal-without-binding + "PLE0241", # duplicate-bases + "PLE0302", # unexpected-special-method-signature + "PLE0604", # invalid-all-object + "PLE0605", # invalid-all-format + "PLE0643", # potential-index-error + "PLE0704", # misplaced-bare-raise + "PLE1141", # dict-iter-missing-items + "PLE1142", # await-outside-async + "PLE1205", # logging-too-many-args + "PLE1206", # logging-too-few-args + "PLE1307", # bad-string-format-type + "PLE1310", # bad-str-strip-call + "PLE1507", # invalid-envvar-value + "PLE2502", # bidirectional-unicode + "PLE2510", # invalid-character-backspace + "PLE2512", # invalid-character-sub + "PLE2513", # invalid-character-esc + "PLE2514", # invalid-character-nul + "PLE2515", # invalid-character-zero-width-space + "PLR0124", # comparison-with-itself + "PLR0202", # no-classmethod-decorator + "PLR0203", # no-staticmethod-decorator + "UP004", # useless-object-inheritance + "PLR0206", # property-with-parameters + "PLR0904", # too-many-public-methods + "PLR0911", # too-many-return-statements + "PLR0912", # too-many-branches + "PLR0913", # too-many-arguments + "PLR0914", # too-many-locals + "PLR0915", # too-many-statements + "PLR0916", # too-many-boolean-expressions + "PLR1702", # too-many-nested-blocks + "PLR1704", # redefined-argument-from-local + "PLR1711", # useless-return + "C416", # unnecessary-comprehension + "PLR1733", # unnecessary-dict-index-lookup + "PLR1736", # unnecessary-list-index-lookup + + # ruff reports this rule is unstable + #"PLR6301", # no-self-use + + "PLW0108", # unnecessary-lambda + "PLW0120", # useless-else-on-loop + "PLW0127", # self-assigning-variable + "PLW0129", # assert-on-string-literal + "B033", # duplicate-value + "PLW0131", # named-expr-without-context + "PLW0245", # super-without-brackets + "PLW0406", # import-self + "PLW0602", # global-variable-not-assigned + "PLW0603", # global-statement + "PLW0604", # global-at-module-level + + # fails on the try: import typing used by libraries + #"F401", # unused-import + + "F841", # unused-variable + "E722", # bare-except + "PLW0711", # binary-op-exception + "PLW1501", # bad-open-mode + "PLW1508", # invalid-envvar-default + "PLW1509", # subprocess-popen-preexec-fn + "PLW2101", # useless-with-lock + "PLW3301", # nested-min-max +] + +ignore = [ + "PLR2004", # magic-value-comparison + "UP030", # format literals + "PLW1514", # unspecified-encoding + +] + +[format] +line-ending = "lf" From f6026b2b6abbaffc617a566d3cb92e3d03a59e14 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 4 Dec 2024 15:40:45 -0600 Subject: [PATCH 20/27] merge main, resolve conflicts --- adafruit_button/button.py | 2 +- adafruit_button/button_base.py | 1 - adafruit_button/sprite_button.py | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 6264822..1ab13f0 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -28,7 +28,7 @@ from adafruit_button.button_base import ButtonBase, _check_color try: - from typing import Optional, Union, Tuple, Any, List + from typing import Optional, Union, Tuple from fontio import FontProtocol from displayio import Group except ImportError: diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index e31e040..d118d04 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -32,7 +32,6 @@ pass - def _check_color(color: Optional[Union[int, tuple[int, int, int]]]) -> int: # if a tuple is supplied, convert it to a RGB number if isinstance(color, tuple): diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 069b18e..337466a 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -26,7 +26,7 @@ from adafruit_button.button_base import ButtonBase try: - from typing import Optional, Union, Tuple, List + from typing import Optional, Union, Tuple from fontio import FontProtocol except ImportError: pass From 3466a7dfcb6d592b4390e8e6281656539f46f15a Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 4 Dec 2024 15:49:33 -0600 Subject: [PATCH 21/27] merge main, resolve conflicts --- adafruit_button/button.py | 3 ++- adafruit_button/button_base.py | 5 +++-- adafruit_button/sprite_button.py | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index e4af791..2e2f3ec 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -29,7 +29,8 @@ from adafruit_button.button_base import ButtonBase, _check_color try: - from typing import Optional, Union, Tuple + from typing import Optional, Tuple, Union + from displayio import Group from fontio import FontProtocol except ImportError: diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index eedf2a3..ce54eee 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -22,12 +22,13 @@ """ +import terminalio from adafruit_display_text.bitmap_label import Label from displayio import Group -import terminalio try: - from typing import Optional, Union, Tuple + from typing import Optional, Tuple, Union + from fontio import FontProtocol except ImportError: pass diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index d82e842..4ca220f 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -74,7 +74,7 @@ def __init__( # noqa: PLR0913 Too many arguments bmp_path: str = None, selected_bmp_path: Optional[str] = None, transparent_index: Optional[Union[int, Tuple]] = None, - label_scale: Optional[int] = 1 + label_scale: Optional[int] = 1, ): if bmp_path is None: raise ValueError("Please supply bmp_path. It cannot be None.") From 68760d1e06c93845e4e8ddcf33726464d66ffd40 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 5 Dec 2024 10:04:00 -0600 Subject: [PATCH 22/27] move contains() to base and add support for FocalTouch --- adafruit_button/button.py | 9 --------- adafruit_button/button_base.py | 26 +++++++++++++++++++++++++- adafruit_button/sprite_button.py | 9 --------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 2e2f3ec..d81e994 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -211,15 +211,6 @@ def group(self) -> Group: ) return self - def contains(self, point: tuple[int, int]) -> bool: - """Used to determine if a point is contained within a button. For example, - ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for - determining that a button has been touched. - """ - return (self.x <= point[0] <= self.x + self.width) and ( - self.y <= point[1] <= self.y + self.height - ) - @property def fill_color(self) -> Optional[int]: """The fill color of the button body""" diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index f4adaf9..820bc9d 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -27,7 +27,7 @@ from displayio import Group try: - from typing import Optional, Tuple, Union + from typing import Dict, List, Optional, Tuple, Union from fontio import FontProtocol except ImportError: @@ -177,3 +177,27 @@ def name(self) -> str: @name.setter def name(self, new_name: str) -> None: self._name = new_name + + def contains(self, point: Union[tuple[int, int], List[int, int], List[Dict[str, int]]]) -> bool: + """Used to determine if a point is contained within a button. For example, + ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for + determining that a button has been touched. + """ + if isinstance(point, tuple) or (isinstance(point, list) and isinstance(point[0], int)): + return (self.x <= point[0] <= self.x + self.width) and ( + self.y <= point[1] <= self.y + self.height + ) + elif isinstance(point, list): + touch_points = point + if len(touch_points) == 0: + return False + for touch_point in touch_points: + if ( + isinstance(touch_point, dict) + and "x" in touch_point.keys() + and "y" in touch_point.keys() + ): + if self.contains((touch_point["x"], touch_point["y"])): + return True + + return False diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 4ca220f..e8f2f44 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -130,15 +130,6 @@ def height(self) -> int: """The height of the button. Read-Only""" return self._height - def contains(self, point: Tuple[int, int]) -> bool: - """Used to determine if a point is contained within a button. For example, - ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for - determining that a button has been touched. - """ - return (self.x <= point[0] <= self.x + self.width) and ( - self.y <= point[1] <= self.y + self.height - ) - def _subclass_selected_behavior(self, value: bool) -> None: if self._selected: if self._selected_bmp is not None: From ca0f247c4e35f0e099a728fcd40079bb661b8aef Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 5 Dec 2024 10:51:10 -0600 Subject: [PATCH 23/27] fix List annotation --- adafruit_button/button_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 820bc9d..7e430b6 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -178,7 +178,7 @@ def name(self) -> str: def name(self, new_name: str) -> None: self._name = new_name - def contains(self, point: Union[tuple[int, int], List[int, int], List[Dict[str, int]]]) -> bool: + def contains(self, point: Union[tuple[int, int], List[int], List[Dict[str, int]]]) -> bool: """Used to determine if a point is contained within a button. For example, ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for determining that a button has been touched. From 7da4638b1a38c047207edc7bea62ae137e3e26c1 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 5 Dec 2024 14:19:56 -0600 Subject: [PATCH 24/27] add debounced examples --- examples/display_button_debounced.py | 73 ++++++++++++++++++ .../display_button_spritebutton_debounced.py | 74 +++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 examples/display_button_debounced.py create mode 100644 examples/display_button_spritebutton_debounced.py diff --git a/examples/display_button_debounced.py b/examples/display_button_debounced.py new file mode 100644 index 0000000..8a0471b --- /dev/null +++ b/examples/display_button_debounced.py @@ -0,0 +1,73 @@ +# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +Basic debounced button example. +""" + +import adafruit_touchscreen +import board +import displayio +import terminalio + +from adafruit_button import Button + +# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.) +# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.) +# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus +display = board.DISPLAY + +# --| Button Config |------------------------------------------------- +BUTTON_X = 110 +BUTTON_Y = 95 +BUTTON_WIDTH = 100 +BUTTON_HEIGHT = 50 +BUTTON_STYLE = Button.ROUNDRECT +BUTTON_FILL_COLOR = 0x00FFFF +BUTTON_OUTLINE_COLOR = 0xFF00FF +BUTTON_LABEL = "HELLO WORLD" +BUTTON_LABEL_COLOR = 0x000000 +# --| Button Config |------------------------------------------------- + +# Setup touchscreen (PyPortal) +ts = adafruit_touchscreen.Touchscreen( + board.TOUCH_XL, + board.TOUCH_XR, + board.TOUCH_YD, + board.TOUCH_YU, + calibration=((5200, 59000), (5800, 57000)), + size=(display.width, display.height), +) + +# Make the display context +splash = displayio.Group() +display.root_group = splash + +# Make the button +button = Button( + x=BUTTON_X, + y=BUTTON_Y, + width=BUTTON_WIDTH, + height=BUTTON_HEIGHT, + style=BUTTON_STYLE, + fill_color=BUTTON_FILL_COLOR, + outline_color=BUTTON_OUTLINE_COLOR, + label=BUTTON_LABEL, + label_font=terminalio.FONT, + label_color=BUTTON_LABEL_COLOR, +) + +# Add button to the display context +splash.append(button) + +# Loop and look for touches +while True: + p = ts.touch_point + if p: + if button.contains(p): + if not button.selected: + button.selected = True + print("pressed") + else: + button.selected = False # if touch is dragged outside of button + else: + button.selected = False # if touch is released diff --git a/examples/display_button_spritebutton_debounced.py b/examples/display_button_spritebutton_debounced.py new file mode 100644 index 0000000..6d6f65f --- /dev/null +++ b/examples/display_button_spritebutton_debounced.py @@ -0,0 +1,74 @@ +# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries +# SPDX-License-Identifier: MIT +import time + +import adafruit_touchscreen +import board +import displayio +import terminalio + +from adafruit_button.sprite_button import SpriteButton + +""" +Sprite button debounced example +""" + +# These pins are used as both analog and digital! XL, XR and YU must be analog +# and digital capable. YD just need to be digital +ts = adafruit_touchscreen.Touchscreen( + board.TOUCH_XL, + board.TOUCH_XR, + board.TOUCH_YD, + board.TOUCH_YU, + calibration=((5200, 59000), (5800, 57000)), + size=(board.DISPLAY.width, board.DISPLAY.height), +) + +# Make the display context +main_group = displayio.Group() +board.DISPLAY.root_group = main_group + +BUTTON_WIDTH = 10 * 16 +BUTTON_HEIGHT = 3 * 16 +BUTTON_MARGIN = 20 + +font = terminalio.FONT + +buttons = [] + + +button_0 = SpriteButton( + x=BUTTON_MARGIN, + y=BUTTON_MARGIN, + width=BUTTON_WIDTH, + height=BUTTON_HEIGHT, + label="button0", + label_font=font, + bmp_path="bmps/gradient_button_0.bmp", + selected_bmp_path="bmps/gradient_button_1.bmp", + transparent_index=0, +) + +buttons.append(button_0) + +for b in buttons: + main_group.append(b) +while True: + p = ts.touch_point + if p: + for i, b in enumerate(buttons): + if b.contains(p): + if not b.selected: + print("Button %d pressed" % i) + b.selected = True + b.label = "pressed" + else: + b.selected = False + b.label = f"button{i}" + + else: + for i, b in enumerate(buttons): + if b.selected: + b.selected = False + b.label = f"button{i}" + time.sleep(0.01) From 5d9aa8d15aab8b6813690fb23886e71e59ba1f61 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 14 Jan 2025 11:32:34 -0600 Subject: [PATCH 25/27] add sphinx configuration to rtd.yaml Signed-off-by: foamyguy --- .readthedocs.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 33c2a61..88bca9f 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -8,6 +8,9 @@ # Required version: 2 +sphinx: + configuration: docs/conf.py + build: os: ubuntu-20.04 tools: From 5bc4267c64b7b6f8515858fb577a7f19a64b4f16 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 16 May 2025 15:01:19 +0000 Subject: [PATCH 26/27] change to ruff --- .pre-commit-config.yaml | 3 +- .pylintrc | 399 ---------------------------------------- ruff.toml | 11 +- 3 files changed, 11 insertions(+), 402 deletions(-) delete mode 100644 .pylintrc diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f27b786..ff19dde 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,4 @@ -# SPDX-FileCopyrightText: 2020 Diego Elio Pettenò -# SPDX-FileCopyrightText: 2024 Justin Myers +# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries # # SPDX-License-Identifier: Unlicense diff --git a/.pylintrc b/.pylintrc deleted file mode 100644 index fe3f21d..0000000 --- a/.pylintrc +++ /dev/null @@ -1,399 +0,0 @@ -# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries -# -# SPDX-License-Identifier: Unlicense - -[MASTER] - -# A comma-separated list of package or module names from where C extensions may -# be loaded. Extensions are loading into the active Python interpreter and may -# run arbitrary code -extension-pkg-whitelist= - -# Add files or directories to the ignore-list. They should be base names, not -# paths. -ignore=CVS - -# Add files or directories matching the regex patterns to the ignore-list. The -# regex matches against base names, not paths. -ignore-patterns= - -# Python code to execute, usually for sys.path manipulation such as -# pygtk.require(). -#init-hook= - -# Use multiple processes to speed up Pylint. -jobs=1 - -# List of plugins (as comma separated values of python modules names) to load, -# usually to register additional checkers. -load-plugins=pylint.extensions.no_self_use - -# Pickle collected data for later comparisons. -persistent=yes - -# Specify a configuration file. -#rcfile= - -# Allow loading of arbitrary C extensions. Extensions are imported into the -# active Python interpreter and may run arbitrary code. -unsafe-load-any-extension=no - - -[MESSAGES CONTROL] - -# Only show warnings with the listed confidence levels. Leave empty to show -# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED -confidence= - -# Disable the message, report, category or checker with the given id(s). You -# can either give multiple identifiers separated by comma (,) or put this -# option multiple times (only on the command line, not in the configuration -# file where it should appear only once).You can also use "--disable=all" to -# disable everything first and then reenable specific checks. For example, if -# you want to run only the similarities checker, you can use "--disable=all -# --enable=similarities". If you want to run only the classes checker, but have -# no Warning level messages displayed, use"--disable=all --enable=classes -# --disable=W" -# disable=import-error,raw-checker-failed,bad-inline-option,locally-disabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,deprecated-str-translate-call -disable=raw-checker-failed,bad-inline-option,locally-disabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,import-error,pointless-string-statement,unspecified-encoding - -# Enable the message, report, category or checker with the given id(s). You can -# either give multiple identifier separated by comma (,) or put this option -# multiple time (only on the command line, not in the configuration file where -# it should appear only once). See also the "--disable" option for examples. -enable= - - -[REPORTS] - -# Python expression which should return a note less than 10 (10 is the highest -# note). You have access to the variables errors warning, statement which -# respectively contain the number of errors / warnings messages and the total -# number of statements analyzed. This is used by the global evaluation report -# (RP0004). -evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) - -# Template used to display messages. This is a python new-style format string -# used to format the message information. See doc for all details -#msg-template= - -# Set the output format. Available formats are text, parseable, colorized, json -# and msvs (visual studio).You can also give a reporter class, eg -# mypackage.mymodule.MyReporterClass. -output-format=text - -# Tells whether to display a full report or only the messages -reports=no - -# Activate the evaluation score. -score=yes - - -[REFACTORING] - -# Maximum number of nested blocks for function / method body -max-nested-blocks=5 - - -[LOGGING] - -# Logging modules to check that the string format arguments are in logging -# function parameter format -logging-modules=logging - - -[SPELLING] - -# Spelling dictionary name. Available dictionaries: none. To make it working -# install python-enchant package. -spelling-dict= - -# List of comma separated words that should not be checked. -spelling-ignore-words= - -# A path to a file that contains private dictionary; one word per line. -spelling-private-dict-file= - -# Tells whether to store unknown words to indicated private dictionary in -# --spelling-private-dict-file option instead of raising a message. -spelling-store-unknown-words=no - - -[MISCELLANEOUS] - -# List of note tags to take in consideration, separated by a comma. -# notes=FIXME,XXX,TODO -notes=FIXME,XXX - - -[TYPECHECK] - -# List of decorators that produce context managers, such as -# contextlib.contextmanager. Add to this list to register other decorators that -# produce valid context managers. -contextmanager-decorators=contextlib.contextmanager - -# List of members which are set dynamically and missed by pylint inference -# system, and so shouldn't trigger E1101 when accessed. Python regular -# expressions are accepted. -generated-members= - -# Tells whether missing members accessed in mixin class should be ignored. A -# mixin class is detected if its name ends with "mixin" (case insensitive). -ignore-mixin-members=yes - -# This flag controls whether pylint should warn about no-member and similar -# checks whenever an opaque object is returned when inferring. The inference -# can return multiple potential results while evaluating a Python object, but -# some branches might not be evaluated, which results in partial inference. In -# that case, it might be useful to still emit no-member and other checks for -# the rest of the inferred objects. -ignore-on-opaque-inference=yes - -# List of class names for which member attributes should not be checked (useful -# for classes with dynamically set attributes). This supports the use of -# qualified names. -ignored-classes=optparse.Values,thread._local,_thread._local - -# List of module names for which member attributes should not be checked -# (useful for modules/projects where namespaces are manipulated during runtime -# and thus existing member attributes cannot be deduced by static analysis. It -# supports qualified module names, as well as Unix pattern matching. -ignored-modules=board - -# Show a hint with possible names when a member name was not found. The aspect -# of finding the hint is based on edit distance. -missing-member-hint=yes - -# The minimum edit distance a name should have in order to be considered a -# similar match for a missing member name. -missing-member-hint-distance=1 - -# The total number of similar names that should be taken in consideration when -# showing a hint for a missing member. -missing-member-max-choices=1 - - -[VARIABLES] - -# List of additional names supposed to be defined in builtins. Remember that -# you should avoid to define new builtins when possible. -additional-builtins= - -# Tells whether unused global variables should be treated as a violation. -allow-global-unused-variables=yes - -# List of strings which can identify a callback function by name. A callback -# name must start or end with one of those strings. -callbacks=cb_,_cb - -# A regular expression matching the name of dummy variables (i.e. expectedly -# not used). -dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ - -# Argument names that match this expression will be ignored. Default to name -# with leading underscore -ignored-argument-names=_.*|^ignored_|^unused_ - -# Tells whether we should check for unused import in __init__ files. -init-import=no - -# List of qualified module names which can have objects that can redefine -# builtins. -redefining-builtins-modules=six.moves,future.builtins - - -[FORMAT] - -# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. -# expected-line-ending-format= -expected-line-ending-format=LF - -# Regexp for a line that is allowed to be longer than the limit. -ignore-long-lines=^\s*(# )??$ - -# Number of spaces of indent required inside a hanging or continued line. -indent-after-paren=4 - -# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 -# tab). -indent-string=' ' - -# Maximum number of characters on a single line. -max-line-length=100 - -# Maximum number of lines in a module -max-module-lines=1000 - -# Allow the body of a class to be on the same line as the declaration if body -# contains single statement. -single-line-class-stmt=no - -# Allow the body of an if to be on the same line as the test if there is no -# else. -single-line-if-stmt=no - - -[SIMILARITIES] - -# Ignore comments when computing similarities. -ignore-comments=yes - -# Ignore docstrings when computing similarities. -ignore-docstrings=yes - -# Ignore imports when computing similarities. -ignore-imports=yes - -# Minimum lines number of a similarity. -min-similarity-lines=12 - - -[BASIC] - -# Regular expression matching correct argument names -argument-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$ - -# Regular expression matching correct attribute names -attr-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$ - -# Bad variable names which should always be refused, separated by a comma -bad-names=foo,bar,baz,toto,tutu,tata - -# Regular expression matching correct class attribute names -class-attribute-rgx=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$ - -# Regular expression matching correct class names -# class-rgx=[A-Z_][a-zA-Z0-9]+$ -class-rgx=[A-Z_][a-zA-Z0-9_]+$ - -# Regular expression matching correct constant names -const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$ - -# Minimum line length for functions/classes that require docstrings, shorter -# ones are exempt. -docstring-min-length=-1 - -# Regular expression matching correct function names -function-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$ - -# Good variable names which should always be accepted, separated by a comma -# good-names=i,j,k,ex,Run,_ -good-names=r,g,b,w,i,j,k,n,x,y,z,ex,ok,Run,_ - -# Include a hint for the correct naming format with invalid-name -include-naming-hint=no - -# Regular expression matching correct inline iteration names -inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$ - -# Regular expression matching correct method names -method-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$ - -# Regular expression matching correct module names -module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ - -# Colon-delimited sets of names that determine each other's naming style when -# the name regexes allow several styles. -name-group= - -# Regular expression which should only match function or class names that do -# not require a docstring. -no-docstring-rgx=^_ - -# List of decorators that produce properties, such as abc.abstractproperty. Add -# to this list to register other decorators that produce valid properties. -property-classes=abc.abstractproperty - -# Regular expression matching correct variable names -variable-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$ - - -[IMPORTS] - -# Allow wildcard imports from modules that define __all__. -allow-wildcard-with-all=no - -# Analyse import fallback blocks. This can be used to support both Python 2 and -# 3 compatible code, which means that the block might have code that exists -# only in one or another interpreter, leading to false positives when analysed. -analyse-fallback-blocks=no - -# Deprecated modules which should not be used, separated by a comma -deprecated-modules=optparse,tkinter.tix - -# Create a graph of external dependencies in the given file (report RP0402 must -# not be disabled) -ext-import-graph= - -# Create a graph of every (i.e. internal and external) dependencies in the -# given file (report RP0402 must not be disabled) -import-graph= - -# Create a graph of internal dependencies in the given file (report RP0402 must -# not be disabled) -int-import-graph= - -# Force import order to recognize a module as part of the standard -# compatibility libraries. -known-standard-library= - -# Force import order to recognize a module as part of a third party library. -known-third-party=enchant - - -[CLASSES] - -# List of method names used to declare (i.e. assign) instance attributes. -defining-attr-methods=__init__,__new__,setUp - -# List of member names, which should be excluded from the protected access -# warning. -exclude-protected=_asdict,_fields,_replace,_source,_make - -# List of valid names for the first argument in a class method. -valid-classmethod-first-arg=cls - -# List of valid names for the first argument in a metaclass class method. -valid-metaclass-classmethod-first-arg=mcs - - -[DESIGN] - -# Maximum number of arguments for function / method -max-args=18 - -# Maximum number of attributes for a class (see R0902). -# max-attributes=7 -max-attributes=11 - -# Maximum number of boolean expressions in a if statement -max-bool-expr=5 - -# Maximum number of branch for function / method body -max-branches=12 - -# Maximum number of locals for function / method body -max-locals=15 - -# Maximum number of parents for a class (see R0901). -max-parents=7 - -# Maximum number of public methods for a class (see R0904). -max-public-methods=20 - -# Maximum number of return / yield for function / method body -max-returns=6 - -# Maximum number of statements in function / method body -max-statements=50 - -# Minimum number of public methods for a class (see R0903). -min-public-methods=1 - - -[EXCEPTIONS] - -# Exceptions that will emit a warning when being caught. Defaults to -# "Exception" -overgeneral-exceptions=builtins.Exception diff --git a/ruff.toml b/ruff.toml index db37c83..73e9efc 100644 --- a/ruff.toml +++ b/ruff.toml @@ -6,6 +6,7 @@ target-version = "py38" line-length = 100 [lint] +preview = true select = ["I", "PL", "UP"] extend-select = [ @@ -92,7 +93,15 @@ ignore = [ "PLR2004", # magic-value-comparison "UP030", # format literals "PLW1514", # unspecified-encoding - + "PLR0913", # too-many-arguments + "PLR0915", # too-many-statements + "PLR0917", # too-many-positional-arguments + "PLR0904", # too-many-public-methods + "PLR0912", # too-many-branches + "PLR0916", # too-many-boolean-expressions + "PLR6301", # could-be-static no-self-use + "PLC0415", # import outside toplevel + "PLC2701", # private import ] [format] From 8566650725e22c3943b39025dc18ca5b123c42c6 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sat, 31 May 2025 11:09:17 -0500 Subject: [PATCH 27/27] displayio api update --- .../display_button_spritebutton_tft_featherwing_simpletest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/display_button_spritebutton_tft_featherwing_simpletest.py b/examples/display_button_spritebutton_tft_featherwing_simpletest.py index 006fd51..3c6ffd9 100644 --- a/examples/display_button_spritebutton_tft_featherwing_simpletest.py +++ b/examples/display_button_spritebutton_tft_featherwing_simpletest.py @@ -6,6 +6,7 @@ import board import digitalio import displayio +import fourwire import terminalio from adafruit_hx8357 import HX8357 # TFT Featherwing display driver @@ -20,7 +21,7 @@ spi = board.SPI() tft_cs = board.D9 tft_dc = board.D10 -display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs) +display_bus = fourwire.FourWire(spi, command=tft_dc, chip_select=tft_cs) display = HX8357(display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT) display.rotation = 0 _touch_flip = (False, True)