From fc1be268f350eb6824a3e17b719374d85d85169c Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 22 Jul 2019 11:35:07 -0400 Subject: [PATCH 1/5] fix joystick center position --- adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 72e20c3..c5946dd 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -53,6 +53,8 @@ def __init__(self, cursor): self._cursor = cursor self._is_clicked = False self._init_hardware() + self._center_x = self._read_joystick_x(samples=10) + self._center_y = self._read_joystick_y(samples=10) def __enter__(self): return self @@ -151,13 +153,13 @@ def _check_cursor_movement(self, pressed=None): 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 + 100: self._cursor.x += self._cursor.speed - elif joy_x < -700: + elif joy_x < self._center_x - 100: self._cursor.x -= self._cursor.speed - if joy_y > 700: + if joy_y > self._center_y + 100: self._cursor.y += self._cursor.speed - elif joy_y < -700: + elif joy_y < self._center_y - 100: self._cursor.y -= self._cursor.speed else: raise AttributeError('Board must have a D-Pad or Joystick for use with CursorManager!') From 57d9562e325e99a7df34b54b5eb06ee6b437e0cc Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 22 Jul 2019 11:39:07 -0400 Subject: [PATCH 2/5] add variance kwarg --- .../cursorcontrol_cursormanager.py | 81 +++++++++++-------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index c5946dd..77090a9 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -44,11 +44,13 @@ 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 @@ -72,27 +74,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): @@ -107,7 +116,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): @@ -116,7 +125,7 @@ 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 @@ -129,40 +138,43 @@ 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): + def _check_cursor_movement(self, pressed=None, variance = 100): """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(). + :param int variance: Drift amount, defaults to 100. Adjust for lower sensitivity. """ - 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 > self._center_x + 100: + if joy_x > self._center_x + variance: self._cursor.x += self._cursor.speed - elif joy_x < self._center_x - 100: + elif joy_x < self._center_x - variance: self._cursor.x -= self._cursor.speed - if joy_y > self._center_y + 100: + if joy_y > self._center_y + variance: self._cursor.y += self._cursor.speed - elif joy_y < self._center_y - 100: + elif joy_y < self._center_y - variance: 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): @@ -173,11 +185,14 @@ 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): @@ -185,6 +200,7 @@ def is_clicked(self): during previous call to update() """ return self._debouncer.rose + pressed = is_clicked @property @@ -200,7 +216,6 @@ def held(self): """ return self._debouncer.value - def update(self): """Updates the cursor object.""" self._pressed = self._pad.get_pressed() From d81ba2df2891a49d2c2ae3eb98c5bab522604f5e Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 22 Jul 2019 12:38:39 -0400 Subject: [PATCH 3/5] remove variance kwarg from private method! --- adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 77090a9..5215ff4 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -145,11 +145,10 @@ def _read_joystick_y(self, samples=3): reading -= JOY_Y_CTR return reading - def _check_cursor_movement(self, pressed=None, variance = 100): + 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(). - :param int variance: Drift amount, defaults to 100. Adjust for lower sensitivity. """ if hasattr(board, "BUTTON_CLOCK") and not hasattr(board, "JOYSTICK_X"): if pressed & self._pad_btns["btn_right"]: @@ -163,13 +162,13 @@ def _check_cursor_movement(self, pressed=None, variance = 100): elif hasattr(board, "JOYSTICK_X"): joy_x = self._read_joystick_x() joy_y = self._read_joystick_y() - if joy_x > self._center_x + variance: + if joy_x > self._center_x + 100: self._cursor.x += self._cursor.speed - elif joy_x < self._center_x - variance: + elif joy_x < self._center_x - 100: self._cursor.x -= self._cursor.speed - if joy_y > self._center_y + variance: + if joy_y > self._center_y + 100: self._cursor.y += self._cursor.speed - elif joy_y < self._center_y - variance: + elif joy_y < self._center_y - 100: self._cursor.y -= self._cursor.speed else: raise AttributeError( From 9c3ecdf71ed06f05965adf2c2b382d464d4060f8 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 22 Jul 2019 15:06:01 -0400 Subject: [PATCH 4/5] remove joyxctr, joyctr --- .../cursorcontrol_cursormanager.py | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 5215ff4..6c59298 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -41,10 +41,6 @@ # 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. @@ -55,8 +51,8 @@ def __init__(self, cursor): self._cursor = cursor self._is_clicked = False self._init_hardware() - self._center_x = self._read_joystick_x(samples=10) - self._center_y = self._read_joystick_y(samples=10) + self._center_x = self._joystick_x.value + self._center_y = self._joystick_y.value def __enter__(self): return self @@ -129,7 +125,6 @@ def _read_joystick_x(self, samples=3): 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): @@ -142,7 +137,6 @@ def _read_joystick_y(self, samples=3): 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): @@ -162,13 +156,13 @@ def _check_cursor_movement(self, pressed=None): elif hasattr(board, "JOYSTICK_X"): joy_x = self._read_joystick_x() joy_y = self._read_joystick_y() - if joy_x > self._center_x + 100: + if joy_x > self._center_x + 1000: self._cursor.x += self._cursor.speed - elif joy_x < self._center_x - 100: + elif joy_x < self._center_x - 1000: self._cursor.x -= self._cursor.speed - if joy_y > self._center_y + 100: + if joy_y > self._center_y + 1000: self._cursor.y += self._cursor.speed - elif joy_y < self._center_y - 100: + elif joy_y < self._center_y - 1000: self._cursor.y -= self._cursor.speed else: raise AttributeError( @@ -219,4 +213,4 @@ def update(self): """Updates the cursor object.""" self._pressed = self._pad.get_pressed() self._check_cursor_movement(self._pressed) - self._debouncer.update() + self._debouncer.update() \ No newline at end of file From eb8c28683136ba2ddc1b751121015bc7ac1a9c82 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 22 Jul 2019 16:02:58 -0400 Subject: [PATCH 5/5] fix CRLF/LF --- 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 6c59298..4038403 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -213,4 +213,4 @@ def update(self): """Updates the cursor object.""" self._pressed = self._pad.get_pressed() self._check_cursor_movement(self._pressed) - self._debouncer.update() \ No newline at end of file + self._debouncer.update()