Simplify type check in tick and init last

This commit is contained in:
Scott Shawcroft 2019-04-16 10:44:09 -07:00
parent 6132a05fd9
commit 190bdf8117
No known key found for this signature in database
GPG Key ID: FD0EDC4B6C53CA59
4 changed files with 31 additions and 33 deletions

View File

@ -49,6 +49,7 @@ void common_hal_gamepad_gamepad_init(gamepad_obj_t *gamepad,
} }
gamepad->pins[i] = pin; gamepad->pins[i] = pin;
} }
gamepad->last = 0;
} }
void common_hal_gamepad_gamepad_deinit(gamepad_obj_t *self) { void common_hal_gamepad_gamepad_deinit(gamepad_obj_t *self) {

View File

@ -38,12 +38,11 @@ void gamepad_tick(void) {
uint8_t bit = 1; uint8_t bit = 1;
void* singleton = MP_STATE_VM(gamepad_singleton); void* singleton = MP_STATE_VM(gamepad_singleton);
if (!singleton) { if (singleton == NULL || !MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepad_type)) {
return; return;
} }
if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepad_type)) {
// buttons connected directly to pins gamepad_obj_t *self = MP_OBJ_TO_PTR(singleton);
gamepad_obj_t *self = singleton;
for (int i = 0; i < 8; ++i) { for (int i = 0; i < 8; ++i) {
digitalio_digitalinout_obj_t* pin = self->pins[i]; digitalio_digitalinout_obj_t* pin = self->pins[i];
if (!pin) { if (!pin) {
@ -57,7 +56,6 @@ void gamepad_tick(void) {
current ^= self->pulls; current ^= self->pulls;
self->pressed |= self->last & current; self->pressed |= self->last & current;
self->last = current; self->last = current;
}
} }
void gamepad_reset(void) { void gamepad_reset(void) {

View File

@ -40,6 +40,8 @@ void common_hal_gamepadshift_gamepadshift_init(gamepadshift_obj_t *gamepadshift,
common_hal_digitalio_digitalinout_switch_to_output(latch_pin, 1, common_hal_digitalio_digitalinout_switch_to_output(latch_pin, 1,
DRIVE_MODE_PUSH_PULL); DRIVE_MODE_PUSH_PULL);
gamepadshift->latch_pin = latch_pin; gamepadshift->latch_pin = latch_pin;
gamepadshift->last = 0;
} }
void common_hal_gamepadshift_gamepadshift_deinit(gamepadshift_obj_t *gamepadshift) { void common_hal_gamepadshift_gamepadshift_deinit(gamepadshift_obj_t *gamepadshift) {

View File

@ -31,12 +31,10 @@
void gamepadshift_tick(void) { void gamepadshift_tick(void) {
void* singleton = MP_STATE_VM(gamepad_singleton); void* singleton = MP_STATE_VM(gamepad_singleton);
if (!singleton) { if (singleton == NULL || !MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) {
return; 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); gamepadshift_obj_t *self = MP_OBJ_TO_PTR(singleton);
uint8_t current = 0; uint8_t current = 0;
uint8_t bit = 1; uint8_t bit = 1;
@ -52,7 +50,6 @@ void gamepadshift_tick(void) {
common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0); common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0);
self->pressed |= self->last & current; self->pressed |= self->last & current;
self->last = current; self->last = current;
}
} }
void gamepadshift_reset(void) { void gamepadshift_reset(void) {