Skip to content

Commit 280374f

Browse files
committed
Respect pin's pull in gamepad
While it is traditional to have buttons on pins that are pulled up, and have the button connect them to the ground, some CircuitPython boards (notably the CPX) have the button pins pulled low and the button connects them to VCC. This patch makes the gamepad only change the pin's pull if it wasn't already set when passed to the constructor, and also makes it consider a button pressed when its value is the opposite of its pull.
1 parent 3215b85 commit 280374f

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

shared-module/gamepad/GamePad.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "shared-bindings/digitalio/Pull.h"
3333
#include "shared-bindings/digitalio/DigitalInOut.h"
34+
#include "shared-bindings/util.h"
3435

3536

3637
void gamepad_init(size_t n_pins, const mp_obj_t* pins) {
@@ -39,8 +40,13 @@ void gamepad_init(size_t n_pins, const mp_obj_t* pins) {
3940
}
4041
for (size_t i=0; i<n_pins; ++i) {
4142
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(pins[i]);
43+
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(pin));
44+
digitalio_direction_t direction = common_hal_digitalio_digitalinout_get_direction(pin);
45+
digitalio_pull_t pull = common_hal_digitalio_digitalinout_get_pull(pin);
46+
if (direction != DIRECTION_INPUT || pull == PULL_NONE) {
47+
common_hal_digitalio_digitalinout_switch_to_input(pin, PULL_UP);
48+
}
4249
gamepad_singleton->pins[i] = pin;
43-
common_hal_digitalio_digitalinout_switch_to_input(pin, PULL_UP);
4450
}
4551
gamepad_singleton->last = 0;
4652
}

shared-module/gamepad/__init__.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ void gamepad_tick(void) {
4242
if (!pin) {
4343
break;
4444
}
45-
if (!common_hal_digitalio_digitalinout_get_value(pin)) {
46-
gamepad_current |= 1<<i;
45+
digitalio_pull_t pull = common_hal_digitalio_digitalinout_get_pull(pin);
46+
bool value = common_hal_digitalio_digitalinout_get_value(pin);
47+
if ((pull == PULL_UP && !value) || (pull == PULL_DOWN && value)) {
48+
gamepad_current |= 1 << i;
4749
}
4850
}
4951
gamepad_singleton->pressed |= gamepad_singleton->last & gamepad_current;

0 commit comments

Comments
 (0)