From 73653bbc7c50b4282702b8e52a5661b7aab6e0b0 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 15 Dec 2021 09:53:15 -0500 Subject: [PATCH 01/11] Update cursor param to be of type Cursor in CursorManager class docstring --- adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 3ebe4c6..439eb01 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -30,7 +30,7 @@ class CursorManager: """Simple interaction user interface interaction for Adafruit_CursorControl. - :param adafruit_cursorcontrol cursor: The cursor object we are using. + :param Cursor cursor: The cursor object we are using. """ def __init__(self, cursor): From 41badb4150fe1a5947269c0f58f171ab0d046bf8 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 15 Dec 2021 09:53:43 -0500 Subject: [PATCH 02/11] Remove event param from _store_button_states() method docstring --- adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 439eb01..983891d 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -136,8 +136,6 @@ def _read_joystick_y(self, samples=3): def _store_button_states(self): """Stores the state of the PyBadge's D-Pad or the PyGamer's Joystick into a byte - - :param Event event: The latest button press transition event detected. """ bit_index = self._event.key_number current_state = (self._pad_states >> bit_index) & 1 From e11fd780afb4733e2af48054bd4c74af657aaa2d Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 15 Dec 2021 09:54:25 -0500 Subject: [PATCH 03/11] Change cursor param type to Cursor in DebouncedCursorManager class docstring --- adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 983891d..5850c73 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -177,7 +177,7 @@ class DebouncedCursorManager(CursorManager): the button is just pressed, and just released, as well it's current state. "Just" in this context means "since the previous call to update." - :param adafruit_cursorcontrol cursor: The cursor object we are using. + :param Cursor cursor: The cursor object we are using. """ def __init__(self, cursor, debounce_interval=0.01): From df9845722ca3b37cbcdf1bea37e6dc00bce8f92d Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 15 Dec 2021 09:54:37 -0500 Subject: [PATCH 04/11] Add type hints for cursorcontrol_cursormanager.py --- .../cursorcontrol_cursormanager.py | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 5850c73..06be50b 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -14,6 +14,13 @@ from keypad import ShiftRegisterKeys, Event from adafruit_debouncer import Debouncer +try: + from typing import Optional, Type + from types import TracebackType + from adafruit_cursorcontrol.cursorcontrol import Cursor +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CursorControl.git" @@ -33,20 +40,20 @@ class CursorManager: :param Cursor cursor: The cursor object we are using. """ - def __init__(self, cursor): + def __init__(self, cursor: Cursor) -> None: self._cursor = cursor self._is_clicked = False self._pad_states = 0 self._event = Event() self._init_hardware() - def __enter__(self): + def __enter__(self) -> 'CursorManager': return self - def __exit__(self, exception_type, exception_value, traceback): + def __exit__(self, exception_type: Optional[Type[type]], exception_value: Optional[BaseException], traceback: Optional[TracebackType]) -> None: self.deinit() - def deinit(self): + def deinit(self) -> None: """Deinitializes a CursorManager object.""" self._is_deinited() self._pad.deinit() @@ -54,7 +61,7 @@ def deinit(self): self._cursor = None self._event = None - def _is_deinited(self): + def _is_deinited(self) -> None: """Checks if CursorManager object has been deinitd.""" if self._cursor is None: raise ValueError( @@ -62,7 +69,7 @@ def _is_deinited(self): "be used. Create a new CursorManager object." ) - def _init_hardware(self): + def _init_hardware(self) -> None: """Initializes PyBadge or PyGamer hardware.""" if hasattr(board, "BUTTON_CLOCK") and not hasattr(board, "JOYSTICK_X"): self._pad_btns = { @@ -93,13 +100,13 @@ def _init_hardware(self): ) @property - def is_clicked(self): + def is_clicked(self) -> bool: """Returns True if the cursor button was pressed during previous call to update() """ return self._is_clicked - def update(self): + def update(self) -> None: """Updates the cursor object.""" if self._pad.events.get_into(self._event): self._store_button_states() @@ -109,7 +116,7 @@ def update(self): elif self._pad_states & (1 << self._pad_btns["btn_a"]): self._is_clicked = True - def _read_joystick_x(self, samples=3): + def _read_joystick_x(self, samples: int = 3) -> float: """Read the X analog joystick on the PyGamer. :param int samples: How many samples to read and average. """ @@ -121,7 +128,7 @@ def _read_joystick_x(self, samples=3): reading /= samples return reading - def _read_joystick_y(self, samples=3): + def _read_joystick_y(self, samples: int = 3) -> float: """Read the Y analog joystick on the PyGamer. :param int samples: How many samples to read and average. """ @@ -133,7 +140,7 @@ def _read_joystick_y(self, samples=3): reading /= samples return reading - def _store_button_states(self): + def _store_button_states(self) -> None: """Stores the state of the PyBadge's D-Pad or the PyGamer's Joystick into a byte """ @@ -142,7 +149,7 @@ def _store_button_states(self): if current_state != self._event.pressed: self._pad_states = (1 << bit_index) ^ self._pad_states - def _check_cursor_movement(self): + def _check_cursor_movement(self) -> None: """Checks the PyBadge D-Pad or the PyGamer's Joystick for movement.""" if hasattr(board, "BUTTON_CLOCK") and not hasattr(board, "JOYSTICK_X"): if self._pad_states & (1 << self._pad_btns["btn_right"]): @@ -180,7 +187,7 @@ class DebouncedCursorManager(CursorManager): :param Cursor cursor: The cursor object we are using. """ - def __init__(self, cursor, debounce_interval=0.01): + def __init__(self, cursor: Cursor, debounce_interval: float = 0.01) -> None: CursorManager.__init__(self, cursor) self._debouncer = Debouncer( lambda: bool(self._pad_states & (1 << self._pad_btns["btn_a"])), @@ -188,7 +195,7 @@ def __init__(self, cursor, debounce_interval=0.01): ) @property - def is_clicked(self): + def is_clicked(self) -> bool: """Returns True if the cursor button was pressed during previous call to update() """ @@ -197,18 +204,18 @@ def is_clicked(self): pressed = is_clicked @property - def released(self): + def released(self) -> bool: """Returns True if the cursor button was released during previous call to update() """ return self._debouncer.fell @property - def held(self): + def held(self) -> bool: """Returns True if the cursor button is currently being held""" return self._debouncer.value - def update(self): + def update(self) -> None: """Updates the cursor object.""" if self._pad.events.get_into(self._event): self._store_button_states() From 2830cd2a4a347d1077d0751ff0caf503e82a30c8 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 15 Dec 2021 10:13:23 -0500 Subject: [PATCH 05/11] Moved is_hidden param for Cursor class docstring before cursor_speed --- adafruit_cursorcontrol/cursorcontrol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_cursorcontrol/cursorcontrol.py b/adafruit_cursorcontrol/cursorcontrol.py index 2e6e6a3..72c618d 100644 --- a/adafruit_cursorcontrol/cursorcontrol.py +++ b/adafruit_cursorcontrol/cursorcontrol.py @@ -31,9 +31,9 @@ class Cursor: :param ~displayio.Display display: CircuitPython display object. :param ~displayio.Group display_group: CircuitPython group object to append the cursor to. + :param bool is_hidden: Cursor is hidden on init. :param int cursor_speed: Speed of the cursor, in pixels. :param int scale: Scale amount for the cursor in both directions. - :param bool is_hidden: Cursor is hidden on init. Example for creating a cursor layer From 1d3c90f9e36dc72ec7542e4c84e2af683dbc2e9b Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 15 Dec 2021 10:15:21 -0500 Subject: [PATCH 06/11] Added bmp param in Cursor class docstring --- adafruit_cursorcontrol/cursorcontrol.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_cursorcontrol/cursorcontrol.py b/adafruit_cursorcontrol/cursorcontrol.py index 72c618d..815dd81 100644 --- a/adafruit_cursorcontrol/cursorcontrol.py +++ b/adafruit_cursorcontrol/cursorcontrol.py @@ -31,6 +31,7 @@ class Cursor: :param ~displayio.Display display: CircuitPython display object. :param ~displayio.Group display_group: CircuitPython group object to append the cursor to. + :param ~displayio.Bitmap bmp: CircuitPython bitmap object to use as the cursor :param bool is_hidden: Cursor is hidden on init. :param int cursor_speed: Speed of the cursor, in pixels. :param int scale: Scale amount for the cursor in both directions. From d9cc55efb5de8a69a6b9c6f7fbc74095edda8808 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 15 Dec 2021 10:23:21 -0500 Subject: [PATCH 07/11] Add type to bmp param in Cursor.cursor_bitmap() docstring --- adafruit_cursorcontrol/cursorcontrol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_cursorcontrol/cursorcontrol.py b/adafruit_cursorcontrol/cursorcontrol.py index 815dd81..532891f 100644 --- a/adafruit_cursorcontrol/cursorcontrol.py +++ b/adafruit_cursorcontrol/cursorcontrol.py @@ -218,7 +218,7 @@ def cursor_bitmap(self): def cursor_bitmap(self, bmp): """Set a new cursor bitmap. - :param bmp: A Bitmap to use for the cursor + :param ~displayio.Bitmap bmp: A Bitmap to use for the cursor """ self._cursor_bitmap = bmp self._cursor_grp.remove(self._cur_sprite) From 47547ad65d8f6af41644c4da07f8eb1ad5aadd53 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 15 Dec 2021 10:27:01 -0500 Subject: [PATCH 08/11] Add bmp param to docstring of Cursor.generate_cursor() --- adafruit_cursorcontrol/cursorcontrol.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/adafruit_cursorcontrol/cursorcontrol.py b/adafruit_cursorcontrol/cursorcontrol.py index 532891f..ce9c3be 100644 --- a/adafruit_cursorcontrol/cursorcontrol.py +++ b/adafruit_cursorcontrol/cursorcontrol.py @@ -226,7 +226,10 @@ def cursor_bitmap(self, bmp): self._cursor_grp.append(self._cur_sprite) def generate_cursor(self, bmp): - """Generates a cursor icon""" + """Generates a cursor icon + + :param ~displayio.Bitmap bmp: Bitmap to use for the cursor + """ self._is_deinited() self._cursor_grp = displayio.Group(scale=self._scale) self._cur_palette = displayio.Palette(3) From 73297547e37224ae83dbab9799adc780c728c32c Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 15 Dec 2021 10:29:38 -0500 Subject: [PATCH 09/11] Update bmp param text in Cursor.generate_cursor() docstring --- adafruit_cursorcontrol/cursorcontrol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_cursorcontrol/cursorcontrol.py b/adafruit_cursorcontrol/cursorcontrol.py index ce9c3be..6355fb7 100644 --- a/adafruit_cursorcontrol/cursorcontrol.py +++ b/adafruit_cursorcontrol/cursorcontrol.py @@ -228,7 +228,7 @@ def cursor_bitmap(self, bmp): def generate_cursor(self, bmp): """Generates a cursor icon - :param ~displayio.Bitmap bmp: Bitmap to use for the cursor + :param ~displayio.Bitmap bmp: A Bitmap to use for the cursor """ self._is_deinited() self._cursor_grp = displayio.Group(scale=self._scale) From 74b48dedb994991e6cee4355d5228fc071d18498 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 15 Dec 2021 10:30:12 -0500 Subject: [PATCH 10/11] Add type hints for cursorcontrol.py --- adafruit_cursorcontrol/cursorcontrol.py | 58 ++++++++++++++----------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol.py b/adafruit_cursorcontrol/cursorcontrol.py index 6355fb7..9f58e33 100644 --- a/adafruit_cursorcontrol/cursorcontrol.py +++ b/adafruit_cursorcontrol/cursorcontrol.py @@ -22,6 +22,12 @@ """ import displayio +try: + from typing import Optional, Type + from types import TracebackType +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CursorControl.git" @@ -54,12 +60,12 @@ class Cursor: # pylint: disable=too-many-arguments,line-too-long def __init__( self, - display=None, - display_group=None, - bmp=None, - is_hidden=False, - cursor_speed=5, - scale=1, + display: Optional[displayio.Display] = None, + display_group: Optional[displayio.Group] = None, + bmp: Optional[displayio.Bitmap] = None, + is_hidden: bool = False, + cursor_speed: int = 5, + scale: int = 1, ): self._display = display self._scale = scale @@ -76,19 +82,19 @@ def __init__( # pylint: enable=too-many-arguments,line-too-long - def __enter__(self): + def __enter__(self) -> 'Cursor': return self - def __exit__(self, exception_type, exception_value, traceback): + def __exit__(self, exception_type: Optional[Type[type]], exception_value: Optional[BaseException], traceback: Optional[TracebackType]) -> None: self.deinit() - def deinit(self): + def deinit(self) -> None: """deinitializes the cursor object.""" self._is_deinited() self._scale = None self._display_grp.remove(self._cursor_grp) - def _is_deinited(self): + def _is_deinited(self) -> None: """checks cursor deinitialization""" if self._scale is None: raise ValueError( @@ -97,12 +103,12 @@ def _is_deinited(self): ) @property - def scale(self): + def scale(self) -> int: """Returns the cursor's scale amount as an integer.""" return self._scale @scale.setter - def scale(self, scale_value): + def scale(self, scale_value: int) -> None: """Scales the cursor by scale_value in both directions. :param int scale_value: Amount to scale the cursor by. """ @@ -112,12 +118,12 @@ def scale(self, scale_value): self._cursor_grp.scale = scale_value @property - def speed(self): + def speed(self) -> int: """Returns the cursor's speed, in pixels.""" return self._speed @speed.setter - def speed(self, speed): + def speed(self, speed: int) -> None: """Sets the speed of the cursor. :param int speed: Cursor movement speed, in pixels. """ @@ -126,12 +132,12 @@ def speed(self, speed): self._speed = speed @property - def x(self): + def x(self) -> int: """Returns the cursor's x-coordinate.""" return self._cursor_grp.x @x.setter - def x(self, x_val): + def x(self, x_val: int) -> None: """Sets the x-value of the cursor. :param int x_val: cursor x-position, in pixels. """ @@ -144,12 +150,12 @@ def x(self, x_val): self._cursor_grp.x = x_val @property - def y(self): + def y(self) -> int: """Returns the cursor's y-coordinate.""" return self._cursor_grp.y @y.setter - def y(self, y_val): + def y(self, y_val: int) -> None: """Sets the y-value of the cursor. :param int y_val: cursor y-position, in pixels. """ @@ -162,12 +168,12 @@ def y(self, y_val): self._cursor_grp.y = y_val @property - def hidden(self): + def hidden(self) -> bool: """Returns True if the cursor is hidden or visible on the display.""" return self._is_hidden @hidden.setter - def hidden(self, is_hidden): + def hidden(self, is_hidden: bool) -> None: self._is_deinited() if is_hidden: self._is_hidden = True @@ -176,16 +182,16 @@ def hidden(self, is_hidden): self._is_hidden = False self._display_grp.append(self._cursor_grp) - def hide(self): + def hide(self) -> None: """Hide the cursor.""" self.hidden = True - def show(self): + def show(self) -> None: """Show the cursor.""" self.hidden = False # pylint:disable=no-self-use - def _default_cursor_bitmap(self): + def _default_cursor_bitmap(self) -> displayio.Bitmap: bmp = displayio.Bitmap(20, 20, 3) # left edge, outline for i in range(0, bmp.height): @@ -210,12 +216,12 @@ def _default_cursor_bitmap(self): # pylint:enable=no-self-use @property - def cursor_bitmap(self): + def cursor_bitmap(self) -> displayio.Bitmap: """Return the cursor bitmap.""" return self._cursor_bitmap @cursor_bitmap.setter - def cursor_bitmap(self, bmp): + def cursor_bitmap(self, bmp: displayio.Bitmap) -> None: """Set a new cursor bitmap. :param ~displayio.Bitmap bmp: A Bitmap to use for the cursor @@ -225,7 +231,7 @@ def cursor_bitmap(self, bmp): self._cur_sprite = displayio.TileGrid(bmp, pixel_shader=self._cur_palette) self._cursor_grp.append(self._cur_sprite) - def generate_cursor(self, bmp): + def generate_cursor(self, bmp: displayio.Bitmap) -> None: """Generates a cursor icon :param ~displayio.Bitmap bmp: A Bitmap to use for the cursor From 214cbc6e2d3db7a8e2e5373106d9160d9ede22c6 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 15 Dec 2021 10:31:10 -0500 Subject: [PATCH 11/11] Reformatted per pre-commit --- adafruit_cursorcontrol/cursorcontrol.py | 11 ++++++++--- adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 9 +++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol.py b/adafruit_cursorcontrol/cursorcontrol.py index 9f58e33..7eec0cf 100644 --- a/adafruit_cursorcontrol/cursorcontrol.py +++ b/adafruit_cursorcontrol/cursorcontrol.py @@ -82,10 +82,15 @@ def __init__( # pylint: enable=too-many-arguments,line-too-long - def __enter__(self) -> 'Cursor': + def __enter__(self) -> "Cursor": return self - def __exit__(self, exception_type: Optional[Type[type]], exception_value: Optional[BaseException], traceback: Optional[TracebackType]) -> None: + def __exit__( + self, + exception_type: Optional[Type[type]], + exception_value: Optional[BaseException], + traceback: Optional[TracebackType], + ) -> None: self.deinit() def deinit(self) -> None: @@ -233,7 +238,7 @@ def cursor_bitmap(self, bmp: displayio.Bitmap) -> None: def generate_cursor(self, bmp: displayio.Bitmap) -> None: """Generates a cursor icon - + :param ~displayio.Bitmap bmp: A Bitmap to use for the cursor """ self._is_deinited() diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 06be50b..7526ff3 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -47,10 +47,15 @@ def __init__(self, cursor: Cursor) -> None: self._event = Event() self._init_hardware() - def __enter__(self) -> 'CursorManager': + def __enter__(self) -> "CursorManager": return self - def __exit__(self, exception_type: Optional[Type[type]], exception_value: Optional[BaseException], traceback: Optional[TracebackType]) -> None: + def __exit__( + self, + exception_type: Optional[Type[type]], + exception_value: Optional[BaseException], + traceback: Optional[TracebackType], + ) -> None: self.deinit() def deinit(self) -> None: