Merge pull request #864 from pewpew-game/master
Multiple fixes in gamepad
This commit is contained in:
commit
ff592ac791
|
@ -28,6 +28,7 @@
|
|||
#include "py/mphal.h"
|
||||
#include "shared-module/gamepad/GamePad.h"
|
||||
#include "shared-bindings/digitalio/DigitalInOut.h"
|
||||
#include "shared-bindings/util.h"
|
||||
#include "GamePad.h"
|
||||
|
||||
|
||||
|
@ -93,15 +94,20 @@ gamepad_obj_t* gamepad_singleton = NULL;
|
|||
//|
|
||||
STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args,
|
||||
size_t n_kw, const mp_obj_t *args) {
|
||||
if (!gamepad_singleton) {
|
||||
gamepad_singleton = m_new_obj(gamepad_obj_t);
|
||||
gamepad_singleton->base.type = &gamepad_type;
|
||||
if (n_args > 8) {
|
||||
mp_raise_TypeError("too many arguments");
|
||||
}
|
||||
for (size_t i = 0; i < n_args; ++i) {
|
||||
if (!MP_OBJ_IS_TYPE(args[i], &digitalio_digitalinout_type)) {
|
||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
||||
"Expected a %q", digitalio_digitalinout_type.name));
|
||||
mp_raise_TypeError("expected a DigitalInOut");
|
||||
}
|
||||
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(args[i]);
|
||||
raise_error_if_deinited(
|
||||
common_hal_digitalio_digitalinout_deinited(pin));
|
||||
}
|
||||
if (!gamepad_singleton) {
|
||||
gamepad_singleton = m_new_obj(gamepad_obj_t);
|
||||
gamepad_singleton->base.type = &gamepad_type;
|
||||
}
|
||||
gamepad_init(n_args, args);
|
||||
return MP_OBJ_FROM_PTR(gamepad_singleton);
|
||||
|
@ -119,9 +125,8 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args,
|
|||
//| held down) can be recorded for the next call.
|
||||
//|
|
||||
STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) {
|
||||
gamepad_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(self->pressed);
|
||||
self->pressed = 0;
|
||||
mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(gamepad_singleton->pressed);
|
||||
gamepad_singleton->pressed = 0;
|
||||
return gamepad;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed);
|
||||
|
|
|
@ -35,17 +35,23 @@
|
|||
|
||||
|
||||
void gamepad_init(size_t n_pins, const mp_obj_t* pins) {
|
||||
for (size_t i=0; i<8; ++i) {
|
||||
for (size_t i = 0; i < 8; ++i) {
|
||||
gamepad_singleton->pins[i] = NULL;
|
||||
}
|
||||
for (size_t i=0; i<n_pins; ++i) {
|
||||
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,6 +36,7 @@ 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;
|
||||
|
|
|
@ -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