Cache pullup state in gamepad
Don't check the pin's pull direction on every tick, instead cache it at the beginning. Also avoid a "can't get pull of output pin" error when one of the pins passed is in output mode.
This commit is contained in:
parent
e274a02ff0
commit
52d05bbc37
|
@ -31,7 +31,7 @@
|
|||
#include "GamePad.h"
|
||||
|
||||
|
||||
gamepad_obj_t* gamepad_singleton = NULL;
|
||||
gamepad_obj_t* volatile gamepad_singleton = NULL;
|
||||
|
||||
//| .. currentmodule:: gamepad
|
||||
//|
|
||||
|
|
|
@ -38,14 +38,21 @@ void gamepad_init(size_t n_pins, const mp_obj_t* pins) {
|
|||
for (size_t i=0; i<8; ++i) {
|
||||
gamepad_singleton->pins[i] = NULL;
|
||||
}
|
||||
gamepad_singleton->pulls = 0;
|
||||
for (size_t i=0; i<n_pins; ++i) {
|
||||
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(pins[i]);
|
||||
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(pin));
|
||||
digitalio_direction_t direction = common_hal_digitalio_digitalinout_get_direction(pin);
|
||||
digitalio_pull_t pull = common_hal_digitalio_digitalinout_get_pull(pin);
|
||||
if (direction != DIRECTION_INPUT || pull == PULL_NONE) {
|
||||
if (direction != DIRECTION_INPUT) {
|
||||
common_hal_digitalio_digitalinout_switch_to_input(pin, PULL_UP);
|
||||
}
|
||||
digitalio_pull_t pull = common_hal_digitalio_digitalinout_get_pull(pin);
|
||||
if (pull == PULL_NONE) {
|
||||
common_hal_digitalio_digitalinout_set_pull(pin, PULL_UP);
|
||||
}
|
||||
if (pull != PULL_DOWN) {
|
||||
gamepad_singleton->pulls |= 1 << i;
|
||||
}
|
||||
gamepad_singleton->pins[i] = pin;
|
||||
}
|
||||
gamepad_singleton->last = 0;
|
||||
|
|
|
@ -36,9 +36,10 @@ typedef struct {
|
|||
digitalio_digitalinout_obj_t* pins[8];
|
||||
volatile uint8_t last;
|
||||
volatile uint8_t pressed;
|
||||
uint8_t pulls;
|
||||
} gamepad_obj_t;
|
||||
|
||||
extern gamepad_obj_t* gamepad_singleton;
|
||||
extern gamepad_obj_t* volatile gamepad_singleton;
|
||||
|
||||
void gamepad_init(size_t n_pins, const mp_obj_t* pins);
|
||||
|
||||
|
|
|
@ -37,17 +37,18 @@ void gamepad_tick(void) {
|
|||
return;
|
||||
}
|
||||
uint8_t gamepad_current = 0;
|
||||
for (int i=0; i<8; ++i) {
|
||||
uint8_t bit = 1;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
digitalio_digitalinout_obj_t* pin = gamepad_singleton->pins[i];
|
||||
if (!pin) {
|
||||
break;
|
||||
}
|
||||
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;
|
||||
if (common_hal_digitalio_digitalinout_get_value(pin)) {
|
||||
gamepad_current |= bit;
|
||||
}
|
||||
bit <<= 1;
|
||||
}
|
||||
gamepad_current ^= gamepad_singleton->pulls;
|
||||
gamepad_singleton->pressed |= gamepad_singleton->last & gamepad_current;
|
||||
gamepad_singleton->last = gamepad_current;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue