Skip to content

Fix cursor centering issue #10

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 5 commits into from
Jul 22, 2019
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
84 changes: 47 additions & 37 deletions adafruit_cursorcontrol/cursorcontrol_cursormanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@
# PyBadge & PyGamer
PYBADGE_BUTTON_A = const(2)

JOY_X_CTR = 32767.5
JOY_Y_CTR = 32767.5

class CursorManager(object):
"""Simple interaction user interface interaction for Adafruit_CursorControl.

:param adafruit_cursorcontrol cursor: The cursor object we are using.
"""

def __init__(self, cursor):
self._cursor = cursor
self._is_clicked = False
self._init_hardware()
self._center_x = self._joystick_x.value
self._center_y = self._joystick_y.value

def __enter__(self):
return self
Expand All @@ -70,27 +70,34 @@ def deinit(self):
def _is_deinited(self):
"""Checks if CursorManager object has been deinitd."""
if self._cursor is None:
raise ValueError("CursorManager object has been deinitialized and can no longer "
"be used. Create a new CursorManager object.")
raise ValueError(
"CursorManager object has been deinitialized and can no longer "
"be used. Create a new CursorManager object."
)

def _init_hardware(self):
"""Initializes PyBadge or PyGamer hardware."""
if hasattr(board, 'BUTTON_CLOCK') and not hasattr(board, 'JOYSTICK_X'):
self._pad_btns = {'btn_left' : PYBADGE_BUTTON_LEFT,
'btn_right' : PYBADGE_BUTTON_RIGHT,
'btn_up' : PYBADGE_BUTTON_UP,
'btn_down' : PYBADGE_BUTTON_DOWN,
'btn_a' : PYBADGE_BUTTON_A}
elif hasattr(board, 'JOYSTICK_X'):
if hasattr(board, "BUTTON_CLOCK") and not hasattr(board, "JOYSTICK_X"):
self._pad_btns = {
"btn_left": PYBADGE_BUTTON_LEFT,
"btn_right": PYBADGE_BUTTON_RIGHT,
"btn_up": PYBADGE_BUTTON_UP,
"btn_down": PYBADGE_BUTTON_DOWN,
"btn_a": PYBADGE_BUTTON_A,
}
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}
else:
raise AttributeError('Board must have a D-Pad or Joystick for use with CursorManager!')
self._pad = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
digitalio.DigitalInOut(board.BUTTON_OUT),
digitalio.DigitalInOut(board.BUTTON_LATCH))

raise AttributeError(
"Board must have a D-Pad or Joystick for use with CursorManager!"
)
self._pad = GamePadShift(
digitalio.DigitalInOut(board.BUTTON_CLOCK),
digitalio.DigitalInOut(board.BUTTON_OUT),
digitalio.DigitalInOut(board.BUTTON_LATCH),
)

@property
def is_clicked(self):
Expand All @@ -105,7 +112,7 @@ def update(self):
self._check_cursor_movement(pressed)
if self._is_clicked:
self._is_clicked = False
elif pressed & self._pad_btns['btn_a']:
elif pressed & self._pad_btns["btn_a"]:
self._is_clicked = True

def _read_joystick_x(self, samples=3):
Expand All @@ -114,11 +121,10 @@ def _read_joystick_x(self, samples=3):
"""
reading = 0
# pylint: disable=unused-variable
if hasattr(board, 'JOYSTICK_X'):
if hasattr(board, "JOYSTICK_X"):
for sample in range(0, samples):
reading += self._joystick_x.value
reading /= samples
reading -= JOY_X_CTR
return reading

def _read_joystick_y(self, samples=3):
Expand All @@ -127,40 +133,41 @@ def _read_joystick_y(self, samples=3):
"""
reading = 0
# pylint: disable=unused-variable
if hasattr(board, 'JOYSTICK_Y'):
if hasattr(board, "JOYSTICK_Y"):
for sample in range(0, samples):
reading += self._joystick_y.value
reading /= samples
reading -= JOY_Y_CTR
return reading

def _check_cursor_movement(self, pressed=None):
"""Checks the PyBadge D-Pad or the PyGamer's Joystick for movement.
:param int pressed: 8-bit number with bits that correspond to buttons
which have been pressed down since the last call to get_pressed().
"""
if hasattr(board, 'BUTTON_CLOCK') and not hasattr(board, 'JOYSTICK_X'):
if pressed & self._pad_btns['btn_right']:
if hasattr(board, "BUTTON_CLOCK") and not hasattr(board, "JOYSTICK_X"):
if pressed & self._pad_btns["btn_right"]:
self._cursor.x += self._cursor.speed
elif pressed & self._pad_btns['btn_left']:
elif pressed & self._pad_btns["btn_left"]:
self._cursor.x -= self._cursor.speed
if pressed & self._pad_btns['btn_up']:
if pressed & self._pad_btns["btn_up"]:
self._cursor.y -= self._cursor.speed
elif pressed & self._pad_btns['btn_down']:
elif pressed & self._pad_btns["btn_down"]:
self._cursor.y += self._cursor.speed
elif hasattr(board, 'JOYSTICK_X'):
elif hasattr(board, "JOYSTICK_X"):
joy_x = self._read_joystick_x()
joy_y = self._read_joystick_y()
if joy_x > 700:
if joy_x > self._center_x + 1000:
self._cursor.x += self._cursor.speed
elif joy_x < -700:
elif joy_x < self._center_x - 1000:
self._cursor.x -= self._cursor.speed
if joy_y > 700:
if joy_y > self._center_y + 1000:
self._cursor.y += self._cursor.speed
elif joy_y < -700:
elif joy_y < self._center_y - 1000:
self._cursor.y -= self._cursor.speed
else:
raise AttributeError('Board must have a D-Pad or Joystick for use with CursorManager!')
raise AttributeError(
"Board must have a D-Pad or Joystick for use with CursorManager!"
)


class DebouncedCursorManager(CursorManager):
Expand All @@ -171,18 +178,22 @@ class DebouncedCursorManager(CursorManager):

:param adafruit_cursorcontrol cursor: The cursor object we are using.
"""

def __init__(self, cursor, debounce_interval=0.01):
CursorManager.__init__(self, cursor)
self._pressed = 0
self._debouncer = Debouncer(lambda: bool(self._pressed & self._pad_btns['btn_a']),
interval=debounce_interval)
self._debouncer = Debouncer(
lambda: bool(self._pressed & self._pad_btns["btn_a"]),
interval=debounce_interval,
)

@property
def is_clicked(self):
"""Returns True if the cursor button was pressed
during previous call to update()
"""
return self._debouncer.rose

pressed = is_clicked

@property
Expand All @@ -198,7 +209,6 @@ def held(self):
"""
return self._debouncer.value


def update(self):
"""Updates the cursor object."""
self._pressed = self._pad.get_pressed()
Expand Down