Skip to content

Commit

Permalink
Merge pull request #32 from FoamyGuy/alt_click
Browse files Browse the repository at this point in the history
implement is_alt_clicked
  • Loading branch information
TheKitty committed Jan 7, 2022
2 parents 386e235 + 5d70444 commit 0b6e980
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion adafruit_cursorcontrol/cursorcontrol_cursormanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
PYBADGE_BUTTON_RIGHT = const(4)
# PyBadge & PyGamer
PYBADGE_BUTTON_A = const(1)
PYBADGE_BUTTON_B = const(0)


class CursorManager:
Expand All @@ -43,6 +44,7 @@ class CursorManager:
def __init__(self, cursor: Cursor) -> None:
self._cursor = cursor
self._is_clicked = False
self._is_alt_clicked = False
self._pad_states = 0
self._event = Event()
self._init_hardware()
Expand Down Expand Up @@ -83,12 +85,13 @@ def _init_hardware(self) -> None:
"btn_up": PYBADGE_BUTTON_UP,
"btn_down": PYBADGE_BUTTON_DOWN,
"btn_a": PYBADGE_BUTTON_A,
"btn_b": PYBADGE_BUTTON_B,
}
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}
self._pad_btns = {"btn_a": PYBADGE_BUTTON_A, "btn_b": PYBADGE_BUTTON_B}
# Sample the center points of the joystick
self._center_x = self._joystick_x.value
self._center_y = self._joystick_y.value
Expand All @@ -111,6 +114,13 @@ def is_clicked(self) -> bool:
"""
return self._is_clicked

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

def update(self) -> None:
"""Updates the cursor object."""
if self._pad.events.get_into(self._event):
Expand All @@ -121,6 +131,11 @@ def update(self) -> None:
elif self._pad_states & (1 << self._pad_btns["btn_a"]):
self._is_clicked = True

if self._is_alt_clicked:
self._is_alt_clicked = False
elif self._pad_states & (1 << self._pad_btns["btn_b"]):
self._is_alt_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

0 comments on commit 0b6e980

Please sign in to comment.