From 190bdf811716e35df086d317f01f97a10746790d Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 16 Apr 2019 10:44:09 -0700 Subject: [PATCH] Simplify type check in tick and init last --- shared-module/gamepad/GamePad.c | 1 + shared-module/gamepad/__init__.c | 30 ++++++++++------------ shared-module/gamepadshift/GamePadShift.c | 2 ++ shared-module/gamepadshift/__init__.c | 31 ++++++++++------------- 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/shared-module/gamepad/GamePad.c b/shared-module/gamepad/GamePad.c index b3e3fabf60..fdce0caab4 100644 --- a/shared-module/gamepad/GamePad.c +++ b/shared-module/gamepad/GamePad.c @@ -49,6 +49,7 @@ void common_hal_gamepad_gamepad_init(gamepad_obj_t *gamepad, } gamepad->pins[i] = pin; } + gamepad->last = 0; } void common_hal_gamepad_gamepad_deinit(gamepad_obj_t *self) { diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c index 46630fd329..3e17a0fb5b 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -38,26 +38,24 @@ void gamepad_tick(void) { uint8_t bit = 1; void* singleton = MP_STATE_VM(gamepad_singleton); - if (!singleton) { + if (singleton == NULL || !MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepad_type)) { return; } - if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepad_type)) { - // buttons connected directly to pins - gamepad_obj_t *self = singleton; - for (int i = 0; i < 8; ++i) { - digitalio_digitalinout_obj_t* pin = self->pins[i]; - if (!pin) { - break; - } - if (common_hal_digitalio_digitalinout_get_value(pin)) { - current |= bit; - } - bit <<= 1; + + gamepad_obj_t *self = MP_OBJ_TO_PTR(singleton); + for (int i = 0; i < 8; ++i) { + digitalio_digitalinout_obj_t* pin = self->pins[i]; + if (!pin) { + break; } - current ^= self->pulls; - self->pressed |= self->last & current; - self->last = current; + if (common_hal_digitalio_digitalinout_get_value(pin)) { + current |= bit; + } + bit <<= 1; } + current ^= self->pulls; + self->pressed |= self->last & current; + self->last = current; } void gamepad_reset(void) { diff --git a/shared-module/gamepadshift/GamePadShift.c b/shared-module/gamepadshift/GamePadShift.c index f9303938fe..b8fb4d3f44 100644 --- a/shared-module/gamepadshift/GamePadShift.c +++ b/shared-module/gamepadshift/GamePadShift.c @@ -40,6 +40,8 @@ void common_hal_gamepadshift_gamepadshift_init(gamepadshift_obj_t *gamepadshift, common_hal_digitalio_digitalinout_switch_to_output(latch_pin, 1, DRIVE_MODE_PUSH_PULL); gamepadshift->latch_pin = latch_pin; + + gamepadshift->last = 0; } void common_hal_gamepadshift_gamepadshift_deinit(gamepadshift_obj_t *gamepadshift) { diff --git a/shared-module/gamepadshift/__init__.c b/shared-module/gamepadshift/__init__.c index 728c2187ee..47a008c503 100644 --- a/shared-module/gamepadshift/__init__.c +++ b/shared-module/gamepadshift/__init__.c @@ -31,28 +31,25 @@ void gamepadshift_tick(void) { void* singleton = MP_STATE_VM(gamepad_singleton); - if (!singleton) { + if (singleton == NULL || !MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) { return; } - if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) { - // buttons connected to a shift register - gamepadshift_obj_t *self = MP_OBJ_TO_PTR(singleton); - uint8_t current = 0; - uint8_t bit = 1; - common_hal_digitalio_digitalinout_set_value(self->latch_pin, 1); - for (int i = 0; i < 8; ++i) { - common_hal_digitalio_digitalinout_set_value(self->clock_pin, 0); - if (common_hal_digitalio_digitalinout_get_value(self->data_pin)) { - current |= bit; - } - common_hal_digitalio_digitalinout_set_value(self->clock_pin, 1); - bit <<= 1; + gamepadshift_obj_t *self = MP_OBJ_TO_PTR(singleton); + uint8_t current = 0; + uint8_t bit = 1; + common_hal_digitalio_digitalinout_set_value(self->latch_pin, 1); + for (int i = 0; i < 8; ++i) { + common_hal_digitalio_digitalinout_set_value(self->clock_pin, 0); + if (common_hal_digitalio_digitalinout_get_value(self->data_pin)) { + current |= bit; } - common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0); - self->pressed |= self->last & current; - self->last = current; + common_hal_digitalio_digitalinout_set_value(self->clock_pin, 1); + bit <<= 1; } + common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0); + self->pressed |= self->last & current; + self->last = current; } void gamepadshift_reset(void) {