diff options
| author | Radomir Dopieralski <openstack@sheep.art.pl> | 2018-03-31 21:13:02 +0200 |
|---|---|---|
| committer | Radomir Dopieralski <openstack@sheep.art.pl> | 2018-03-31 21:13:02 +0200 |
| commit | 280374fa63e284f272b714358aac32b75a4bee6a (patch) | |
| tree | 3ecd37c4170bb792410f07f01411932705ea5e22 /shared-module/gamepad/__init__.c | |
| parent | 3215b85568af98987c8d54af4e691637a74e91bc (diff) | |
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.
Diffstat (limited to 'shared-module/gamepad/__init__.c')
| -rw-r--r-- | shared-module/gamepad/__init__.c | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c index b6c8be215..591a2fa0a 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -42,8 +42,10 @@ void gamepad_tick(void) { if (!pin) { break; } - if (!common_hal_digitalio_digitalinout_get_value(pin)) { - gamepad_current |= 1<<i; + digitalio_pull_t pull = common_hal_digitalio_digitalinout_get_pull(pin); + bool value = common_hal_digitalio_digitalinout_get_value(pin); + if ((pull == PULL_UP && !value) || (pull == PULL_DOWN && value)) { + gamepad_current |= 1 << i; } } gamepad_singleton->pressed |= gamepad_singleton->last & gamepad_current; |
