Skip to content

CursorManager: Add support for Start and Select buttons for PyGamer. #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions adafruit_cursorcontrol/cursorcontrol_cursormanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
PYBADGE_BUTTON_DOWN = const(5)
PYBADGE_BUTTON_RIGHT = const(4)
# PyBadge & PyGamer
PYBADGE_BUTTON_SELECT = const(3)
PYBADGE_BUTTON_START = const(2)
PYBADGE_BUTTON_A = const(1)
PYBADGE_BUTTON_B = const(0)

Expand All @@ -41,10 +43,14 @@ class CursorManager:
:param Cursor cursor: The cursor object we are using.
"""

# pylint: disable=too-many-instance-attributes

def __init__(self, cursor: Cursor) -> None:
self._cursor = cursor
self._is_clicked = False
self._is_alt_clicked = False
self._is_select_clicked = False
self._is_start_clicked = False
self._pad_states = 0
self._event = Event()
self._init_hardware()
Expand Down Expand Up @@ -86,12 +92,19 @@ def _init_hardware(self) -> None:
"btn_down": PYBADGE_BUTTON_DOWN,
"btn_a": PYBADGE_BUTTON_A,
"btn_b": PYBADGE_BUTTON_B,
"btn_select": PYBADGE_BUTTON_SELECT,
"btn_start": PYBADGE_BUTTON_START,
}
self._pad_states = 0
elif hasattr(board, "JOYSTICK_X"):
self._joystick_x = analogio.AnalogIn(board.JOYSTICK_X)
self._joystick_y = analogio.AnalogIn(board.JOYSTICK_Y)
self._pad_btns = {"btn_a": PYBADGE_BUTTON_A, "btn_b": PYBADGE_BUTTON_B}
self._pad_btns = {
"btn_a": PYBADGE_BUTTON_A,
"btn_b": PYBADGE_BUTTON_B,
"btn_select": PYBADGE_BUTTON_SELECT,
"btn_start": PYBADGE_BUTTON_START,
}
# Sample the center points of the joystick
self._center_x = self._joystick_x.value
self._center_y = self._joystick_y.value
Expand All @@ -109,18 +122,32 @@ def _init_hardware(self) -> None:

@property
def is_clicked(self) -> bool:
"""Returns True if the cursor button was pressed
"""Returns True if the cursor A button was pressed
during previous call to update()
"""
return self._is_clicked

@property
def is_alt_clicked(self) -> bool:
"""Returns True if the cursor button was pressed
"""Returns True if the cursor B button was pressed
during previous call to update()
"""
return self._is_alt_clicked

@property
def is_select_clicked(self) -> bool:
"""Returns True if the Select button was pressed
during previous call to update()
"""
return self._is_select_clicked

@property
def is_start_clicked(self) -> bool:
"""Returns True if the Start button was pressed
during previous call to update()
"""
return self._is_start_clicked

def update(self) -> None:
"""Updates the cursor object."""
if self._pad.events.get_into(self._event):
Expand All @@ -136,6 +163,16 @@ def update(self) -> None:
elif self._pad_states & (1 << self._pad_btns["btn_b"]):
self._is_alt_clicked = True

if self._is_select_clicked:
self._is_select_clicked = False
elif self._pad_states & (1 << self._pad_btns["btn_select"]):
self._is_select_clicked = True

if self._is_start_clicked:
self._is_start_clicked = False
elif self._pad_states & (1 << self._pad_btns["btn_start"]):
self._is_start_clicked = True

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.
Expand Down